• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2008 Xiph.Org Foundation
2    Written by Jean-Marc Valin */
3 /*
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #define SKIP_CONFIG_H
33 
34 #ifndef CUSTOM_MODES
35 #define CUSTOM_MODES
36 #endif
37 
38 #include <stdio.h>
39 
40 #define CELT_C
41 #define TEST_UNIT_DFT_C
42 #include "stack_alloc.h"
43 #include "kiss_fft.h"
44 #include "kiss_fft.c"
45 #include "mathops.c"
46 #include "entcode.c"
47 
48 #if defined(OPUS_X86_MAY_HAVE_SSE2) || defined(OPUS_X86_MAY_HAVE_SSE4_1)
49 # include "x86/x86cpu.c"
50 #elif defined(OPUS_ARM_ASM) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
51 # include "arm/armcpu.c"
52 # include "celt_lpc.c"
53 # include "pitch.c"
54 # if defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
55 #  include "arm/celt_neon_intr.c"
56 #  if defined(HAVE_ARM_NE10)
57 #   include "mdct.c"
58 #   include "arm/celt_ne10_fft.c"
59 #   include "arm/celt_ne10_mdct.c"
60 #  endif
61 # endif
62 # include "arm/arm_celt_map.c"
63 #endif
64 
65 #ifndef M_PI
66 #define M_PI 3.141592653
67 #endif
68 
69 int ret = 0;
70 
check(kiss_fft_cpx * in,kiss_fft_cpx * out,int nfft,int isinverse)71 void check(kiss_fft_cpx  * in,kiss_fft_cpx  * out,int nfft,int isinverse)
72 {
73     int bin,k;
74     double errpow=0,sigpow=0, snr;
75 
76     for (bin=0;bin<nfft;++bin) {
77         double ansr = 0;
78         double ansi = 0;
79         double difr;
80         double difi;
81 
82         for (k=0;k<nfft;++k) {
83             double phase = -2*M_PI*bin*k/nfft;
84             double re = cos(phase);
85             double im = sin(phase);
86             if (isinverse)
87                 im = -im;
88 
89             if (!isinverse)
90             {
91                re /= nfft;
92                im /= nfft;
93             }
94 
95             ansr += in[k].r * re - in[k].i * im;
96             ansi += in[k].r * im + in[k].i * re;
97         }
98         /*printf ("%d %d ", (int)ansr, (int)ansi);*/
99         difr = ansr - out[bin].r;
100         difi = ansi - out[bin].i;
101         errpow += difr*difr + difi*difi;
102         sigpow += ansr*ansr+ansi*ansi;
103     }
104     snr = 10*log10(sigpow/errpow);
105     printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
106     if (snr<60) {
107        printf( "** poor snr: %f ** \n", snr);
108        ret = 1;
109     }
110 }
111 
test1d(int nfft,int isinverse,int arch)112 void test1d(int nfft,int isinverse,int arch)
113 {
114     size_t buflen = sizeof(kiss_fft_cpx)*nfft;
115 
116     kiss_fft_cpx  * in = (kiss_fft_cpx*)malloc(buflen);
117     kiss_fft_cpx  * out= (kiss_fft_cpx*)malloc(buflen);
118     kiss_fft_state *cfg = opus_fft_alloc(nfft,0,0,arch);
119     int k;
120 
121     for (k=0;k<nfft;++k) {
122         in[k].r = (rand() % 32767) - 16384;
123         in[k].i = (rand() % 32767) - 16384;
124     }
125 
126     for (k=0;k<nfft;++k) {
127        in[k].r *= 32768;
128        in[k].i *= 32768;
129     }
130 
131     if (isinverse)
132     {
133        for (k=0;k<nfft;++k) {
134           in[k].r /= nfft;
135           in[k].i /= nfft;
136        }
137     }
138 
139     /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
140 
141     if (isinverse)
142        opus_ifft(cfg,in,out, arch);
143     else
144        opus_fft(cfg,in,out, arch);
145 
146     /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
147 
148     check(in,out,nfft,isinverse);
149 
150     free(in);
151     free(out);
152     opus_fft_free(cfg, arch);
153 }
154 
main(int argc,char ** argv)155 int main(int argc,char ** argv)
156 {
157     ALLOC_STACK;
158     int arch = opus_select_arch();
159 
160     if (argc>1) {
161         int k;
162         for (k=1;k<argc;++k) {
163             test1d(atoi(argv[k]),0,arch);
164             test1d(atoi(argv[k]),1,arch);
165         }
166     }else{
167         test1d(32,0,arch);
168         test1d(32,1,arch);
169         test1d(128,0,arch);
170         test1d(128,1,arch);
171         test1d(256,0,arch);
172         test1d(256,1,arch);
173 #ifndef RADIX_TWO_ONLY
174         test1d(36,0,arch);
175         test1d(36,1,arch);
176         test1d(50,0,arch);
177         test1d(50,1,arch);
178         test1d(60,0,arch);
179         test1d(60,1,arch);
180         test1d(120,0,arch);
181         test1d(120,1,arch);
182         test1d(240,0,arch);
183         test1d(240,1,arch);
184         test1d(480,0,arch);
185         test1d(480,1,arch);
186 #endif
187     }
188     return ret;
189 }
190