diff data/fft/FFTapi.h @ 1091:bdebff3265ae simple-fft-model

Simplest naive FFTModel implementation (+ fill in tests)
author Chris Cannam
date Fri, 12 Jun 2015 18:08:57 +0100
parents 7c53640bb9ba
children 44b079427b36
line wrap: on
line diff
--- a/data/fft/FFTapi.h	Fri Jun 12 14:51:46 2015 +0100
+++ b/data/fft/FFTapi.h	Fri Jun 12 18:08:57 2015 +0100
@@ -50,5 +50,46 @@
 
 #endif
 
+#include <vector>
+#include <complex>
+
+class FFTForward // with fft shift but not window
+{
+public:
+    FFTForward(int size) :
+        m_size(size),
+        m_input((float *)fftf_malloc(size * sizeof(float))),
+        m_output((fftf_complex *)fftf_malloc((size/2 + 1) * sizeof(fftf_complex))),
+        m_plan(fftf_plan_dft_r2c_1d(size, m_input, m_output, FFTW_MEASURE))
+    { }
+
+    ~FFTForward() {
+        fftf_destroy_plan(m_plan);
+        fftf_free(m_input);
+        fftf_free(m_output);
+    }
+
+    std::vector<std::complex<float> > process(std::vector<float> in) const {
+        const int hs = m_size/2;
+        for (int i = 0; i < hs; ++i) {
+            m_input[i] = in[i + hs];
+            m_input[i + hs] = in[i];
+        }
+        fftf_execute(m_plan);
+        std::vector<std::complex<float> > result;
+        result.reserve(hs + 1);
+        for (int i = 0; i <= hs; ++i) {
+            result[i] = { m_output[i][0], m_output[i][1] };
+        }
+        return result;
+    }
+
+private:
+    int m_size;
+    float *m_input;
+    fftf_complex *m_output;
+    fftf_plan m_plan;
+};
+
 #endif