Skip to content

Commit

Permalink
improved testset for csm
Browse files Browse the repository at this point in the history
  • Loading branch information
1oly committed Nov 28, 2024
1 parent 256ac73 commit 709994d
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions test/csm_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ import DSP
# https://github.com/scipy/scipy/blob/f2ec91c4908f9d67b5445fbfacce7f47518b35d1/scipy/signal/spectral.py#L223

@testset "CSM test:" begin
fs = 10e3
N = 1e5
amp = 2*sqrt(2)
fr = 1234.0
noise_power = 0.001*fs/2
time = (1:N)/fs
x = amp*sin.(2*pi*fr.*time)
x .+= sqrt.(noise_power).*rand(length(time))
n = 1024
Pxx_d = csm(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="density")
fc = Pxx_d.fc
Pxx_d1 = real.(Pxx_d[1,1,:])
Pxx_w = DSP.welch_pgram(x,n;onesided=true,fs=fs,window=DSP.hanning(n))
@test Pxx_d1 Pxx_w.power
@test Pxx_d == AeroAcoustics.csm_slow(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="density")

Pxx_s = csm(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="spectrum")
fc = Pxx_s.fc
Pxx_s1 = real.(Pxx_s[1,1,:])
ENBW = enbw(fs,DSP.hanning(1024))
@test Pxx_s1 Pxx_w.power*ENBW
@test Pxx_s == AeroAcoustics.csm_slow(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="spectrum")
for N in (1e4,1e5,1e6)
for fs in (10e3,10e4,10e5)
for n = (2^10,2^11,2^12,2^13)
amp = 2*sqrt(2)
fr = 1234.0
noise_power = 0.001*fs/2
time = (1:N)/fs
x = amp*sin.(2*pi*fr.*time)
x .+= sqrt.(noise_power).*rand(length(time))
@time Pxx_d = csm(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="density")
fc = Pxx_d.fc
Pxx_d1 = real.(Pxx_d[1,1,:])
Pxx_w = DSP.welch_pgram(x,n;onesided=true,fs=fs,window=DSP.hanning(n))
@test Pxx_d1 Pxx_w.power
@test Pxx_d AeroAcoustics.csm_slow(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="density")
# spectrum scaling
Pxx_s = csm(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="spectrum")
fc = Pxx_s.fc
Pxx_s1 = real.(Pxx_s[1,1,:])
ENBW = enbw(fs,DSP.hanning(n))
@test Pxx_s1 Pxx_w.power*ENBW
@test Pxx_s AeroAcoustics.csm_slow(x;n=n,noverlap=div(n,2),win=DSP.hanning(n),fs=fs,scaling="spectrum")
end
end
end
end

0 comments on commit 709994d

Please sign in to comment.