简体   繁体   中英

FFT calculation from introduction to algorithm

from MIT books(introduction to algorithm) here is described Fast Fourier transform algorithm,here is my code for implementation it

#include <iostream>
#include<complex>
#include<math.h>
#include<vector>
#include<iterator>
#include<complex>
using namespace std;
#define E 2.718
#define PI 3.14
#define n 8 
void FFT(vector<float>&a){
    vector<float>::iterator it;
        const double pi = std::acos(-1.0);
const std::complex<double> i(0,1);

complex<double>  wn =std::exp((2*pi*i)/8.0);
    vector<float>y;
    vector<float>y0;
    vector<float>y1;

    int omega=1;
    vector<float>a0;
     vector<float>a1;
      for (int i=0;i<n-1;i+=2)
          a0[i]=a[i];
       for (int i=1;i<n;i+=2)
           a1[i]=a[1];
       y0=FFT(a0);
       y1=FFT(a1);

        for (int k=0;k<n/2-1;k++){

            y[0]=y0[k]+omega*y1[k];
            y[k+n/2]=y0[k]-omega*y1[k];
            omega=omega*imag(wn);


        }
        for(it=y.begin();it!=y.end();it++){
            cout<<*it<<"  ";

        }

}

int main(){
    vector<int>a;
    a.push_back(2);
    a.push_back(3);
    a.push_back(5);
    a.push_back(1);
    a.push_back(100);
    a.push_back(200);
    a.push_back(300);
    a.push_back(400);
    cout<<FFT(a)<<endl;

    return 0;
}

but it shows me error list

Error   1   error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)  c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  28  1   FFT
Error   2   error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)  c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  29  1   FFT
Warning 3   warning C4244: '=' : conversion from 'double' to 'int', possible loss of data   c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  35  1   FFT
Error   4   error C2664: 'FFT' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> &' c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  56  1   FFT
    5   IntelliSense: no operator "=" matches these operands    c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  28  7   FFT
    6   IntelliSense: no operator "=" matches these operands    c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  29  7   FFT
    7   IntelliSense: a reference of type "std::vector<float, std::allocator<float>> &" (not const-qualified) cannot be initialized with a value of type "std::vector<int, std::allocator<int>>"    c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  56  12  FFT

UPDATED: here is my updated code

#include <iostream>
#include<complex>
#include<math.h>
#include<vector>
#include<iterator>
#include<complex>
using namespace std;
#define E 2.718
#define PI 3.14
#define n 8 
vector<float>FFT(vector<double>a)
{
    vector<float>::iterator it;
        const double pi = std::acos(-1.0);
const std::complex<double> i(0,1);

complex<double>  wn =std::exp((2*pi*i)/8.0);
    vector<double>y;
    vector<double>y0;
    vector<double>y1;

    double omega=1;
    vector<double>a0;
     vector<double>a1;
      for (int i=0;i<n-1;i+=2)
          a0[i]=a[i];
       for (int i=1;i<n;i+=2)
           a1[i]=a[1];
       FFT(a0);
       y0=a0;
       FFT(a1);
       y1=a1;

        for (int k=0;k<n/2-1;k++){

            y[0]=y0[k]+omega*y1[k];
            y[k+n/2]=y0[k]-omega*y1[k];
            omega=omega*imag(wn);


        }
         return y;



}

int main(){
    vector<double>a;
    a.push_back(2);
    a.push_back(3);
    a.push_back(5);
    a.push_back(1);
    a.push_back(100);
    a.push_back(200);
    a.push_back(300);
    a.push_back(400);
FFT(a);

    return 0;
}

but it shows me errors yet

Error   1   error C2664: 'std::vector<_Ty>::vector(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'const std::vector<_Ty> &'    c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  42  1   FFT
    2   IntelliSense: no suitable user-defined conversion from "std::vector<double, std::allocator<double>>" to "std::vector<float, std::allocator<float>>" exists  c:\users\datuashvili\documents\visual studio 2010\projects\fft\fft\fft.cpp  42  11  FFT

please help me guys

y0=FFT(a0);

You cannot assign void to something else...

Also you can't pass a std::vector<int> to a function expecting a reference to std::vector<float> &

And finally

cout<<FFT(a)<<endl;

Will fail. You can't print void

As FailedDev said, you can't assign a void to a vector. Instead return the vector y which you are currently only printing out with cout .

vector<float> FFT(vector<float> a) // Consider changing the name to a lower case fft. 
{
    ...
    return y;
}

Btw: The input a should be passed by reference not copied as it is in my example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM