2011-10-19 30 views
0

,这里是我的实现代码它FFT计算算法从这里描述的快速傅立叶变换算法MIT书(介绍算法)

#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; 
} 

,但它为我的错误列表

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 

更新: 这里是我更新的代码

#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; 
} 

,但它显示我的错误又

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 

请帮我家伙

+0

我认为编译器错误应该足够清楚。 –

回答

1
y0=FFT(a0); 

您不能分配无效别的东西......

你也可以不通过std::vector<int>给函数期待参照std::vector<float> &

最后

cout<<FFT(a)<<endl; 

会失败。您无法打印void

1

正如FailedDev所述,您无法将void分配给向量。取而代之的是返回当前只打印出的矢量y,用cout

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

顺便说一句:输入a应由不会被复制,因为它是在我的例子引用传递。

+0

所以我的代码如何改变totaly?请帮助我@Unapiedra –

+0

保持一切,在FFT函数的末尾添加'return y;'行。如果你愿意,你可以在FFT函数结束时删除cout。 – Unapiedra

+0

我已编辑我的代码并请参阅 –