blob: 29cecbb3f9a8a0d983d059b4fc45f38aefda29ef [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 ayguen61ec6da2020-03-29 08:48:01 +020066 const bool cplx = Fft::isComplexTransform();
dario mambrocb971842020-03-28 00:22:33 +010067
hayati ayguen61ec6da2020-03-29 08:48:01 +020068 const double EXPECTED_DYN_RANGE = Fft::isDoubleScalar() ? 215.0 : 140.0;
dario mambrocb971842020-03-28 00:22:33 +010069
dario mambrocb971842020-03-28 00:22:33 +010070 int k, j, m, iter, kmaxOther;
71 bool retError = false;
72 double freq, dPhi, phi, phi0;
73 double pwr, pwrCar, pwrOther, err, errSum, mag, expextedMag;
74 double amp = 1.0;
75
76 assert(pffft::isPowerOfTwo(N));
77
hayati ayguen61ec6da2020-03-29 08:48:01 +020078 Fft fft = Fft(N); // instantiate and prepareLength() for length N
dario mambrocb971842020-03-28 00:22:33 +010079
hayati ayguen1c193e92020-03-29 16:49:05 +020080 // with C++11 and auto - this gets a bit easier
81 typename Fft::ValueVector X = fft.valueVector(); // for X = input vector
82 typename Fft::ComplexVector Y = fft.spectrumVector(); // for Y = forward(X)
83 typename Fft::InternalLayoutVector R = fft.internalLayoutVector(); // for R = forwardInternalLayout(X)
84 typename Fft::ValueVector Z = fft.valueVector(); // for Z = inverse(Y) = inverse( forward(X) )
85 // or Z = inverseInternalLayout(R)
hayati ayguen61ec6da2020-03-29 08:48:01 +020086
87 // work with complex - without the capabilities of a higher c++ standard
hayati ayguen1c193e92020-03-29 16:49:05 +020088 Scalar* Xs = reinterpret_cast<Scalar*>(X.data()); // for X = input vector
89 Scalar* Ys = reinterpret_cast<Scalar*>(Y.data()); // for Y = forward(X)
90 Scalar* Zs = reinterpret_cast<Scalar*>(Z.data()); // for Z = inverse(Y) = inverse( forward(X) )
dario mambrocb971842020-03-28 00:22:33 +010091
92 for (k = m = 0; k < (cplx ? N : (1 + N / 2)); k += N / 16, ++m) {
93 amp = ((m % 3) == 0) ? 1.0F : 1.1F;
94 freq = (k < N / 2) ? ((double)k / N) : ((double)(k - N) / N);
95 dPhi = 2.0 * M_PI * freq;
96 if (dPhi < 0.0)
97 dPhi += 2.0 * M_PI;
98
99 iter = -1;
100 while (1) {
101 ++iter;
102
103 if (iter)
104 printf("bin %d: dphi = %f for freq %f\n", k, dPhi, freq);
105
106 /* generate cosine carrier as time signal - start at defined phase phi0 */
107 phi = phi0 =
108 (m % 4) * 0.125 * M_PI; /* have phi0 < 90 deg to be normalized */
109 for (j = 0; j < N; ++j) {
110 if (cplx) {
111 Xs[2 * j] = amp * cos(phi); /* real part */
112 Xs[2 * j + 1] = amp * sin(phi); /* imag part */
113 } else
114 Xs[j] = amp * cos(phi); /* only real part */
115
116 /* phase increment .. stay normalized - cos()/sin() might degrade! */
117 phi += dPhi;
118 if (phi >= M_PI)
119 phi -= 2.0 * M_PI;
120 }
121
122 /* forward transform from X --> Y .. using work buffer W */
123 if (useOrdered)
124 fft.forward(X, Y);
125 else {
hayati ayguen1c193e92020-03-29 16:49:05 +0200126 fft.forwardToInternalLayout(X, R); /* use R for reordering */
127 fft.reorderSpectrum(R, Y); /* have canonical order in Y[] for power calculations */
dario mambrocb971842020-03-28 00:22:33 +0100128 }
129
130 pwrOther = -1.0;
131 pwrCar = 0;
132
133 /* for positive frequencies: 0 to 0.5 * samplerate */
134 /* and also for negative frequencies: -0.5 * samplerate to 0 */
135 for (j = 0; j < (cplx ? N : (1 + N / 2)); ++j) {
136 if (!cplx && !j) /* special treatment for DC for real input */
137 pwr = Ys[j] * Ys[j];
138 else if (!cplx && j == N / 2) /* treat 0.5 * samplerate */
139 pwr = Ys[1] *
140 Ys[1]; /* despite j (for freq calculation) we have index 1 */
141 else
142 pwr = Ys[2 * j] * Ys[2 * j] + Ys[2 * j + 1] * Ys[2 * j + 1];
143 if (iter || PRINT_SPEC)
144 printf("%s fft %d: pwr[j = %d] = %g == %f dB\n",
145 (cplx ? "cplx" : "real"),
146 N,
147 j,
148 pwr,
149 PWR2LOG(pwr));
150 if (k == j)
151 pwrCar = pwr;
152 else if (pwr > pwrOther) {
153 pwrOther = pwr;
154 kmaxOther = j;
155 }
156 }
157
158 if (PWR2LOG(pwrCar) - PWR2LOG(pwrOther) < EXPECTED_DYN_RANGE) {
159 printf("%s fft %d amp %f iter %d:\n",
160 (cplx ? "cplx" : "real"),
161 N,
162 amp,
163 iter);
164 printf(" carrier power at bin %d: %g == %f dB\n",
165 k,
166 pwrCar,
167 PWR2LOG(pwrCar));
168 printf(" carrier mag || at bin %d: %g\n", k, sqrt(pwrCar));
169 printf(" max other pwr at bin %d: %g == %f dB\n",
170 kmaxOther,
171 pwrOther,
172 PWR2LOG(pwrOther));
173 printf(" dynamic range: %f dB\n\n",
174 PWR2LOG(pwrCar) - PWR2LOG(pwrOther));
175 retError = true;
176 if (iter == 0)
177 continue;
178 }
179
180 if (k > 0 && k != N / 2) {
181 phi = atan2(Ys[2 * k + 1], Ys[2 * k]);
182 if (fabs(phi - phi0) > DEG_ERR_LIMIT * M_PI / 180.0) {
183 retError = true;
184 printf("%s fft %d bin %d amp %f : phase mismatch! phase = %f deg "
185 "expected = %f deg\n",
186 (cplx ? "cplx" : "real"),
187 N,
188 k,
189 amp,
190 phi * 180.0 / M_PI,
191 phi0 * 180.0 / M_PI);
192 }
193 }
194
195 expextedMag = cplx ? amp : ((k == 0 || k == N / 2) ? amp : (amp / 2));
196 mag = sqrt(pwrCar) / N;
197 if (fabs(mag - expextedMag) > MAG_ERR_LIMIT) {
198 retError = true;
199 printf("%s fft %d bin %d amp %f : mag = %g expected = %g\n",
200 (cplx ? "cplx" : "real"),
201 N,
202 k,
203 amp,
204 mag,
205 expextedMag);
206 }
207
208 /* now convert spectrum back */
hayati ayguen61ec6da2020-03-29 08:48:01 +0200209 if (useOrdered)
210 fft.inverse(Y, Z);
211 else
hayati ayguen1c193e92020-03-29 16:49:05 +0200212 fft.inverseFromInternalLayout(R, Z); /* inverse() from internal Layout */
dario mambrocb971842020-03-28 00:22:33 +0100213
214 errSum = 0.0;
215 for (j = 0; j < (cplx ? (2 * N) : N); ++j) {
216 /* scale back */
hayati ayguen61ec6da2020-03-29 08:48:01 +0200217 Zs[j] /= N;
dario mambrocb971842020-03-28 00:22:33 +0100218 /* square sum errors over real (and imag parts) */
219 err = (Xs[j] - Zs[j]) * (Xs[j] - Zs[j]);
220 errSum += err;
221 }
222
223 if (errSum > N * 1E-7) {
224 retError = true;
225 printf("%s fft %d bin %d : inverse FFT doesn't match original signal! "
226 "errSum = %g ; mean err = %g\n",
227 (cplx ? "cplx" : "real"),
228 N,
229 k,
230 errSum,
231 errSum / N);
232 }
233
234 break;
235 }
236 }
hayati ayguen1c193e92020-03-29 16:49:05 +0200237
238 // using the std::vector<> base classes .. no need for alignedFree() for X, Y, Z and R
dario mambrocb971842020-03-28 00:22:33 +0100239
240 return retError;
241}
242
243bool
244test(int N, bool useComplex, bool useOrdered)
245{
246 if (useComplex) {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200247 return Ttest< std::complex<float> >(N, useOrdered) &&
248 Ttest< std::complex<double> >(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100249 } else {
hayati ayguen7b3ca7d2020-03-29 03:18:35 +0200250 return Ttest<float>(N, useOrdered) &&
251 Ttest<double>(N, useOrdered);
dario mambrocb971842020-03-28 00:22:33 +0100252 }
253}
254
255int
256main(int argc, char** argv)
257{
258 int N, result, resN, resAll, k, resNextPw2, resIsPw2, resFFT;
259
260 int inp_power_of_two[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 511, 512, 513 };
261 int ref_power_of_two[] = { 1, 2, 4, 4, 8, 8, 8, 8, 16, 512, 512, 1024 };
262
263 resNextPw2 = 0;
264 resIsPw2 = 0;
265 for (k = 0; k < (sizeof(inp_power_of_two) / sizeof(inp_power_of_two[0]));
266 ++k) {
267 N = pffft::nextPowerOfTwo(inp_power_of_two[k]);
268 if (N != ref_power_of_two[k]) {
269 resNextPw2 = 1;
270 printf("pffft_next_power_of_two(%d) does deliver %d, which is not "
271 "reference result %d!\n",
272 inp_power_of_two[k],
273 N,
274 ref_power_of_two[k]);
275 }
276
277 result = pffft::isPowerOfTwo(inp_power_of_two[k]);
278 if (inp_power_of_two[k] == ref_power_of_two[k]) {
279 if (!result) {
280 resIsPw2 = 1;
281 printf("pffft_is_power_of_two(%d) delivers false; expected true!\n",
282 inp_power_of_two[k]);
283 }
284 } else {
285 if (result) {
286 resIsPw2 = 1;
287 printf("pffft_is_power_of_two(%d) delivers true; expected false!\n",
288 inp_power_of_two[k]);
289 }
290 }
291 }
292 if (!resNextPw2)
293 printf("tests for pffft_next_power_of_two() succeeded successfully.\n");
294 if (!resIsPw2)
295 printf("tests for pffft_is_power_of_two() succeeded successfully.\n");
296
297 resFFT = 0;
298 for (N = 32; N <= 65536; N *= 2) {
299 result = test(N, 1 /* cplx fft */, 1 /* useOrdered */);
300 resN = result;
301 resFFT |= result;
302
303 result = test(N, 0 /* cplx fft */, 1 /* useOrdered */);
304 resN |= result;
305 resFFT |= result;
306
307 result = test(N, 1 /* cplx fft */, 0 /* useOrdered */);
308 resN |= result;
309 resFFT |= result;
310
311 result = test(N, 0 /* cplx fft */, 0 /* useOrdered */);
312 resN |= result;
313 resFFT |= result;
314
315 if (!resN)
316 printf("tests for size %d succeeded successfully.\n", N);
317 }
318
319 if (!resFFT)
hayati ayguen63794b22020-03-29 03:14:43 +0200320 printf("all pffft transform tests (FORWARD/BACKWARD, REAL/COMPLEX, float/double) "
dario mambrocb971842020-03-28 00:22:33 +0100321 "succeeded successfully.\n");
322
323 resAll = resNextPw2 | resIsPw2 | resFFT;
324 if (!resAll)
325 printf("all tests succeeded successfully.\n");
326 else
327 printf("there are failed tests!\n");
328
329 return resAll;
330}