blob: a5448706c00251bc51d633716eee0c0a7c2eb149 [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
hayati ayguenc974c1d2020-03-29 03:39:30 +020044/* define own constants required to turn off g++ extensions .. */
45#ifndef M_PI
46 #define M_PI 3.14159265358979323846 /* pi */
47#endif
48
dario mambrocb971842020-03-28 00:22:33 +010049/* maximum allowed phase error in degree */
50#define DEG_ERR_LIMIT 1E-4
51
52/* maximum allowed magnitude error in amplitude (of 1.0 or 1.1) */
53#define MAG_ERR_LIMIT 1E-6
54
55#define PRINT_SPEC 0
56
57#define PWR2LOG(PWR) ((PWR) < 1E-30 ? 10.0 * log10(1E-30) : 10.0 * log10(PWR))
58
59template<typename T>
60bool
61Ttest(int N, bool useOrdered)
62{
hayati ayguen63794b22020-03-29 03:14:43 +020063 typedef typename pffft::Fft<T> Fft;
64 typedef typename Fft::Scalar Scalar;
dario mambrocb971842020-03-28 00:22:33 +010065
hayati ayguen63794b22020-03-29 03:14:43 +020066 bool cplx = ( sizeof(T) == sizeof(std::complex<Scalar>) );
dario mambrocb971842020-03-28 00:22:33 +010067
hayati ayguen63794b22020-03-29 03:14:43 +020068 double EXPECTED_DYN_RANGE = ( sizeof(double) == sizeof(Scalar) ) ? 215.0 : 140.0;
dario mambrocb971842020-03-28 00:22:33 +010069
70 int Nsca = (cplx ? N * 2 : N);
71 int Ncplx = (cplx ? N : N / 2);
hayati ayguen63794b22020-03-29 03:14:43 +020072 T* X = Fft::alignedAllocType(Nsca);
73 T* Z = Fft::alignedAllocType(Nsca);
dario mambrocb971842020-03-28 00:22:33 +010074 Scalar* R = Fft::alignedAllocScalar(Nsca);
75 std::complex<Scalar>* Y = Fft::alignedAllocComplex(Nsca);
76 int k, j, m, iter, kmaxOther;
77 bool retError = false;
78 double freq, dPhi, phi, phi0;
79 double pwr, pwrCar, pwrOther, err, errSum, mag, expextedMag;
80 double amp = 1.0;
81
82 assert(pffft::isPowerOfTwo(N));
83
84 Fft fft = Fft(N);
85
86 Scalar* Xs = reinterpret_cast<Scalar*>(X);
87 Scalar* Ys = reinterpret_cast<Scalar*>(Y);
88 Scalar* Zs = reinterpret_cast<Scalar*>(Z);
89
90 for (k = m = 0; k < (cplx ? N : (1 + N / 2)); k += N / 16, ++m) {
91 amp = ((m % 3) == 0) ? 1.0F : 1.1F;
92 freq = (k < N / 2) ? ((double)k / N) : ((double)(k - N) / N);
93 dPhi = 2.0 * M_PI * freq;
94 if (dPhi < 0.0)
95 dPhi += 2.0 * M_PI;
96
97 iter = -1;
98 while (1) {
99 ++iter;
100
101 if (iter)
102 printf("bin %d: dphi = %f for freq %f\n", k, dPhi, freq);
103
104 /* generate cosine carrier as time signal - start at defined phase phi0 */
105 phi = phi0 =
106 (m % 4) * 0.125 * M_PI; /* have phi0 < 90 deg to be normalized */
107 for (j = 0; j < N; ++j) {
108 if (cplx) {
109 Xs[2 * j] = amp * cos(phi); /* real part */
110 Xs[2 * j + 1] = amp * sin(phi); /* imag part */
111 } else
112 Xs[j] = amp * cos(phi); /* only real part */
113
114 /* phase increment .. stay normalized - cos()/sin() might degrade! */
115 phi += dPhi;
116 if (phi >= M_PI)
117 phi -= 2.0 * M_PI;
118 }
119
120 /* forward transform from X --> Y .. using work buffer W */
121 if (useOrdered)
122 fft.forward(X, Y);
123 else {
124 fft.forwardInternalLayout(X, R); /* temporarily use R for reordering */
125 fft.reorderSpectrum(R, Y, PFFFT_FORWARD);
126 }
127
128 pwrOther = -1.0;
129 pwrCar = 0;
130
131 /* for positive frequencies: 0 to 0.5 * samplerate */
132 /* and also for negative frequencies: -0.5 * samplerate to 0 */
133 for (j = 0; j < (cplx ? N : (1 + N / 2)); ++j) {
134 if (!cplx && !j) /* special treatment for DC for real input */
135 pwr = Ys[j] * Ys[j];
136 else if (!cplx && j == N / 2) /* treat 0.5 * samplerate */
137 pwr = Ys[1] *
138 Ys[1]; /* despite j (for freq calculation) we have index 1 */
139 else
140 pwr = Ys[2 * j] * Ys[2 * j] + Ys[2 * j + 1] * Ys[2 * j + 1];
141 if (iter || PRINT_SPEC)
142 printf("%s fft %d: pwr[j = %d] = %g == %f dB\n",
143 (cplx ? "cplx" : "real"),
144 N,
145 j,
146 pwr,
147 PWR2LOG(pwr));
148 if (k == j)
149 pwrCar = pwr;
150 else if (pwr > pwrOther) {
151 pwrOther = pwr;
152 kmaxOther = j;
153 }
154 }
155
156 if (PWR2LOG(pwrCar) - PWR2LOG(pwrOther) < EXPECTED_DYN_RANGE) {
157 printf("%s fft %d amp %f iter %d:\n",
158 (cplx ? "cplx" : "real"),
159 N,
160 amp,
161 iter);
162 printf(" carrier power at bin %d: %g == %f dB\n",
163 k,
164 pwrCar,
165 PWR2LOG(pwrCar));
166 printf(" carrier mag || at bin %d: %g\n", k, sqrt(pwrCar));
167 printf(" max other pwr at bin %d: %g == %f dB\n",
168 kmaxOther,
169 pwrOther,
170 PWR2LOG(pwrOther));
171 printf(" dynamic range: %f dB\n\n",
172 PWR2LOG(pwrCar) - PWR2LOG(pwrOther));
173 retError = true;
174 if (iter == 0)
175 continue;
176 }
177
178 if (k > 0 && k != N / 2) {
179 phi = atan2(Ys[2 * k + 1], Ys[2 * k]);
180 if (fabs(phi - phi0) > DEG_ERR_LIMIT * M_PI / 180.0) {
181 retError = true;
182 printf("%s fft %d bin %d amp %f : phase mismatch! phase = %f deg "
183 "expected = %f deg\n",
184 (cplx ? "cplx" : "real"),
185 N,
186 k,
187 amp,
188 phi * 180.0 / M_PI,
189 phi0 * 180.0 / M_PI);
190 }
191 }
192
193 expextedMag = cplx ? amp : ((k == 0 || k == N / 2) ? amp : (amp / 2));
194 mag = sqrt(pwrCar) / N;
195 if (fabs(mag - expextedMag) > MAG_ERR_LIMIT) {
196 retError = true;
197 printf("%s fft %d bin %d amp %f : mag = %g expected = %g\n",
198 (cplx ? "cplx" : "real"),
199 N,
200 k,
201 amp,
202 mag,
203 expextedMag);
204 }
205
206 /* now convert spectrum back */
207 fft.inverse(Y, Z);
208
209 errSum = 0.0;
210 for (j = 0; j < (cplx ? (2 * N) : N); ++j) {
211 /* scale back */
212 Z[j] /= N;
213 /* square sum errors over real (and imag parts) */
214 err = (Xs[j] - Zs[j]) * (Xs[j] - Zs[j]);
215 errSum += err;
216 }
217
218 if (errSum > N * 1E-7) {
219 retError = true;
220 printf("%s fft %d bin %d : inverse FFT doesn't match original signal! "
221 "errSum = %g ; mean err = %g\n",
222 (cplx ? "cplx" : "real"),
223 N,
224 k,
225 errSum,
226 errSum / N);
227 }
228
229 break;
230 }
231 }
232 pffft::alignedFree(X);
233 pffft::alignedFree(Y);
234 pffft::alignedFree(Z);
235
236 return retError;
237}
238
239bool
240test(int N, bool useComplex, bool useOrdered)
241{
242 if (useComplex) {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200243 return Ttest< std::complex<float> >(N, useOrdered) &&
244 Ttest< std::complex<double> >(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100245 } else {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200246 return Ttest<float>(N, useOrdered) &&
247 Ttest<double>(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100248 }
249}
250
251int
252main(int argc, char** argv)
253{
254 int N, result, resN, resAll, k, resNextPw2, resIsPw2, resFFT;
255
256 int inp_power_of_two[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 511, 512, 513 };
257 int ref_power_of_two[] = { 1, 2, 4, 4, 8, 8, 8, 8, 16, 512, 512, 1024 };
258
259 resNextPw2 = 0;
260 resIsPw2 = 0;
261 for (k = 0; k < (sizeof(inp_power_of_two) / sizeof(inp_power_of_two[0]));
262 ++k) {
263 N = pffft::nextPowerOfTwo(inp_power_of_two[k]);
264 if (N != ref_power_of_two[k]) {
265 resNextPw2 = 1;
266 printf("pffft_next_power_of_two(%d) does deliver %d, which is not "
267 "reference result %d!\n",
268 inp_power_of_two[k],
269 N,
270 ref_power_of_two[k]);
271 }
272
273 result = pffft::isPowerOfTwo(inp_power_of_two[k]);
274 if (inp_power_of_two[k] == ref_power_of_two[k]) {
275 if (!result) {
276 resIsPw2 = 1;
277 printf("pffft_is_power_of_two(%d) delivers false; expected true!\n",
278 inp_power_of_two[k]);
279 }
280 } else {
281 if (result) {
282 resIsPw2 = 1;
283 printf("pffft_is_power_of_two(%d) delivers true; expected false!\n",
284 inp_power_of_two[k]);
285 }
286 }
287 }
288 if (!resNextPw2)
289 printf("tests for pffft_next_power_of_two() succeeded successfully.\n");
290 if (!resIsPw2)
291 printf("tests for pffft_is_power_of_two() succeeded successfully.\n");
292
293 resFFT = 0;
294 for (N = 32; N <= 65536; N *= 2) {
295 result = test(N, 1 /* cplx fft */, 1 /* useOrdered */);
296 resN = result;
297 resFFT |= result;
298
299 result = test(N, 0 /* cplx fft */, 1 /* useOrdered */);
300 resN |= result;
301 resFFT |= result;
302
303 result = test(N, 1 /* cplx fft */, 0 /* useOrdered */);
304 resN |= result;
305 resFFT |= result;
306
307 result = test(N, 0 /* cplx fft */, 0 /* useOrdered */);
308 resN |= result;
309 resFFT |= result;
310
311 if (!resN)
312 printf("tests for size %d succeeded successfully.\n", N);
313 }
314
315 if (!resFFT)
hayati ayguen63794b22020-03-29 03:14:43 +0200316 printf("all pffft transform tests (FORWARD/BACKWARD, REAL/COMPLEX, float/double) "
dario mambrocb971842020-03-28 00:22:33 +0100317 "succeeded successfully.\n");
318
319 resAll = resNextPw2 | resIsPw2 | resFFT;
320 if (!resAll)
321 printf("all tests succeeded successfully.\n");
322 else
323 printf("there are failed tests!\n");
324
325 return resAll;
326}