stefano22

November 26th, 2014, 01:11 PM

Hello everyone, I am trying to make this code run, but it gives me segmentation error (core dump). Can anyone tell me how to fix it? Thanks

#include <iostream>

#include <complex>

#include <fstream>

#define MAX 200

using namespace std;

int log2(int N)

{

int k = N, i = 0;

while(k) {

k >>= 1;

i++;

}

return i - 1;

}

int check(int n)

{

return n > 0 && (n & (n - 1)) == 0;

}

int reverse(int N, int n)

{

int j, p = 0;

for(j = 1; j <= log2(N); j++) {

if(n & (1 << (log2(N) - j)))

p |= 1 << (j - 1);

}

return p;

}

void ordina(complex<double>* f1, int N)

{

complex<double> f2[MAX];

for(int i = 0; i < N; i++)

f2[i] = f1[reverse(N, i)];

for(int j = 0; j < N; j++)

f1[j] = f2[j];

}

void transform(complex<double>* f, int N)

{

ordina(f, N);

complex<double> W[N / 2];

W[1] = polar(1., +2. * M_PI / N);

W[0] = 1;

for(int i = 2; i < N / 2; i++)

W[i] = pow(W[1], i);

int n = 1;

int a = N / 2;

for(int j = 0; j < log2(N); j++) {

for(int i = 0; i < N; i++) {

if(!(i & n)) {

complex<double> temp = f[i];

complex<double> Temp = W[(i * a) % (n * a)] * f[i + n];

f[i] = temp + Temp;

f[i + n] = temp - Temp;

}

}

n *= 2;

a = a / 2;

}

}

void FFT(complex<double>* f, int N, double d)

{

transform(f, N);

for(int i = 0; i < N; i++)

f[i] *= d;

}

int main()

{

int n;

do {

cout << "specify the size of the array, which has to be a power of 2" << endl;

cin >> n;

} while(!check(n));

double d;

cout << "insert the sampling interval" << endl;

cin >> d;

complex<double> vec[MAX];

ifstream f1;

f1.open ("example.txt");

if(!f1){

cout << "Error while opening the file" << endl;

} else {

for(int i = 0; i < n; i++) {

f1 >> vec[i];

cout << vec[i] << endl;

}}

f1.close();

FFT(vec, n, d);

cout << "transformed vector" << endl;

for(int j = 0; j < n; j++)

cout << vec[j] << endl;

return 0;

}

#include <iostream>

#include <complex>

#include <fstream>

#define MAX 200

using namespace std;

int log2(int N)

{

int k = N, i = 0;

while(k) {

k >>= 1;

i++;

}

return i - 1;

}

int check(int n)

{

return n > 0 && (n & (n - 1)) == 0;

}

int reverse(int N, int n)

{

int j, p = 0;

for(j = 1; j <= log2(N); j++) {

if(n & (1 << (log2(N) - j)))

p |= 1 << (j - 1);

}

return p;

}

void ordina(complex<double>* f1, int N)

{

complex<double> f2[MAX];

for(int i = 0; i < N; i++)

f2[i] = f1[reverse(N, i)];

for(int j = 0; j < N; j++)

f1[j] = f2[j];

}

void transform(complex<double>* f, int N)

{

ordina(f, N);

complex<double> W[N / 2];

W[1] = polar(1., +2. * M_PI / N);

W[0] = 1;

for(int i = 2; i < N / 2; i++)

W[i] = pow(W[1], i);

int n = 1;

int a = N / 2;

for(int j = 0; j < log2(N); j++) {

for(int i = 0; i < N; i++) {

if(!(i & n)) {

complex<double> temp = f[i];

complex<double> Temp = W[(i * a) % (n * a)] * f[i + n];

f[i] = temp + Temp;

f[i + n] = temp - Temp;

}

}

n *= 2;

a = a / 2;

}

}

void FFT(complex<double>* f, int N, double d)

{

transform(f, N);

for(int i = 0; i < N; i++)

f[i] *= d;

}

int main()

{

int n;

do {

cout << "specify the size of the array, which has to be a power of 2" << endl;

cin >> n;

} while(!check(n));

double d;

cout << "insert the sampling interval" << endl;

cin >> d;

complex<double> vec[MAX];

ifstream f1;

f1.open ("example.txt");

if(!f1){

cout << "Error while opening the file" << endl;

} else {

for(int i = 0; i < n; i++) {

f1 >> vec[i];

cout << vec[i] << endl;

}}

f1.close();

FFT(vec, n, d);

cout << "transformed vector" << endl;

for(int j = 0; j < n; j++)

cout << vec[j] << endl;

return 0;

}