|
22 | 22 |
|
23 | 23 | @testset "jacobsen" begin |
24 | 24 | fs = 100 |
25 | | - t = range(0, 5, step = 1/fs) |
| 25 | + t = range(0, 5; step=1/fs) |
| 26 | + function test_complex_jacobsen(fs, fc, f=0, t=t) |
| 27 | + sc = cispi.(2 * fc * t .+ f) |
| 28 | + f_est_complex = jacobsen(sc, fs) |
| 29 | + isapprox(f_est_complex, fc; atol=1e-5) |
| 30 | + end |
26 | 31 | # test at two arbitrary frequencies |
27 | | - fc = -40.3 |
28 | | - sc = cis.(2π*fc*t .+ π/1.4) |
29 | | - f_est_complex = jacobsen(sc, fs) |
30 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
31 | | - fc = 14.3 |
32 | | - sc = cis.(2π*fc*t .+ π/3) |
33 | | - f_est_complex = jacobsen(sc, fs) |
34 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
| 32 | + @test test_complex_jacobsen(fs, -40.3, 1 / 1.4) |
| 33 | + @test test_complex_jacobsen(fs, 14.3, 1 / 3) |
35 | 34 | # test near fs/2 |
36 | | - fc = 49.90019 |
37 | | - sc = cis.(2π*fc*t) |
38 | | - f_est_complex = jacobsen(sc, fs) |
39 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
| 35 | + @test test_complex_jacobsen(fs, 49.90019) |
40 | 36 | # test near -fs/2 |
41 | | - fc = -49.90019 |
42 | | - sc = cis.(2π*fc*t) |
43 | | - f_est_complex = jacobsen(sc, fs) |
44 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
| 37 | + @test test_complex_jacobsen(fs, -49.90019) |
45 | 38 | # test near +zero |
46 | | - fc = 0.04 |
47 | | - sc = cis.(2π*fc*t) |
48 | | - f_est_complex = jacobsen(sc, fs) |
49 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
| 39 | + @test test_complex_jacobsen(fs, 0.04) |
50 | 40 | # test near -zero |
51 | | - fc = -0.1 |
52 | | - sc = cis.(2π*fc*t) |
53 | | - f_est_complex = jacobsen(sc, fs) |
54 | | - @test isapprox(f_est_complex, fc, atol = 1e-5) |
| 41 | + @test test_complex_jacobsen(fs, -0.1) |
55 | 42 | # tests for real signals: test only around fs/4, where the |
56 | 43 | # expected error is small. |
57 | 44 | fr = 28.3 |
58 | | - sr = cos.(2π*fr*t .+ π/4.2) |
| 45 | + sr = cospi.(2 * fr * t .+ 1 / 4.2) |
59 | 46 | f_est_real = jacobsen(sr, fs) |
60 | | - @test isapprox(f_est_real, fr, atol = 1e-5) |
| 47 | + @test isapprox(f_est_real, fr; atol=1e-5) |
61 | 48 | fr = 23.45 |
62 | | - sr = sin.(2π*fr*t .+ 3π/2.2) |
| 49 | + sr = sinpi.(2 * fr * t .+ 3 / 2.2) |
63 | 50 | f_est_real = jacobsen(sr, fs) |
64 | | - @test isapprox(f_est_real, fr, atol = 1e-5) |
| 51 | + @test isapprox(f_est_real, fr; atol=1e-5) |
65 | 52 | end |
66 | 53 |
|
67 | 54 | @testset "quinn" begin |
| 55 | + function test_quinn(f, s, args...) |
| 56 | + (f_est_real, maxiter) = quinn(s, args...) |
| 57 | + @test maxiter == false |
| 58 | + @test isapprox(f_est_real, f; atol=1e-3) |
| 59 | + return nothing |
| 60 | + end |
68 | 61 | ### real input |
69 | 62 | fs = 100 |
70 | | - t = range(0, 5, step = 1/fs) |
| 63 | + t = range(0, 5; step=1/fs) |
71 | 64 | fr = 28.3 |
72 | | - sr = cos.(2π*fr*t .+ π/4.2) |
73 | | - (f_est_real, maxiter) = quinn(sr, 50, fs) |
74 | | - @test maxiter == false |
75 | | - @test isapprox(f_est_real, fr, atol = 1e-3) |
| 65 | + sr = cospi.(2 * fr * t .+ 1 / 4.2) |
| 66 | + test_quinn(fr, sr, 50, fs) |
76 | 67 | # use default initial guess |
77 | | - (f_est_real, maxiter) = quinn(sr, fs) # initial guess given by Jacobsen |
78 | | - @test maxiter == false |
79 | | - @test isapprox(f_est_real, fr, atol = 1e-3) |
| 68 | + test_quinn(fr, sr, fs) # initial guess given by Jacobsen |
80 | 69 | # use default fs |
81 | | - (f_est_real, maxiter) = quinn(sr) # fs = 1.0, initial guess given by Jacobsen |
82 | | - @test maxiter == false |
83 | | - @test isapprox(f_est_real, fr/fs, atol = 1e-3) |
| 70 | + test_quinn(fr / fs, sr) # fs = 1.0, initial guess given by Jacobsen |
| 71 | + |
84 | 72 | ### complex input |
85 | 73 | fc = -40.3 |
86 | | - sc = cis.(2π*fc*t .+ π/1.4) |
87 | | - (f_est_real, maxiter) = quinn(sc, -20, fs) |
88 | | - @test maxiter == false |
89 | | - @test isapprox(f_est_real, fc, atol = 1e-3) |
| 74 | + sc = cispi.(2 * fc * t .+ 1 / 1.4) |
| 75 | + test_quinn(fc, sc, -20, fs) |
90 | 76 | # use default initial guess |
91 | | - (f_est_real, maxiter) = quinn(sc, fs) # initial guess given by Jacobsen |
92 | | - @test maxiter == false |
93 | | - @test isapprox(f_est_real, fc, atol = 1e-3) |
| 77 | + test_quinn(fc, sc, fs) # initial guess given by Jacobsen |
94 | 78 | # use default fs |
95 | | - (f_est_real, maxiter) = quinn(sc) # fs = 1.0, initial guess by Jacobsen |
96 | | - @test maxiter == false |
97 | | - @test isapprox(f_est_real, fc/fs, atol = 1e-3) |
| 79 | + test_quinn(fc / fs, sc) # fs = 1.0, initial guess by Jacobsen |
98 | 80 | end |
0 commit comments