1 #include <assert.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <math.h> 6 // Include CUDA runtime and CUFFT 7 #include <cuda_runtime.h> 8 #include <cufft.h> 9 10 // Helper functions for CUDA 11 #include <helper_functions.h> 12 #include <helper_cuda.h> 13 #include "device_launch_parameters.h" 14 15 #define pi 3.1415926535 16 #define LENGTH 100 //signal sampling points 17 int main() 18 { 19 // data gen 20 float Data[LENGTH] = {1,2,3,4}; 21 float fs = 1000000.000;//sampling frequency 22 float f0 = 200000.00;// signal frequency 23 for(int i=0;i<LENGTH;i++) 24 { 25 Data[i] = 1.35*cos(2*pi*f0*i/fs);//signal gen, 26 27 } 28 29 cufftComplex *CompData=(cufftComplex*)malloc(LENGTH*sizeof(cufftComplex));//allocate memory for the data in host 30 int i; 31 for(i=0;i<LENGTH;i++) 32 { 33 CompData[i].x=Data[i]; 34 CompData[i].y=0; 35 } 36 37 cufftComplex *d_fftData; 38 cudaMalloc((void**)&d_fftData,LENGTH*sizeof(cufftComplex));// allocate memory for the data in device 39 cudaMemcpy(d_fftData,CompData,LENGTH*sizeof(cufftComplex),cudaMemcpyHostToDevice);// copy data from host to device 40 41 cufftHandle plan;// cuda library function handle 42 cufftPlan1d(&plan,LENGTH,CUFFT_C2C,1);//declaration 43 cufftExecC2C(plan,(cufftComplex*)d_fftData,(cufftComplex*)d_fftData,CUFFT_FORWARD);//execute 44 cudaDeviceSynchronize();//wait to be done 45 cudaMemcpy(CompData,d_fftData,LENGTH*sizeof(cufftComplex),cudaMemcpyDeviceToHost);// copy the result from device to host 46 47 for(i=0;i<LENGTH/2;i++) 48 { 49 //if(CompData[i].x != 0) 50 //{ 51 printf("i=%d\tf= %6.1fHz\tRealAmp=%3.1f\t",i,fs*i/LENGTH,CompData[i].x*2.0/LENGTH);//print the result: 52 //} 53 //if(CompData[i].y != 0 ) 54 //{ 55 printf("ImagAmp=+%3.1fi",CompData[i].y*2.0/LENGTH); 56 // } 57 printf("\n"); 58 } 59 cufftDestroy(plan); 60 free(CompData); 61 cudaFree(d_fftData); 62 63 }
i=0 f= 0.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=1 f= 10000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=2 f= 20000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=3 f= 30000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=4 f= 40000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=5 f= 50000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=6 f= 60000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=7 f= 70000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=8 f= 80000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=9 f= 90000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=10 f= 100000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=11 f= 110000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=12 f= 120000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=13 f= 130000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=14 f= 140000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=15 f= 150000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=16 f= 160000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=17 f= 170000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=18 f= 180000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=19 f= 190000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=20 f= 200000.0Hz RealAmp=1.4 ImagAmp=+0.0i
i=21 f= 210000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=22 f= 220000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=23 f= 230000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=24 f= 240000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=25 f= 250000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=26 f= 260000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=27 f= 270000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=28 f= 280000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=29 f= 290000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=30 f= 300000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=31 f= 310000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=32 f= 320000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=33 f= 330000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=34 f= 340000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=35 f= 350000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=36 f= 360000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=37 f= 370000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=38 f= 380000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=39 f= 390000.0Hz RealAmp=-0.0 ImagAmp=+-0.0i
i=40 f= 400000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=41 f= 410000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=42 f= 420000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=43 f= 430000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=44 f= 440000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
i=45 f= 450000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=46 f= 460000.0Hz RealAmp=0.0 ImagAmp=+0.0i
i=47 f= 470000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=48 f= 480000.0Hz RealAmp=-0.0 ImagAmp=+0.0i
i=49 f= 490000.0Hz RealAmp=0.0 ImagAmp=+-0.0i
请按任意键继续. . .