我正在使用dif基数-2算法在复杂向量上执行fft。我递归地将我的输入分成2组,第一和第二半,然后为前半部分和后半部分执行复杂的加法*旋转因子。
函数完成但我在尝试输出结果向量时收到Segmentation错误。怎么了?
int main(int argc, char *argv[]){
int n = 8;
complex<double> *x = new complex<double>[n];
// Test data
x[0] = sin(M_PI/2);
x[1] = sin(0);
x[2] = sin(0);
x[3] = sin(0);
x[4] = sin(0);
x[5] = sin(0);
x[6] = sin(0);
x[7] = sin(0);
for(int i = 0; i<n; i++){
cout << x[i] << endl;
}
fft(x,n);
cout << endl;
for(int i = 0; i<n; i++){
cout << x[i] << endl;
}
}
void fft(complex<double> *X, int N){
if(N < 1){return;}
double w = 2 * M_PI / (N/2);
for(int i = 0; i<N/2; i++){
double ang = w * i;
complex<double> tw(cos(ang),sin(ang)); // twiddle factor
complex<double> first_half = X[i];
complex<double> second_half = X[i+N/2];
X[i] = first_half+second_half;
X[i+N/2] = (first_half-second_half) * tw;
cout << X[i] << " " <<X[i+N/2] << endl;;
}
fft(X,N-1);
fft(X+N/2,N-1);
}
答案 0 :(得分:7)
fft(X+N/2,N-1);
这将超出界限;数组后半部分的大小仅为N/2
。我的傅里叶理论有点生疏,但我想你想要
fft(X, N/2);
fft(X+N/2, N/2);