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 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "kiss_fftr_s32.h"
19 #include "_kiss_fft_guts_s32.h"
20 
21 struct kiss_fftr_s32_state
22 {
23   kiss_fft_s32_cfg substate;
24   kiss_fft_s32_cpx *tmpbuf;
25   kiss_fft_s32_cpx *super_twiddles;
26 #ifdef USE_SIMD
27   long pad;
28 #endif
29 };
30 
31 kiss_fftr_s32_cfg
kiss_fftr_s32_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)32 kiss_fftr_s32_alloc (int nfft, int inverse_fft, void *mem, size_t * lenmem)
33 {
34   int i;
35   kiss_fftr_s32_cfg st = NULL;
36   size_t subsize, memneeded;
37 
38   if (nfft & 1) {
39     fprintf (stderr, "Real FFT optimization must be even.\n");
40     return NULL;
41   }
42   nfft >>= 1;
43 
44   kiss_fft_s32_alloc (nfft, inverse_fft, NULL, &subsize);
45   memneeded = ALIGN_STRUCT (sizeof (struct kiss_fftr_s32_state))
46       + ALIGN_STRUCT (subsize)
47       + sizeof (kiss_fft_s32_cpx) * (nfft * 3 / 2);
48 
49   if (lenmem == NULL) {
50     st = (kiss_fftr_s32_cfg) KISS_FFT_S32_MALLOC (memneeded);
51   } else {
52     if (*lenmem >= memneeded)
53       st = (kiss_fftr_s32_cfg) mem;
54     *lenmem = memneeded;
55   }
56   if (!st)
57     return NULL;
58 
59   st->substate = (kiss_fft_s32_cfg) (((char *) st) + ALIGN_STRUCT (sizeof (struct kiss_fftr_s32_state)));       /*just beyond kiss_fftr_s32_state struct */
60   st->tmpbuf =
61       (kiss_fft_s32_cpx *) (((char *) st->substate) + ALIGN_STRUCT (subsize));
62   st->super_twiddles = st->tmpbuf + nfft;
63   kiss_fft_s32_alloc (nfft, inverse_fft, st->substate, &subsize);
64 
65   for (i = 0; i < nfft / 2; ++i) {
66     double phase =
67         -3.14159265358979323846264338327 * ((double) (i + 1) / nfft + .5);
68 
69     if (inverse_fft)
70       phase *= -1;
71     kf_cexp (st->super_twiddles + i, phase);
72   }
73   return st;
74 }
75 
76 void
kiss_fftr_s32(kiss_fftr_s32_cfg st,const kiss_fft_s32_scalar * timedata,kiss_fft_s32_cpx * freqdata)77 kiss_fftr_s32 (kiss_fftr_s32_cfg st, const kiss_fft_s32_scalar * timedata,
78     kiss_fft_s32_cpx * freqdata)
79 {
80   /* input buffer timedata is stored row-wise */
81   int k, ncfft;
82   kiss_fft_s32_cpx fpnk, fpk, f1k, f2k, tw, tdc;
83 
84   /* kiss fft usage error: improper alloc */
85   g_return_if_fail (st->substate->inverse == 0);
86 
87   ncfft = st->substate->nfft;
88 
89   /*perform the parallel fft of two real signals packed in real,imag */
90   kiss_fft_s32 (st->substate, (const kiss_fft_s32_cpx *) timedata, st->tmpbuf);
91   /* The real part of the DC element of the frequency spectrum in st->tmpbuf
92    * contains the sum of the even-numbered elements of the input time sequence
93    * The imag part is the sum of the odd-numbered elements
94    *
95    * The sum of tdc.r and tdc.i is the sum of the input time sequence.
96    *      yielding DC of input time sequence
97    * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
98    *      yielding Nyquist bin of input time sequence
99    */
100 
101   tdc.r = st->tmpbuf[0].r;
102   tdc.i = st->tmpbuf[0].i;
103   C_FIXDIV (tdc, 2);
104   CHECK_OVERFLOW_OP (tdc.r, +, tdc.i);
105   CHECK_OVERFLOW_OP (tdc.r, -, tdc.i);
106   freqdata[0].r = tdc.r + tdc.i;
107   freqdata[ncfft].r = tdc.r - tdc.i;
108 #ifdef USE_SIMD
109   freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps (0);
110 #else
111   freqdata[ncfft].i = freqdata[0].i = 0;
112 #endif
113 
114   for (k = 1; k <= ncfft / 2; ++k) {
115     fpk = st->tmpbuf[k];
116     fpnk.r = st->tmpbuf[ncfft - k].r;
117     fpnk.i = -st->tmpbuf[ncfft - k].i;
118     C_FIXDIV (fpk, 2);
119     C_FIXDIV (fpnk, 2);
120 
121     C_ADD (f1k, fpk, fpnk);
122     C_SUB (f2k, fpk, fpnk);
123     C_MUL (tw, f2k, st->super_twiddles[k - 1]);
124 
125     freqdata[k].r = HALF_OF (f1k.r + tw.r);
126     freqdata[k].i = HALF_OF (f1k.i + tw.i);
127     freqdata[ncfft - k].r = HALF_OF (f1k.r - tw.r);
128     freqdata[ncfft - k].i = HALF_OF (tw.i - f1k.i);
129   }
130 }
131 
132 void
kiss_fftri_s32(kiss_fftr_s32_cfg st,const kiss_fft_s32_cpx * freqdata,kiss_fft_s32_scalar * timedata)133 kiss_fftri_s32 (kiss_fftr_s32_cfg st, const kiss_fft_s32_cpx * freqdata,
134     kiss_fft_s32_scalar * timedata)
135 {
136   /* input buffer timedata is stored row-wise */
137   int k, ncfft;
138 
139   /* kiss fft usage error: improper alloc */
140   g_return_if_fail (st->substate->inverse != 0);
141 
142   ncfft = st->substate->nfft;
143 
144   st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
145   st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
146   C_FIXDIV (st->tmpbuf[0], 2);
147 
148   for (k = 1; k <= ncfft / 2; ++k) {
149     kiss_fft_s32_cpx fk, fnkc, fek, fok, tmp;
150 
151     fk = freqdata[k];
152     fnkc.r = freqdata[ncfft - k].r;
153     fnkc.i = -freqdata[ncfft - k].i;
154     C_FIXDIV (fk, 2);
155     C_FIXDIV (fnkc, 2);
156 
157     C_ADD (fek, fk, fnkc);
158     C_SUB (tmp, fk, fnkc);
159     C_MUL (fok, tmp, st->super_twiddles[k - 1]);
160     C_ADD (st->tmpbuf[k], fek, fok);
161     C_SUB (st->tmpbuf[ncfft - k], fek, fok);
162 #ifdef USE_SIMD
163     st->tmpbuf[ncfft - k].i *= _mm_set1_ps (-1.0);
164 #else
165     st->tmpbuf[ncfft - k].i *= -1;
166 #endif
167   }
168   kiss_fft_s32 (st->substate, st->tmpbuf, (kiss_fft_s32_cpx *) timedata);
169 }
170