comparison fft/kissfft/tools/kiss_fftr.c @ 31:59a1ee198dca

Add KissFFT sources (including change to make free a function)
author Chris Cannam
date Mon, 09 Nov 2015 11:46:35 +0000
parents
children
comparison
equal deleted inserted replaced
30:55f715002fe5 31:59a1ee198dca
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 struct kiss_fftr_state{
19 kiss_fft_cfg substate;
20 kiss_fft_cpx * tmpbuf;
21 kiss_fft_cpx * super_twiddles;
22 #ifdef USE_SIMD
23 void * pad;
24 #endif
25 };
26
27 void kiss_fftr_free(struct kiss_fftr_state *state) {
28 free(state);
29 }
30
31 kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
32 {
33 int i;
34 kiss_fftr_cfg st = NULL;
35 size_t subsize, memneeded;
36
37 if (nfft & 1) {
38 fprintf(stderr,"Real FFT optimization must be even.\n");
39 return NULL;
40 }
41 nfft >>= 1;
42
43 kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
44 memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
45
46 if (lenmem == NULL) {
47 st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
48 } else {
49 if (*lenmem >= memneeded)
50 st = (kiss_fftr_cfg) mem;
51 *lenmem = memneeded;
52 }
53 if (!st)
54 return NULL;
55
56 st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
57 st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
58 st->super_twiddles = st->tmpbuf + nfft;
59 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
60
61 for (i = 0; i < nfft/2; ++i) {
62 double phase =
63 -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
64 if (inverse_fft)
65 phase *= -1;
66 kf_cexp (st->super_twiddles+i,phase);
67 }
68 return st;
69 }
70
71 void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
72 {
73 /* input buffer timedata is stored row-wise */
74 int k,ncfft;
75 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
76
77 if ( st->substate->inverse) {
78 fprintf(stderr,"kiss fft usage error: improper alloc\n");
79 exit(1);
80 }
81
82 ncfft = st->substate->nfft;
83
84 /*perform the parallel fft of two real signals packed in real,imag*/
85 kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
86 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
87 * contains the sum of the even-numbered elements of the input time sequence
88 * The imag part is the sum of the odd-numbered elements
89 *
90 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
91 * yielding DC of input time sequence
92 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
93 * yielding Nyquist bin of input time sequence
94 */
95
96 tdc.r = st->tmpbuf[0].r;
97 tdc.i = st->tmpbuf[0].i;
98 C_FIXDIV(tdc,2);
99 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
100 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
101 freqdata[0].r = tdc.r + tdc.i;
102 freqdata[ncfft].r = tdc.r - tdc.i;
103 #ifdef USE_SIMD
104 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
105 #else
106 freqdata[ncfft].i = freqdata[0].i = 0;
107 #endif
108
109 for ( k=1;k <= ncfft/2 ; ++k ) {
110 fpk = st->tmpbuf[k];
111 fpnk.r = st->tmpbuf[ncfft-k].r;
112 fpnk.i = - st->tmpbuf[ncfft-k].i;
113 C_FIXDIV(fpk,2);
114 C_FIXDIV(fpnk,2);
115
116 C_ADD( f1k, fpk , fpnk );
117 C_SUB( f2k, fpk , fpnk );
118 C_MUL( tw , f2k , st->super_twiddles[k-1]);
119
120 freqdata[k].r = HALF_OF(f1k.r + tw.r);
121 freqdata[k].i = HALF_OF(f1k.i + tw.i);
122 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
123 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
124 }
125 }
126
127 void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
128 {
129 /* input buffer timedata is stored row-wise */
130 int k, ncfft;
131
132 if (st->substate->inverse == 0) {
133 fprintf (stderr, "kiss fft usage error: improper alloc\n");
134 exit (1);
135 }
136
137 ncfft = st->substate->nfft;
138
139 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
140 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
141 C_FIXDIV(st->tmpbuf[0],2);
142
143 for (k = 1; k <= ncfft / 2; ++k) {
144 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
145 fk = freqdata[k];
146 fnkc.r = freqdata[ncfft - k].r;
147 fnkc.i = -freqdata[ncfft - k].i;
148 C_FIXDIV( fk , 2 );
149 C_FIXDIV( fnkc , 2 );
150
151 C_ADD (fek, fk, fnkc);
152 C_SUB (tmp, fk, fnkc);
153 C_MUL (fok, tmp, st->super_twiddles[k-1]);
154 C_ADD (st->tmpbuf[k], fek, fok);
155 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
156 #ifdef USE_SIMD
157 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
158 #else
159 st->tmpbuf[ncfft - k].i *= -1;
160 #endif
161 }
162 kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
163 }