简体   繁体   中英

Segmentation fault when trying to do a descrete Fourier Transform with complex numbers

I tried to write a DFT function with C++. This function will take a vector of complex numbers, apply DFT then return another vector of complex numbers. Then I tried to make that read files from my computer, applied that function, then wrote the result to other files. When I run it, I got a segmentation fault. Follow the first comment in this site ( Segmentation fault (core dumped) in c++ Can't find error ), I ran gdb to the code, then it returned this:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555557088 in std::complex<double>::__rep (this=0x0) at /usr/include/c++/10/complex:1363
1363          _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }

I then dig deeper and found that g++, the complier I used, got that problem in version 4.9. I checked mine, and got this:

g++ (Ubuntu 10.3.0-1ubuntu1) 10.3.0

Here is the code:

#include <bits/stdc++.h>
using namespace std;

const float pi = 3.14159265358979;

vector<complex<double>> dft(vector<complex<double>> x){
    int N = x.size();
    vector<complex<double>> X;
    for (int k = 0; k < N; k++){
        double re = 0;
        double im = 0;        
        for (int n = 0; n < N; n++){
            double phi = (2 * pi * k * n) / N;
            re += x[n].real() * cos(phi);
            im -= x[n].imag() * sin(phi);
        }
        complex<double> result (re, im);
        X.push_back(result);
    }
    return X;
}

int main(){
    string temp;
    vector<complex<double>> x, y;
    vector<complex<double>> result_x, result_y;
    ifstream x_data, y_data;
    x_data.open("x.txt", ios::in);
    y_data.open("y.txt", ios::in);
    ofstream res_x, res_y;
    res_x.open("result_x.txt", ios::out | ios::app);
    res_y.open("result_y.txt", ios::out | ios::app); 
    while(x_data >> temp){
        float temp_0 = stof(temp);
        complex<double> adder (temp_0, 0);
        x.push_back(adder); 
    }
    while (y_data >> temp){
        float temp_0 = stof(temp);
        complex<double> adder (temp_0, 0);
        y.push_back(adder);
    }
    result_x = dft(x);
    result_y = dft(y);
    for (unsigned int k = 0; k <= result_x.size(); k++){
        int freq = k;
        float amp_x = abs(result_x[k]);
        float phase_x = arg(result_x[k]);
        float amp_y = abs(result_y[k]);
        float phase_y = arg(result_y[k]);

        res_x << "[" << freq << ", " << amp_x << ", " << phase_x << "]" << endl;
        res_y << "[" << freq << ", " << amp_y << ", " << phase_y << "]" << endl;

    }
    x_data.close();
    y_data.close();
    res_x.close();
    res_y.close();
    cout << "DONE!" << endl;
    return 0;
}

The problem is that you have used k <= result_x.size() in your for loop instead of k < result_x.size() . So at the last iteration(when k = result_x.size() ) you get undefined behavior for result_x[k]; . You can solve this by modifying the for loop to look like :

for (unsigned int k = 0; k < result_x.size(); k++){//note i've used < instead of <=
//...other code here
}

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