summaryrefslogtreecommitdiff
path: root/apps/plugins/fft/kiss_fftr.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/fft/kiss_fftr.c')
-rw-r--r--apps/plugins/fft/kiss_fftr.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/apps/plugins/fft/kiss_fftr.c b/apps/plugins/fft/kiss_fftr.c
new file mode 100644
index 0000000000..7fa29b98e8
--- /dev/null
+++ b/apps/plugins/fft/kiss_fftr.c
@@ -0,0 +1,153 @@
1/*
2Copyright (c) 2003-2004, Mark Borgerding
3
4All rights reserved.
5
6Redistribution 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
12THIS 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
18kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
19{
20 int i;
21 kiss_fftr_cfg st = NULL;
22 size_t subsize, memneeded;
23
24 if (nfft & 1) {
25 DEBUGF("Real FFT optimization must be even.\n");
26 return NULL;
27 }
28 nfft >>= 1;
29
30 kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
31 memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
32
33 if (lenmem == NULL) {
34 DEBUGF("Cannot use malloc!"); /*st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);*/
35 } else {
36 if (*lenmem >= memneeded)
37 st = (kiss_fftr_cfg) mem;
38 *lenmem = memneeded;
39 }
40 if (!st)
41 return NULL;
42
43 st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
44 st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
45 st->super_twiddles = st->tmpbuf + nfft;
46 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
47
48 for (i = 0; i < nfft/2; ++i) {
49 /*double phase =
50 -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);*/
51 if (inverse_fft)
52 {
53 DEBUGF("Inverse FFT not implemented!"); /*phase *= -1;*/
54 }
55
56 kf_cexp_round (st->super_twiddles+i, i+1, nfft);
57 }
58 return st;
59}
60
61void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
62{
63 /* input buffer timedata is stored row-wise */
64 int k,ncfft;
65 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
66
67 if ( st->substate->inverse) {
68 DEBUGF("kiss fft usage error: improper alloc\n");
69 return;
70 }
71
72 ncfft = st->substate->nfft;
73
74 /*perform the parallel fft of two real signals packed in real,imag*/
75 kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
76 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
77 * contains the sum of the even-numbered elements of the input time sequence
78 * The imag part is the sum of the odd-numbered elements
79 *
80 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
81 * yielding DC of input time sequence
82 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
83 * yielding Nyquist bin of input time sequence
84 */
85
86 tdc.r = st->tmpbuf[0].r;
87 tdc.i = st->tmpbuf[0].i;
88 C_FIXDIV(tdc,2);
89 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
90 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
91 freqdata[0].r = tdc.r + tdc.i;
92 freqdata[ncfft].r = tdc.r - tdc.i;
93#ifdef USE_SIMD
94 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
95#else
96 freqdata[ncfft].i = freqdata[0].i = 0;
97#endif
98
99 for ( k=1;k <= ncfft/2 ; ++k ) {
100 fpk = st->tmpbuf[k];
101 fpnk.r = st->tmpbuf[ncfft-k].r;
102 fpnk.i = - st->tmpbuf[ncfft-k].i;
103 C_FIXDIV(fpk,2);
104 C_FIXDIV(fpnk,2);
105
106 C_ADD( f1k, fpk , fpnk );
107 C_SUB( f2k, fpk , fpnk );
108 C_MUL( tw , f2k , st->super_twiddles[k-1]);
109
110 freqdata[k].r = HALF_OF(f1k.r + tw.r);
111 freqdata[k].i = HALF_OF(f1k.i + tw.i);
112 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
113 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
114 }
115}
116
117void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
118{
119 /* input buffer timedata is stored row-wise */
120 int k, ncfft;
121
122 if (st->substate->inverse == 0) {
123 DEBUGF("kiss fft usage error: improper alloc\n");
124 return;
125 }
126
127 ncfft = st->substate->nfft;
128
129 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
130 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
131 C_FIXDIV(st->tmpbuf[0],2);
132
133 for (k = 1; k <= ncfft / 2; ++k) {
134 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
135 fk = freqdata[k];
136 fnkc.r = freqdata[ncfft - k].r;
137 fnkc.i = -freqdata[ncfft - k].i;
138 C_FIXDIV( fk , 2 );
139 C_FIXDIV( fnkc , 2 );
140
141 C_ADD (fek, fk, fnkc);
142 C_SUB (tmp, fk, fnkc);
143 C_MUL (fok, tmp, st->super_twiddles[k-1]);
144 C_ADD (st->tmpbuf[k], fek, fok);
145 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
146#ifdef USE_SIMD
147 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
148#else
149 st->tmpbuf[ncfft - k].i *= -1;
150#endif
151 }
152 kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
153}