/
test_fourier.jl
229 lines (190 loc) · 7.68 KB
/
test_fourier.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
fourier_types = (Float32, Float64, BigFloat)
#####
# Fourier series
#####
function test_fourier_series(T)
### Test bounds checking
fb0 = Fourier{T}(5)
@test ~in_support(fb0, 1, -one(T)/10)
@test in_support(fb0, 1, zero(T))
@test in_support(fb0, 1, one(T)/2)
@test in_support(fb0, 1, one(T))
@test in_support(fb0, 1, zero(T)-0.9*sqrt(eps(T)))
@test in_support(fb0, 1, one(T)+0.9*sqrt(eps(T)))
@test support(fb0) == UnitInterval{T}()
@test convert(Fourier{BigFloat}, Fourier{Float64}(10)) isa Fourier{BigFloat}
## Even length
n = 12
a = -T(1.2)
b = T(3.4)
fb = Fourier{T}(n) → a..b
@test ~isreal(fb)
@test infimum(support(fb)) ≈ a
@test supremum(support(fb)) ≈ b
g = interpolation_grid(fb)
@test typeof(g) <: GridArrays.MappedGrid
@test leftendpoint(coverdomain(g)) ≈ a
@test rightendpoint(coverdomain(g)) ≈ b
@test length(g) == length(fb)
# Take a random point in the domain
x = T(a+rand()*(b-a))
y = (x-a)/(b-a)
# Is the 0-index basis function the constant 1?
freq = 0
idx = frequency2idx(superdict(fb), freq)
@test fb[idx](x) ≈ 1
# Evaluate in a point in the interior
freq = 3
idx = frequency2idx(superdict(fb), freq)
@test fb[idx](x) ≈ exp(2*T(pi)*1im*freq*y)
# Evaluate the largest frequency, which is a cosine in this case
freq = n >> 1
idx = frequency2idx(superdict(fb), freq)
@test fb[idx](x) ≈ cos(2*T(pi)*freq*y)
# Evaluate an expansion
coef = T[1; 2; 3; 4] * (1+im)
e = Expansion(rescale(Fourier{T}(4), a, b), coef)
@test e(x) ≈ coef[1]*T(1) + coef[2]*exp(2*T(pi)*im*y) + coef[3]*cos(4*T(pi)*y) + coef[4]*exp(-2*T(pi)*im*y)
# Check type promotion: evaluate at an integer and at a rational point
for i in [1 2]
@test typeof(BasisFunctions.unsafe_eval_element(fb, i, 0)) == Complex{T}
@test typeof(BasisFunctions.unsafe_eval_element(fb, i, 1//2)) == Complex{T}
end
# Try an extension
n = 12
coef = rand(complex(T), n)
b1 = rescale(Fourier{T}(n), a, b)
b2 = rescale(Fourier{T}(n+1), a, b)
b3 = rescale(Fourier{T}(n+15), a, b)
E2 = extension(b1, b2)
E3 = extension(b1, b3)
e1 = Expansion(b1, coef)
e2 = Expansion(b2, E2*coef)
e3 = Expansion(b3, E3*coef)
x = T(2//10)
@test e1(x) ≈ e2(x)
@test e1(x) ≈ e3(x)
# Differentiation test
coef = rand(complex(T), size(fb))
D = differentiation(fb)
coef2 = D*coef
e1 = Expansion(fb, coef)
e2 = Expansion(rescale(Fourier{T}(length(fb)+1),support(fb)), coef2)
x = T(2//10)
delta = sqrt(eps(T))
@test abs( (e1(x+delta)-e1(x))/delta - e2(x) ) / abs(e2(x)) < 100delta
## Odd length
fbo = rescale(Fourier{T}(13), a, b)
@test ~isreal(fbo)
# Is the 0-index basis function the constant 1?
freq = 0
idx = frequency2idx(superdict(fbo), freq)
@test fbo[idx](T(2//10)) ≈ 1
# Evaluate in a point in the interior
freq = 3
idx = frequency2idx(superdict(fbo), freq)
x = T(2//10)
y = (x-a)/(b-a)
@test fbo[idx](x) ≈ exp(2*T(pi)*1im*freq*y)
# Evaluate an expansion
coef = [one(T)+im; 2*one(T)-im; 3*one(T)+2im]
e = Expansion(Fourier{T}(3) → a..b, coef)
x = T(2//10)
y = (x-a)/(b-a)
@test e(x) ≈ coef[1]*one(T) + coef[2]*exp(2*T(pi)*im*y) + coef[3]*exp(-2*T(pi)*im*y)
# evaluate on a grid
g = interpolation_grid(dictionary(e))
result = e(g)
# Don't compare to zero with isapprox because the default absolute tolerance is zero.
# So: add 1 and compare to 1
@test sum([abs(result[i] - e(g[i])) for i in 1:length(g)]) + 1 ≈ 1
# Try an extension
n = 13
coef = rand(complex(T), n)
b1 = Fourier{T}(n)
b2 = Fourier{T}(n+1)
b3 = Fourier{T}(n+15)
E2 = extension(b1, b2)
E3 = extension(b1, b3)
e1 = Expansion(b1, coef)
e2 = Expansion(b2, E2*coef)
e3 = Expansion(b3, E3*coef)
x = T(2//10)
@test e1(x) ≈ e2(x)
@test e1(x) ≈ e3(x)
# Restriction
n = 14
b1 = Fourier{T}(n)
b2 = Fourier{T}(n-1)
b3 = Fourier{T}(n-5)
E1 = restriction(b1, b2) # source has even length
E2 = restriction(b2, b3) # source has odd length
coef1 = rand(complex(T), length(b1))
coef2 = E1*coef1
coef3 = E2*coef2
@test reduce(&, [ coef2[i+1] == coef1[i+1] for i=0:BasisFunctions.nhalf(b2) ] )
@test reduce(&, [ coef2[end-i+1] == coef1[end-i+1] for i=1:BasisFunctions.nhalf(b2) ] )
@test reduce(&, [ coef3[i+1] == coef2[i+1] for i=0:BasisFunctions.nhalf(b3) ] )
@test reduce(&, [ coef3[end-i+1] == coef2[end-i+1] for i=1:BasisFunctions.nhalf(b3) ] )
# Differentiation test
coef = rand(complex(T), size(fbo))
D = differentiation(fbo)
coef2 = D*coef
e1 = Expansion(fbo, coef)
e2 = Expansion(fbo, coef2)
x = T(2//10)
delta = sqrt(eps(T))
@test abs( (e1(x+delta)-e1(x))/delta - e2(x) ) / abs(e2(x)) < 150delta
# Transforms
b1 = Fourier{T}(161)
A = approximation(b1)
f = x -> 1/(2+cos(2*T(pi)*x))
e = approximate(b1, f)
x0 = T(1//2)
@test abs(e(T(x0))-f(x0)) < sqrt(eps(T))
# Arithmetic
b2 = Fourier{T}(162)
f2 = x -> 1/(2+cos(2*T(pi)*x))
e2 = approximate(b2, f2)
x0 = T(1//2)
@test abs((e*e2)(T(x0))-f(x0)*f2(x0)) < sqrt(eps(T))
@test abs((e+2*e2)(T(x0))-(f(x0)+2*f2(x0))) < sqrt(eps(T))
@test abs((3*e-e2)(T(x0))-(3*f(x0)-f2(x0))) < sqrt(eps(T))
end
function test_fourier_orthogonality()
test_orthogonality_orthonormality(Fourier(10), true, false, FourierMeasure())
test_orthogonality_orthonormality(Fourier(10), true, false, DiracComb(interpolation_grid(Fourier(10))))
test_orthogonality_orthonormality(Fourier(10), true, true, NormalizedDiracComb(interpolation_grid(Fourier(10))))
test_orthogonality_orthonormality(Fourier(10), true, false, DiracComb(interpolation_grid(Fourier(20))))
test_orthogonality_orthonormality(Fourier(10), true, false, NormalizedDiracComb(interpolation_grid(Fourier(20))))
test_orthogonality_orthonormality(Fourier(11), true, true, FourierMeasure())
test_orthogonality_orthonormality(Fourier(11), true, false, DiracComb(interpolation_grid(Fourier(11))))
test_orthogonality_orthonormality(Fourier(11), true, true, NormalizedDiracComb(interpolation_grid(Fourier(11))))
test_orthogonality_orthonormality(Fourier(11), true, false, DiracComb(interpolation_grid(Fourier(22))))
test_orthogonality_orthonormality(Fourier(11), true, true, NormalizedDiracComb(interpolation_grid(Fourier(22))))
test_orthogonality_orthonormality(Fourier(11), true, false, DiracComb(interpolation_grid(Fourier(23))))
test_orthogonality_orthonormality(Fourier(11), true, true, NormalizedDiracComb(interpolation_grid(Fourier(23))))
test_orthogonality_orthonormality(Fourier(10), true, false, DiracComb(interpolation_grid(Fourier(23))))
test_orthogonality_orthonormality(Fourier(10), true, false, NormalizedDiracComb(interpolation_grid(Fourier(23))))
test_orthogonality_orthonormality(Fourier(11), false, false, BasisFunctions.DiracComb(EquispacedGrid(20,0,1)))
end
for T in fourier_types
@testset "$(rpad("Fourier expansions ($T)",80))" begin
test_fourier_series(T)
end
end
@testset "$(rpad("Fourier orthogonality, gram functionality",80))" begin
test_fourier_orthogonality()
F = Fourier(10)
P = ProjectionSampling(F)
@test (P')'*P' == gram(F)
for P in (Fourier(4),Fourier(5)), M in (BasisFunctions.discretemeasure(PeriodicEquispacedGrid(11,0,1)),FourierMeasure())
D = dual(P, M)
@test Matrix(gram(P, M)) ≈ Matrix(inv(gram(D,M)))
A = SynthesisOperator(P, M)
Z = SynthesisOperator(D, M)
Zt = Z'
G1 = Zt*A
@test Matrix(G1) ≈ Matrix(I,size(G1))
end
end