/* To get some feel for Fourier transforms, try changing the parameters NMAX (total number of points) and dt (interval between points), or the function being transformed (defined in the first loop). Here's a refresher on some possibly useful Unix commands: g++ discft.c -o discft (if you save this file as discft.c, this compiles and creates an executable, discft) ./discft (runs the program; the first two characters may not be necessary) ./discft > outputfile (runs the program, putting results in a file instead of on the screen) gnuplot (runs gnuplot, a simple graphing package) plot [xrange=0:10] "outputfile" with lines (within gnuplot, plots the first two columns of outputfile, using the range from 0 to 10 for the horizontal axis. These columns are frequency and the real part of the Fourier transform.) plot [0:5][0:0.5] "outputfile" with linespoints (within gnuplot, plots first two columns of outputfile, using range 0 to 10 for horizontal axis and 0 to 0.5 for vertical axis, with both symbols and lines connecting the data points) */ #include #include #include #include using namespace std; #define NMAX 80000 // You may need to change the number of points! #define NFILE 80000 // Number of data points in file main () { int i, k, n; double creal[NMAX], cimag[NMAX], freal[NFILE], fimag[NFILE]; double expreal[NMAX], expimag[NMAX]; double dt, tval, const1, const2, freq; /* Choose an appropriate dt. */ dt = 1; //one of the questions on Set 3 is to find the "real" dt value /* Parameters for the truncated cosine below. */ double cosfreq = 6.6, numperiods = 2.5, tmax; /* This loop, presently commented out, calculates values of a cosine function at evenly spaced points, in preparation for a discrete Fourier transform. */ /* for (k=0; k> freal[k]; fimag[k]=0; } /* The next two loops, also commented out, do the same job as the previous two function assignments, but for a truncated cosine wave. (``Truncated" because the function is a cosine for small enough |k| but zero outside.) */ /* const1 = NMAX*dt; tmax = M_PI * numperiods / cosfreq; for (k=0; k<=NMAX/2; k++) { //the <= will work whether NMAX is even or odd tval = k * dt; freal[k] = 0; if (fabs(tval)<=tmax) { freal[k] = cos(cosfreq*tval); } fimag[k] = 0; } */ /* assign the second half of array by using evenness of cosine; note that periodicity means that freal[NMAX-k] is completely equivalent to freal[-k] */ /* for (k=1; k