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 int verbose; |
| 25 int signal_value; |
| 26 |
| 27 #define MAX_FFT_ORDER 12 |
| 28 |
| 29 void TestFFT(int fft_log_size, int signal_type, int scale_factor); |
| 30 |
| 31 void main(int argc, char* argv[]) { |
| 32 struct Options options; |
| 33 |
| 34 SetDefaultOptions(&options, 1, MAX_FFT_ORDER); |
| 35 |
| 36 ProcessCommandLine(&options, argc, argv, |
| 37 "Test forward and inverse real 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.min_fft_order_ = options.min_fft_order_; |
| 50 info.max_fft_order_ = options.max_fft_order_; |
| 51 info.do_forward_tests_ = options.do_forward_tests_; |
| 52 info.do_inverse_tests_ = options.do_inverse_tests_; |
| 53 /* No known failures */ |
| 54 info.known_failures_ = 0; |
| 55 /* These thresholds are set for the default signal_value below */ |
| 56 info.forward_threshold_ = 105.94; |
| 57 info.inverse_threshold_ = 104.62; |
| 58 if (!options.signal_value_given_) { |
| 59 signal_value = 262144; |
| 60 } |
| 61 RunAllTests(&info); |
| 62 } else { |
| 63 TestFFT(options.fft_log_size_, |
| 64 options.signal_type_, |
| 65 options.scale_factor_); |
| 66 } |
| 67 } |
| 68 |
| 69 void GenerateSignal(OMX_S32* x, OMX_SC32* fft, int size, int signal_type) { |
| 70 int k; |
| 71 struct ComplexFloat *test_signal; |
| 72 struct ComplexFloat *true_fft; |
| 73 |
| 74 test_signal = (struct ComplexFloat*) malloc(sizeof(*test_signal) * size); |
| 75 true_fft = (struct ComplexFloat*) malloc(sizeof(*true_fft) * size); |
| 76 GenerateTestSignalAndFFT(test_signal, true_fft, size, |
| 77 signal_type, signal_value, 1); |
| 78 |
| 79 /* |
| 80 * Convert the complex result to what we want |
| 81 */ |
| 82 |
| 83 for (k = 0; k < size; ++k) { |
| 84 x[k] = 0.5 + test_signal[k].Re; |
| 85 } |
| 86 |
| 87 for (k = 0; k < size / 2 + 1; ++k) { |
| 88 fft[k].Re = 0.5 + true_fft[k].Re; |
| 89 fft[k].Im = 0.5 + true_fft[k].Im; |
| 90 } |
| 91 |
| 92 free(test_signal); |
| 93 free(true_fft); |
| 94 } |
| 95 |
| 96 void TestFFT(int fft_log_size, int signal_type, int scale_factor) { |
| 97 struct SnrResult snr; |
| 98 |
| 99 RunOneForwardTest(fft_log_size, signal_type, signal_value, &snr); |
| 100 printf("Forward float FFT\n"); |
| 101 printf("SNR: real part %f dB\n", snr.real_snr_); |
| 102 printf(" imag part %f dB\n", snr.imag_snr_); |
| 103 printf(" complex part %f dB\n", snr.complex_snr_); |
| 104 |
| 105 RunOneInverseTest(fft_log_size, signal_type, signal_value, &snr); |
| 106 printf("Inverse float FFT\n"); |
| 107 printf("SNR: %f dB\n", snr.real_snr_); |
| 108 } |
| 109 |
| 110 float RunOneForwardTest(int fft_log_size, int signal_type, float signal_value, |
| 111 struct SnrResult* snr) { |
| 112 OMX_S32* x; |
| 113 OMX_SC32* y; |
| 114 |
| 115 struct AlignedPtr* x_aligned; |
| 116 struct AlignedPtr* y_aligned; |
| 117 |
| 118 OMX_SC32* y_true; |
| 119 |
| 120 OMX_INT n, fft_spec_buffer_size; |
| 121 OMXResult status; |
| 122 OMXFFTSpec_R_S32 * pFwdSpec = NULL; |
| 123 int fft_size; |
| 124 |
| 125 fft_size = 1 << fft_log_size; |
| 126 |
| 127 status = omxSP_FFTGetBufSize_R_S32(fft_log_size, &fft_spec_buffer_size); |
| 128 if (verbose > 63) { |
| 129 printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size); |
| 130 } |
| 131 |
| 132 pFwdSpec = (OMXFFTSpec_R_S32*) malloc(fft_spec_buffer_size); |
| 133 status = omxSP_FFTInit_R_S32(pFwdSpec, fft_log_size); |
| 134 if (status) { |
| 135 fprintf(stderr, "Failed to init forward FFT: status = %d\n", status); |
| 136 exit(1); |
| 137 } |
| 138 |
| 139 x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size); |
| 140 y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size + 2)); |
| 141 y_true = (OMX_SC32*) malloc(sizeof(*y_true) * (fft_size / 2 + 1)); |
| 142 |
| 143 x = x_aligned->aligned_pointer_; |
| 144 y = y_aligned->aligned_pointer_; |
| 145 |
| 146 GenerateSignal(x, y_true, fft_size, signal_type); |
| 147 |
| 148 if (verbose > 63) { |
| 149 printf("Signal\n"); |
| 150 DumpArrayReal32("x", fft_size, x); |
| 151 |
| 152 printf("Expected FFT output\n"); |
| 153 DumpArrayComplex32("y", fft_size / 2, y_true); |
| 154 } |
| 155 |
| 156 status = omxSP_FFTFwd_RToCCS_S32_Sfs(x, (OMX_S32*) y, pFwdSpec, 0); |
| 157 if (status) { |
| 158 fprintf(stderr, "Forward FFT failed: status = %d\n", status); |
| 159 exit(1); |
| 160 } |
| 161 |
| 162 if (verbose > 63) { |
| 163 printf("FFT Output\n"); |
| 164 DumpArrayComplex32("y", fft_size / 2, y); |
| 165 } |
| 166 |
| 167 CompareComplex32(snr, y, y_true, fft_size / 2 + 1); |
| 168 |
| 169 FreeAlignedPointer(x_aligned); |
| 170 FreeAlignedPointer(y_aligned); |
| 171 free(y_true); |
| 172 free(pFwdSpec); |
| 173 |
| 174 return snr->complex_snr_; |
| 175 } |
| 176 |
| 177 float RunOneInverseTest(int fft_log_size, int signal_type, float signal_value, |
| 178 struct SnrResult* snr) { |
| 179 OMX_S32* x; |
| 180 OMX_SC32* y; |
| 181 OMX_S32* z; |
| 182 OMX_SC32* y_true; |
| 183 |
| 184 struct AlignedPtr* x_aligned; |
| 185 struct AlignedPtr* y_aligned; |
| 186 struct AlignedPtr* z_aligned; |
| 187 struct AlignedPtr* y_true_aligned; |
| 188 |
| 189 OMX_INT n, fft_spec_buffer_size; |
| 190 OMXResult status; |
| 191 OMXFFTSpec_R_S32 * pFwdSpec = NULL; |
| 192 OMXFFTSpec_R_S32 * pInvSpec = NULL; |
| 193 int fft_size; |
| 194 |
| 195 fft_size = 1 << fft_log_size; |
| 196 |
| 197 status = omxSP_FFTGetBufSize_R_S32(fft_log_size, &fft_spec_buffer_size); |
| 198 if (verbose > 3) { |
| 199 printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size); |
| 200 } |
| 201 |
| 202 pInvSpec = (OMXFFTSpec_R_S32*)malloc(fft_spec_buffer_size); |
| 203 status = omxSP_FFTInit_R_S32(pInvSpec, fft_log_size); |
| 204 if (status) { |
| 205 fprintf(stderr, "Failed to init backward FFT: status = %d\n", status); |
| 206 exit(1); |
| 207 } |
| 208 |
| 209 x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size); |
| 210 y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size / 2 + 1)); |
| 211 z_aligned = AllocAlignedPointer(32, sizeof(*z) * fft_size); |
| 212 y_true_aligned = AllocAlignedPointer(32, |
| 213 sizeof(*y_true) * (fft_size / 2 + 1)); |
| 214 |
| 215 x = x_aligned->aligned_pointer_; |
| 216 y = y_aligned->aligned_pointer_; |
| 217 z = z_aligned->aligned_pointer_; |
| 218 y_true = y_true_aligned->aligned_pointer_; |
| 219 |
| 220 GenerateSignal(x, y_true, fft_size, signal_type); |
| 221 |
| 222 if (verbose > 63) { |
| 223 printf("Inverse FFT Input Signal\n"); |
| 224 DumpArrayComplex32("y", fft_size / 2, y_true); |
| 225 |
| 226 printf("Expected Inverse FFT output\n"); |
| 227 DumpArrayReal32("x", fft_size, x); |
| 228 } |
| 229 |
| 230 status = omxSP_FFTInv_CCSToR_S32_Sfs((OMX_S32*) y_true, z, pInvSpec, 0); |
| 231 if (status) { |
| 232 fprintf(stderr, "Inverse FFT failed: status = %d\n", status); |
| 233 exit(1); |
| 234 } |
| 235 |
| 236 if (verbose > 63) { |
| 237 printf("Actual Inverse FFT Output\n"); |
| 238 DumpArrayReal32("x", fft_size, z); |
| 239 } |
| 240 |
| 241 CompareReal32(snr, z, x, fft_size); |
| 242 |
| 243 FreeAlignedPointer(x_aligned); |
| 244 FreeAlignedPointer(y_aligned); |
| 245 FreeAlignedPointer(z_aligned); |
| 246 FreeAlignedPointer(y_true_aligned); |
| 247 free(pInvSpec); |
| 248 |
| 249 return snr->real_snr_; |
| 250 } |
OLD | NEW |