summaryrefslogtreecommitdiff
path: root/kiss_fftr.c
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2025-08-15 12:42:40 +0200
committererdgeist <erdgeist@erdgeist.org>2025-08-15 12:42:40 +0200
commit30325d24d107dbf133da39f7c96d1510fd1c9449 (patch)
tree932baa5b2a4475821f16dccf9e3e05011daa6d92 /kiss_fftr.c
parent9022d768021bbe15c7815cc6f8b64218b46f0e10 (diff)
Bump to codec2 version 1.2.0erdgeist-bump-to-1.2.0
Diffstat (limited to 'kiss_fftr.c')
-rw-r--r--kiss_fftr.c272
1 files changed, 143 insertions, 129 deletions
diff --git a/kiss_fftr.c b/kiss_fftr.c
index 7cc0286..e3aee37 100644
--- a/kiss_fftr.c
+++ b/kiss_fftr.c
@@ -3,152 +3,166 @@ Copyright (c) 2003-2004, Mark Borgerding
3 3
4All rights reserved. 4All rights reserved.
5 5
6Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6Redistribution and use in source and binary forms, with or without modification,
7 7are 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. 8
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. 9 * Redistributions of source code must retain the above copyright notice,
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. 10this list of conditions and the following disclaimer.
11 11 * Redistributions in binary form must reproduce the above copyright notice,
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. 12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14 * Neither the author nor the names of any contributors may be used to
15endorse or promote products derived from this software without specific prior
16written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*/ 28*/
14 29
15#include "kiss_fftr.h" 30#include "kiss_fftr.h"
31
16#include "_kiss_fft_guts.h" 32#include "_kiss_fft_guts.h"
17#include "assert.h" 33#include "assert.h"
18 34
19struct kiss_fftr_state{ 35struct kiss_fftr_state {
20 kiss_fft_cfg substate; 36 kiss_fft_cfg substate;
21 kiss_fft_cpx * tmpbuf; 37 kiss_fft_cpx *tmpbuf;
22 kiss_fft_cpx * super_twiddles; 38 kiss_fft_cpx *super_twiddles;
23#ifdef USE_SIMD 39#ifdef USE_SIMD
24 void * pad; 40 void *pad;
25#endif 41#endif
26}; 42};
27 43
28kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) 44kiss_fftr_cfg kiss_fftr_alloc(int nfft, int inverse_fft, void *mem,
29{ 45 size_t *lenmem) {
30 int i; 46 int i;
31 kiss_fftr_cfg st = NULL; 47 kiss_fftr_cfg st = NULL;
32 size_t subsize, memneeded; 48 size_t subsize, memneeded;
33 49
34 if (nfft & 1) { 50 if (nfft & 1) {
35 fprintf(stderr,"Real FFT optimization must be even.\n"); 51 fprintf(stderr, "Real FFT optimization must be even.\n");
36 return NULL; 52 return NULL;
37 } 53 }
38 nfft >>= 1; 54 nfft >>= 1;
39 55
40 kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); 56 kiss_fft_alloc(nfft, inverse_fft, NULL, &subsize);
41 memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); 57 memneeded = sizeof(struct kiss_fftr_state) + subsize +
42 58 sizeof(kiss_fft_cpx) * (nfft * 3 / 2);
43 if (lenmem == NULL) { 59
44 st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); 60 if (lenmem == NULL) {
45 } else { 61 st = (kiss_fftr_cfg)KISS_FFT_MALLOC(memneeded);
46 if (*lenmem >= memneeded) 62 } else {
47 st = (kiss_fftr_cfg) mem; 63 if (*lenmem >= memneeded) st = (kiss_fftr_cfg)mem;
48 *lenmem = memneeded; 64 *lenmem = memneeded;
49 } 65 }
50 if (!st) 66 if (!st) return NULL;
51 return NULL; 67
52 68 st->substate = (kiss_fft_cfg)(st + 1); /*just beyond kiss_fftr_state struct */
53 st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ 69 st->tmpbuf = (kiss_fft_cpx *)(((char *)st->substate) + subsize);
54 st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); 70 st->super_twiddles = st->tmpbuf + nfft;
55 st->super_twiddles = st->tmpbuf + nfft; 71 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
56 kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); 72
57 73 for (i = 0; i < nfft / 2; ++i) {
58 for (i = 0; i < nfft/2; ++i) { 74 float phase =
59 float phase = 75 -3.14159265358979323846264338327 * ((float)(i + 1) / nfft + .5);
60 -3.14159265358979323846264338327 * ((float) (i+1) / nfft + .5); 76 if (inverse_fft) phase *= -1;
61 if (inverse_fft) 77 kf_cexp(st->super_twiddles + i, phase);
62 phase *= -1; 78 }
63 kf_cexp (st->super_twiddles+i,phase); 79 return st;
64 }
65 return st;
66} 80}
67 81
68void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 82void kiss_fftr(kiss_fftr_cfg st, const kiss_fft_scalar *timedata,
69{ 83 kiss_fft_cpx *freqdata) {
70 /* input buffer timedata is stored row-wise */ 84 /* input buffer timedata is stored row-wise */
71 int k,ncfft; 85 int k, ncfft;
72 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; 86 kiss_fft_cpx fpnk, fpk, f1k, f2k, tw, tdc;
73 87
74 assert(st->substate->inverse==0); 88 assert(st->substate->inverse == 0);
75 89
76 ncfft = st->substate->nfft; 90 ncfft = st->substate->nfft;
77 91
78 /*perform the parallel fft of two real signals packed in real,imag*/ 92 /*perform the parallel fft of two real signals packed in real,imag*/
79 kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); 93 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 94 /* 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 95 * 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 96 * The imag part is the sum of the odd-numbered elements
83 * 97 *
84 * The sum of tdc.r and tdc.i is the sum of the input time sequence. 98 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
85 * yielding DC of input time sequence 99 * 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... 100 * The difference of tdc.r - tdc.i is the sum of the input (dot product)
87 * yielding Nyquist bin of input time sequence 101 * [1,-1,1,-1... yielding Nyquist bin of input time sequence
88 */ 102 */
89 103
90 tdc.r = st->tmpbuf[0].r; 104 tdc.r = st->tmpbuf[0].r;
91 tdc.i = st->tmpbuf[0].i; 105 tdc.i = st->tmpbuf[0].i;
92 C_FIXDIV(tdc,2); 106 C_FIXDIV(tdc, 2);
93 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); 107 CHECK_OVERFLOW_OP(tdc.r, +, tdc.i);
94 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); 108 CHECK_OVERFLOW_OP(tdc.r, -, tdc.i);
95 freqdata[0].r = tdc.r + tdc.i; 109 freqdata[0].r = tdc.r + tdc.i;
96 freqdata[ncfft].r = tdc.r - tdc.i; 110 freqdata[ncfft].r = tdc.r - tdc.i;
97#ifdef USE_SIMD 111#ifdef USE_SIMD
98 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); 112 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
99#else 113#else
100 freqdata[ncfft].i = freqdata[0].i = 0; 114 freqdata[ncfft].i = freqdata[0].i = 0;
101#endif 115#endif
102 116
103 for ( k=1;k <= ncfft/2 ; ++k ) { 117 for (k = 1; k <= ncfft / 2; ++k) {
104 fpk = st->tmpbuf[k]; 118 fpk = st->tmpbuf[k];
105 fpnk.r = st->tmpbuf[ncfft-k].r; 119 fpnk.r = st->tmpbuf[ncfft - k].r;
106 fpnk.i = - st->tmpbuf[ncfft-k].i; 120 fpnk.i = -st->tmpbuf[ncfft - k].i;
107 C_FIXDIV(fpk,2); 121 C_FIXDIV(fpk, 2);
108 C_FIXDIV(fpnk,2); 122 C_FIXDIV(fpnk, 2);
109 123
110 C_ADD( f1k, fpk , fpnk ); 124 C_ADD(f1k, fpk, fpnk);
111 C_SUB( f2k, fpk , fpnk ); 125 C_SUB(f2k, fpk, fpnk);
112 C_MUL( tw , f2k , st->super_twiddles[k-1]); 126 C_MUL(tw, f2k, st->super_twiddles[k - 1]);
113 127
114 freqdata[k].r = HALF_OF(f1k.r + tw.r); 128 freqdata[k].r = HALF_OF(f1k.r + tw.r);
115 freqdata[k].i = HALF_OF(f1k.i + tw.i); 129 freqdata[k].i = HALF_OF(f1k.i + tw.i);
116 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); 130 freqdata[ncfft - k].r = HALF_OF(f1k.r - tw.r);
117 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); 131 freqdata[ncfft - k].i = HALF_OF(tw.i - f1k.i);
118 } 132 }
119} 133}
120 134
121void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) 135void kiss_fftri(kiss_fftr_cfg st, const kiss_fft_cpx *freqdata,
122{ 136 kiss_fft_scalar *timedata) {
123 /* input buffer timedata is stored row-wise */ 137 /* input buffer timedata is stored row-wise */
124 int k, ncfft; 138 int k, ncfft;
125 139
126 assert(st->substate->inverse == 1); 140 assert(st->substate->inverse == 1);
127 141
128 ncfft = st->substate->nfft; 142 ncfft = st->substate->nfft;
129 143
130 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; 144 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
131 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; 145 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
132 C_FIXDIV(st->tmpbuf[0],2); 146 C_FIXDIV(st->tmpbuf[0], 2);
133 147
134 for (k = 1; k <= ncfft / 2; ++k) { 148 for (k = 1; k <= ncfft / 2; ++k) {
135 kiss_fft_cpx fk, fnkc, fek, fok, tmp; 149 kiss_fft_cpx fk, fnkc, fek, fok, tmp;
136 fk = freqdata[k]; 150 fk = freqdata[k];
137 fnkc.r = freqdata[ncfft - k].r; 151 fnkc.r = freqdata[ncfft - k].r;
138 fnkc.i = -freqdata[ncfft - k].i; 152 fnkc.i = -freqdata[ncfft - k].i;
139 C_FIXDIV( fk , 2 ); 153 C_FIXDIV(fk, 2);
140 C_FIXDIV( fnkc , 2 ); 154 C_FIXDIV(fnkc, 2);
141 155
142 C_ADD (fek, fk, fnkc); 156 C_ADD(fek, fk, fnkc);
143 C_SUB (tmp, fk, fnkc); 157 C_SUB(tmp, fk, fnkc);
144 C_MUL (fok, tmp, st->super_twiddles[k-1]); 158 C_MUL(fok, tmp, st->super_twiddles[k - 1]);
145 C_ADD (st->tmpbuf[k], fek, fok); 159 C_ADD(st->tmpbuf[k], fek, fok);
146 C_SUB (st->tmpbuf[ncfft - k], fek, fok); 160 C_SUB(st->tmpbuf[ncfft - k], fek, fok);
147#ifdef USE_SIMD 161#ifdef USE_SIMD
148 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); 162 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
149#else 163#else
150 st->tmpbuf[ncfft - k].i *= -1; 164 st->tmpbuf[ncfft - k].i *= -1;
151#endif 165#endif
152 } 166 }
153 kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); 167 kiss_fft(st->substate, st->tmpbuf, (kiss_fft_cpx *)timedata);
154} 168}