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