comparison src/vamp-sdk/ext/vamp_kiss_fftr.c @ 501:90571dcc371a vamp-kiss-naming

Extensively rename things in the KissFFT headers to use a Vamp prefix. The motivation is not to change anything about the Vamp SDK library builds, but to avoid confusion in case any other code (for example that pulls in the Vamp SDK as part of a wider project definition) accidentally includes these headers instead of, or as well as, some other copy of KissFFT.
author Chris Cannam
date Tue, 30 Jan 2018 09:56:46 +0000
parents src/vamp-sdk/ext/kiss_fftr.c@e979a9c4ffb6
children
comparison
equal deleted inserted replaced
500:4a86f866bb6b 501:90571dcc371a
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 "vamp_kiss_fftr.h"
16 #include "vamp_kiss_fft_guts.h"
17
18 struct vamp_kiss_fftr_state{
19 vamp_kiss_fft_cfg substate;
20 vamp_kiss_fft_cpx * tmpbuf;
21 vamp_kiss_fft_cpx * super_twiddles;
22 };
23
24 vamp_kiss_fftr_cfg vamp_kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
25 {
26 int i;
27 vamp_kiss_fftr_cfg st = NULL;
28 size_t subsize, memneeded;
29
30 if (nfft & 1) {
31 fprintf(stderr,"Real FFT optimization must be even.\n");
32 return NULL;
33 }
34 nfft >>= 1;
35
36 vamp_kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
37 memneeded = sizeof(struct vamp_kiss_fftr_state) + subsize + sizeof(vamp_kiss_fft_cpx) * ( nfft * 3 / 2);
38
39 if (lenmem == NULL) {
40 st = (vamp_kiss_fftr_cfg) VAMP_KISS_FFT_MALLOC (memneeded);
41 } else {
42 if (*lenmem >= memneeded)
43 st = (vamp_kiss_fftr_cfg) mem;
44 *lenmem = memneeded;
45 }
46 if (!st)
47 return NULL;
48
49 st->substate = (vamp_kiss_fft_cfg) (st + 1); /*just beyond vamp_kiss_fftr_state struct */
50 st->tmpbuf = (vamp_kiss_fft_cpx *) (((char *) st->substate) + subsize);
51 st->super_twiddles = st->tmpbuf + nfft;
52 vamp_kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
53
54 for (i = 0; i < nfft/2; ++i) {
55 double phase =
56 -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
57 if (inverse_fft)
58 phase *= -1;
59 kf_cexp (st->super_twiddles+i,phase);
60 }
61 return st;
62 }
63
64 void vamp_kiss_fftr_free(void *mem)
65 {
66 free(mem);
67 }
68
69 void vamp_kiss_fftr(vamp_kiss_fftr_cfg st,const vamp_kiss_fft_scalar *timedata,vamp_kiss_fft_cpx *freqdata)
70 {
71 /* input buffer timedata is stored row-wise */
72 int k,ncfft;
73 vamp_kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
74
75 if ( st->substate->inverse) {
76 fprintf(stderr,"kiss fft usage error: improper alloc\n");
77 exit(1);
78 }
79
80 ncfft = st->substate->nfft;
81
82 /*perform the parallel fft of two real signals packed in real,imag*/
83 vamp_kiss_fft( st->substate , (const vamp_kiss_fft_cpx*)timedata, st->tmpbuf );
84 /* The real part of the DC element of the frequency spectrum in st->tmpbuf
85 * contains the sum of the even-numbered elements of the input time sequence
86 * The imag part is the sum of the odd-numbered elements
87 *
88 * The sum of tdc.r and tdc.i is the sum of the input time sequence.
89 * yielding DC of input time sequence
90 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
91 * yielding Nyquist bin of input time sequence
92 */
93
94 tdc.r = st->tmpbuf[0].r;
95 tdc.i = st->tmpbuf[0].i;
96 C_FIXDIV(tdc,2);
97 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
98 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
99 freqdata[0].r = tdc.r + tdc.i;
100 freqdata[ncfft].r = tdc.r - tdc.i;
101 freqdata[ncfft].i = freqdata[0].i = 0;
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
121 void vamp_kiss_fftri(vamp_kiss_fftr_cfg st,const vamp_kiss_fft_cpx *freqdata,vamp_kiss_fft_scalar *timedata)
122 {
123 /* input buffer timedata is stored row-wise */
124 int k, ncfft;
125
126 if (st->substate->inverse == 0) {
127 fprintf (stderr, "kiss fft usage error: improper alloc\n");
128 exit (1);
129 }
130
131 ncfft = st->substate->nfft;
132
133 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
134 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
135 C_FIXDIV(st->tmpbuf[0],2);
136
137 for (k = 1; k <= ncfft / 2; ++k) {
138 vamp_kiss_fft_cpx fk, fnkc, fek, fok, tmp;
139 fk = freqdata[k];
140 fnkc.r = freqdata[ncfft - k].r;
141 fnkc.i = -freqdata[ncfft - k].i;
142 C_FIXDIV( fk , 2 );
143 C_FIXDIV( fnkc , 2 );
144
145 C_ADD (fek, fk, fnkc);
146 C_SUB (tmp, fk, fnkc);
147 C_MUL (fok, tmp, st->super_twiddles[k-1]);
148 C_ADD (st->tmpbuf[k], fek, fok);
149 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
150 st->tmpbuf[ncfft - k].i *= -1;
151 }
152 vamp_kiss_fft (st->substate, st->tmpbuf, (vamp_kiss_fft_cpx *) timedata);
153 }