Chris@226
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@226
|
2
|
Chris@226
|
3 /*
|
Chris@226
|
4 Sonic Visualiser
|
Chris@226
|
5 An audio file viewer and annotation editor.
|
Chris@226
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@226
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@226
|
8
|
Chris@226
|
9 This program is free software; you can redistribute it and/or
|
Chris@226
|
10 modify it under the terms of the GNU General Public License as
|
Chris@226
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@226
|
12 License, or (at your option) any later version. See the file
|
Chris@226
|
13 COPYING included with this distribution for more information.
|
Chris@226
|
14 */
|
Chris@226
|
15
|
Chris@226
|
16 #ifndef _FFT_API_H_
|
Chris@226
|
17 #define _FFT_API_H_
|
Chris@226
|
18
|
Chris@226
|
19 #ifdef HAVE_FFTW3F
|
Chris@226
|
20
|
Chris@226
|
21 #include <fftw3.h>
|
Chris@226
|
22
|
Chris@226
|
23 #define fftf_complex fftwf_complex
|
Chris@226
|
24 #define fftf_malloc fftwf_malloc
|
Chris@226
|
25 #define fftf_free fftwf_free
|
Chris@226
|
26 #define fftf_plan fftwf_plan
|
Chris@226
|
27 #define fftf_plan_dft_r2c_1d fftwf_plan_dft_r2c_1d
|
Chris@226
|
28 #define fftf_plan_dft_c2r_1d fftwf_plan_dft_c2r_1d
|
Chris@226
|
29 #define fftf_execute fftwf_execute
|
Chris@226
|
30 #define fftf_destroy_plan fftwf_destroy_plan
|
Chris@226
|
31
|
Chris@226
|
32 #else
|
Chris@226
|
33
|
Chris@226
|
34 // Provide a fallback FFT implementation if FFTW3f is not available.
|
Chris@226
|
35
|
Chris@226
|
36 typedef float fftf_complex[2];
|
Chris@226
|
37 #define fftf_malloc malloc
|
Chris@226
|
38 #define fftf_free free
|
Chris@226
|
39
|
Chris@226
|
40 struct fftf_plan_;
|
Chris@226
|
41 typedef fftf_plan_ *fftf_plan;
|
Chris@226
|
42
|
Chris@226
|
43 fftf_plan fftf_plan_dft_r2c_1d(int n, float *in, fftf_complex *out, unsigned);
|
Chris@226
|
44 fftf_plan fftf_plan_dft_c2r_1d(int n, fftf_complex *in, float *out, unsigned);
|
Chris@226
|
45 void fftf_execute(const fftf_plan p);
|
Chris@226
|
46 void fftf_destroy_plan(fftf_plan p);
|
Chris@226
|
47
|
Chris@226
|
48 #define FFTW_ESTIMATE 0
|
Chris@293
|
49 #define FFTW_MEASURE 0
|
Chris@226
|
50
|
Chris@226
|
51 #endif
|
Chris@226
|
52
|
Chris@1091
|
53 #include <vector>
|
Chris@1091
|
54 #include <complex>
|
Chris@1091
|
55
|
Chris@1091
|
56 class FFTForward // with fft shift but not window
|
Chris@1091
|
57 {
|
Chris@1091
|
58 public:
|
Chris@1091
|
59 FFTForward(int size) :
|
Chris@1091
|
60 m_size(size),
|
Chris@1091
|
61 m_input((float *)fftf_malloc(size * sizeof(float))),
|
Chris@1091
|
62 m_output((fftf_complex *)fftf_malloc((size/2 + 1) * sizeof(fftf_complex))),
|
Chris@1091
|
63 m_plan(fftf_plan_dft_r2c_1d(size, m_input, m_output, FFTW_MEASURE))
|
Chris@1091
|
64 { }
|
Chris@1091
|
65
|
Chris@1091
|
66 ~FFTForward() {
|
Chris@1091
|
67 fftf_destroy_plan(m_plan);
|
Chris@1091
|
68 fftf_free(m_input);
|
Chris@1091
|
69 fftf_free(m_output);
|
Chris@1091
|
70 }
|
Chris@1091
|
71
|
Chris@1091
|
72 std::vector<std::complex<float> > process(std::vector<float> in) const {
|
Chris@1091
|
73 const int hs = m_size/2;
|
Chris@1091
|
74 for (int i = 0; i < hs; ++i) {
|
Chris@1091
|
75 m_input[i] = in[i + hs];
|
Chris@1095
|
76 }
|
Chris@1095
|
77 for (int i = 0; i < hs; ++i) {
|
Chris@1091
|
78 m_input[i + hs] = in[i];
|
Chris@1091
|
79 }
|
Chris@1091
|
80 fftf_execute(m_plan);
|
Chris@1091
|
81 std::vector<std::complex<float> > result;
|
Chris@1091
|
82 result.reserve(hs + 1);
|
Chris@1091
|
83 for (int i = 0; i <= hs; ++i) {
|
Chris@1093
|
84 result.push_back({ m_output[i][0], m_output[i][1] });
|
Chris@1091
|
85 }
|
Chris@1091
|
86 return result;
|
Chris@1091
|
87 }
|
Chris@1091
|
88
|
Chris@1091
|
89 private:
|
Chris@1091
|
90 int m_size;
|
Chris@1091
|
91 float *m_input;
|
Chris@1091
|
92 fftf_complex *m_output;
|
Chris@1091
|
93 fftf_plan m_plan;
|
Chris@1091
|
94 };
|
Chris@1091
|
95
|
Chris@226
|
96 #endif
|
Chris@226
|
97
|