Chris@337
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@337
|
2
|
Chris@337
|
3 /*
|
Chris@337
|
4 Vamp
|
Chris@337
|
5
|
Chris@337
|
6 An API for audio analysis and feature extraction plugins.
|
Chris@337
|
7
|
Chris@337
|
8 Centre for Digital Music, Queen Mary, University of London.
|
Chris@337
|
9 Copyright 2006-2012 Chris Cannam and QMUL.
|
Chris@337
|
10
|
Chris@337
|
11 Permission is hereby granted, free of charge, to any person
|
Chris@337
|
12 obtaining a copy of this software and associated documentation
|
Chris@337
|
13 files (the "Software"), to deal in the Software without
|
Chris@337
|
14 restriction, including without limitation the rights to use, copy,
|
Chris@337
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@337
|
16 of the Software, and to permit persons to whom the Software is
|
Chris@337
|
17 furnished to do so, subject to the following conditions:
|
Chris@337
|
18
|
Chris@337
|
19 The above copyright notice and this permission notice shall be
|
Chris@337
|
20 included in all copies or substantial portions of the Software.
|
Chris@337
|
21
|
Chris@337
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@337
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@337
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@337
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@337
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@337
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@337
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@337
|
29
|
Chris@337
|
30 Except as contained in this notice, the names of the Centre for
|
Chris@337
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
Chris@337
|
32 shall not be used in advertising or otherwise to promote the sale,
|
Chris@337
|
33 use or other dealings in this Software without prior written
|
Chris@337
|
34 authorization.
|
Chris@337
|
35 */
|
Chris@337
|
36
|
Chris@337
|
37 #include <vamp-sdk/FFT.h>
|
Chris@337
|
38
|
Chris@434
|
39 #include <stdlib.h>
|
Chris@434
|
40 #include <stdio.h>
|
Chris@434
|
41 #include <math.h>
|
Chris@434
|
42 #include <string.h>
|
Chris@337
|
43
|
Chris@424
|
44 #if ( VAMP_SDK_MAJOR_VERSION != 2 || VAMP_SDK_MINOR_VERSION != 7 )
|
Chris@337
|
45 #error Unexpected version of Vamp SDK header included
|
Chris@337
|
46 #endif
|
Chris@337
|
47
|
Chris@434
|
48 _VAMP_SDK_PLUGSPACE_BEGIN(FFT.cpp)
|
chris@340
|
49
|
Chris@445
|
50 #include "FFTimpl.cpp"
|
Chris@337
|
51
|
Chris@337
|
52 namespace Vamp {
|
Chris@337
|
53
|
Chris@337
|
54 void
|
Chris@434
|
55 FFT::forward(unsigned int un,
|
Chris@337
|
56 const double *ri, const double *ii,
|
Chris@337
|
57 double *ro, double *io)
|
Chris@337
|
58 {
|
Chris@434
|
59 int n(un);
|
Chris@445
|
60 Kiss::kiss_fft_cfg c = Kiss::kiss_fft_alloc(n, false, 0, 0);
|
Chris@445
|
61 Kiss::kiss_fft_cpx *in = new Kiss::kiss_fft_cpx[n];
|
Chris@445
|
62 Kiss::kiss_fft_cpx *out = new Kiss::kiss_fft_cpx[n];
|
Chris@434
|
63 for (int i = 0; i < n; ++i) {
|
Chris@434
|
64 in[i].r = ri[i];
|
Chris@434
|
65 in[i].i = 0;
|
Chris@434
|
66 }
|
Chris@434
|
67 if (ii) {
|
Chris@434
|
68 for (int i = 0; i < n; ++i) {
|
Chris@434
|
69 in[i].i = ii[i];
|
Chris@434
|
70 }
|
Chris@434
|
71 }
|
Chris@434
|
72 kiss_fft(c, in, out);
|
Chris@434
|
73 for (int i = 0; i < n; ++i) {
|
Chris@434
|
74 ro[i] = out[i].r;
|
Chris@434
|
75 io[i] = out[i].i;
|
Chris@434
|
76 }
|
Chris@445
|
77 Kiss::kiss_fft_free(c);
|
Chris@434
|
78 delete[] in;
|
Chris@434
|
79 delete[] out;
|
Chris@337
|
80 }
|
Chris@337
|
81
|
Chris@337
|
82 void
|
Chris@434
|
83 FFT::inverse(unsigned int un,
|
Chris@337
|
84 const double *ri, const double *ii,
|
Chris@337
|
85 double *ro, double *io)
|
Chris@337
|
86 {
|
Chris@434
|
87 int n(un);
|
Chris@445
|
88 Kiss::kiss_fft_cfg c = Kiss::kiss_fft_alloc(n, true, 0, 0);
|
Chris@445
|
89 Kiss::kiss_fft_cpx *in = new Kiss::kiss_fft_cpx[n];
|
Chris@445
|
90 Kiss::kiss_fft_cpx *out = new Kiss::kiss_fft_cpx[n];
|
Chris@434
|
91 for (int i = 0; i < n; ++i) {
|
Chris@434
|
92 in[i].r = ri[i];
|
Chris@434
|
93 in[i].i = 0;
|
Chris@434
|
94 }
|
Chris@434
|
95 if (ii) {
|
Chris@434
|
96 for (int i = 0; i < n; ++i) {
|
Chris@434
|
97 in[i].i = ii[i];
|
Chris@434
|
98 }
|
Chris@434
|
99 }
|
Chris@434
|
100 kiss_fft(c, in, out);
|
Chris@434
|
101 double scale = 1.0 / double(n);
|
Chris@434
|
102 for (int i = 0; i < n; ++i) {
|
Chris@434
|
103 ro[i] = out[i].r * scale;
|
Chris@434
|
104 io[i] = out[i].i * scale;
|
Chris@434
|
105 }
|
Chris@445
|
106 Kiss::kiss_fft_free(c);
|
Chris@434
|
107 delete[] in;
|
Chris@434
|
108 delete[] out;
|
Chris@434
|
109 }
|
Chris@434
|
110
|
Chris@434
|
111 class FFTReal::D
|
Chris@434
|
112 {
|
Chris@434
|
113 public:
|
Chris@434
|
114
|
Chris@434
|
115 D(int n) :
|
Chris@434
|
116 m_n(n),
|
Chris@445
|
117 m_cf(Kiss::kiss_fftr_alloc(n, false, 0, 0)),
|
Chris@445
|
118 m_ci(Kiss::kiss_fftr_alloc(n, true, 0, 0)),
|
Chris@445
|
119 m_ri(new Kiss::kiss_fft_scalar[m_n]),
|
Chris@445
|
120 m_ro(new Kiss::kiss_fft_scalar[m_n]),
|
Chris@445
|
121 m_freq(new Kiss::kiss_fft_cpx[n/2+1]) { }
|
Chris@434
|
122
|
Chris@434
|
123 ~D() {
|
Chris@445
|
124 Kiss::kiss_fftr_free(m_cf);
|
Chris@445
|
125 Kiss::kiss_fftr_free(m_ci);
|
Chris@445
|
126 delete[] m_ri;
|
Chris@445
|
127 delete[] m_ro;
|
Chris@434
|
128 delete[] m_freq;
|
Chris@434
|
129 }
|
Chris@434
|
130
|
Chris@445
|
131 void forward(const double *ri, double *co) {
|
Chris@445
|
132 for (int i = 0; i < m_n; ++i) {
|
Chris@445
|
133 // in case kiss_fft_scalar is float
|
Chris@445
|
134 m_ri[i] = ri[i];
|
Chris@445
|
135 }
|
Chris@445
|
136 Kiss::kiss_fftr(m_cf, m_ri, m_freq);
|
Chris@434
|
137 int hs = m_n/2 + 1;
|
Chris@434
|
138 for (int i = 0; i < hs; ++i) {
|
Chris@434
|
139 co[i*2] = m_freq[i].r;
|
Chris@434
|
140 co[i*2+1] = m_freq[i].i;
|
Chris@434
|
141 }
|
Chris@434
|
142 }
|
Chris@434
|
143
|
Chris@445
|
144 void inverse(const double *ci, double *ro) {
|
Chris@434
|
145 int hs = m_n/2 + 1;
|
Chris@434
|
146 for (int i = 0; i < hs; ++i) {
|
Chris@434
|
147 m_freq[i].r = ci[i*2];
|
Chris@434
|
148 m_freq[i].i = ci[i*2+1];
|
Chris@434
|
149 }
|
Chris@445
|
150 Kiss::kiss_fftri(m_ci, m_freq, m_ro);
|
Chris@434
|
151 double scale = 1.0 / double(m_n);
|
Chris@434
|
152 for (int i = 0; i < m_n; ++i) {
|
Chris@445
|
153 ro[i] = m_ro[i] * scale;
|
Chris@434
|
154 }
|
Chris@434
|
155 }
|
Chris@434
|
156
|
Chris@434
|
157 private:
|
Chris@434
|
158 int m_n;
|
Chris@445
|
159 Kiss::kiss_fftr_cfg m_cf;
|
Chris@445
|
160 Kiss::kiss_fftr_cfg m_ci;
|
Chris@445
|
161 Kiss::kiss_fft_scalar *m_ri;
|
Chris@445
|
162 Kiss::kiss_fft_scalar *m_ro;
|
Chris@445
|
163 Kiss::kiss_fft_cpx *m_freq;
|
Chris@434
|
164 };
|
Chris@434
|
165
|
Chris@434
|
166 FFTReal::FFTReal(unsigned int n) :
|
Chris@434
|
167 m_d(new D(n))
|
Chris@434
|
168 {
|
Chris@434
|
169 }
|
Chris@434
|
170
|
Chris@434
|
171 FFTReal::~FFTReal()
|
Chris@434
|
172 {
|
Chris@434
|
173 delete m_d;
|
Chris@434
|
174 }
|
Chris@434
|
175
|
Chris@434
|
176 void
|
Chris@445
|
177 FFTReal::forward(const double *ri, double *co)
|
Chris@434
|
178 {
|
Chris@434
|
179 m_d->forward(ri, co);
|
Chris@434
|
180 }
|
Chris@434
|
181
|
Chris@434
|
182 void
|
Chris@445
|
183 FFTReal::inverse(const double *ci, double *ro)
|
Chris@434
|
184 {
|
Chris@434
|
185 m_d->inverse(ci, ro);
|
Chris@337
|
186 }
|
Chris@337
|
187
|
Chris@337
|
188 }
|
Chris@337
|
189
|
Chris@337
|
190 _VAMP_SDK_PLUGSPACE_END(FFT.cpp)
|
Chris@337
|
191
|