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 */
|
c@225
|
8
|
c@225
|
9 #include "FFT.h"
|
c@280
|
10
|
c@280
|
11 #include "maths/MathUtilities.h"
|
c@280
|
12
|
c@355
|
13 #include "kiss_fft.h"
|
c@355
|
14 #include "kiss_fftr.h"
|
c@355
|
15
|
c@225
|
16 #include <cmath>
|
c@225
|
17
|
c@280
|
18 #include <iostream>
|
c@280
|
19
|
c@355
|
20 #include <stdexcept>
|
c@355
|
21
|
c@355
|
22 class FFT::D
|
c@355
|
23 {
|
c@355
|
24 public:
|
c@355
|
25 D(int n) : m_n(n) {
|
c@355
|
26 m_planf = kiss_fft_alloc(m_n, 0, NULL, NULL);
|
c@355
|
27 m_plani = kiss_fft_alloc(m_n, 1, NULL, NULL);
|
c@355
|
28 m_kin = new kiss_fft_cpx[m_n];
|
c@355
|
29 m_kout = new kiss_fft_cpx[m_n];
|
c@355
|
30 }
|
c@355
|
31
|
c@355
|
32 ~D() {
|
c@355
|
33 kiss_fft_free(m_planf);
|
c@355
|
34 kiss_fft_free(m_plani);
|
c@355
|
35 delete[] m_kin;
|
c@355
|
36 delete[] m_kout;
|
c@355
|
37 }
|
c@355
|
38
|
c@355
|
39 void process(bool inverse,
|
c@355
|
40 const double *ri,
|
c@355
|
41 const double *ii,
|
c@355
|
42 double *ro,
|
c@355
|
43 double *io) {
|
c@355
|
44
|
c@355
|
45 for (int i = 0; i < m_n; ++i) {
|
c@355
|
46 m_kin[i].r = ri[i];
|
c@355
|
47 m_kin[i].i = (ii ? ii[i] : 0.0);
|
c@355
|
48 }
|
c@355
|
49
|
c@355
|
50 if (!inverse) {
|
c@355
|
51
|
c@355
|
52 kiss_fft(m_planf, m_kin, m_kout);
|
c@355
|
53
|
c@355
|
54 for (int i = 0; i < m_n; ++i) {
|
c@355
|
55 ro[i] = m_kout[i].r;
|
c@355
|
56 io[i] = m_kout[i].i;
|
c@355
|
57 }
|
c@355
|
58
|
c@355
|
59 } else {
|
c@355
|
60
|
c@355
|
61 kiss_fft(m_plani, m_kin, m_kout);
|
c@355
|
62
|
c@355
|
63 double scale = 1.0 / m_n;
|
c@355
|
64
|
c@355
|
65 for (int i = 0; i < m_n; ++i) {
|
c@355
|
66 ro[i] = m_kout[i].r * scale;
|
c@355
|
67 io[i] = m_kout[i].i * scale;
|
c@355
|
68 }
|
c@355
|
69 }
|
c@355
|
70 }
|
c@355
|
71
|
c@355
|
72 private:
|
c@355
|
73 int m_n;
|
c@355
|
74 kiss_fft_cfg m_planf;
|
c@355
|
75 kiss_fft_cfg m_plani;
|
c@355
|
76 kiss_fft_cpx *m_kin;
|
c@355
|
77 kiss_fft_cpx *m_kout;
|
c@355
|
78 };
|
c@355
|
79
|
c@339
|
80 FFT::FFT(int n) :
|
c@355
|
81 m_d(new D(n))
|
c@225
|
82 {
|
c@225
|
83 }
|
c@225
|
84
|
c@225
|
85 FFT::~FFT()
|
c@225
|
86 {
|
c@355
|
87 delete m_d;
|
c@225
|
88 }
|
c@225
|
89
|
c@355
|
90 void
|
c@355
|
91 FFT::process(bool inverse,
|
c@355
|
92 const double *p_lpRealIn, const double *p_lpImagIn,
|
c@355
|
93 double *p_lpRealOut, double *p_lpImagOut)
|
c@355
|
94 {
|
c@355
|
95 m_d->process(inverse,
|
c@355
|
96 p_lpRealIn, p_lpImagIn,
|
c@355
|
97 p_lpRealOut, p_lpImagOut);
|
c@355
|
98 }
|
c@355
|
99
|
c@355
|
100 class FFTReal::D
|
c@355
|
101 {
|
c@355
|
102 public:
|
c@355
|
103 D(int n) : m_n(n) {
|
c@355
|
104 if (n % 2) {
|
c@355
|
105 throw std::invalid_argument
|
c@355
|
106 ("nsamples must be even in FFTReal constructor");
|
c@355
|
107 }
|
c@355
|
108 m_planf = kiss_fftr_alloc(m_n, 0, NULL, NULL);
|
c@355
|
109 m_plani = kiss_fftr_alloc(m_n, 1, NULL, NULL);
|
c@355
|
110 m_c = new kiss_fft_cpx[m_n];
|
c@355
|
111 }
|
c@355
|
112
|
c@355
|
113 ~D() {
|
c@355
|
114 kiss_fftr_free(m_planf);
|
c@355
|
115 kiss_fftr_free(m_plani);
|
c@355
|
116 delete[] m_c;
|
c@355
|
117 }
|
c@355
|
118
|
c@355
|
119 void forward(const double *ri, double *ro, double *io) {
|
c@355
|
120
|
c@355
|
121 kiss_fftr(m_planf, ri, m_c);
|
c@355
|
122
|
c@355
|
123 for (int i = 0; i <= m_n/2; ++i) {
|
c@355
|
124 ro[i] = m_c[i].r;
|
c@355
|
125 io[i] = m_c[i].i;
|
c@355
|
126 }
|
c@355
|
127
|
c@355
|
128 for (int i = 0; i + 1 < m_n/2; ++i) {
|
c@355
|
129 ro[m_n - i - 1] = ro[i + 1];
|
c@355
|
130 io[m_n - i - 1] = -io[i + 1];
|
c@355
|
131 }
|
c@355
|
132 }
|
c@355
|
133
|
c@357
|
134 void forwardMagnitude(const double *ri, double *mo) {
|
c@357
|
135
|
c@357
|
136 double *io = new double[m_n];
|
c@357
|
137
|
c@357
|
138 forward(ri, mo, io);
|
c@357
|
139
|
c@357
|
140 for (int i = 0; i < m_n; ++i) {
|
c@357
|
141 mo[i] = sqrt(mo[i] * mo[i] + io[i] * io[i]);
|
c@357
|
142 }
|
c@357
|
143
|
c@357
|
144 delete[] io;
|
c@357
|
145 }
|
c@357
|
146
|
c@355
|
147 void inverse(const double *ri, const double *ii, double *ro) {
|
c@355
|
148
|
c@395
|
149 // kiss_fftr.h says
|
c@395
|
150 // "input freqdata has nfft/2+1 complex points"
|
c@395
|
151
|
c@395
|
152 for (int i = 0; i < m_n/2 + 1; ++i) {
|
c@355
|
153 m_c[i].r = ri[i];
|
c@355
|
154 m_c[i].i = ii[i];
|
c@355
|
155 }
|
c@355
|
156
|
c@355
|
157 kiss_fftri(m_plani, m_c, ro);
|
c@355
|
158
|
c@355
|
159 double scale = 1.0 / m_n;
|
c@355
|
160
|
c@355
|
161 for (int i = 0; i < m_n; ++i) {
|
c@355
|
162 ro[i] *= scale;
|
c@355
|
163 }
|
c@355
|
164 }
|
c@355
|
165
|
c@355
|
166 private:
|
c@355
|
167 int m_n;
|
c@355
|
168 kiss_fftr_cfg m_planf;
|
c@355
|
169 kiss_fftr_cfg m_plani;
|
c@355
|
170 kiss_fft_cpx *m_c;
|
c@355
|
171 };
|
c@355
|
172
|
c@339
|
173 FFTReal::FFTReal(int n) :
|
c@355
|
174 m_d(new D(n))
|
c@225
|
175 {
|
c@289
|
176 }
|
c@225
|
177
|
c@289
|
178 FFTReal::~FFTReal()
|
c@289
|
179 {
|
c@355
|
180 delete m_d;
|
c@289
|
181 }
|
c@289
|
182
|
c@289
|
183 void
|
c@355
|
184 FFTReal::forward(const double *ri, double *ro, double *io)
|
c@289
|
185 {
|
c@355
|
186 m_d->forward(ri, ro, io);
|
c@289
|
187 }
|
c@289
|
188
|
c@339
|
189 void
|
c@357
|
190 FFTReal::forwardMagnitude(const double *ri, double *mo)
|
c@357
|
191 {
|
c@357
|
192 m_d->forwardMagnitude(ri, mo);
|
c@357
|
193 }
|
c@357
|
194
|
c@357
|
195 void
|
c@355
|
196 FFTReal::inverse(const double *ri, const double *ii, double *ro)
|
c@339
|
197 {
|
c@355
|
198 m_d->inverse(ri, ii, ro);
|
c@339
|
199 }
|
c@339
|
200
|
c@289
|
201
|
c@355
|
202
|