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