OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <math.h> |
| 12 #include <stdio.h> |
| 13 #include <stdlib.h> |
| 14 #include <time.h> |
| 15 #include <unistd.h> |
| 16 |
| 17 #include "dl/sp/api/armSP.h" |
| 18 #include "dl/sp/api/omxSP.h" |
| 19 #include "dl/sp/src/test/aligned_ptr.h" |
| 20 #include "dl/sp/src/test/compare.h" |
| 21 #include "dl/sp/src/test/gensig.h" |
| 22 #include "dl/sp/src/test/test_util.h" |
| 23 |
| 24 #define MAX_FFT_ORDER 12 |
| 25 |
| 26 int verbose; |
| 27 int signal_value; |
| 28 |
| 29 void TestFFT(int fft_log_size, int sigtype, int scale_factor); |
| 30 |
| 31 void main(int argc, char* argv[]) { |
| 32 struct Options options; |
| 33 |
| 34 SetDefaultOptions(&options, 0, MAX_FFT_ORDER); |
| 35 |
| 36 ProcessCommandLine(&options, argc, argv, |
| 37 "Test forward and inverse 32-bit fixed-point FFT\n"); |
| 38 |
| 39 verbose = options.verbose_; |
| 40 signal_value = options.signal_value_; |
| 41 |
| 42 if (verbose > 255) |
| 43 DumpOptions(stderr, &options); |
| 44 |
| 45 if (options.test_mode_) { |
| 46 struct TestInfo info; |
| 47 |
| 48 info.real_only_ = options.real_only_; |
| 49 info.max_fft_order_ = options.max_fft_order_; |
| 50 info.min_fft_order_ = options.min_fft_order_; |
| 51 info.do_forward_tests_ = options.do_forward_tests_; |
| 52 info.do_inverse_tests_ = options.do_inverse_tests_; |
| 53 info.known_failures_ = 0; |
| 54 /* |
| 55 * These threshold values assume that we're using the default |
| 56 * signal_value set below. |
| 57 */ |
| 58 info.forward_threshold_ = 107.33; |
| 59 info.inverse_threshold_ = 79.02; |
| 60 |
| 61 if (!options.signal_value_given_) { |
| 62 signal_value = 262144; /* 18 bits */ |
| 63 } |
| 64 RunAllTests(&info); |
| 65 } else { |
| 66 TestFFT(options.fft_log_size_, |
| 67 options.signal_type_, |
| 68 options.scale_factor_); |
| 69 } |
| 70 } |
| 71 |
| 72 void GenerateSignal(OMX_SC32* x, OMX_SC32* fft, int size, int signal_type) { |
| 73 int k; |
| 74 struct ComplexFloat *test_signal; |
| 75 struct ComplexFloat *true_fft; |
| 76 |
| 77 test_signal = (struct ComplexFloat*) malloc(sizeof(*test_signal) * size); |
| 78 true_fft = (struct ComplexFloat*) malloc(sizeof(*true_fft) * size); |
| 79 GenerateTestSignalAndFFT(test_signal, true_fft, size, signal_type, |
| 80 signal_value, 0); |
| 81 |
| 82 /* |
| 83 * Convert the complex float result to SC32 format. Just round. |
| 84 * No error-checking here! |
| 85 */ |
| 86 |
| 87 for (k = 0; k < size; ++k) { |
| 88 x[k].Re = 0.5 + test_signal[k].Re; |
| 89 x[k].Im = 0.5 + test_signal[k].Im; |
| 90 fft[k].Re = 0.5 + true_fft[k].Re; |
| 91 fft[k].Im = 0.5 + true_fft[k].Im; |
| 92 } |
| 93 |
| 94 free(test_signal); |
| 95 free(true_fft); |
| 96 } |
| 97 |
| 98 void DumpFFTSpec(OMXFFTSpec_C_SC32* pSpec) { |
| 99 ARMsFFTSpec_SC32* p = (ARMsFFTSpec_SC32*) pSpec; |
| 100 printf(" N = %d\n", p->N); |
| 101 printf(" pBitRev = %p\n", p->pBitRev); |
| 102 printf(" pTwiddle = %p\n", p->pTwiddle); |
| 103 printf(" pBuf = %p\n", p->pBuf); |
| 104 } |
| 105 |
| 106 /* |
| 107 * Compute forward and inverse FFT for one test case and compare the |
| 108 * result with the expected result. |
| 109 */ |
| 110 void TestFFT(int fft_log_size, int signal_type, int scale_factor) { |
| 111 struct SnrResult snr; |
| 112 |
| 113 RunOneForwardTest(fft_log_size, signal_type, signal_value, &snr); |
| 114 printf("Forward float FFT\n"); |
| 115 printf("SNR: real part %f dB\n", snr.real_snr_); |
| 116 printf(" imag part %f dB\n", snr.imag_snr_); |
| 117 printf(" complex part %f dB\n", snr.complex_snr_); |
| 118 |
| 119 RunOneInverseTest(fft_log_size, signal_type, signal_value, &snr); |
| 120 printf("Inverse float FFT\n"); |
| 121 printf("SNR: real part %f dB\n", snr.real_snr_); |
| 122 printf(" imag part %f dB\n", snr.imag_snr_); |
| 123 printf(" complex part %f dB\n", snr.complex_snr_); |
| 124 } |
| 125 |
| 126 /* |
| 127 * Like TestFFT, but do just the forward FFT. |
| 128 */ |
| 129 float RunOneForwardTest(int fft_log_size, int signal_type, float signal_value, |
| 130 struct SnrResult* snr) { |
| 131 OMX_SC32* x; |
| 132 OMX_SC32* y; |
| 133 |
| 134 struct AlignedPtr* x_aligned; |
| 135 struct AlignedPtr* y_aligned; |
| 136 |
| 137 OMX_SC32* y_true; |
| 138 |
| 139 OMX_INT n, fft_spec_buffer_size; |
| 140 OMXResult status; |
| 141 OMXFFTSpec_C_SC32 * fft_fwd_spec = NULL; |
| 142 int fft_size; |
| 143 |
| 144 fft_size = 1 << fft_log_size; |
| 145 |
| 146 status = omxSP_FFTGetBufSize_C_SC32(fft_log_size, &fft_spec_buffer_size); |
| 147 if (verbose > 63) { |
| 148 printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size); |
| 149 } |
| 150 |
| 151 fft_fwd_spec = (OMXFFTSpec_C_SC32*) malloc(fft_spec_buffer_size); |
| 152 status = omxSP_FFTInit_C_SC32(fft_fwd_spec, fft_log_size); |
| 153 if (status) { |
| 154 fprintf(stderr, "Failed to init forward FFT: status = %d\n", status); |
| 155 exit(1); |
| 156 } |
| 157 |
| 158 x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size); |
| 159 y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size + 2)); |
| 160 y_true = (OMX_SC32*) malloc(sizeof(*y_true) * fft_size); |
| 161 |
| 162 x = x_aligned->aligned_pointer_; |
| 163 y = y_aligned->aligned_pointer_; |
| 164 |
| 165 GenerateSignal(x, y_true, fft_size, signal_type); |
| 166 |
| 167 if (verbose > 63) { |
| 168 printf("Signal\n"); |
| 169 DumpArrayComplex32("x", fft_size, x); |
| 170 |
| 171 printf("Expected FFT output\n"); |
| 172 DumpArrayComplex32("y", fft_size, y_true); |
| 173 } |
| 174 |
| 175 status = omxSP_FFTFwd_CToC_SC32_Sfs(x, y, fft_fwd_spec, 0); |
| 176 if (status) { |
| 177 fprintf(stderr, "Forward FFT failed: status = %d\n", status); |
| 178 exit(1); |
| 179 } |
| 180 |
| 181 if (verbose > 63) { |
| 182 printf("FFT Output\n"); |
| 183 DumpArrayComplex32("y", fft_size, y); |
| 184 } |
| 185 |
| 186 CompareComplex32(snr, y, y_true, fft_size); |
| 187 |
| 188 FreeAlignedPointer(x_aligned); |
| 189 FreeAlignedPointer(y_aligned); |
| 190 free(fft_fwd_spec); |
| 191 |
| 192 return snr->complex_snr_; |
| 193 } |
| 194 |
| 195 /* |
| 196 * Like TestFFT, but do just the inverse FFT |
| 197 */ |
| 198 float RunOneInverseTest(int fft_log_size, int signal_type, float signal_value, |
| 199 struct SnrResult* snr) { |
| 200 OMX_SC32* x; |
| 201 OMX_SC32* y; |
| 202 OMX_SC32* z; |
| 203 |
| 204 struct AlignedPtr* x_aligned; |
| 205 struct AlignedPtr* y_aligned; |
| 206 struct AlignedPtr* z_aligned; |
| 207 |
| 208 OMX_INT n, fft_spec_buffer_size; |
| 209 OMXResult status; |
| 210 OMXFFTSpec_C_SC32 * fft_fwd_spec = NULL; |
| 211 OMXFFTSpec_C_SC32 * fft_inv_spec = NULL; |
| 212 int fft_size; |
| 213 |
| 214 fft_size = 1 << fft_log_size; |
| 215 |
| 216 status = omxSP_FFTGetBufSize_C_SC32(fft_log_size, &fft_spec_buffer_size); |
| 217 if (verbose > 3) { |
| 218 printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size); |
| 219 } |
| 220 |
| 221 fft_inv_spec = (OMXFFTSpec_C_SC32*)malloc(fft_spec_buffer_size); |
| 222 status = omxSP_FFTInit_C_SC32(fft_inv_spec, fft_log_size); |
| 223 if (status) { |
| 224 fprintf(stderr, "Failed to init backward FFT: status = %d\n", status); |
| 225 exit(1); |
| 226 } |
| 227 |
| 228 x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size); |
| 229 y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size + 2)); |
| 230 z_aligned = AllocAlignedPointer(32, sizeof(*z) * fft_size); |
| 231 |
| 232 x = x_aligned->aligned_pointer_; |
| 233 y = y_aligned->aligned_pointer_; |
| 234 z = z_aligned->aligned_pointer_; |
| 235 |
| 236 GenerateSignal(x, y, fft_size, signal_type); |
| 237 |
| 238 if (verbose > 63) { |
| 239 printf("Inverse FFT Input Signal\n"); |
| 240 printf("n\tx[n]\n"); |
| 241 DumpArrayComplex32("x", fft_size, y); |
| 242 |
| 243 printf("Expected Inverse FFT output\n"); |
| 244 DumpArrayComplex32("y", fft_size, x); |
| 245 } |
| 246 |
| 247 status = omxSP_FFTInv_CToC_SC32_Sfs(y, z, fft_inv_spec, 0); |
| 248 if (status) { |
| 249 fprintf(stderr, "Inverse FFT failed: status = %d\n", status); |
| 250 exit(1); |
| 251 } |
| 252 |
| 253 if (verbose > 63) { |
| 254 printf("Actual Inverse FFT Output\n"); |
| 255 DumpArrayComplex32("y", fft_size, z); |
| 256 } |
| 257 |
| 258 CompareComplex32(snr, z, x, fft_size); |
| 259 |
| 260 FreeAlignedPointer(x_aligned); |
| 261 FreeAlignedPointer(y_aligned); |
| 262 FreeAlignedPointer(z_aligned); |
| 263 free(fft_inv_spec); |
| 264 |
| 265 return snr->complex_snr_; |
| 266 } |
OLD | NEW |