1 /*
2 Copyright (c) 2003-2004, Mark Borgerding
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 */
14
15 #include "kiss_fftr.h"
16 #include "_kiss_fft_guts.h"
17
18 // CHRE modifications begin
19 #if defined(__clang__)
20 #pragma clang diagnostic push
21 #pragma clang diagnostic ignored "-Wcast-align"
22 #pragma clang diagnostic ignored "-Wbad-function-cast"
23 #endif
24 // CHRE modifications end
25
26 struct kiss_fftr_state{
27 kiss_fft_cfg substate;
28 kiss_fft_cpx * tmpbuf;
29 kiss_fft_cpx * super_twiddles;
30 #ifdef USE_SIMD
31 void * pad;
32 #endif
33 };
34
kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)35 kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
36 {
37 int i;
38 kiss_fftr_cfg st = NULL;
39 size_t subsize, memneeded;
40
41 if (nfft & 1) {
42 #if CHRE_KISS_FFT_CAN_USE_STDIO
43 fprintf(stderr,"Real FFT optimization must be even.\n");
44 #endif
45 return NULL;
46 }
47 nfft >>= 1;
48
49 kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
50 memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * (size_t)( nfft * 3 / 2);
51
52 if (lenmem == NULL) {
53 st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
54 } else {
55 if (*lenmem >= memneeded)
56 st = (kiss_fftr_cfg) mem;
57 *lenmem = memneeded;
58 }
59 if (!st)
60 return NULL;
61
62 st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
63 st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
64 st->super_twiddles = st->tmpbuf + nfft;
65 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
66
67 for (i = 0; i < nfft/2; ++i) {
68 double phase =
69 -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
70 if (inverse_fft)
71 phase *= -1;
72 kf_cexp (st->super_twiddles+i,phase);
73 }
74 return st;
75 }
76
kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar * timedata,kiss_fft_cpx * freqdata)77 void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
78 {
79 /* input buffer timedata is stored row-wise */
80 int k,ncfft;
81 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
82
83 if ( st->substate->inverse) {
84 #if CHRE_KISS_FFT_CAN_USE_STDIO
85 fprintf(stderr,"kiss fft usage error: improper alloc\n");
86 exit(1);
87 #else
88 return;
89 #endif
90 }
91
92 ncfft = st->substate->nfft;
93
94 /*perform the parallel fft of two real signals packed in real,imag*/
95 kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
96 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
97 * contains the sum of the even-numbered elements of the input time sequence
98 * The imag part is the sum of the odd-numbered elements
99 *
100 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
101 * yielding DC of input time sequence
102 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
103 * yielding Nyquist bin of input time sequence
104 */
105
106 tdc.r = st->tmpbuf[0].r;
107 tdc.i = st->tmpbuf[0].i;
108 C_FIXDIV(tdc,2);
109 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
110 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
111 freqdata[0].r = tdc.r + tdc.i;
112 freqdata[ncfft].r = tdc.r - tdc.i;
113 #ifdef USE_SIMD
114 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
115 #else
116 freqdata[ncfft].i = freqdata[0].i = 0;
117 #endif
118
119 for ( k=1;k <= ncfft/2 ; ++k ) {
120 fpk = st->tmpbuf[k];
121 fpnk.r = st->tmpbuf[ncfft-k].r;
122 fpnk.i = - st->tmpbuf[ncfft-k].i;
123 C_FIXDIV(fpk,2);
124 C_FIXDIV(fpnk,2);
125
126 C_ADD( f1k, fpk , fpnk );
127 C_SUB( f2k, fpk , fpnk );
128 C_MUL( tw , f2k , st->super_twiddles[k-1]);
129
130 freqdata[k].r = HALF_OF(f1k.r + tw.r);
131 freqdata[k].i = HALF_OF(f1k.i + tw.i);
132 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
133 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
134 }
135 }
136
kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx * freqdata,kiss_fft_scalar * timedata)137 void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
138 {
139 /* input buffer timedata is stored row-wise */
140 int k, ncfft;
141
142 if (st->substate->inverse == 0) {
143 #if CHRE_KISS_FFT_CAN_USE_STDIO
144 fprintf (stderr, "kiss fft usage error: improper alloc\n");
145 exit (1);
146 #else
147 return;
148 #endif
149 }
150
151 ncfft = st->substate->nfft;
152
153 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
154 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
155 C_FIXDIV(st->tmpbuf[0],2);
156
157 for (k = 1; k <= ncfft / 2; ++k) {
158 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
159 fk = freqdata[k];
160 fnkc.r = freqdata[ncfft - k].r;
161 fnkc.i = -freqdata[ncfft - k].i;
162 C_FIXDIV( fk , 2 );
163 C_FIXDIV( fnkc , 2 );
164
165 C_ADD (fek, fk, fnkc);
166 C_SUB (tmp, fk, fnkc);
167 C_MUL (fok, tmp, st->super_twiddles[k-1]);
168 C_ADD (st->tmpbuf[k], fek, fok);
169 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
170 #ifdef USE_SIMD
171 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
172 #else
173 st->tmpbuf[ncfft - k].i *= -1;
174 #endif
175 }
176 kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
177 }
178
179 // CHRE modifications begin
180 #if defined(__clang__)
181 #pragma clang diagnostic pop
182 #endif
183 // CHRE modifications end
184