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.
15 #include "kiss_fftr_f64.h"
16 #include "_kiss_fft_guts_f64.h"
18 struct kiss_fftr_f64_state
20 kiss_fft_f64_cfg substate;
21 kiss_fft_f64_cpx *tmpbuf;
22 kiss_fft_f64_cpx *super_twiddles;
29 kiss_fftr_f64_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem)
32 kiss_fftr_f64_cfg st = NULL;
33 size_t subsize, memneeded;
36 fprintf (stderr, "Real FFT optimization must be even.\n");
41 kiss_fft_f64_alloc (nfft, inverse_fft, NULL, &subsize);
43 sizeof (struct kiss_fftr_f64_state) + subsize +
44 sizeof (kiss_fft_f64_cpx) * (nfft * 2);
47 st = (kiss_fftr_f64_cfg) KISS_FFT_F64_MALLOC (memneeded);
49 if (*lenmem >= memneeded)
50 st = (kiss_fftr_f64_cfg) mem;
56 st->substate = (kiss_fft_f64_cfg) (st + 1); /*just beyond kiss_fftr_f64_state struct */
57 st->tmpbuf = (kiss_fft_f64_cpx *) (((char *) st->substate) + subsize);
58 st->super_twiddles = st->tmpbuf + nfft;
59 kiss_fft_f64_alloc (nfft, inverse_fft, st->substate, &subsize);
61 for (i = 0; i < nfft; ++i) {
62 double phase = -3.14159265358979323846264338327 * ((double) i / nfft + .5);
66 kf_cexp (st->super_twiddles + i, phase);
72 kiss_fftr_f64 (kiss_fftr_f64_cfg st, const kiss_fft_f64_scalar * timedata,
73 kiss_fft_f64_cpx * freqdata)
75 /* input buffer timedata is stored row-wise */
77 kiss_fft_f64_cpx fpnk, fpk, f1k, f2k, tw, tdc;
79 if (st->substate->inverse) {
80 fprintf (stderr, "kiss fft usage error: improper alloc\n");
84 ncfft = st->substate->nfft;
86 /*perform the parallel fft of two real signals packed in real,imag */
87 kiss_fft_f64 (st->substate, (const kiss_fft_f64_cpx *) timedata, st->tmpbuf);
88 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
89 * contains the sum of the even-numbered elements of the input time sequence
90 * The imag part is the sum of the odd-numbered elements
92 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
93 * yielding DC of input time sequence
94 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
95 * yielding Nyquist bin of input time sequence
98 tdc.r = st->tmpbuf[0].r;
99 tdc.i = st->tmpbuf[0].i;
101 CHECK_OVERFLOW_OP (tdc.r, +, tdc.i);
102 CHECK_OVERFLOW_OP (tdc.r, -, tdc.i);
103 freqdata[0].r = tdc.r + tdc.i;
104 freqdata[ncfft].r = tdc.r - tdc.i;
106 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0);
108 freqdata[ncfft].i = freqdata[0].i = 0;
111 for (k = 1; k <= ncfft / 2; ++k) {
113 fpnk.r = st->tmpbuf[ncfft - k].r;
114 fpnk.i = -st->tmpbuf[ncfft - k].i;
118 C_ADD (f1k, fpk, fpnk);
119 C_SUB (f2k, fpk, fpnk);
120 C_MUL (tw, f2k, st->super_twiddles[k]);
122 freqdata[k].r = HALF_OF (f1k.r + tw.r);
123 freqdata[k].i = HALF_OF (f1k.i + tw.i);
124 freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r);
125 freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i);
130 kiss_fftri_f64 (kiss_fftr_f64_cfg st, const kiss_fft_f64_cpx * freqdata,
131 kiss_fft_f64_scalar * timedata)
133 /* input buffer timedata is stored row-wise */
136 if (st->substate->inverse == 0) {
137 fprintf (stderr, "kiss fft usage error: improper alloc\n");
141 ncfft = st->substate->nfft;
143 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
144 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
145 C_FIXDIV (st->tmpbuf[0], 2);
147 for (k = 1; k <= ncfft / 2; ++k) {
148 kiss_fft_f64_cpx fk, fnkc, fek, fok, tmp;
151 fnkc.r = freqdata[ncfft - k].r;
152 fnkc.i = -freqdata[ncfft - k].i;
156 C_ADD (fek, fk, fnkc);
157 C_SUB (tmp, fk, fnkc);
158 C_MUL (fok, tmp, st->super_twiddles[k]);
159 C_ADD (st->tmpbuf[k], fek, fok);
160 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
162 st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0);
164 st->tmpbuf[ncfft - k].i *= -1;
167 kiss_fft_f64 (st->substate, st->tmpbuf, (kiss_fft_f64_cpx *) timedata);