c@225
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@225
|
2
|
c@225
|
3 /*
|
c@225
|
4 QM DSP Library
|
c@225
|
5
|
c@225
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@225
|
7 This file is based on Don Cross's public domain FFT implementation.
|
c@225
|
8 */
|
c@225
|
9
|
c@225
|
10 #include "FFT.h"
|
c@280
|
11
|
c@280
|
12 #include "maths/MathUtilities.h"
|
c@280
|
13
|
c@225
|
14 #include <cmath>
|
c@225
|
15
|
c@280
|
16 #include <iostream>
|
c@280
|
17
|
c@289
|
18 #define USE_BUILTIN_FFT 1
|
c@225
|
19
|
c@289
|
20 #ifdef USE_BUILTIN_FFT
|
c@289
|
21
|
c@289
|
22 FFT::FFT(unsigned int n) :
|
c@289
|
23 m_n(n),
|
c@289
|
24 m_private(0)
|
c@225
|
25 {
|
c@289
|
26 if( !MathUtilities::isPowerOfTwo(m_n) )
|
c@289
|
27 {
|
c@289
|
28 std::cerr << "ERROR: FFT: Non-power-of-two FFT size "
|
c@289
|
29 << m_n << " not supported in this implementation"
|
c@289
|
30 << std::endl;
|
c@289
|
31 return;
|
c@289
|
32 }
|
c@225
|
33 }
|
c@225
|
34
|
c@225
|
35 FFT::~FFT()
|
c@225
|
36 {
|
c@225
|
37
|
c@225
|
38 }
|
c@225
|
39
|
c@289
|
40 FFTReal::FFTReal(unsigned int n) :
|
c@289
|
41 m_n(n),
|
c@289
|
42 m_private(0)
|
c@225
|
43 {
|
c@289
|
44 m_private = new FFT(m_n);
|
c@289
|
45 }
|
c@225
|
46
|
c@289
|
47 FFTReal::~FFTReal()
|
c@289
|
48 {
|
c@289
|
49 delete (FFT *)m_private;
|
c@289
|
50 }
|
c@289
|
51
|
c@289
|
52 void
|
c@289
|
53 FFTReal::process(bool inverse,
|
c@289
|
54 const double *realIn,
|
c@289
|
55 double *realOut, double *imagOut)
|
c@289
|
56 {
|
c@289
|
57 ((FFT *)m_private)->process(inverse, realIn, 0, realOut, imagOut);
|
c@289
|
58 }
|
c@289
|
59
|
c@289
|
60 static unsigned int numberOfBitsNeeded(unsigned int p_nSamples)
|
c@289
|
61 {
|
c@289
|
62 int i;
|
c@289
|
63
|
c@289
|
64 if( p_nSamples < 2 )
|
c@289
|
65 {
|
c@289
|
66 return 0;
|
c@289
|
67 }
|
c@289
|
68
|
c@289
|
69 for ( i=0; ; i++ )
|
c@289
|
70 {
|
c@289
|
71 if( p_nSamples & (1 << i) ) return i;
|
c@289
|
72 }
|
c@289
|
73 }
|
c@289
|
74
|
c@289
|
75 static unsigned int reverseBits(unsigned int p_nIndex, unsigned int p_nBits)
|
c@289
|
76 {
|
c@289
|
77 unsigned int i, rev;
|
c@289
|
78
|
c@289
|
79 for(i=rev=0; i < p_nBits; i++)
|
c@289
|
80 {
|
c@289
|
81 rev = (rev << 1) | (p_nIndex & 1);
|
c@289
|
82 p_nIndex >>= 1;
|
c@289
|
83 }
|
c@289
|
84
|
c@289
|
85 return rev;
|
c@289
|
86 }
|
c@289
|
87
|
c@289
|
88 void
|
c@289
|
89 FFT::process(bool p_bInverseTransform,
|
c@289
|
90 const double *p_lpRealIn, const double *p_lpImagIn,
|
c@289
|
91 double *p_lpRealOut, double *p_lpImagOut)
|
c@289
|
92 {
|
c@225
|
93 if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;
|
c@225
|
94
|
c@225
|
95 unsigned int NumBits;
|
c@225
|
96 unsigned int i, j, k, n;
|
c@225
|
97 unsigned int BlockSize, BlockEnd;
|
c@225
|
98
|
c@225
|
99 double angle_numerator = 2.0 * M_PI;
|
c@225
|
100 double tr, ti;
|
c@225
|
101
|
c@289
|
102 if( !MathUtilities::isPowerOfTwo(m_n) )
|
c@225
|
103 {
|
c@280
|
104 std::cerr << "ERROR: FFT::process: Non-power-of-two FFT size "
|
c@289
|
105 << m_n << " not supported in this implementation"
|
c@280
|
106 << std::endl;
|
c@225
|
107 return;
|
c@225
|
108 }
|
c@225
|
109
|
c@225
|
110 if( p_bInverseTransform ) angle_numerator = -angle_numerator;
|
c@225
|
111
|
c@289
|
112 NumBits = numberOfBitsNeeded ( m_n );
|
c@225
|
113
|
c@225
|
114
|
c@289
|
115 for( i=0; i < m_n; i++ )
|
c@225
|
116 {
|
c@225
|
117 j = reverseBits ( i, NumBits );
|
c@225
|
118 p_lpRealOut[j] = p_lpRealIn[i];
|
c@225
|
119 p_lpImagOut[j] = (p_lpImagIn == 0) ? 0.0 : p_lpImagIn[i];
|
c@225
|
120 }
|
c@225
|
121
|
c@225
|
122
|
c@225
|
123 BlockEnd = 1;
|
c@289
|
124 for( BlockSize = 2; BlockSize <= m_n; BlockSize <<= 1 )
|
c@225
|
125 {
|
c@225
|
126 double delta_angle = angle_numerator / (double)BlockSize;
|
c@225
|
127 double sm2 = -sin ( -2 * delta_angle );
|
c@225
|
128 double sm1 = -sin ( -delta_angle );
|
c@225
|
129 double cm2 = cos ( -2 * delta_angle );
|
c@225
|
130 double cm1 = cos ( -delta_angle );
|
c@225
|
131 double w = 2 * cm1;
|
c@225
|
132 double ar[3], ai[3];
|
c@225
|
133
|
c@289
|
134 for( i=0; i < m_n; i += BlockSize )
|
c@225
|
135 {
|
c@225
|
136
|
c@225
|
137 ar[2] = cm2;
|
c@225
|
138 ar[1] = cm1;
|
c@225
|
139
|
c@225
|
140 ai[2] = sm2;
|
c@225
|
141 ai[1] = sm1;
|
c@225
|
142
|
c@225
|
143 for ( j=i, n=0; n < BlockEnd; j++, n++ )
|
c@225
|
144 {
|
c@225
|
145
|
c@225
|
146 ar[0] = w*ar[1] - ar[2];
|
c@225
|
147 ar[2] = ar[1];
|
c@225
|
148 ar[1] = ar[0];
|
c@225
|
149
|
c@225
|
150 ai[0] = w*ai[1] - ai[2];
|
c@225
|
151 ai[2] = ai[1];
|
c@225
|
152 ai[1] = ai[0];
|
c@225
|
153
|
c@225
|
154 k = j + BlockEnd;
|
c@225
|
155 tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
|
c@225
|
156 ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];
|
c@225
|
157
|
c@225
|
158 p_lpRealOut[k] = p_lpRealOut[j] - tr;
|
c@225
|
159 p_lpImagOut[k] = p_lpImagOut[j] - ti;
|
c@225
|
160
|
c@225
|
161 p_lpRealOut[j] += tr;
|
c@225
|
162 p_lpImagOut[j] += ti;
|
c@225
|
163
|
c@225
|
164 }
|
c@225
|
165 }
|
c@225
|
166
|
c@225
|
167 BlockEnd = BlockSize;
|
c@225
|
168
|
c@225
|
169 }
|
c@225
|
170
|
c@225
|
171
|
c@225
|
172 if( p_bInverseTransform )
|
c@225
|
173 {
|
c@289
|
174 double denom = (double)m_n;
|
c@225
|
175
|
c@289
|
176 for ( i=0; i < m_n; i++ )
|
c@225
|
177 {
|
c@225
|
178 p_lpRealOut[i] /= denom;
|
c@225
|
179 p_lpImagOut[i] /= denom;
|
c@225
|
180 }
|
c@225
|
181 }
|
c@225
|
182 }
|
c@225
|
183
|
c@289
|
184 #else
|
c@225
|
185
|
c@289
|
186 #include "kissfft/kiss_fft.h"
|
c@289
|
187 #include "kissfft/kiss_fftr.h"
|
c@225
|
188
|
c@289
|
189 #endif
|