Hi,
For confirming frequency spectrum of cuFFT with that of FFT of Matlab, I did cuFFT for a sin function. The result of the spectrum of cuFFT is different from that of FFT of Matlab. My codes are below:
#include “cuda_runtime.h”
#include “device_launch_parameters.h”
#include “cufft.h”
#include
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define GNUPLOT_NAME “C:/gnuplot/bin/gnuplot -persist”
#define NX 256 // number of fft
typedef float2 Complex;
int main()
{
#ifdef WIN32
FILE *pipe = _popen(GNUPLOT_NAME, “w”);
#else
FILE *pipe = popen(GNUPLOT_NAME, “w”);
#endif
//Complex *x;
float arg3, pi = 3.1415926, dt=0.1;
int size = 128;
float t;
int fc = 35;
cufftComplex *d_data, *x;
// define *h_data with cufftComplex or float2 has same result
float2 *h_data=0;
// alloc memory
x = (cufftComplex *)malloc(sizeof(cufftComplex) * size);
h_data = (float2 *)malloc(sizeof(float2) * size);
for (int i = 0; i < size; i++) {
printf("%d %lf \n", i,t);
x[i].x = sin((float)(i+1) / (float)5);
x[i].y = 0.0f;
}
// alloc memory for device variables and copy variables from host to device
cudaMalloc((void **)&d_data, sizeof(cufftComplex)*size);
cudaMemcpy(d_data, x, sizeof(cufftComplex)*size, cudaMemcpyHostToDevice);
// set up fft plan
cufftHandle plan;
cufftPlan1d(&plan, sizeof(cufftComplex)*size, CUFFT_C2C, 1);
// do fft
cufftExecC2C(plan, d_data, d_data, CUFFT_FORWARD);
// copy varibales from device to host for plotting
cudaMemcpy(h_data, d_data, sizeof(cufftComplex)*size, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
// plot sin wave and frequency spectrum by using gnuplot
if (pipe != NULL)
{
fprintf(pipe, "set multiplot layout 1,2 \n"); // set multiple plots
fprintf(pipe, "plot '-' with linespoints \n"); // plot type
for (int i = 0; i < size; i++) // loop over the data [0,...,9]
fprintf(pipe, "%lf \n", x[i].x); // data terminated with
fprintf(pipe, "%s\n", "e"); // termination character
fflush(pipe); // flush the pipe
fprintf(pipe, "plot '-' with linespoints\n"); // plot type
for (int i = 0; i < size; i++) // loop over the data [0,...,9]
fprintf(pipe, "%lf \n", cuCabsf(h_data[i])); // cuCabsf
fprintf(pipe, "%s\n", "e"); // termination character
fflush(pipe); // flush the pipe
fprintf(pipe, "unset multiplot \n");
// wait for key press
std::cin.clear();
std::cin.ignore(std::cin.rdbuf()->in_avail());
std::cin.get();
#ifdef WIN32
_pclose(pipe);
#else
pclose(pipe);
#endif
}
else
std::cout << “Could not open pipe” << std::endl;
cufftDestroy(plan);
free(x);
free(h_data);
cudaFree(d_data);
return 0;
}
So, please help me find out what causes the frequency spectrum of cuFFT is different from that of FFT by Matlab (spectrum in Matlab is correct).
Thanks very much in advanced.
Dawn