dario mambro | cb97184 | 2020-03-28 00:22:33 +0100 | [diff] [blame^] | 1 | /* |
| 2 | Copyright (c) 2013 Julien Pommier. |
| 3 | |
| 4 | Small test & bench for PFFFT, comparing its performance with the scalar |
| 5 | FFTPACK, FFTW, and Apple vDSP |
| 6 | |
| 7 | How to build: |
| 8 | |
| 9 | on linux, with fftw3: |
| 10 | gcc -o test_pffft -DHAVE_FFTW -msse -mfpmath=sse -O3 -Wall -W pffft.c |
| 11 | test_pffft.c fftpack.c -L/usr/local/lib -I/usr/local/include/ -lfftw3f -lm |
| 12 | |
| 13 | on macos, without fftw3: |
| 14 | clang -o test_pffft -DHAVE_VECLIB -O3 -Wall -W pffft.c test_pffft.c fftpack.c |
| 15 | -L/usr/local/lib -I/usr/local/include/ -framework Accelerate |
| 16 | |
| 17 | on macos, with fftw3: |
| 18 | clang -o test_pffft -DHAVE_FFTW -DHAVE_VECLIB -O3 -Wall -W pffft.c |
| 19 | test_pffft.c fftpack.c -L/usr/local/lib -I/usr/local/include/ -lfftw3f |
| 20 | -framework Accelerate |
| 21 | |
| 22 | as alternative: replace clang by gcc. |
| 23 | |
| 24 | on windows, with visual c++: |
| 25 | cl /Ox -D_USE_MATH_DEFINES /arch:SSE test_pffft.c pffft.c fftpack.c |
| 26 | |
| 27 | build without SIMD instructions: |
| 28 | gcc -o test_pffft -DPFFFT_SIMD_DISABLE -O3 -Wall -W pffft.c test_pffft.c |
| 29 | fftpack.c -lm |
| 30 | |
| 31 | */ |
| 32 | |
| 33 | #include "pffft.hpp" |
| 34 | |
| 35 | #include <assert.h> |
| 36 | #include <math.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <time.h> |
| 41 | |
| 42 | /* maximum allowed phase error in degree */ |
| 43 | #define DEG_ERR_LIMIT 1E-4 |
| 44 | |
| 45 | /* maximum allowed magnitude error in amplitude (of 1.0 or 1.1) */ |
| 46 | #define MAG_ERR_LIMIT 1E-6 |
| 47 | |
| 48 | #define PRINT_SPEC 0 |
| 49 | |
| 50 | #define PWR2LOG(PWR) ((PWR) < 1E-30 ? 10.0 * log10(1E-30) : 10.0 * log10(PWR)) |
| 51 | |
| 52 | template<typename T> |
| 53 | bool |
| 54 | Ttest(int N, bool useOrdered) |
| 55 | { |
| 56 | using Fft = typename pffft::Fft<T>; |
| 57 | using Scalar = typename Fft::Scalar; |
| 58 | |
| 59 | bool cplx = std::is_same<T, std::complex<float>>::value || |
| 60 | std::is_same<T, std::complex<double>>::value; |
| 61 | |
| 62 | double EXPECTED_DYN_RANGE = |
| 63 | std::is_same<double, Scalar>::value ? 215.0 : 140.0; |
| 64 | |
| 65 | int Nsca = (cplx ? N * 2 : N); |
| 66 | int Ncplx = (cplx ? N : N / 2); |
| 67 | T* X = Fft::alignedAlloc<T>(Nsca); |
| 68 | T* Z = Fft::alignedAlloc<T>(Nsca); |
| 69 | 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 | |
| 234 | bool |
| 235 | test(int N, bool useComplex, bool useOrdered) |
| 236 | { |
| 237 | if (useComplex) { |
| 238 | return Ttest<std::complex<float>>(N, useOrdered) && |
| 239 | Ttest<std::complex<double>>(N, useOrdered); |
| 240 | } else { |
| 241 | return Ttest<float>(N, useOrdered) && Ttest<double>(N, useOrdered); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | main(int argc, char** argv) |
| 247 | { |
| 248 | int N, result, resN, resAll, k, resNextPw2, resIsPw2, resFFT; |
| 249 | |
| 250 | int inp_power_of_two[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 511, 512, 513 }; |
| 251 | int ref_power_of_two[] = { 1, 2, 4, 4, 8, 8, 8, 8, 16, 512, 512, 1024 }; |
| 252 | |
| 253 | resNextPw2 = 0; |
| 254 | resIsPw2 = 0; |
| 255 | for (k = 0; k < (sizeof(inp_power_of_two) / sizeof(inp_power_of_two[0])); |
| 256 | ++k) { |
| 257 | N = pffft::nextPowerOfTwo(inp_power_of_two[k]); |
| 258 | if (N != ref_power_of_two[k]) { |
| 259 | resNextPw2 = 1; |
| 260 | printf("pffft_next_power_of_two(%d) does deliver %d, which is not " |
| 261 | "reference result %d!\n", |
| 262 | inp_power_of_two[k], |
| 263 | N, |
| 264 | ref_power_of_two[k]); |
| 265 | } |
| 266 | |
| 267 | result = pffft::isPowerOfTwo(inp_power_of_two[k]); |
| 268 | if (inp_power_of_two[k] == ref_power_of_two[k]) { |
| 269 | if (!result) { |
| 270 | resIsPw2 = 1; |
| 271 | printf("pffft_is_power_of_two(%d) delivers false; expected true!\n", |
| 272 | inp_power_of_two[k]); |
| 273 | } |
| 274 | } else { |
| 275 | if (result) { |
| 276 | resIsPw2 = 1; |
| 277 | printf("pffft_is_power_of_two(%d) delivers true; expected false!\n", |
| 278 | inp_power_of_two[k]); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | if (!resNextPw2) |
| 283 | printf("tests for pffft_next_power_of_two() succeeded successfully.\n"); |
| 284 | if (!resIsPw2) |
| 285 | printf("tests for pffft_is_power_of_two() succeeded successfully.\n"); |
| 286 | |
| 287 | resFFT = 0; |
| 288 | for (N = 32; N <= 65536; N *= 2) { |
| 289 | result = test(N, 1 /* cplx fft */, 1 /* useOrdered */); |
| 290 | resN = result; |
| 291 | resFFT |= result; |
| 292 | |
| 293 | result = test(N, 0 /* cplx fft */, 1 /* useOrdered */); |
| 294 | resN |= result; |
| 295 | resFFT |= result; |
| 296 | |
| 297 | result = test(N, 1 /* cplx fft */, 0 /* useOrdered */); |
| 298 | resN |= result; |
| 299 | resFFT |= result; |
| 300 | |
| 301 | result = test(N, 0 /* cplx fft */, 0 /* useOrdered */); |
| 302 | resN |= result; |
| 303 | resFFT |= result; |
| 304 | |
| 305 | if (!resN) |
| 306 | printf("tests for size %d succeeded successfully.\n", N); |
| 307 | } |
| 308 | |
| 309 | if (!resFFT) |
| 310 | printf("all pffft transform tests (FORWARD/BACKWARD, REAL/COMPLEX) " |
| 311 | "succeeded successfully.\n"); |
| 312 | |
| 313 | resAll = resNextPw2 | resIsPw2 | resFFT; |
| 314 | if (!resAll) |
| 315 | printf("all tests succeeded successfully.\n"); |
| 316 | else |
| 317 | printf("there are failed tests!\n"); |
| 318 | |
| 319 | return resAll; |
| 320 | } |