blob: f5d085dba3caf60c275ebcec896af7024afecd46 [file] [log] [blame]
dario mambrocb971842020-03-28 00:22:33 +01001/*
hayati ayguen63794b22020-03-29 03:14:43 +02002 Copyright (c) 2013 Julien Pommier ( pommier@modartt.com )
3 Copyright (c) 2020 Dario Mambro ( dario.mambro@gmail.com )
4 Copyright (c) 2020 Hayati Ayguen ( h_ayguen@web.de )
dario mambrocb971842020-03-28 00:22:33 +01005
6 Small test & bench for PFFFT, comparing its performance with the scalar
7 FFTPACK, FFTW, and Apple vDSP
8
9 How to build:
10
11 on linux, with fftw3:
12 gcc -o test_pffft -DHAVE_FFTW -msse -mfpmath=sse -O3 -Wall -W pffft.c
13 test_pffft.c fftpack.c -L/usr/local/lib -I/usr/local/include/ -lfftw3f -lm
14
15 on macos, without fftw3:
16 clang -o test_pffft -DHAVE_VECLIB -O3 -Wall -W pffft.c test_pffft.c fftpack.c
17 -L/usr/local/lib -I/usr/local/include/ -framework Accelerate
18
19 on macos, with fftw3:
20 clang -o test_pffft -DHAVE_FFTW -DHAVE_VECLIB -O3 -Wall -W pffft.c
21 test_pffft.c fftpack.c -L/usr/local/lib -I/usr/local/include/ -lfftw3f
22 -framework Accelerate
23
24 as alternative: replace clang by gcc.
25
26 on windows, with visual c++:
27 cl /Ox -D_USE_MATH_DEFINES /arch:SSE test_pffft.c pffft.c fftpack.c
28
29 build without SIMD instructions:
30 gcc -o test_pffft -DPFFFT_SIMD_DISABLE -O3 -Wall -W pffft.c test_pffft.c
31 fftpack.c -lm
32
33 */
34
35#include "pffft.hpp"
36
37#include <assert.h>
38#include <math.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43
44/* maximum allowed phase error in degree */
45#define DEG_ERR_LIMIT 1E-4
46
47/* maximum allowed magnitude error in amplitude (of 1.0 or 1.1) */
48#define MAG_ERR_LIMIT 1E-6
49
50#define PRINT_SPEC 0
51
52#define PWR2LOG(PWR) ((PWR) < 1E-30 ? 10.0 * log10(1E-30) : 10.0 * log10(PWR))
53
54template<typename T>
55bool
56Ttest(int N, bool useOrdered)
57{
hayati ayguen63794b22020-03-29 03:14:43 +020058 typedef typename pffft::Fft<T> Fft;
59 typedef typename Fft::Scalar Scalar;
dario mambrocb971842020-03-28 00:22:33 +010060
hayati ayguen63794b22020-03-29 03:14:43 +020061 bool cplx = ( sizeof(T) == sizeof(std::complex<Scalar>) );
dario mambrocb971842020-03-28 00:22:33 +010062
hayati ayguen63794b22020-03-29 03:14:43 +020063 double EXPECTED_DYN_RANGE = ( sizeof(double) == sizeof(Scalar) ) ? 215.0 : 140.0;
dario mambrocb971842020-03-28 00:22:33 +010064
65 int Nsca = (cplx ? N * 2 : N);
66 int Ncplx = (cplx ? N : N / 2);
hayati ayguen63794b22020-03-29 03:14:43 +020067 T* X = Fft::alignedAllocType(Nsca);
68 T* Z = Fft::alignedAllocType(Nsca);
dario mambrocb971842020-03-28 00:22:33 +010069 Scalar* R = Fft::alignedAllocScalar(Nsca);
70 std::complex<Scalar>* Y = Fft::alignedAllocComplex(Nsca);
71 int k, j, m, iter, kmaxOther;
72 bool retError = false;
73 double freq, dPhi, phi, phi0;
74 double pwr, pwrCar, pwrOther, err, errSum, mag, expextedMag;
75 double amp = 1.0;
76
77 assert(pffft::isPowerOfTwo(N));
78
79 Fft fft = Fft(N);
80
81 Scalar* Xs = reinterpret_cast<Scalar*>(X);
82 Scalar* Ys = reinterpret_cast<Scalar*>(Y);
83 Scalar* Zs = reinterpret_cast<Scalar*>(Z);
84
85 for (k = m = 0; k < (cplx ? N : (1 + N / 2)); k += N / 16, ++m) {
86 amp = ((m % 3) == 0) ? 1.0F : 1.1F;
87 freq = (k < N / 2) ? ((double)k / N) : ((double)(k - N) / N);
88 dPhi = 2.0 * M_PI * freq;
89 if (dPhi < 0.0)
90 dPhi += 2.0 * M_PI;
91
92 iter = -1;
93 while (1) {
94 ++iter;
95
96 if (iter)
97 printf("bin %d: dphi = %f for freq %f\n", k, dPhi, freq);
98
99 /* generate cosine carrier as time signal - start at defined phase phi0 */
100 phi = phi0 =
101 (m % 4) * 0.125 * M_PI; /* have phi0 < 90 deg to be normalized */
102 for (j = 0; j < N; ++j) {
103 if (cplx) {
104 Xs[2 * j] = amp * cos(phi); /* real part */
105 Xs[2 * j + 1] = amp * sin(phi); /* imag part */
106 } else
107 Xs[j] = amp * cos(phi); /* only real part */
108
109 /* phase increment .. stay normalized - cos()/sin() might degrade! */
110 phi += dPhi;
111 if (phi >= M_PI)
112 phi -= 2.0 * M_PI;
113 }
114
115 /* forward transform from X --> Y .. using work buffer W */
116 if (useOrdered)
117 fft.forward(X, Y);
118 else {
119 fft.forwardInternalLayout(X, R); /* temporarily use R for reordering */
120 fft.reorderSpectrum(R, Y, PFFFT_FORWARD);
121 }
122
123 pwrOther = -1.0;
124 pwrCar = 0;
125
126 /* for positive frequencies: 0 to 0.5 * samplerate */
127 /* and also for negative frequencies: -0.5 * samplerate to 0 */
128 for (j = 0; j < (cplx ? N : (1 + N / 2)); ++j) {
129 if (!cplx && !j) /* special treatment for DC for real input */
130 pwr = Ys[j] * Ys[j];
131 else if (!cplx && j == N / 2) /* treat 0.5 * samplerate */
132 pwr = Ys[1] *
133 Ys[1]; /* despite j (for freq calculation) we have index 1 */
134 else
135 pwr = Ys[2 * j] * Ys[2 * j] + Ys[2 * j + 1] * Ys[2 * j + 1];
136 if (iter || PRINT_SPEC)
137 printf("%s fft %d: pwr[j = %d] = %g == %f dB\n",
138 (cplx ? "cplx" : "real"),
139 N,
140 j,
141 pwr,
142 PWR2LOG(pwr));
143 if (k == j)
144 pwrCar = pwr;
145 else if (pwr > pwrOther) {
146 pwrOther = pwr;
147 kmaxOther = j;
148 }
149 }
150
151 if (PWR2LOG(pwrCar) - PWR2LOG(pwrOther) < EXPECTED_DYN_RANGE) {
152 printf("%s fft %d amp %f iter %d:\n",
153 (cplx ? "cplx" : "real"),
154 N,
155 amp,
156 iter);
157 printf(" carrier power at bin %d: %g == %f dB\n",
158 k,
159 pwrCar,
160 PWR2LOG(pwrCar));
161 printf(" carrier mag || at bin %d: %g\n", k, sqrt(pwrCar));
162 printf(" max other pwr at bin %d: %g == %f dB\n",
163 kmaxOther,
164 pwrOther,
165 PWR2LOG(pwrOther));
166 printf(" dynamic range: %f dB\n\n",
167 PWR2LOG(pwrCar) - PWR2LOG(pwrOther));
168 retError = true;
169 if (iter == 0)
170 continue;
171 }
172
173 if (k > 0 && k != N / 2) {
174 phi = atan2(Ys[2 * k + 1], Ys[2 * k]);
175 if (fabs(phi - phi0) > DEG_ERR_LIMIT * M_PI / 180.0) {
176 retError = true;
177 printf("%s fft %d bin %d amp %f : phase mismatch! phase = %f deg "
178 "expected = %f deg\n",
179 (cplx ? "cplx" : "real"),
180 N,
181 k,
182 amp,
183 phi * 180.0 / M_PI,
184 phi0 * 180.0 / M_PI);
185 }
186 }
187
188 expextedMag = cplx ? amp : ((k == 0 || k == N / 2) ? amp : (amp / 2));
189 mag = sqrt(pwrCar) / N;
190 if (fabs(mag - expextedMag) > MAG_ERR_LIMIT) {
191 retError = true;
192 printf("%s fft %d bin %d amp %f : mag = %g expected = %g\n",
193 (cplx ? "cplx" : "real"),
194 N,
195 k,
196 amp,
197 mag,
198 expextedMag);
199 }
200
201 /* now convert spectrum back */
202 fft.inverse(Y, Z);
203
204 errSum = 0.0;
205 for (j = 0; j < (cplx ? (2 * N) : N); ++j) {
206 /* scale back */
207 Z[j] /= N;
208 /* square sum errors over real (and imag parts) */
209 err = (Xs[j] - Zs[j]) * (Xs[j] - Zs[j]);
210 errSum += err;
211 }
212
213 if (errSum > N * 1E-7) {
214 retError = true;
215 printf("%s fft %d bin %d : inverse FFT doesn't match original signal! "
216 "errSum = %g ; mean err = %g\n",
217 (cplx ? "cplx" : "real"),
218 N,
219 k,
220 errSum,
221 errSum / N);
222 }
223
224 break;
225 }
226 }
227 pffft::alignedFree(X);
228 pffft::alignedFree(Y);
229 pffft::alignedFree(Z);
230
231 return retError;
232}
233
234bool
235test(int N, bool useComplex, bool useOrdered)
236{
237 if (useComplex) {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200238 return Ttest< std::complex<float> >(N, useOrdered) &&
239 Ttest< std::complex<double> >(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100240 } else {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200241 return Ttest<float>(N, useOrdered) &&
242 Ttest<double>(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100243 }
244}
245
246int
247main(int argc, char** argv)
248{
249 int N, result, resN, resAll, k, resNextPw2, resIsPw2, resFFT;
250
251 int inp_power_of_two[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 511, 512, 513 };
252 int ref_power_of_two[] = { 1, 2, 4, 4, 8, 8, 8, 8, 16, 512, 512, 1024 };
253
254 resNextPw2 = 0;
255 resIsPw2 = 0;
256 for (k = 0; k < (sizeof(inp_power_of_two) / sizeof(inp_power_of_two[0]));
257 ++k) {
258 N = pffft::nextPowerOfTwo(inp_power_of_two[k]);
259 if (N != ref_power_of_two[k]) {
260 resNextPw2 = 1;
261 printf("pffft_next_power_of_two(%d) does deliver %d, which is not "
262 "reference result %d!\n",
263 inp_power_of_two[k],
264 N,
265 ref_power_of_two[k]);
266 }
267
268 result = pffft::isPowerOfTwo(inp_power_of_two[k]);
269 if (inp_power_of_two[k] == ref_power_of_two[k]) {
270 if (!result) {
271 resIsPw2 = 1;
272 printf("pffft_is_power_of_two(%d) delivers false; expected true!\n",
273 inp_power_of_two[k]);
274 }
275 } else {
276 if (result) {
277 resIsPw2 = 1;
278 printf("pffft_is_power_of_two(%d) delivers true; expected false!\n",
279 inp_power_of_two[k]);
280 }
281 }
282 }
283 if (!resNextPw2)
284 printf("tests for pffft_next_power_of_two() succeeded successfully.\n");
285 if (!resIsPw2)
286 printf("tests for pffft_is_power_of_two() succeeded successfully.\n");
287
288 resFFT = 0;
289 for (N = 32; N <= 65536; N *= 2) {
290 result = test(N, 1 /* cplx fft */, 1 /* useOrdered */);
291 resN = result;
292 resFFT |= result;
293
294 result = test(N, 0 /* cplx fft */, 1 /* useOrdered */);
295 resN |= result;
296 resFFT |= result;
297
298 result = test(N, 1 /* cplx fft */, 0 /* useOrdered */);
299 resN |= result;
300 resFFT |= result;
301
302 result = test(N, 0 /* cplx fft */, 0 /* useOrdered */);
303 resN |= result;
304 resFFT |= result;
305
306 if (!resN)
307 printf("tests for size %d succeeded successfully.\n", N);
308 }
309
310 if (!resFFT)
hayati ayguen63794b22020-03-29 03:14:43 +0200311 printf("all pffft transform tests (FORWARD/BACKWARD, REAL/COMPLEX, float/double) "
dario mambrocb971842020-03-28 00:22:33 +0100312 "succeeded successfully.\n");
313
314 resAll = resNextPw2 | resIsPw2 | resFFT;
315 if (!resAll)
316 printf("all tests succeeded successfully.\n");
317 else
318 printf("there are failed tests!\n");
319
320 return resAll;
321}