annotate data/fft/FFTapi.h @ 1230:9ae2ce9190e6 project-file-rework

Merge
author Chris Cannam
date Tue, 25 Oct 2016 11:04:26 +0100
parents e94719f941ba
children
rev   line source
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@1131 55 #include <mutex>
Chris@1091 56
Chris@1091 57 class FFTForward // with fft shift but not window
Chris@1091 58 {
Chris@1131 59 static std::mutex m_mutex;
Chris@1131 60
Chris@1091 61 public:
Chris@1091 62 FFTForward(int size) :
Chris@1131 63 m_size(size)
Chris@1131 64 {
Chris@1131 65 std::lock_guard<std::mutex> lock(m_mutex);
Chris@1131 66 m_input = (float *)fftf_malloc(size * sizeof(float));
Chris@1131 67 m_output = (fftf_complex *)fftf_malloc((size/2 + 1) * sizeof(fftf_complex));
Chris@1131 68 m_plan = fftf_plan_dft_r2c_1d(size, m_input, m_output, FFTW_ESTIMATE);
Chris@1131 69 }
Chris@1091 70
Chris@1091 71 ~FFTForward() {
Chris@1131 72 std::lock_guard<std::mutex> lock(m_mutex);
Chris@1091 73 fftf_destroy_plan(m_plan);
Chris@1091 74 fftf_free(m_input);
Chris@1091 75 fftf_free(m_output);
Chris@1091 76 }
Chris@1091 77
Chris@1136 78 std::vector<std::complex<float> > process(const std::vector<float> &in) const {
Chris@1091 79 const int hs = m_size/2;
Chris@1091 80 for (int i = 0; i < hs; ++i) {
Chris@1091 81 m_input[i] = in[i + hs];
Chris@1095 82 }
Chris@1095 83 for (int i = 0; i < hs; ++i) {
Chris@1091 84 m_input[i + hs] = in[i];
Chris@1091 85 }
Chris@1091 86 fftf_execute(m_plan);
Chris@1091 87 std::vector<std::complex<float> > result;
Chris@1091 88 result.reserve(hs + 1);
Chris@1091 89 for (int i = 0; i <= hs; ++i) {
Chris@1093 90 result.push_back({ m_output[i][0], m_output[i][1] });
Chris@1091 91 }
Chris@1091 92 return result;
Chris@1091 93 }
Chris@1091 94
Chris@1091 95 private:
Chris@1091 96 int m_size;
Chris@1091 97 float *m_input;
Chris@1091 98 fftf_complex *m_output;
Chris@1091 99 fftf_plan m_plan;
Chris@1091 100 };
Chris@1091 101
Chris@226 102 #endif
Chris@226 103