Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier tests #608

Merged
merged 8 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/diric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ julia> diric(0, 4)
```
"""
function diric(Ω::T, n::Integer) where T <: AbstractFloat
n > 0 || throw(ArgumentError("n=$n not positive"))
n > 0 || throw(DomainError(n, "n not positive"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't file this under "prettier tests", but still a good change.

sign = one(T)
if isodd(n)
Ω = rem2pi(Ω, RoundNearest) # [-π,π)
Expand Down
36 changes: 12 additions & 24 deletions test/FilterTestHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ function zpkfilter_eq(f1, f2)
@test map(Float64, f1.k) ≈ map(Float64, f2.k)
end

function zpkfilter_eq(f1, f2, eps)
function zpkfilter_eq(f1, f2, atol)
if !isempty(f1.z) || !isempty(f2.z)
@test ≈(map(ComplexF64, sort(f1.z; lt)), map(ComplexF64, sort(f2.z; lt)); atol=eps)
@test ≈(map(ComplexF64, sort(f1.z; lt)), map(ComplexF64, sort(f2.z; lt)); atol)
end
@test ≈(map(ComplexF64, sort(f1.p; lt)), map(ComplexF64, sort(f2.p; lt)); atol=eps)
@test ≈(map(Float64, f1.k), map(Float64, f2.k); atol=eps)
@test ≈(map(ComplexF64, sort(f1.p; lt)), map(ComplexF64, sort(f2.p; lt)); atol)
@test ≈(map(Float64, f1.k), map(Float64, f2.k); atol)
end

loss(x::Real, y::Real) = abs(float(x) - float(y))/eps(float(x))
Expand All @@ -58,33 +58,21 @@ function tffilter_accuracy(f1, f2, accurate_f)
accuracy_check(loss(a1, accurate_a), loss(a2, accurate_a), "a")
end

function zpkfilter_accuracy(f1, f2, accurate_f; relerr=1, compare_gain_at=nothing, eps=nothing)
function zpkfilter_accuracy(f1, f2, accurate_f; relerr=1, compare_gain_at=nothing, atol=0.0)
z1, p1 = sort(f1.z; lt), sort(f1.p; lt)
z2, p2 = sort(f2.z; lt), sort(f2.p; lt)
accurate_z, accurate_p = sort(accurate_f.z; lt), sort(accurate_f.p; lt)
if !isempty(z1) || !isempty(z2) || !isempty(accurate_z)
if eps !== nothing
@test ≈(z1, accurate_z; atol=eps)
@test ≈(z2, accurate_z; atol=eps)
else
@test z1 ≈ accurate_z
@test z2 ≈ accurate_z
end
@test ≈(z1, accurate_z; atol)
@test ≈(z2, accurate_z; atol)
accuracy_check(loss(z1, accurate_z), loss(z2, accurate_z), "z", relerr)
end
if eps !== nothing
@test ≈(p1, accurate_p; atol=eps)
@test ≈(p2, accurate_p; atol=eps)
@test ≈(f1.k, accurate_f.k; atol=eps)
@test ≈(f2.k, accurate_f.k; atol=eps)
else
@test p1 ≈ accurate_p
@test p2 ≈ accurate_p
@test f1.k ≈ accurate_f.k
@test f2.k ≈ accurate_f.k
end
@test ≈(p1, accurate_p; atol)
@test ≈(p2, accurate_p; atol)
@test ≈(f1.k, accurate_f.k; atol)
@test ≈(f2.k, accurate_f.k; atol)
accuracy_check(loss(p1, accurate_p), loss(p2, accurate_p), "p", relerr)
if compare_gain_at === nothing
if isnothing(compare_gain_at)
accuracy_check(loss(f1.k, accurate_f.k), loss(f2.k, accurate_f.k), "k", relerr)
else
jω = compare_gain_at*im
Expand Down
2 changes: 1 addition & 1 deletion test/diric.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# diric.jl Dirichlet kernel tests

@testset "diric" begin
@test_throws ArgumentError diric(0, -2)
@test_throws DomainError diric(0, -2)
@test @inferred(diric(0, 4)) ≈ 1
@test @inferred(diric(0 // 1, 5)) == 1
@test @inferred(diric(4π, 4)) ≈ 1
Expand Down
82 changes: 32 additions & 50 deletions test/estimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,77 +22,59 @@ end

@testset "jacobsen" begin
fs = 100
t = range(0, 5, step = 1/fs)
t = range(0, 5; step=1/fs)
function test_complex_jacobsen(fs, fc, f=0, t=t)
sc = cispi.(2 * fc * t .+ f)
f_est_complex = jacobsen(sc, fs)
isapprox(f_est_complex, fc; atol=1e-5)
end
# test at two arbitrary frequencies
fc = -40.3
sc = cis.(2π*fc*t .+ π/1.4)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
fc = 14.3
sc = cis.(2π*fc*t .+ π/3)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
@test test_complex_jacobsen(fs, -40.3, 1 / 1.4)
@test test_complex_jacobsen(fs, 14.3, 1 / 3)
# test near fs/2
fc = 49.90019
sc = cis.(2π*fc*t)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
@test test_complex_jacobsen(fs, 49.90019)
# test near -fs/2
fc = -49.90019
sc = cis.(2π*fc*t)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
@test test_complex_jacobsen(fs, -49.90019)
# test near +zero
fc = 0.04
sc = cis.(2π*fc*t)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
@test test_complex_jacobsen(fs, 0.04)
# test near -zero
fc = -0.1
sc = cis.(2π*fc*t)
f_est_complex = jacobsen(sc, fs)
@test isapprox(f_est_complex, fc, atol = 1e-5)
@test test_complex_jacobsen(fs, -0.1)
# tests for real signals: test only around fs/4, where the
# expected error is small.
fr = 28.3
sr = cos.(2π*fr*t .+ π/4.2)
sr = cospi.(2 * fr * t .+ 1 / 4.2)
f_est_real = jacobsen(sr, fs)
@test isapprox(f_est_real, fr, atol = 1e-5)
@test isapprox(f_est_real, fr; atol=1e-5)
fr = 23.45
sr = sin.(2π*fr*t .+ 3π/2.2)
sr = sinpi.(2 * fr * t .+ 3 / 2.2)
f_est_real = jacobsen(sr, fs)
@test isapprox(f_est_real, fr, atol = 1e-5)
@test isapprox(f_est_real, fr; atol=1e-5)
end

@testset "quinn" begin
function test_quinn(f, s, args...)
(f_est_real, maxiter) = quinn(s, args...)
@test maxiter == false
@test isapprox(f_est_real, f; atol=1e-3)
return nothing
end
wheeheee marked this conversation as resolved.
Show resolved Hide resolved
### real input
fs = 100
t = range(0, 5, step = 1/fs)
t = range(0, 5; step=1/fs)
fr = 28.3
sr = cos.(2π*fr*t .+ π/4.2)
(f_est_real, maxiter) = quinn(sr, 50, fs)
@test maxiter == false
@test isapprox(f_est_real, fr, atol = 1e-3)
sr = cospi.(2 * fr * t .+ 1 / 4.2)
test_quinn(fr, sr, 50, fs)
# use default initial guess
(f_est_real, maxiter) = quinn(sr, fs) # initial guess given by Jacobsen
@test maxiter == false
@test isapprox(f_est_real, fr, atol = 1e-3)
test_quinn(fr, sr, fs) # initial guess given by Jacobsen
# use default fs
(f_est_real, maxiter) = quinn(sr) # fs = 1.0, initial guess given by Jacobsen
@test maxiter == false
@test isapprox(f_est_real, fr/fs, atol = 1e-3)
test_quinn(fr / fs, sr) # fs = 1.0, initial guess given by Jacobsen

### complex input
fc = -40.3
sc = cis.(2π*fc*t .+ π/1.4)
(f_est_real, maxiter) = quinn(sc, -20, fs)
@test maxiter == false
@test isapprox(f_est_real, fc, atol = 1e-3)
sc = cispi.(2 * fc * t .+ 1 / 1.4)
test_quinn(fc, sc, -20, fs)
# use default initial guess
(f_est_real, maxiter) = quinn(sc, fs) # initial guess given by Jacobsen
@test maxiter == false
@test isapprox(f_est_real, fc, atol = 1e-3)
test_quinn(fc, sc, fs) # initial guess given by Jacobsen
# use default fs
(f_est_real, maxiter) = quinn(sc) # fs = 1.0, initial guess by Jacobsen
@test maxiter == false
@test isapprox(f_est_real, fc/fs, atol = 1e-3)
test_quinn(fc / fs, sc) # fs = 1.0, initial guess by Jacobsen
end
6 changes: 3 additions & 3 deletions test/filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ end
@testset "filtfilt SOS" begin
x = readdlm(joinpath(dirname(@__FILE__), "data", "spectrogram_x.txt"),'\t')

f = DSP.digitalfilter(DSP.Lowpass(0.2), DSP.Butterworth(4))
f = digitalfilter(Lowpass(0.2), Butterworth(4))
@test filtfilt(convert(SecondOrderSections, f), x) ≈ filtfilt(convert(PolynomialRatio, f), x)

f = DSP.digitalfilter(DSP.Highpass(0.1), DSP.Butterworth(6))
f = digitalfilter(Highpass(0.1), Butterworth(6))
@test filtfilt(convert(SecondOrderSections, f), x) ≈ filtfilt(convert(PolynomialRatio, f), x)

f = DSP.digitalfilter(DSP.Bandpass(0.1, 0.3), DSP.Butterworth(2))
f = digitalfilter(Bandpass(0.1, 0.3), Butterworth(2))
@test filtfilt(convert(SecondOrderSections, f), x) ≈ filtfilt(convert(PolynomialRatio, f), x)
end

Expand Down
Loading
Loading