I am fairly new to c++. For my project I tried to write a class for fast fourier transform based on the FFTW3
library in Ubuntu 17.04. my code is :
my *.hpp file
#include <iostream>
#include <cmath>
#include "fftw3.h"
#include <complex>
#ifndef FG_HPP
#define FG_HPP
class myfft
{
public:
int n;
fftw_complex *x, *y;
fftw_plan p;
void setvalue() ;
void fftforward();
void fftbackward();
void mfree();
myfft(int );
~myfft();
};
#endif
my *.cpp file:
#include "fg.hpp"
using namespace std;
//Constructure
myfft::myfft(int a){
n = a;
x = new fftw_complex [n]; // <= allocate array
y = new fftw_complex [n];
for (int i = 0; i<n; i++) {
x[i][0] = i+1;
x[i][1] = i+i;
//y[i][0] = i+1;
//y[i][1] = i+i;
}
}
//---------------------------------------------------
//setvalue
void myfft::setvalue(){
for (int i = 0; i<n; i++) {
cout<<"Please enter real "<<i+1<<endl;
cin>> x[i][0];
cout<<"Please enter image "<<i+1<<endl;
cin>> x[i][1];
}
}
//---------------------------------------------------
// forward mapping
void myfft::fftforward()
{
p=fftw_plan_dft_2d(n, n, x, y, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(p);
}
//-----------------------------------------------------------
// backward mapping
void myfft::fftbackward()
{
p=fftw_plan_dft_2d(n, n, x, y, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(p);
}
//-----------------------------------------------------------
// Destructor and clean up
myfft::~myfft(){
if (x) {delete[] x;}
if (y) {delete[] y;}
}
void myfft::mfree(){fftw_destroy_plan(p); fftw_cleanup();}
//-----------------------------------------------------------
// Main Program
int main(int argc, char** argv)
{
myfft* g;
g = new myfft(25); // enter number of array as argument
if (!g) {
cout << "Not enough memory available or error in allocation.\n";
return(-1);
}
g->setvalue();
g->fftforward();
g->mfree();
for(int i=0; i<5; i++){
cout << g->x[i][0]
<<" "
<< g->x[i][1]
<< endl;
}
for(int i=0; i<5; i++){
cout << g->y[i][0]
<< " "
<< g->y[i][1]
<< endl;
}
return 0;
}
When I tried to compile the code I faced the issue of "segfault(core dumped)
". This problem completely confused me and I can't understand which part of program produced the error .Can anybody tell me how I can fix it? Any help would be highly appreciated.