Mercurial > hg > gpsynth
comparison src/spectrum_analyzer.hpp @ 0:add35537fdbb tip
Initial import
author | irh <ian.r.hobson@gmail.com> |
---|---|
date | Thu, 25 Aug 2011 11:05:55 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:add35537fdbb |
---|---|
1 // Copyright 2011, Ian Hobson. | |
2 // | |
3 // This file is part of gpsynth. | |
4 // | |
5 // gpsynth is free software: you can redistribute it and/or modify | |
6 // it under the terms of the GNU General Public License as published by | |
7 // the Free Software Foundation, either version 3 of the License, or | |
8 // (at your option) any later version. | |
9 // | |
10 // gpsynth is distributed in the hope that it will be useful, | |
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 // GNU General Public License for more details. | |
14 // | |
15 // You should have received a copy of the GNU General Public License | |
16 // along with gpsynth in the file COPYING. | |
17 // If not, see http://www.gnu.org/licenses/. | |
18 | |
19 // Wrapper classes for FFTW | |
20 | |
21 #pragma once | |
22 | |
23 #include "fftw3.h" | |
24 | |
25 #include <complex> | |
26 | |
27 namespace dsp { | |
28 | |
29 // Float version | |
30 class SpectrumAnalyzerFloat { | |
31 fftwf_plan plan_; | |
32 int frame_size_; | |
33 float* input_; | |
34 std::complex<float>* output_; | |
35 | |
36 public: | |
37 SpectrumAnalyzerFloat(int frame_size = 1024) | |
38 : frame_size_(frame_size) | |
39 { | |
40 input_ = (float*)fftw_malloc(sizeof(float) * frame_size); | |
41 int buffer_size = sizeof(std::complex<float>) * frame_size / 2; | |
42 output_ = (std::complex<float>*)fftw_malloc(buffer_size); | |
43 plan_ = fftwf_plan_dft_r2c_1d(frame_size, | |
44 input_, | |
45 reinterpret_cast<fftwf_complex*>(output_), | |
46 FFTW_ESTIMATE); | |
47 } | |
48 | |
49 SpectrumAnalyzerFloat() { | |
50 fftw_free(input_); | |
51 fftw_free(output_); | |
52 fftwf_destroy_plan(plan_); | |
53 } | |
54 | |
55 void Execute() { | |
56 fftwf_execute(plan_); | |
57 } | |
58 | |
59 int Size() { return frame_size_; } | |
60 float* Input() { return input_; } | |
61 std::complex<float>* Output() { return output_; } | |
62 }; | |
63 | |
64 // Double version | |
65 class SpectrumAnalyzer { | |
66 fftw_plan plan_; | |
67 int frame_size_; | |
68 double* input_; | |
69 std::complex<double>* output_; | |
70 | |
71 public: | |
72 SpectrumAnalyzer(int frame_size = 1024) | |
73 : frame_size_(frame_size) | |
74 { | |
75 input_ = (double*)fftw_malloc(sizeof(double) * frame_size); | |
76 int buffer_size = sizeof(std::complex<double>) * frame_size; | |
77 output_ = (std::complex<double>*)fftw_malloc(buffer_size); | |
78 plan_ = fftw_plan_dft_r2c_1d(frame_size, | |
79 input_, | |
80 reinterpret_cast<fftw_complex*>(output_), | |
81 FFTW_ESTIMATE); | |
82 } | |
83 | |
84 SpectrumAnalyzer() { | |
85 fftw_free(input_); | |
86 fftw_free(output_); | |
87 fftw_destroy_plan(plan_); | |
88 } | |
89 | |
90 void Execute() { | |
91 fftw_execute(plan_); | |
92 } | |
93 | |
94 int Size() { return frame_size_; } | |
95 double* Input() { return input_; } | |
96 std::complex<double>* Output() { return output_; } | |
97 }; | |
98 | |
99 } // dsp namespace |