// // Programmer: Craig Stuart Sapp // Creation Date: Fri Feb 7 00:44:33 PST 1997 // Last Modified: Thu Dec 18 18:39:40 GMT-0800 1997 // Last Modified: Sun Jun 11 12:55:07 PDT 2000 (improved memory management) // Last Modified: Sun Jul 8 14:58:52 PDT 2001 (removed templates) // Filename: ...sig/maint/code/utilities/transforms/transforms.cpp // Web Address: http://sig.sapp.org/src/sig/transforms.cpp // Syntax: C++ // // Description: Basic signal <-> frequency transforms. // #ifndef _TRANSFORMS_CPP_INCLUDED #define _TRANSFORMS_CPP_INCLUDED #include "Block.h" #include "ComplexD.h" #include "transforms.h" #include "transforms-private.h" #include #include #include #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // FFT -- Fast Fourier Transform O(N Log N) // Returns the complex spectrum of the given complex input signal. // Length of Block must be a power of 2. // Block& FFT(Block& outputSpectrum, const Block& inputSignal) { int N = inputSignal.getSize(); if (!poweroftwo(N)) { cerr << "You can only take the FFT of a block with length being" << " a power of 2.\nRequested transform length: " << N << endl; exit(1); } outputSpectrum = inputSignal; fft_destructive(outputSpectrum); return outputSpectrum; } ////////////////////////////// // // IFFT -- Inverse Fast Fourier Transform O(N Log N). // Returns the complex signal of the given complex input spectrum. // Length of Block must be a power of 2. // Block& IFFT(Block& outputSignal, const Block& inputSpectrum) { int N = inputSpectrum.getSize(); if (!poweroftwo(N)) { cerr << "You can only take the IFFT of a block with length being" << " a power of 2.\nRequested transform length: " << N << endl; exit(1); } outputSignal = inputSpectrum; ifft_destructive(outputSignal); return outputSignal; } ////////////////////////////// // // DirectDFT -- Direct Discrete Fourier Transform O(N^2). // Returns the complex spectrum of the given complex input signal. // Quickly becomes slower than the FFT if the length of the input // block is greater than about 100. // Block& DirectDFT(Block& outputSpectrum, const Block& inputSignal) { int N = inputSignal.getSize(); const Block& x = inputSignal; outputSpectrum.setSize(N); Block& X = outputSpectrum; ComplexD sum; int k, n; ComplexD kernel(0,0); double mtpn = -2 * 3.14159265359 / N; for (k=0; k& DFT(Block& outputSpectrum, const Block& inputSignal) { int N = inputSignal.getSize(); if (poweroftwo(N) && N > 63) { return FFT(outputSpectrum, inputSignal); } else { return DirectDFT(outputSpectrum, inputSignal); } } ////////////////////////////// // // DirectIDFT -- Direct Inverse Discrete Fourier Transform O(N^2). // Returns the complex spectrum of the given complex input signal. // Quickly becomes slower than the IFFT if the length of the input // block is greater than about 100. // Block& DirectIDFT(Block& outputSignal, const Block& inputSpectrum) { int N = inputSpectrum.getSize(); const Block& X = inputSpectrum; outputSignal.setSize(N); Block& x = outputSignal; ComplexD sum; int k, n; ComplexD kernel(0,0); double tpn = 2.0 * 3.14159265359 / (double)N; for (n=0; n& IDFT(Block& outputSignal, const Block& inputSpectrum) { int N = inputSpectrum.getSize(); if (poweroftwo(N) && N > 63) { return IFFT(outputSignal, inputSpectrum); } else { return DirectIDFT(outputSignal, inputSpectrum); } } #endif /* _TRANSFORMS_CPP_INCLUDED */ // md5sum: 14d6cb9182c5c4eca979b24a829214b7 transforms.cpp [20050403]