Mercurial > hg > gpsynth
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/spectrum_analyzer.hpp Thu Aug 25 11:05:55 2011 +0100 @@ -0,0 +1,99 @@ +// Copyright 2011, Ian Hobson. +// +// This file is part of gpsynth. +// +// gpsynth is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// gpsynth is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with gpsynth in the file COPYING. +// If not, see http://www.gnu.org/licenses/. + +// Wrapper classes for FFTW + +#pragma once + +#include "fftw3.h" + +#include <complex> + +namespace dsp { + +// Float version +class SpectrumAnalyzerFloat { + fftwf_plan plan_; + int frame_size_; + float* input_; + std::complex<float>* output_; + +public: + SpectrumAnalyzerFloat(int frame_size = 1024) + : frame_size_(frame_size) + { + input_ = (float*)fftw_malloc(sizeof(float) * frame_size); + int buffer_size = sizeof(std::complex<float>) * frame_size / 2; + output_ = (std::complex<float>*)fftw_malloc(buffer_size); + plan_ = fftwf_plan_dft_r2c_1d(frame_size, + input_, + reinterpret_cast<fftwf_complex*>(output_), + FFTW_ESTIMATE); + } + + SpectrumAnalyzerFloat() { + fftw_free(input_); + fftw_free(output_); + fftwf_destroy_plan(plan_); + } + + void Execute() { + fftwf_execute(plan_); + } + + int Size() { return frame_size_; } + float* Input() { return input_; } + std::complex<float>* Output() { return output_; } +}; + +// Double version +class SpectrumAnalyzer { + fftw_plan plan_; + int frame_size_; + double* input_; + std::complex<double>* output_; + +public: + SpectrumAnalyzer(int frame_size = 1024) + : frame_size_(frame_size) + { + input_ = (double*)fftw_malloc(sizeof(double) * frame_size); + int buffer_size = sizeof(std::complex<double>) * frame_size; + output_ = (std::complex<double>*)fftw_malloc(buffer_size); + plan_ = fftw_plan_dft_r2c_1d(frame_size, + input_, + reinterpret_cast<fftw_complex*>(output_), + FFTW_ESTIMATE); + } + + SpectrumAnalyzer() { + fftw_free(input_); + fftw_free(output_); + fftw_destroy_plan(plan_); + } + + void Execute() { + fftw_execute(plan_); + } + + int Size() { return frame_size_; } + double* Input() { return input_; } + std::complex<double>* Output() { return output_; } +}; + +} // dsp namespace