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
26int verbose;
27int signal_value;
28
29int main(int argc, char* argv[]) {
30  struct Options options;
31  struct TestInfo info;
32
33  SetDefaultOptions(&options, 0, MAX_FFT_ORDER);
34
35  ProcessCommandLine(&options, argc, argv,
36                     "Test forward and inverse 32-bit fixed-point FFT\n");
37
38  verbose = options.verbose_;
39  signal_value = options.signal_value_;
40
41  if (verbose > 255)
42    DumpOptions(stderr, &options);
43
44  info.real_only_ = options.real_only_;
45  info.max_fft_order_ = options.max_fft_order_;
46  info.min_fft_order_ = options.min_fft_order_;
47  info.do_forward_tests_ = options.do_forward_tests_;
48  info.do_inverse_tests_ = options.do_inverse_tests_;
49  info.known_failures_ = 0;
50  /*
51   * These threshold values assume that we're using the default
52   * signal_value set below.
53   */
54  info.forward_threshold_ = 107.33;
55  info.inverse_threshold_ = 79.02;
56
57  if (!options.signal_value_given_) {
58    signal_value = 262144;         /* 18 bits */
59  }
60
61  if (options.test_mode_) {
62    RunAllTests(&info);
63  } else {
64    TestOneFFT(options.fft_log_size_,
65               options.signal_type_,
66               options.signal_value_,
67               &info,
68               "32-bit FFT");
69  }
70
71  return 0;
72}
73
74void GenerateSignal(OMX_SC32* x, OMX_SC32* fft, int size, int signal_type) {
75  int k;
76  struct ComplexFloat *test_signal;
77  struct ComplexFloat *true_fft;
78
79  test_signal = (struct ComplexFloat*) malloc(sizeof(*test_signal) * size);
80  true_fft = (struct ComplexFloat*) malloc(sizeof(*true_fft) * size);
81  GenerateTestSignalAndFFT(test_signal, true_fft, size, signal_type,
82                           signal_value, 0);
83
84  /*
85   * Convert the complex float result to SC32 format.  Just round.
86   * No error-checking here!
87   */
88
89  for (k = 0; k < size; ++k) {
90    x[k].Re = 0.5 + test_signal[k].Re;
91    x[k].Im = 0.5 + test_signal[k].Im;
92    fft[k].Re = 0.5 + true_fft[k].Re;
93    fft[k].Im = 0.5 + true_fft[k].Im;
94  }
95
96  free(test_signal);
97  free(true_fft);
98}
99
100void DumpFFTSpec(OMXFFTSpec_C_SC32* pSpec) {
101  ARMsFFTSpec_SC32* p = (ARMsFFTSpec_SC32*) pSpec;
102  printf(" N = %d\n", p->N);
103  printf(" pBitRev  = %p\n", p->pBitRev);
104  printf(" pTwiddle = %p\n", p->pTwiddle);
105  printf(" pBuf     = %p\n", p->pBuf);
106}
107
108/*
109 * Like TestFFT, but do just the forward FFT.
110 */
111float RunOneForwardTest(int fft_log_size, int signal_type, float signal_value,
112                        struct SnrResult* snr) {
113  OMX_SC32* x;
114  OMX_SC32* y;
115
116  struct AlignedPtr* x_aligned;
117  struct AlignedPtr* y_aligned;
118
119  OMX_SC32* y_true;
120
121  OMX_INT n, fft_spec_buffer_size;
122  OMXResult status;
123  OMXFFTSpec_C_SC32 * fft_fwd_spec = NULL;
124  int fft_size;
125
126  fft_size = 1 << fft_log_size;
127
128  status = omxSP_FFTGetBufSize_C_SC32(fft_log_size, &fft_spec_buffer_size);
129  if (verbose > 63) {
130    printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size);
131  }
132
133  fft_fwd_spec = (OMXFFTSpec_C_SC32*) malloc(fft_spec_buffer_size);
134  status = omxSP_FFTInit_C_SC32(fft_fwd_spec, fft_log_size);
135  if (status) {
136    fprintf(stderr, "Failed to init forward FFT:  status = %d\n", status);
137    exit(1);
138  }
139
140  x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size);
141  y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size + 2));
142  y_true = (OMX_SC32*) malloc(sizeof(*y_true) * fft_size);
143
144  x = x_aligned->aligned_pointer_;
145  y = y_aligned->aligned_pointer_;
146
147  GenerateSignal(x, y_true, fft_size, signal_type);
148
149  if (verbose > 63) {
150    printf("Signal\n");
151    DumpArrayComplex32("x", fft_size, x);
152
153    printf("Expected FFT output\n");
154    DumpArrayComplex32("y", fft_size, y_true);
155  }
156
157  status = omxSP_FFTFwd_CToC_SC32_Sfs(x, y, fft_fwd_spec, 0);
158  if (status) {
159    fprintf(stderr, "Forward FFT failed: status = %d\n", status);
160    exit(1);
161  }
162
163  if (verbose > 63) {
164    printf("FFT Output\n");
165    DumpArrayComplex32("y", fft_size, y);
166  }
167
168  CompareComplex32(snr, y, y_true, fft_size);
169
170  FreeAlignedPointer(x_aligned);
171  FreeAlignedPointer(y_aligned);
172  free(fft_fwd_spec);
173
174  return snr->complex_snr_;
175}
176
177/*
178 * Like TestFFT, but do just the inverse FFT
179 */
180float RunOneInverseTest(int fft_log_size, int signal_type, float signal_value,
181                        struct SnrResult* snr) {
182  OMX_SC32* x;
183  OMX_SC32* y;
184  OMX_SC32* z;
185
186  struct AlignedPtr* x_aligned;
187  struct AlignedPtr* y_aligned;
188  struct AlignedPtr* z_aligned;
189
190  OMX_INT n, fft_spec_buffer_size;
191  OMXResult status;
192  OMXFFTSpec_C_SC32 * fft_fwd_spec = NULL;
193  OMXFFTSpec_C_SC32 * fft_inv_spec = NULL;
194  int fft_size;
195
196  fft_size = 1 << fft_log_size;
197
198  status = omxSP_FFTGetBufSize_C_SC32(fft_log_size, &fft_spec_buffer_size);
199  if (verbose > 3) {
200    printf("fft_spec_buffer_size = %d\n", fft_spec_buffer_size);
201  }
202
203  fft_inv_spec = (OMXFFTSpec_C_SC32*)malloc(fft_spec_buffer_size);
204  status = omxSP_FFTInit_C_SC32(fft_inv_spec, fft_log_size);
205  if (status) {
206    fprintf(stderr, "Failed to init backward FFT:  status = %d\n", status);
207    exit(1);
208  }
209
210  x_aligned = AllocAlignedPointer(32, sizeof(*x) * fft_size);
211  y_aligned = AllocAlignedPointer(32, sizeof(*y) * (fft_size + 2));
212  z_aligned = AllocAlignedPointer(32, sizeof(*z) * fft_size);
213
214  x = x_aligned->aligned_pointer_;
215  y = y_aligned->aligned_pointer_;
216  z = z_aligned->aligned_pointer_;
217
218  GenerateSignal(x, y, fft_size, signal_type);
219
220  if (verbose > 63) {
221    printf("Inverse FFT Input Signal\n");
222    printf("n\tx[n]\n");
223    DumpArrayComplex32("x", fft_size, y);
224
225    printf("Expected Inverse FFT output\n");
226    DumpArrayComplex32("y", fft_size, x);
227  }
228
229  status = omxSP_FFTInv_CToC_SC32_Sfs(y, z, fft_inv_spec, 0);
230  if (status) {
231    fprintf(stderr, "Inverse FFT failed: status = %d\n", status);
232    exit(1);
233  }
234
235  if (verbose > 63) {
236    printf("Actual Inverse FFT Output\n");
237    DumpArrayComplex32("y", fft_size, z);
238  }
239
240  CompareComplex32(snr, z, x, fft_size);
241
242  FreeAlignedPointer(x_aligned);
243  FreeAlignedPointer(y_aligned);
244  FreeAlignedPointer(z_aligned);
245  free(fft_inv_spec);
246
247  return snr->complex_snr_;
248}
249