-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fourier.m
340 lines (272 loc) · 10.2 KB
/
Fourier.m
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
classdef Fourier < handle
% Contains static methods for discrete Fourier analysis.
%%%%%%%%%%%%%%%%
% Math methods %
%%%%%%%%%%%%%%%%
methods (Static)
function fHat = fft(f)
% Calculates the 1d/2d fast Fourier transform of a vector/
% matrix.
%
% Params:
% - matrix f any complex input matrix
%
% Returns:
% - matrix fHat the complex output matrix
% Determine dimension of input vector
[n1, n2] = size(f);
if n1 > 1 && n2 > 1
fHat = Fourier.fft2(f);
elseif n1 == 1 || n2 == 1
fHat = Fourier.fft1(f);
else
error('Input y has invalid dimensions.');
end
end
function f = ifft(fHat)
% Calculates the 1d/2d inverse fast Fourier transform of a
% vector/matrix.
%
% Params:
% - matrix fHat any complex input matrix
%
% Returns:
% - matrix f the complex output matrix
% Determine dimension of input vector
[n1, n2] = size(fHat);
if n1 > 1 && n2 > 1
f = Fourier.ifft2(fHat);
elseif n1 == 1 || n2 == 1
f = Fourier.ifft1(fHat);
else
error('Input y has invalid dimensions.');
end
end
function xy = conv(x, y)
% Calculates the (circular) convolution using the fast Fourier
% transform.
%
% Params:
% - matrix x any complex input matrix
% - matrix y any complex input matrix
%
% Returns:
% - matrix xy the discrete convolution x*y
% Determine dimension of input data
[n1, n2] = size(x);
% Show error if necessary
if (isequal(size(x), size(y)) == 0)
error("Input vectors/matrices must have same dimensions.");
end
% Calculate convolution depending on dimension
if (n1 > 1 && n2 > 1)
xy = Fourier.ifft2(Fourier.fft2(x) .* Fourier.fft2(y));
elseif (n1 > 1 && n2 == 1) || (n1 == 1 && n2 > 1)
xy = Fourier.ifft(Fourier.fft1(x) .* Fourier.fft1(y));
else
error("Input data is invalid, matrices are of size 0.");
end
end
end
methods (Static, Access = private)
function fHat = fft1(f)
% Calculates the 1d fast Fourier transform of a vector.
%
% Params:
% - vector f any complex input vector
%
% Returns:
% - vector fHat the complex output vector
% Determine dimension of input vector
[n1, n2] = size(f);
m = min(n1, n2);
n = max(n1, n2);
% Show error if necessary
if m ~= 1
error("Input must not be a matrix.");
end
if n < 2 || rem(log2(n), 1) > 0
error("Input vector must be of size 2^m for any positive integer m.");
end
% If n is 2, apply the Fourier matrix
if n == 2
fHat = zeros(n1, n2);
fHat(1) = f(1)+f(2);
fHat(2) = f(1)-f(2);
return;
end
% Halve the value of n
n = n/2;
% Calculate c, d
c = Fourier.fft(f(1:2:2*n));
d = Fourier.fft(f(2:2:2*n));
% Correct d value
for k=1:1:n
d(k) = exp(-1i*pi*(k-1)/n)*d(k);
end
% Set the values and return
fHat = zeros(size(f));
fHat(1:n) = c+d;
fHat(n+1:2*n) = c-d;
end
function f = ifft1(fHat)
% Calculates the 1d inverse fast Fourier transform of a vector.
%
% Params:
% - vector fHat any complex input vector
%
% Returns:
% - vector f the complex output vector
% Calculate the IFFT through the FFT by this equivalence:
% f = 1/n conj(W)*fHat <=> f = 1/n conj(W*conj(fHat))
n = length(fHat);
fHat = conj(fHat);
f = 1/n * conj(Fourier.fft(fHat));
end
function fHat = fft2(f)
% Calculates the 2d fast Fourier transform of a matrix.
%
% Params:
% - matrix f any complex input matrix
%
% Returns:
% - matrix fHat the complex output matrix
% Determine dimension of input matrix
[n1, n2] = size(f);
n = n1;
% Show error if necessary
if abs(n2-n1) > 0
error("Input matrix must be square.");
end
if n < 2 || rem(log2(n), 1) > 0
error("Input matrix must be of size 2^m for any positive integer m.");
end
% Initialize zero matrices
fHatTemp = zeros(n, n);
fHat = zeros(n, n);
% Apply the 1d fast Fourier transform for all rows
for k=1:1:n
fHatTemp(k,:) = Fourier.fft1(f(k, :).').';
end
% Apply the 1d fast Fourier transform for all cols
for k=1:1:n
fHat(:,k) = Fourier.fft1(fHatTemp(:, k));
end
end
function f = ifft2(fHat)
% Calculates the 2d inverse fast Fourier transform of a matrix.
%
% Params:
% - matrix fHat any complex input matrix
%
% Returns:
% - matrix f the complex output matrix
% Calculate the IFFT through the FFT by this equivalence:
% f = 1/n^2 conj(W)*fHat <=> y = 1/n^2 conj(W*conj(fHat))
n = length(fHat);
fHat = conj(fHat);
f = 1/n^2 * conj(Fourier.fft2(fHat));
end
end
%%%%%%%%%%%%%%%%
% Test methods %
%%%%%%%%%%%%%%%%
methods (Static)
function test()
% Tests the functionality of the class and prints the result.
% Setup test variables
tol = 10^(-9);
amount = 5;
fprintf('========================================\n\n');
fprintf('Starting Fourier class test...\n\n');
fprintf('- Error tolerance: %d\n', tol);
fprintf('- Tests per method: %d\n', amount);
fprintf('\n');
% Test FFT
fprintf('----------\nTesting FFT:\n');
for k=1:amount
Fourier.fftTest(tol)
end
% Test IFFT
fprintf('----------\nTesting IFFT:\n');
for k=1:amount
Fourier.ifftTest(tol)
end
% Test FFT/IFFT
fprintf('----------\nTesting FFT&IFFT:\n');
for k=1:amount
Fourier.fftifftTest(tol)
end
% Test FFT2
fprintf('----------\nTesting FFT2:\n');
for k=1:amount
Fourier.fft2Test(tol)
end
% Test IFFT 2
fprintf('----------\nTesting IFFT2:\n');
for k=1:amount
Fourier.ifft2Test(tol)
end
% Test FFT2/IFFT2
fprintf('----------\nTesting FFT2&IFFT2:\n');
for k=1:amount
Fourier.fft2ifft2Test(tol)
end
% Test circular convolution
fprintf('----------\nTesting Convolution:\n');
for k=1:amount
Fourier.convTest(tol)
end
fprintf('----------\nTesting completed with 0 issues.\n');
end
end
methods (Static, Access = private)
function fftTest(tol)
y = Fourier.randc(2^16, 1);
diff = max(abs(Fourier.fft(y) - fft(y)));
Fourier.printTestResult(diff, tol);
end
function ifftTest(tol)
y = Fourier.randc(2^16, 1);
diff = max(abs(Fourier.ifft(y) - ifft(y)));
Fourier.printTestResult(diff, tol);
end
function fftifftTest(tol)
y = Fourier.randc(2^16, 1);
diff = max(abs(Fourier.fft(Fourier.ifft(y)) - y));
Fourier.printTestResult(diff, tol);
end
function fft2Test(tol)
y = Fourier.randc(2^8, 2^8);
diff = max(max(abs(Fourier.fft(y) - fft2(y))));
Fourier.printTestResult(diff, tol);
end
function ifft2Test(tol)
y = Fourier.randc(2^8, 2^8);
diff = max(max(abs(Fourier.ifft(y) - ifft2(y))));
Fourier.printTestResult(diff, tol);
end
function fft2ifft2Test(tol)
y = Fourier.randc(2^8, 2^8);
diff = max(max(abs(Fourier.fft(Fourier.ifft(y)) - y)));
Fourier.printTestResult(diff, tol);
end
function convTest(tol)
x = Fourier.randc(2^10, 1);
y = Fourier.randc(2^10, 1);
diff = max(abs(Fourier.conv(x, y) - cconv(x, y, 2^10)));
Fourier.printTestResult(diff, tol);
end
function y = randc(m, n)
y = randi(100, m, n) .* rand(m, n) + 1i * randi(100, m, n) .* rand(m, n);
end
function printTestResult(diff, tol)
if (diff > tol)
fprintf('[ ] Calculation error: %d\n', diff);
error('Error too high or tolerance too low.');
else
fprintf('[X] Calculation error: %d\n', diff);
end
end
end
end