From 9e238820cb4df45219cf122206aa0ae153e81c60 Mon Sep 17 00:00:00 2001 From: Roland Stigge Date: Tue, 22 Jan 2019 22:03:06 +0100 Subject: Prepared chrono --- fft.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'fft.cpp') diff --git a/fft.cpp b/fft.cpp index 6b2f447..8613dd9 100644 --- a/fft.cpp +++ b/fft.cpp @@ -3,6 +3,7 @@ #include #include #include +#include const double PI = std::acos(-1); const std::complex i(0, 1); @@ -24,13 +25,6 @@ std::vector> dft(const std::vector> &v return result; } -// Cooley-Tukey -std::vector> fft1(const std::vector> &v) { - std::vector> result(v.size(), 0); - - return result; -} - std::vector freqs{ 2, 5, 11, 17, 29 }; // known freqs for testing void generate(std::vector>& v) { @@ -46,8 +40,8 @@ void generate(std::vector>& v) { // separate even/odd elements to lower/upper halves of array respectively. // Due to Butterfly combinations, this turns out to be the simplest way // to get the job done without clobbering the wrong elements. -void separate (complex* a, int n) { - complex* b = new complex[n/2]; // get temp heap storage +void separate (std::complex* a, int n) { + std::complex* b = new std::complex[n/2]; // get temp heap storage for(int i=0; i* a, int n) { // Because of Nyquist theorem, N samples means // only first N/2 FFT results in X[] are the answer. // (upper half of X[] is a reflection with no new information). -void fft2 (complex* X, int N) { +void fft2 (std::complex* X, int N) { if(N < 2) { // bottom of recursion. // Do nothing here, because already X[0] = x[0] @@ -74,16 +68,24 @@ void fft2 (complex* X, int N) { fft2(X+N/2, N/2); // recurse odd items // combine results of two half recursions for(int k=0; k e = X[k ]; // even - complex o = X[k+N/2]; // odd + std::complex e = X[k ]; // even + std::complex o = X[k+N/2]; // odd // w is the "twiddle-factor" - complex w = exp( complex(0,-2.*M_PI*k/N) ); + std::complex w = exp( std::complex(0,-2.*M_PI*k/N) ); X[k ] = e + w * o; X[k+N/2] = e - w * o; } } } +// Cooley-Tukey +// TODO: Use fft2 via iterators! +std::vector> fft1(const std::vector> &v) { + std::vector> result(v.size(), 0); + + return result; +} + int main(int argc, char* argv[]) { std::vector> v(1024, 0); -- cgit v1.2.3