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