kissfft 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,30 @@
1
+ Kiss FFT library
2
+ ==================
3
+
4
+
5
+ Copyright (c) 2003-2010 Mark Borgerding
6
+
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
10
+
11
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
+ * 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.
13
+ * 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.
14
+
15
+ 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.
16
+
17
+ Ruby wrapper layer
18
+ ==================
19
+
20
+ Copyright (C) 2009-2012 H D Moore < hdm[at]rapid7.com >
21
+
22
+ All rights reserved.
23
+
24
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
25
+
26
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
27
+ * 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.
28
+ * 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.
29
+
30
+ 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.
@@ -0,0 +1,164 @@
1
+ /*
2
+ Copyright (c) 2003-2010, 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
+ /* kiss_fft.h
16
+ defines kiss_fft_scalar as either short or a float type
17
+ and defines
18
+ typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
19
+ #include "kiss_fft.h"
20
+ #include <limits.h>
21
+
22
+ #define MAXFACTORS 32
23
+ /* e.g. an fft of length 128 has 4 factors
24
+ as far as kissfft is concerned
25
+ 4*4*4*2
26
+ */
27
+
28
+ struct kiss_fft_state{
29
+ int nfft;
30
+ int inverse;
31
+ int factors[2*MAXFACTORS];
32
+ kiss_fft_cpx twiddles[1];
33
+ };
34
+
35
+ /*
36
+ Explanation of macros dealing with complex math:
37
+
38
+ C_MUL(m,a,b) : m = a*b
39
+ C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
40
+ C_SUB( res, a,b) : res = a - b
41
+ C_SUBFROM( res , a) : res -= a
42
+ C_ADDTO( res , a) : res += a
43
+ * */
44
+ #ifdef FIXED_POINT
45
+ #if (FIXED_POINT==32)
46
+ # define FRACBITS 31
47
+ # define SAMPPROD int64_t
48
+ #define SAMP_MAX 2147483647
49
+ #else
50
+ # define FRACBITS 15
51
+ # define SAMPPROD int32_t
52
+ #define SAMP_MAX 32767
53
+ #endif
54
+
55
+ #define SAMP_MIN -SAMP_MAX
56
+
57
+ #if defined(CHECK_OVERFLOW)
58
+ # define CHECK_OVERFLOW_OP(a,op,b) \
59
+ if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
60
+ fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
61
+ #endif
62
+
63
+
64
+ # define smul(a,b) ( (SAMPPROD)(a)*(b) )
65
+ # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
66
+
67
+ # define S_MUL(a,b) sround( smul(a,b) )
68
+
69
+ # define C_MUL(m,a,b) \
70
+ do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
71
+ (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
72
+
73
+ # define DIVSCALAR(x,k) \
74
+ (x) = sround( smul( x, SAMP_MAX/k ) )
75
+
76
+ # define C_FIXDIV(c,div) \
77
+ do { DIVSCALAR( (c).r , div); \
78
+ DIVSCALAR( (c).i , div); }while (0)
79
+
80
+ # define C_MULBYSCALAR( c, s ) \
81
+ do{ (c).r = sround( smul( (c).r , s ) ) ;\
82
+ (c).i = sround( smul( (c).i , s ) ) ; }while(0)
83
+
84
+ #else /* not FIXED_POINT*/
85
+
86
+ # define S_MUL(a,b) ( (a)*(b) )
87
+ #define C_MUL(m,a,b) \
88
+ do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
89
+ (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
90
+ # define C_FIXDIV(c,div) /* NOOP */
91
+ # define C_MULBYSCALAR( c, s ) \
92
+ do{ (c).r *= (s);\
93
+ (c).i *= (s); }while(0)
94
+ #endif
95
+
96
+ #ifndef CHECK_OVERFLOW_OP
97
+ # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
98
+ #endif
99
+
100
+ #define C_ADD( res, a,b)\
101
+ do { \
102
+ CHECK_OVERFLOW_OP((a).r,+,(b).r)\
103
+ CHECK_OVERFLOW_OP((a).i,+,(b).i)\
104
+ (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
105
+ }while(0)
106
+ #define C_SUB( res, a,b)\
107
+ do { \
108
+ CHECK_OVERFLOW_OP((a).r,-,(b).r)\
109
+ CHECK_OVERFLOW_OP((a).i,-,(b).i)\
110
+ (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
111
+ }while(0)
112
+ #define C_ADDTO( res , a)\
113
+ do { \
114
+ CHECK_OVERFLOW_OP((res).r,+,(a).r)\
115
+ CHECK_OVERFLOW_OP((res).i,+,(a).i)\
116
+ (res).r += (a).r; (res).i += (a).i;\
117
+ }while(0)
118
+
119
+ #define C_SUBFROM( res , a)\
120
+ do {\
121
+ CHECK_OVERFLOW_OP((res).r,-,(a).r)\
122
+ CHECK_OVERFLOW_OP((res).i,-,(a).i)\
123
+ (res).r -= (a).r; (res).i -= (a).i; \
124
+ }while(0)
125
+
126
+
127
+ #ifdef FIXED_POINT
128
+ # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
129
+ # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
130
+ # define HALF_OF(x) ((x)>>1)
131
+ #elif defined(USE_SIMD)
132
+ # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
133
+ # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
134
+ # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
135
+ #else
136
+ # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
137
+ # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
138
+ # define HALF_OF(x) ((x)*.5)
139
+ #endif
140
+
141
+ #define kf_cexp(x,phase) \
142
+ do{ \
143
+ (x)->r = KISS_FFT_COS(phase);\
144
+ (x)->i = KISS_FFT_SIN(phase);\
145
+ }while(0)
146
+
147
+
148
+ /* a debugging function */
149
+ #define pcpx(c)\
150
+ fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
151
+
152
+
153
+ #ifdef KISS_FFT_USE_ALLOCA
154
+ // define this to allow use of alloca instead of malloc for temporary buffers
155
+ // Temporary buffers are used in two case:
156
+ // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
157
+ // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform.
158
+ #include <alloca.h>
159
+ #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
160
+ #define KISS_FFT_TMP_FREE(ptr)
161
+ #else
162
+ #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
163
+ #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
164
+ #endif
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+
3
+ if(have_library("m"))
4
+ create_makefile("kissfft")
5
+ end
@@ -0,0 +1,408 @@
1
+ /*
2
+ Copyright (c) 2003-2010, 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
+
16
+ #include "_kiss_fft_guts.h"
17
+ /* The guts header contains all the multiplication and addition macros that are defined for
18
+ fixed or floating point complex numbers. It also delares the kf_ internal functions.
19
+ */
20
+
21
+ static void kf_bfly2(
22
+ kiss_fft_cpx * Fout,
23
+ const size_t fstride,
24
+ const kiss_fft_cfg st,
25
+ int m
26
+ )
27
+ {
28
+ kiss_fft_cpx * Fout2;
29
+ kiss_fft_cpx * tw1 = st->twiddles;
30
+ kiss_fft_cpx t;
31
+ Fout2 = Fout + m;
32
+ do{
33
+ C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
34
+
35
+ C_MUL (t, *Fout2 , *tw1);
36
+ tw1 += fstride;
37
+ C_SUB( *Fout2 , *Fout , t );
38
+ C_ADDTO( *Fout , t );
39
+ ++Fout2;
40
+ ++Fout;
41
+ }while (--m);
42
+ }
43
+
44
+ static void kf_bfly4(
45
+ kiss_fft_cpx * Fout,
46
+ const size_t fstride,
47
+ const kiss_fft_cfg st,
48
+ const size_t m
49
+ )
50
+ {
51
+ kiss_fft_cpx *tw1,*tw2,*tw3;
52
+ kiss_fft_cpx scratch[6];
53
+ size_t k=m;
54
+ const size_t m2=2*m;
55
+ const size_t m3=3*m;
56
+
57
+
58
+ tw3 = tw2 = tw1 = st->twiddles;
59
+
60
+ do {
61
+ C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
62
+
63
+ C_MUL(scratch[0],Fout[m] , *tw1 );
64
+ C_MUL(scratch[1],Fout[m2] , *tw2 );
65
+ C_MUL(scratch[2],Fout[m3] , *tw3 );
66
+
67
+ C_SUB( scratch[5] , *Fout, scratch[1] );
68
+ C_ADDTO(*Fout, scratch[1]);
69
+ C_ADD( scratch[3] , scratch[0] , scratch[2] );
70
+ C_SUB( scratch[4] , scratch[0] , scratch[2] );
71
+ C_SUB( Fout[m2], *Fout, scratch[3] );
72
+ tw1 += fstride;
73
+ tw2 += fstride*2;
74
+ tw3 += fstride*3;
75
+ C_ADDTO( *Fout , scratch[3] );
76
+
77
+ if(st->inverse) {
78
+ Fout[m].r = scratch[5].r - scratch[4].i;
79
+ Fout[m].i = scratch[5].i + scratch[4].r;
80
+ Fout[m3].r = scratch[5].r + scratch[4].i;
81
+ Fout[m3].i = scratch[5].i - scratch[4].r;
82
+ }else{
83
+ Fout[m].r = scratch[5].r + scratch[4].i;
84
+ Fout[m].i = scratch[5].i - scratch[4].r;
85
+ Fout[m3].r = scratch[5].r - scratch[4].i;
86
+ Fout[m3].i = scratch[5].i + scratch[4].r;
87
+ }
88
+ ++Fout;
89
+ }while(--k);
90
+ }
91
+
92
+ static void kf_bfly3(
93
+ kiss_fft_cpx * Fout,
94
+ const size_t fstride,
95
+ const kiss_fft_cfg st,
96
+ size_t m
97
+ )
98
+ {
99
+ size_t k=m;
100
+ const size_t m2 = 2*m;
101
+ kiss_fft_cpx *tw1,*tw2;
102
+ kiss_fft_cpx scratch[5];
103
+ kiss_fft_cpx epi3;
104
+ epi3 = st->twiddles[fstride*m];
105
+
106
+ tw1=tw2=st->twiddles;
107
+
108
+ do{
109
+ C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
110
+
111
+ C_MUL(scratch[1],Fout[m] , *tw1);
112
+ C_MUL(scratch[2],Fout[m2] , *tw2);
113
+
114
+ C_ADD(scratch[3],scratch[1],scratch[2]);
115
+ C_SUB(scratch[0],scratch[1],scratch[2]);
116
+ tw1 += fstride;
117
+ tw2 += fstride*2;
118
+
119
+ Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
120
+ Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
121
+
122
+ C_MULBYSCALAR( scratch[0] , epi3.i );
123
+
124
+ C_ADDTO(*Fout,scratch[3]);
125
+
126
+ Fout[m2].r = Fout[m].r + scratch[0].i;
127
+ Fout[m2].i = Fout[m].i - scratch[0].r;
128
+
129
+ Fout[m].r -= scratch[0].i;
130
+ Fout[m].i += scratch[0].r;
131
+
132
+ ++Fout;
133
+ }while(--k);
134
+ }
135
+
136
+ static void kf_bfly5(
137
+ kiss_fft_cpx * Fout,
138
+ const size_t fstride,
139
+ const kiss_fft_cfg st,
140
+ int m
141
+ )
142
+ {
143
+ kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
144
+ int u;
145
+ kiss_fft_cpx scratch[13];
146
+ kiss_fft_cpx * twiddles = st->twiddles;
147
+ kiss_fft_cpx *tw;
148
+ kiss_fft_cpx ya,yb;
149
+ ya = twiddles[fstride*m];
150
+ yb = twiddles[fstride*2*m];
151
+
152
+ Fout0=Fout;
153
+ Fout1=Fout0+m;
154
+ Fout2=Fout0+2*m;
155
+ Fout3=Fout0+3*m;
156
+ Fout4=Fout0+4*m;
157
+
158
+ tw=st->twiddles;
159
+ for ( u=0; u<m; ++u ) {
160
+ C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
161
+ scratch[0] = *Fout0;
162
+
163
+ C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
164
+ C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
165
+ C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
166
+ C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
167
+
168
+ C_ADD( scratch[7],scratch[1],scratch[4]);
169
+ C_SUB( scratch[10],scratch[1],scratch[4]);
170
+ C_ADD( scratch[8],scratch[2],scratch[3]);
171
+ C_SUB( scratch[9],scratch[2],scratch[3]);
172
+
173
+ Fout0->r += scratch[7].r + scratch[8].r;
174
+ Fout0->i += scratch[7].i + scratch[8].i;
175
+
176
+ scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
177
+ scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
178
+
179
+ scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
180
+ scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
181
+
182
+ C_SUB(*Fout1,scratch[5],scratch[6]);
183
+ C_ADD(*Fout4,scratch[5],scratch[6]);
184
+
185
+ scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
186
+ scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
187
+ scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
188
+ scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
189
+
190
+ C_ADD(*Fout2,scratch[11],scratch[12]);
191
+ C_SUB(*Fout3,scratch[11],scratch[12]);
192
+
193
+ ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
194
+ }
195
+ }
196
+
197
+ /* perform the butterfly for one stage of a mixed radix FFT */
198
+ static void kf_bfly_generic(
199
+ kiss_fft_cpx * Fout,
200
+ const size_t fstride,
201
+ const kiss_fft_cfg st,
202
+ int m,
203
+ int p
204
+ )
205
+ {
206
+ int u,k,q1,q;
207
+ kiss_fft_cpx * twiddles = st->twiddles;
208
+ kiss_fft_cpx t;
209
+ int Norig = st->nfft;
210
+
211
+ kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p);
212
+
213
+ for ( u=0; u<m; ++u ) {
214
+ k=u;
215
+ for ( q1=0 ; q1<p ; ++q1 ) {
216
+ scratch[q1] = Fout[ k ];
217
+ C_FIXDIV(scratch[q1],p);
218
+ k += m;
219
+ }
220
+
221
+ k=u;
222
+ for ( q1=0 ; q1<p ; ++q1 ) {
223
+ int twidx=0;
224
+ Fout[ k ] = scratch[0];
225
+ for (q=1;q<p;++q ) {
226
+ twidx += fstride * k;
227
+ if (twidx>=Norig) twidx-=Norig;
228
+ C_MUL(t,scratch[q] , twiddles[twidx] );
229
+ C_ADDTO( Fout[ k ] ,t);
230
+ }
231
+ k += m;
232
+ }
233
+ }
234
+ KISS_FFT_TMP_FREE(scratch);
235
+ }
236
+
237
+ static
238
+ void kf_work(
239
+ kiss_fft_cpx * Fout,
240
+ const kiss_fft_cpx * f,
241
+ const size_t fstride,
242
+ int in_stride,
243
+ int * factors,
244
+ const kiss_fft_cfg st
245
+ )
246
+ {
247
+ kiss_fft_cpx * Fout_beg=Fout;
248
+ const int p=*factors++; /* the radix */
249
+ const int m=*factors++; /* stage's fft length/p */
250
+ const kiss_fft_cpx * Fout_end = Fout + p*m;
251
+
252
+ #ifdef _OPENMP
253
+ // use openmp extensions at the
254
+ // top-level (not recursive)
255
+ if (fstride==1 && p<=5)
256
+ {
257
+ int k;
258
+
259
+ // execute the p different work units in different threads
260
+ # pragma omp parallel for
261
+ for (k=0;k<p;++k)
262
+ kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
263
+ // all threads have joined by this point
264
+
265
+ switch (p) {
266
+ case 2: kf_bfly2(Fout,fstride,st,m); break;
267
+ case 3: kf_bfly3(Fout,fstride,st,m); break;
268
+ case 4: kf_bfly4(Fout,fstride,st,m); break;
269
+ case 5: kf_bfly5(Fout,fstride,st,m); break;
270
+ default: kf_bfly_generic(Fout,fstride,st,m,p); break;
271
+ }
272
+ return;
273
+ }
274
+ #endif
275
+
276
+ if (m==1) {
277
+ do{
278
+ *Fout = *f;
279
+ f += fstride*in_stride;
280
+ }while(++Fout != Fout_end );
281
+ }else{
282
+ do{
283
+ // recursive call:
284
+ // DFT of size m*p performed by doing
285
+ // p instances of smaller DFTs of size m,
286
+ // each one takes a decimated version of the input
287
+ kf_work( Fout , f, fstride*p, in_stride, factors,st);
288
+ f += fstride*in_stride;
289
+ }while( (Fout += m) != Fout_end );
290
+ }
291
+
292
+ Fout=Fout_beg;
293
+
294
+ // recombine the p smaller DFTs
295
+ switch (p) {
296
+ case 2: kf_bfly2(Fout,fstride,st,m); break;
297
+ case 3: kf_bfly3(Fout,fstride,st,m); break;
298
+ case 4: kf_bfly4(Fout,fstride,st,m); break;
299
+ case 5: kf_bfly5(Fout,fstride,st,m); break;
300
+ default: kf_bfly_generic(Fout,fstride,st,m,p); break;
301
+ }
302
+ }
303
+
304
+ /* facbuf is populated by p1,m1,p2,m2, ...
305
+ where
306
+ p[i] * m[i] = m[i-1]
307
+ m0 = n */
308
+ static
309
+ void kf_factor(int n,int * facbuf)
310
+ {
311
+ int p=4;
312
+ double floor_sqrt;
313
+ floor_sqrt = floor( sqrt((double)n) );
314
+
315
+ /*factor out powers of 4, powers of 2, then any remaining primes */
316
+ do {
317
+ while (n % p) {
318
+ switch (p) {
319
+ case 4: p = 2; break;
320
+ case 2: p = 3; break;
321
+ default: p += 2; break;
322
+ }
323
+ if (p > floor_sqrt)
324
+ p = n; /* no more factors, skip to end */
325
+ }
326
+ n /= p;
327
+ *facbuf++ = p;
328
+ *facbuf++ = n;
329
+ } while (n > 1);
330
+ }
331
+
332
+ /*
333
+ *
334
+ * User-callable function to allocate all necessary storage space for the fft.
335
+ *
336
+ * The return value is a contiguous block of memory, allocated with malloc. As such,
337
+ * It can be freed with free(), rather than a kiss_fft-specific function.
338
+ * */
339
+ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
340
+ {
341
+ kiss_fft_cfg st=NULL;
342
+ size_t memneeded = sizeof(struct kiss_fft_state)
343
+ + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
344
+
345
+ if ( lenmem==NULL ) {
346
+ st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
347
+ }else{
348
+ if (mem != NULL && *lenmem >= memneeded)
349
+ st = (kiss_fft_cfg)mem;
350
+ *lenmem = memneeded;
351
+ }
352
+ if (st) {
353
+ int i;
354
+ st->nfft=nfft;
355
+ st->inverse = inverse_fft;
356
+
357
+ for (i=0;i<nfft;++i) {
358
+ const double pi=3.141592653589793238462643383279502884197169399375105820974944;
359
+ double phase = -2*pi*i / nfft;
360
+ if (st->inverse)
361
+ phase *= -1;
362
+ kf_cexp(st->twiddles+i, phase );
363
+ }
364
+
365
+ kf_factor(nfft,st->factors);
366
+ }
367
+ return st;
368
+ }
369
+
370
+
371
+ void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
372
+ {
373
+ if (fin == fout) {
374
+ //NOTE: this is not really an in-place FFT algorithm.
375
+ //It just performs an out-of-place FFT into a temp buffer
376
+ kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
377
+ kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
378
+ memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
379
+ KISS_FFT_TMP_FREE(tmpbuf);
380
+ }else{
381
+ kf_work( fout, fin, 1,in_stride, st->factors,st );
382
+ }
383
+ }
384
+
385
+ void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
386
+ {
387
+ kiss_fft_stride(cfg,fin,fout,1);
388
+ }
389
+
390
+
391
+ void kiss_fft_cleanup(void)
392
+ {
393
+ // nothing needed any more
394
+ }
395
+
396
+ int kiss_fft_next_fast_size(int n)
397
+ {
398
+ while(1) {
399
+ int m=n;
400
+ while ( (m%2) == 0 ) m/=2;
401
+ while ( (m%3) == 0 ) m/=3;
402
+ while ( (m%5) == 0 ) m/=5;
403
+ if (m<=1)
404
+ break; /* n is completely factorable by twos, threes, and fives */
405
+ n++;
406
+ }
407
+ return n;
408
+ }
@@ -0,0 +1,124 @@
1
+ #ifndef KISS_FFT_H
2
+ #define KISS_FFT_H
3
+
4
+ #include <stdlib.h>
5
+ #include <stdio.h>
6
+ #include <math.h>
7
+ #include <string.h>
8
+
9
+ #ifdef __cplusplus
10
+ extern "C" {
11
+ #endif
12
+
13
+ /*
14
+ ATTENTION!
15
+ If you would like a :
16
+ -- a utility that will handle the caching of fft objects
17
+ -- real-only (no imaginary time component ) FFT
18
+ -- a multi-dimensional FFT
19
+ -- a command-line utility to perform ffts
20
+ -- a command-line utility to perform fast-convolution filtering
21
+
22
+ Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c
23
+ in the tools/ directory.
24
+ */
25
+
26
+ #ifdef USE_SIMD
27
+ # include <xmmintrin.h>
28
+ # define kiss_fft_scalar __m128
29
+ #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
30
+ #define KISS_FFT_FREE _mm_free
31
+ #else
32
+ #define KISS_FFT_MALLOC malloc
33
+ #define KISS_FFT_FREE free
34
+ #endif
35
+
36
+
37
+ #ifdef FIXED_POINT
38
+ #include <sys/types.h>
39
+ # if (FIXED_POINT == 32)
40
+ # define kiss_fft_scalar int32_t
41
+ # else
42
+ # define kiss_fft_scalar int16_t
43
+ # endif
44
+ #else
45
+ # ifndef kiss_fft_scalar
46
+ /* default is float */
47
+ # define kiss_fft_scalar float
48
+ # endif
49
+ #endif
50
+
51
+ typedef struct {
52
+ kiss_fft_scalar r;
53
+ kiss_fft_scalar i;
54
+ }kiss_fft_cpx;
55
+
56
+ typedef struct kiss_fft_state* kiss_fft_cfg;
57
+
58
+ /*
59
+ * kiss_fft_alloc
60
+ *
61
+ * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
62
+ *
63
+ * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL);
64
+ *
65
+ * The return value from fft_alloc is a cfg buffer used internally
66
+ * by the fft routine or NULL.
67
+ *
68
+ * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc.
69
+ * The returned value should be free()d when done to avoid memory leaks.
70
+ *
71
+ * The state can be placed in a user supplied buffer 'mem':
72
+ * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
73
+ * then the function places the cfg in mem and the size used in *lenmem
74
+ * and returns mem.
75
+ *
76
+ * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
77
+ * then the function returns NULL and places the minimum cfg
78
+ * buffer size in *lenmem.
79
+ * */
80
+
81
+ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem);
82
+
83
+ /*
84
+ * kiss_fft(cfg,in_out_buf)
85
+ *
86
+ * Perform an FFT on a complex input buffer.
87
+ * for a forward FFT,
88
+ * fin should be f[0] , f[1] , ... ,f[nfft-1]
89
+ * fout will be F[0] , F[1] , ... ,F[nfft-1]
90
+ * Note that each element is complex and can be accessed like
91
+ f[k].r and f[k].i
92
+ * */
93
+ void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
94
+
95
+ /*
96
+ A more generic version of the above function. It reads its input from every Nth sample.
97
+ * */
98
+ void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride);
99
+
100
+ /* If kiss_fft_alloc allocated a buffer, it is one contiguous
101
+ buffer and can be simply free()d when no longer needed*/
102
+ #define kiss_fft_free free
103
+
104
+ /*
105
+ Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
106
+ your compiler output to call this before you exit.
107
+ */
108
+ void kiss_fft_cleanup(void);
109
+
110
+
111
+ /*
112
+ * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5)
113
+ */
114
+ int kiss_fft_next_fast_size(int n);
115
+
116
+ /* for real ffts, we need an even size */
117
+ #define kiss_fftr_next_fast_size_real(n) \
118
+ (kiss_fft_next_fast_size( ((n)+1)>>1)<<1)
119
+
120
+ #ifdef __cplusplus
121
+ }
122
+ #endif
123
+
124
+ #endif
@@ -0,0 +1,159 @@
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
+ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem)
28
+ {
29
+ int i;
30
+ kiss_fftr_cfg st = NULL;
31
+ size_t subsize, memneeded;
32
+
33
+ if (nfft & 1) {
34
+ fprintf(stderr,"Real FFT optimization must be even.\n");
35
+ return NULL;
36
+ }
37
+ nfft >>= 1;
38
+
39
+ kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize);
40
+ memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2);
41
+
42
+ if (lenmem == NULL) {
43
+ st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded);
44
+ } else {
45
+ if (*lenmem >= memneeded)
46
+ st = (kiss_fftr_cfg) mem;
47
+ *lenmem = memneeded;
48
+ }
49
+ if (!st)
50
+ return NULL;
51
+
52
+ st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */
53
+ st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize);
54
+ st->super_twiddles = st->tmpbuf + nfft;
55
+ kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
56
+
57
+ for (i = 0; i < nfft/2; ++i) {
58
+ double phase =
59
+ -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
60
+ if (inverse_fft)
61
+ phase *= -1;
62
+ kf_cexp (st->super_twiddles+i,phase);
63
+ }
64
+ return st;
65
+ }
66
+
67
+ void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata)
68
+ {
69
+ /* input buffer timedata is stored row-wise */
70
+ int k,ncfft;
71
+ kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
72
+
73
+ if ( st->substate->inverse) {
74
+ fprintf(stderr,"kiss fft usage error: improper alloc\n");
75
+ exit(1);
76
+ }
77
+
78
+ ncfft = st->substate->nfft;
79
+
80
+ /*perform the parallel fft of two real signals packed in real,imag*/
81
+ kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf );
82
+ /* The real part of the DC element of the frequency spectrum in st->tmpbuf
83
+ * contains the sum of the even-numbered elements of the input time sequence
84
+ * The imag part is the sum of the odd-numbered elements
85
+ *
86
+ * The sum of tdc.r and tdc.i is the sum of the input time sequence.
87
+ * yielding DC of input time sequence
88
+ * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
89
+ * yielding Nyquist bin of input time sequence
90
+ */
91
+
92
+ tdc.r = st->tmpbuf[0].r;
93
+ tdc.i = st->tmpbuf[0].i;
94
+ C_FIXDIV(tdc,2);
95
+ CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
96
+ CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
97
+ freqdata[0].r = tdc.r + tdc.i;
98
+ freqdata[ncfft].r = tdc.r - tdc.i;
99
+ #ifdef USE_SIMD
100
+ freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
101
+ #else
102
+ freqdata[ncfft].i = freqdata[0].i = 0;
103
+ #endif
104
+
105
+ for ( k=1;k <= ncfft/2 ; ++k ) {
106
+ fpk = st->tmpbuf[k];
107
+ fpnk.r = st->tmpbuf[ncfft-k].r;
108
+ fpnk.i = - st->tmpbuf[ncfft-k].i;
109
+ C_FIXDIV(fpk,2);
110
+ C_FIXDIV(fpnk,2);
111
+
112
+ C_ADD( f1k, fpk , fpnk );
113
+ C_SUB( f2k, fpk , fpnk );
114
+ C_MUL( tw , f2k , st->super_twiddles[k-1]);
115
+
116
+ freqdata[k].r = HALF_OF(f1k.r + tw.r);
117
+ freqdata[k].i = HALF_OF(f1k.i + tw.i);
118
+ freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r);
119
+ freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i);
120
+ }
121
+ }
122
+
123
+ void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata)
124
+ {
125
+ /* input buffer timedata is stored row-wise */
126
+ int k, ncfft;
127
+
128
+ if (st->substate->inverse == 0) {
129
+ fprintf (stderr, "kiss fft usage error: improper alloc\n");
130
+ exit (1);
131
+ }
132
+
133
+ ncfft = st->substate->nfft;
134
+
135
+ st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
136
+ st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
137
+ C_FIXDIV(st->tmpbuf[0],2);
138
+
139
+ for (k = 1; k <= ncfft / 2; ++k) {
140
+ kiss_fft_cpx fk, fnkc, fek, fok, tmp;
141
+ fk = freqdata[k];
142
+ fnkc.r = freqdata[ncfft - k].r;
143
+ fnkc.i = -freqdata[ncfft - k].i;
144
+ C_FIXDIV( fk , 2 );
145
+ C_FIXDIV( fnkc , 2 );
146
+
147
+ C_ADD (fek, fk, fnkc);
148
+ C_SUB (tmp, fk, fnkc);
149
+ C_MUL (fok, tmp, st->super_twiddles[k-1]);
150
+ C_ADD (st->tmpbuf[k], fek, fok);
151
+ C_SUB (st->tmpbuf[ncfft - k], fek, fok);
152
+ #ifdef USE_SIMD
153
+ st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
154
+ #else
155
+ st->tmpbuf[ncfft - k].i *= -1;
156
+ #endif
157
+ }
158
+ kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata);
159
+ }
@@ -0,0 +1,46 @@
1
+ #ifndef KISS_FTR_H
2
+ #define KISS_FTR_H
3
+
4
+ #include "kiss_fft.h"
5
+ #ifdef __cplusplus
6
+ extern "C" {
7
+ #endif
8
+
9
+
10
+ /*
11
+
12
+ Real optimized version can save about 45% cpu time vs. complex fft of a real seq.
13
+
14
+
15
+
16
+ */
17
+
18
+ typedef struct kiss_fftr_state *kiss_fftr_cfg;
19
+
20
+
21
+ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem);
22
+ /*
23
+ nfft must be even
24
+
25
+ If you don't care to allocate space, use mem = lenmem = NULL
26
+ */
27
+
28
+
29
+ void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata);
30
+ /*
31
+ input timedata has nfft scalar points
32
+ output freqdata has nfft/2+1 complex points
33
+ */
34
+
35
+ void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata);
36
+ /*
37
+ input freqdata has nfft/2+1 complex points
38
+ output timedata has nfft scalar points
39
+ */
40
+
41
+ #define kiss_fftr_free free
42
+
43
+ #ifdef __cplusplus
44
+ }
45
+ #endif
46
+ #endif
@@ -0,0 +1,155 @@
1
+ /*
2
+ ruby-kissfft: a simple ruby module embedding the Kiss FFT library
3
+ Copyright (C) 2009-2010 Rapid7 LLC - H D Moore <hdm[at]metasploit.com>
4
+
5
+ Derived from "psdpng.c" from the KissFFT tools directory
6
+ Copyright (C) 2003-2006 Mark Borgerding
7
+ */
8
+
9
+ #include "ruby.h"
10
+
11
+
12
+ #include <stdlib.h>
13
+ #include <math.h>
14
+ #include <stdio.h>
15
+ #include <string.h>
16
+ #include <unistd.h>
17
+
18
+ #include "kiss_fft.h"
19
+ #include "kiss_fftr.h"
20
+
21
+ static VALUE rb_cKissFFT;
22
+
23
+ #define KISS_VERSION "1.2.8-1.0"
24
+
25
+
26
+ static VALUE
27
+ rbkiss_s_version(VALUE class)
28
+ {
29
+ return rb_str_new2(KISS_VERSION);
30
+ }
31
+
32
+ #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0)
33
+
34
+ static VALUE
35
+ rbkiss_s_fftr(VALUE class, VALUE r_nfft, VALUE r_rate, VALUE r_buckets, VALUE r_data)
36
+ {
37
+ kiss_fftr_cfg cfg=NULL;
38
+ kiss_fft_scalar *tbuf;
39
+ kiss_fft_cpx *fbuf;
40
+ float *mag2buf;
41
+ int i;
42
+ int avgctr=0;
43
+ int nrows=0;
44
+
45
+ int nfft;
46
+ int rate;
47
+ int navg;
48
+ int nfreqs;
49
+
50
+ int inp_len;
51
+ int inp_idx;
52
+
53
+ // Result set
54
+ VALUE res;
55
+ VALUE tmp;
56
+ VALUE set;
57
+ res = rb_ary_new();
58
+
59
+ if(TYPE(r_nfft) != T_FIXNUM) {
60
+ return Qnil;
61
+ }
62
+ nfft=NUM2INT(r_nfft);
63
+
64
+ if(TYPE(r_rate) != T_FIXNUM) {
65
+ return Qnil;
66
+ }
67
+ rate=NUM2INT(r_rate);
68
+
69
+ if(TYPE(r_buckets) != T_FIXNUM) {
70
+ return Qnil;
71
+ }
72
+ navg=NUM2INT(r_buckets);
73
+
74
+ if(TYPE(r_data) != T_ARRAY) {
75
+ return Qnil;
76
+ }
77
+
78
+ if(RARRAY_LEN(r_data) == 0) {
79
+ return Qnil;
80
+ }
81
+
82
+ if(TYPE(RARRAY_PTR(r_data)[0]) != T_FIXNUM ) {
83
+ return Qnil;
84
+ }
85
+
86
+ nfreqs=nfft/2+1;
87
+
88
+ CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) );
89
+ CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*(nfft + 2) ) );
90
+ CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*(nfft + 2)) );
91
+ CHECKNULL( mag2buf=(float*)malloc(sizeof(float)*(nfft + 2) ));
92
+
93
+ memset(mag2buf,0,sizeof(mag2buf)*nfreqs);
94
+
95
+ inp_len = RARRAY_LEN(r_data);
96
+ inp_idx = 0;
97
+
98
+ while(inp_idx < inp_len) {
99
+
100
+ // Fill tbuf with nfft samples
101
+ for(i=0;i<nfft;i++) {
102
+ if(inp_idx + i >= inp_len) {
103
+ tbuf[i] = 0;
104
+ } else {
105
+ if(TYPE(RARRAY_PTR(r_data)[ inp_idx + i ]) != T_FIXNUM) {
106
+ tbuf[i] = 0;
107
+ } else {
108
+ tbuf[i] = NUM2INT( RARRAY_PTR(r_data)[ inp_idx + i ] );
109
+ }
110
+ }
111
+ }
112
+
113
+
114
+ /* do FFT */
115
+ kiss_fftr(cfg,tbuf,fbuf);
116
+
117
+ for (i=0;i<nfreqs;++i) {
118
+ mag2buf[i] += fbuf[i].r * fbuf[i].r + fbuf[i].i * fbuf[i].i;
119
+ }
120
+
121
+ if (++avgctr == navg) {
122
+ float eps = 1;
123
+ avgctr=0;
124
+ ++nrows;
125
+
126
+ // RESULTS
127
+ set = rb_ary_new();
128
+ for (i=0;i<nfreqs;++i) {
129
+ float pwr = 10 * log10( mag2buf[i] / navg + eps );
130
+ tmp = rb_ary_new();
131
+ rb_ary_push(tmp, rb_float_new( (float)i * ( ( (float)rate / 2) / (float)nfreqs) ));
132
+ rb_ary_push(tmp, rb_float_new( pwr));
133
+ rb_ary_push(set, tmp);
134
+ }
135
+ rb_ary_push(res, set);
136
+ memset(mag2buf,0,sizeof(mag2buf[0])*nfreqs);
137
+ }
138
+ inp_idx += nfft;
139
+ }
140
+
141
+ free(cfg);
142
+ free(tbuf);
143
+ free(fbuf);
144
+ free(mag2buf);
145
+ return(res);
146
+ }
147
+
148
+ void
149
+ Init_kissfft()
150
+ {
151
+ // KissFFT
152
+ rb_cKissFFT = rb_define_class("KissFFT", rb_cObject);
153
+ rb_define_module_function(rb_cKissFFT, "version", rbkiss_s_version, 0);
154
+ rb_define_module_function(rb_cKissFFT, "fftr", rbkiss_s_fftr, 4);
155
+ }
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kissfft
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - HD Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'Provides access to the KissFFT library for performing fast-fourier
15
+ transforms from Ruby '
16
+ email:
17
+ - hdm@rapid7.com
18
+ executables: []
19
+ extensions:
20
+ - ext/kissfft/extconf.rb
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ext/kissfft/kiss_fftr.c
24
+ - ext/kissfft/kiss_fft.c
25
+ - ext/kissfft/main.c
26
+ - ext/kissfft/kiss_fft.h
27
+ - ext/kissfft/_kiss_fft_guts.h
28
+ - ext/kissfft/kiss_fftr.h
29
+ - ext/kissfft/extconf.rb
30
+ - LICENSE
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Ruby wrapper around the KisFFT library for performing FFTs
55
+ test_files: []