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