2 Copyright (c) 2003-2004, Mark Borgerding
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
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.
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.
16 #include "_kiss_fft_guts_s16.h"
17 /* The guts header contains all the multiplication and addition macros that are defined for
18 fixed or floating point complex numbers. It also delares the kf_ internal functions.
21 static kiss_fft_s16_cpx *scratchbuf = NULL;
22 static size_t nscratchbuf = 0;
23 static kiss_fft_s16_cpx *tmpbuf = NULL;
24 static size_t ntmpbuf = 0;
26 #define CHECKBUF(buf,nbuf,n) \
28 if ( nbuf < (size_t)(n) ) {\
30 buf = (kiss_fft_s16_cpx*)KISS_FFT_S16_MALLOC(sizeof(kiss_fft_s16_cpx)*(n)); \
37 kf_bfly2 (kiss_fft_s16_cpx * Fout,
38 const size_t fstride, const kiss_fft_s16_cfg st, int m)
40 kiss_fft_s16_cpx *Fout2;
41 kiss_fft_s16_cpx *tw1 = st->twiddles;
49 C_MUL (t, *Fout2, *tw1);
51 C_SUB (*Fout2, *Fout, t);
59 kf_bfly4 (kiss_fft_s16_cpx * Fout,
60 const size_t fstride, const kiss_fft_s16_cfg st, const size_t m)
62 kiss_fft_s16_cpx *tw1, *tw2, *tw3;
63 kiss_fft_s16_cpx scratch[6];
65 const size_t m2 = 2 * m;
66 const size_t m3 = 3 * m;
68 tw3 = tw2 = tw1 = st->twiddles;
72 C_FIXDIV (Fout[m], 4);
73 C_FIXDIV (Fout[m2], 4);
74 C_FIXDIV (Fout[m3], 4);
76 C_MUL (scratch[0], Fout[m], *tw1);
77 C_MUL (scratch[1], Fout[m2], *tw2);
78 C_MUL (scratch[2], Fout[m3], *tw3);
80 C_SUB (scratch[5], *Fout, scratch[1]);
81 C_ADDTO (*Fout, scratch[1]);
82 C_ADD (scratch[3], scratch[0], scratch[2]);
83 C_SUB (scratch[4], scratch[0], scratch[2]);
84 C_SUB (Fout[m2], *Fout, scratch[3]);
88 C_ADDTO (*Fout, scratch[3]);
91 Fout[m].r = scratch[5].r - scratch[4].i;
92 Fout[m].i = scratch[5].i + scratch[4].r;
93 Fout[m3].r = scratch[5].r + scratch[4].i;
94 Fout[m3].i = scratch[5].i - scratch[4].r;
96 Fout[m].r = scratch[5].r + scratch[4].i;
97 Fout[m].i = scratch[5].i - scratch[4].r;
98 Fout[m3].r = scratch[5].r - scratch[4].i;
99 Fout[m3].i = scratch[5].i + scratch[4].r;
106 kf_bfly3 (kiss_fft_s16_cpx * Fout,
107 const size_t fstride, const kiss_fft_s16_cfg st, size_t m)
110 const size_t m2 = 2 * m;
111 kiss_fft_s16_cpx *tw1, *tw2;
112 kiss_fft_s16_cpx scratch[5];
113 kiss_fft_s16_cpx epi3;
115 epi3 = st->twiddles[fstride * m];
117 tw1 = tw2 = st->twiddles;
121 C_FIXDIV (Fout[m], 3);
122 C_FIXDIV (Fout[m2], 3);
124 C_MUL (scratch[1], Fout[m], *tw1);
125 C_MUL (scratch[2], Fout[m2], *tw2);
127 C_ADD (scratch[3], scratch[1], scratch[2]);
128 C_SUB (scratch[0], scratch[1], scratch[2]);
132 Fout[m].r = Fout->r - HALF_OF (scratch[3].r);
133 Fout[m].i = Fout->i - HALF_OF (scratch[3].i);
135 C_MULBYSCALAR (scratch[0], epi3.i);
137 C_ADDTO (*Fout, scratch[3]);
139 Fout[m2].r = Fout[m].r + scratch[0].i;
140 Fout[m2].i = Fout[m].i - scratch[0].r;
142 Fout[m].r -= scratch[0].i;
143 Fout[m].i += scratch[0].r;
150 kf_bfly5 (kiss_fft_s16_cpx * Fout,
151 const size_t fstride, const kiss_fft_s16_cfg st, int m)
153 kiss_fft_s16_cpx *Fout0, *Fout1, *Fout2, *Fout3, *Fout4;
155 kiss_fft_s16_cpx scratch[13];
156 kiss_fft_s16_cpx *twiddles = st->twiddles;
157 kiss_fft_s16_cpx *tw;
158 kiss_fft_s16_cpx ya, yb;
160 ya = twiddles[fstride * m];
161 yb = twiddles[fstride * 2 * m];
165 Fout2 = Fout0 + 2 * m;
166 Fout3 = Fout0 + 3 * m;
167 Fout4 = Fout0 + 4 * m;
170 for (u = 0; u < m; ++u) {
171 C_FIXDIV (*Fout0, 5);
172 C_FIXDIV (*Fout1, 5);
173 C_FIXDIV (*Fout2, 5);
174 C_FIXDIV (*Fout3, 5);
175 C_FIXDIV (*Fout4, 5);
178 C_MUL (scratch[1], *Fout1, tw[u * fstride]);
179 C_MUL (scratch[2], *Fout2, tw[2 * u * fstride]);
180 C_MUL (scratch[3], *Fout3, tw[3 * u * fstride]);
181 C_MUL (scratch[4], *Fout4, tw[4 * u * fstride]);
183 C_ADD (scratch[7], scratch[1], scratch[4]);
184 C_SUB (scratch[10], scratch[1], scratch[4]);
185 C_ADD (scratch[8], scratch[2], scratch[3]);
186 C_SUB (scratch[9], scratch[2], scratch[3]);
188 Fout0->r += scratch[7].r + scratch[8].r;
189 Fout0->i += scratch[7].i + scratch[8].i;
192 scratch[0].r + S_MUL (scratch[7].r, ya.r) + S_MUL (scratch[8].r, yb.r);
194 scratch[0].i + S_MUL (scratch[7].i, ya.r) + S_MUL (scratch[8].i, yb.r);
196 scratch[6].r = S_MUL (scratch[10].i, ya.i) + S_MUL (scratch[9].i, yb.i);
197 scratch[6].i = -S_MUL (scratch[10].r, ya.i) - S_MUL (scratch[9].r, yb.i);
199 C_SUB (*Fout1, scratch[5], scratch[6]);
200 C_ADD (*Fout4, scratch[5], scratch[6]);
203 scratch[0].r + S_MUL (scratch[7].r, yb.r) + S_MUL (scratch[8].r, ya.r);
205 scratch[0].i + S_MUL (scratch[7].i, yb.r) + S_MUL (scratch[8].i, ya.r);
206 scratch[12].r = -S_MUL (scratch[10].i, yb.i) + S_MUL (scratch[9].i, ya.i);
207 scratch[12].i = S_MUL (scratch[10].r, yb.i) - S_MUL (scratch[9].r, ya.i);
209 C_ADD (*Fout2, scratch[11], scratch[12]);
210 C_SUB (*Fout3, scratch[11], scratch[12]);
220 /* perform the butterfly for one stage of a mixed radix FFT */
222 kf_bfly_generic (kiss_fft_s16_cpx * Fout,
223 const size_t fstride, const kiss_fft_s16_cfg st, int m, int p)
226 kiss_fft_s16_cpx *twiddles = st->twiddles;
228 int Norig = st->nfft;
230 CHECKBUF (scratchbuf, nscratchbuf, p);
232 for (u = 0; u < m; ++u) {
234 for (q1 = 0; q1 < p; ++q1) {
235 scratchbuf[q1] = Fout[k];
236 C_FIXDIV (scratchbuf[q1], p);
241 for (q1 = 0; q1 < p; ++q1) {
244 Fout[k] = scratchbuf[0];
245 for (q = 1; q < p; ++q) {
246 twidx += fstride * k;
249 C_MUL (t, scratchbuf[q], twiddles[twidx]);
250 C_ADDTO (Fout[k], t);
258 kf_work (kiss_fft_s16_cpx * Fout,
259 const kiss_fft_s16_cpx * f,
260 const size_t fstride,
261 int in_stride, int *factors, const kiss_fft_s16_cfg st)
263 kiss_fft_s16_cpx *Fout_beg = Fout;
264 const int p = *factors++; /* the radix */
265 const int m = *factors++; /* stage's fft length/p */
266 const kiss_fft_s16_cpx *Fout_end = Fout + p * m;
269 // use openmp extensions at the
270 // top-level (not recursive)
274 // execute the p different work units in different threads
275 # pragma omp parallel for
276 for (k = 0; k < p; ++k)
277 kf_work (Fout + k * m, f + fstride * in_stride * k, fstride * p,
278 in_stride, factors, st);
279 // all threads have joined by this point
283 kf_bfly2 (Fout, fstride, st, m);
286 kf_bfly3 (Fout, fstride, st, m);
289 kf_bfly4 (Fout, fstride, st, m);
292 kf_bfly5 (Fout, fstride, st, m);
295 kf_bfly_generic (Fout, fstride, st, m, p);
305 f += fstride * in_stride;
306 } while (++Fout != Fout_end);
310 // DFT of size m*p performed by doing
311 // p instances of smaller DFTs of size m,
312 // each one takes a decimated version of the input
313 kf_work (Fout, f, fstride * p, in_stride, factors, st);
314 f += fstride * in_stride;
315 } while ((Fout += m) != Fout_end);
320 // recombine the p smaller DFTs
323 kf_bfly2 (Fout, fstride, st, m);
326 kf_bfly3 (Fout, fstride, st, m);
329 kf_bfly4 (Fout, fstride, st, m);
332 kf_bfly5 (Fout, fstride, st, m);
335 kf_bfly_generic (Fout, fstride, st, m, p);
340 /* facbuf is populated by p1,m1,p2,m2, ...
345 kf_factor (int n, int *facbuf)
350 floor_sqrt = floor (sqrt ((double) n));
352 /*factor out powers of 4, powers of 2, then any remaining primes */
367 p = n; /* no more factors, skip to end */
377 * User-callable function to allocate all necessary storage space for the fft.
379 * The return value is a contiguous block of memory, allocated with malloc. As such,
380 * It can be freed with free(), rather than a kiss_fft-specific function.
383 kiss_fft_s16_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem)
385 kiss_fft_s16_cfg st = NULL;
386 size_t memneeded = sizeof (struct kiss_fft_s16_state)
387 + sizeof (kiss_fft_s16_cpx) * (nfft - 1); /* twiddle factors */
389 if (lenmem == NULL) {
390 st = (kiss_fft_s16_cfg) KISS_FFT_S16_MALLOC (memneeded);
392 if (mem != NULL && *lenmem >= memneeded)
393 st = (kiss_fft_s16_cfg) mem;
400 st->inverse = inverse_fft;
402 for (i = 0; i < nfft; ++i) {
404 3.141592653589793238462643383279502884197169399375105820974944;
405 double phase = -2 * pi * i / nfft;
409 kf_cexp (st->twiddles + i, phase);
412 kf_factor (nfft, st->factors);
421 kiss_fft_s16_stride (kiss_fft_s16_cfg st, const kiss_fft_s16_cpx * fin,
422 kiss_fft_s16_cpx * fout, int in_stride)
425 CHECKBUF (tmpbuf, ntmpbuf, st->nfft);
426 kf_work (tmpbuf, fin, 1, in_stride, st->factors, st);
427 memcpy (fout, tmpbuf, sizeof (kiss_fft_s16_cpx) * st->nfft);
429 kf_work (fout, fin, 1, in_stride, st->factors, st);
434 kiss_fft_s16 (kiss_fft_s16_cfg cfg, const kiss_fft_s16_cpx * fin,
435 kiss_fft_s16_cpx * fout)
437 kiss_fft_s16_stride (cfg, fin, fout, 1);
441 /* not really necessary to call, but if someone is doing in-place ffts, they may want to free the
442 buffers from CHECKBUF
445 kiss_fft_s16_cleanup (void)
456 kiss_fft_s16_next_fast_size (int n)
468 break; /* n is completely factorable by twos, threes, and fives */