简体   繁体   English

递归ftf计算的分割错误

[英]segmentation fault on recursive fft computation

I'm using the dif radix-2 algorithm to perform the fft on a complex vector. 我正在使用dif radix-2算法在复杂向量上执行fft。 I'm recursively splitting my input into 2-sets, the first and second half, then performing a complex add for the first half and the second half * twiddle factor. 我递归地将我的输入分成2组,第一和第二半,然后为前半部分和后半部分执行复杂的加法*旋转因子。

The function finishes but I'm receiving a Segmentation fault when I try to output the resulting vector. 该函数完成了,但是当我尝试输出结果向量时收到分段错误。 What's wrong? 怎么了?

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);
}
fft(X+N/2,N-1);

That's going to go out of bounds; 那将超出范围; the size of the second half of the array is only N/2 . 数组后半部分的大小只有N/2 My Fourier theory is a bit rusty, but I think you want 我的傅里叶理论有点生疏,但我想你想要

fft(X, N/2);
fft(X+N/2, N/2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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