Trying to solve an unknwon segmentation fault in C -
i'm trying learn how use fast fourier transform, , have copied fft algorithm numerical recipies in c called four1. have written small function test fourier transforms simple function. upon execution program returns segmentation fault. can't find fault can me?
#include <stdio.h> #include <math.h> #define swap(a,b) tempr=(a);(a)=(b);(b)=tempr void four1(float data[], unsigned long nn, int isign) { unsigned long n,mmax,m,j,istep,i; double wtemp,wr,wpr,wpi,wi,theta; float tempr,tempi; n=nn << 1; j=1; (i=1;i<n;i+=2) { if (j > i) { swap(data[j],data[i]); swap(data[j+1],data[i+1]); } m=n >> 1; while (m >= 2 && j > m) { j -= m; m >>= 1; } j += m; } mmax=2; while (n > mmax) { istep=mmax << 1; theta=isign*(6.28318530717959/mmax); wtemp=sin(0.5*theta); wpr = -2.0*wtemp*wtemp; wpi=sin(theta); wr=1.0; wi=0.0; (m=1;m<mmax;m+=2) { (i=m;i<=n;i+=istep) { j=i+mmax; tempr=wr*data[j]-wi*data[j+1]; tempi=wr*data[j+1]+wi*data[j]; data[j]=data[i]-tempr; data[j+1]=data[i+1]-tempi; data[i] += tempr; data[i+1] += tempi; } wr=(wtemp=wr)*wpr-wi*wpi+wr; wi=wi*wpr+wtemp*wpi+wi; } mmax=istep; } } void fourier_transform_test (file* output_file) { /* function serves test see whether implementation of fft working or not. */ int n = 30; // number of samples float x[n]; // array holds values x // misc int = 0; printf("running fourier transform tests...\n"); fprintf(output_file, "# x t\n"); // fill array x values transformed (i = 0; <= (n - 1); i++) x[i] = cos((2 * 3.1415 * i) / 10); // according numerical recipies, have decrement pointer data // 1 compensate zero-offset four1(x-1, 64, 1); // loop through transformed array x , print results file (i = 0; <= (n - 1); i++) fprintf(output_file, "%i\t%f\n", i, x[i]); fclose(output_file); } int main (int argc, char *argv[]) { // open data_file write results file* file; if (argc == 1) file = fopen("results.dat", "w"); else file = fopen(argv[1], "w"); fourier_transform_test(file); return 0; }
i'm using latest debian, gcc 4 (pretty sure)
for of want see book yourself: http://www.nrbook.com/a/bookcpdf/c12-2.pdf (its legit)
you have 30 values in array you're telling four1 array 64 floats long.
four1(x-1, n, 1);
Comments
Post a Comment