From 3624574e808bc274ba82af98f00c4ee82e97bfcf Mon Sep 17 00:00:00 2001 From: ameyachawlaggsipu <88154798+ameyachawlaggsipu@users.noreply.github.com> Date: Thu, 7 Oct 2021 20:16:31 +0530 Subject: [PATCH] feat ; Implemented Fast Fourier Transform --- numerical_methods/fast_fourier_transform.cpp | 122 +++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 numerical_methods/fast_fourier_transform.cpp diff --git a/numerical_methods/fast_fourier_transform.cpp b/numerical_methods/fast_fourier_transform.cpp new file mode 100644 index 000000000..1443d1d1b --- /dev/null +++ b/numerical_methods/fast_fourier_transform.cpp @@ -0,0 +1,122 @@ +/** + * @fast_fourier_transform.cpp + * @brief A fast Fourier transform (FFT) is an algorithm that computes the + * discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). + * @details + * https://medium.com/@aiswaryamathur/understanding-fast-fourier-transform-from-scratch-to + -solve-polynomial-multiplication-8018d511162f + * @author [Ameya Chawla](https://github.com/ameyachawlaggsipu) + */ + +#include +#include +#include +#include +# define pi 3.14159265358979323846 +using namespace std; + +/** + * @brief FastFourierTransform is a recursive function which returns list of complex numbers + * @param p List of Coefficents in form of complex numbers + * @param n Count of elements in list p + * @returns p if n==1 + * @returns y if n!=1 + */ + +complex* FastFourierTransform(complex*p,int n) +{ + + if(n==1) return p; ///Base Case To return + + complex om=complex(cos(2*pi/n),sin(2*pi/n)); ///Calculating value of omega + + complex *pe= new complex[n/2]; /// Coefficents of even power + + complex *po= new complex[n/2]; ///Coefficents of odd power + + int k1=0,k2=0; + for(int j=0;j*ye=FastFourierTransform(pe,n/2);///Recursive Call + + complex*yo=FastFourierTransform(po,n/2);///Recursive Call + + complex*y=new complex[n];///Final value representation list + + + for(int i=0;i t1[2]={1,2};///Test case 1 + + complex t2[4]={1,2,3,4};///Test case 2 + + + + int n1=sizeof(t1)/sizeof(complex); + int n2=sizeof(t2)/sizeof(complex); + + + complex r1[2]={{3,0},{-1,0} };///True Answer for test case 1 + + complex r2[4]={{10,0},{-2,-2},{-2,0},{-2,2} };///True Answer for test case 2 + + + complex *o1=FastFourierTransform(t1,n1); + complex *o2=FastFourierTransform(t2,n2); + + + for(int i=0;ireal()<0.000000000001 and r1[i].imag()-o1->imag()<0.000000000001 ); + o1++; + } + + for(int i=0;ireal()<0.000000000001 and r2[i].imag()-o2->imag()<0.000000000001 ); + o2++; + } + + +} + +/** + * @brief Main function + * @param argc commandline argument count (ignored) + * @param argv commandline array of arguments (ignored) + * @returns 0 on exit + */ + + +int main(int argc, char const *argv[]) +{ + + + test(); + + return 0; +} \ No newline at end of file