1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h>
#ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define SIZE 1024*16 #define VALUE_MAX 1000
struct Complex_{ double real; double imagin; }; typedef struct Complex_ Complex;
void Add_Complex(Complex * src1,Complex *src2,Complex *dst){ dst->imagin=src1->imagin+src2->imagin; dst->real=src1->real+src2->real; } void Sub_Complex(Complex * src1,Complex *src2,Complex *dst){ dst->imagin=src1->imagin-src2->imagin; dst->real=src1->real-src2->real; } void Multy_Complex(Complex * src1,Complex *src2,Complex *dst){ double r1=0.0,r2=0.0; double i1=0.0,i2=0.0; r1=src1->real; r2=src2->real; i1=src1->imagin; i2=src2->imagin; dst->imagin=r1*i2+r2*i1; dst->real=r1*r2-i1*i2; }
void getWN(double n,double size_n,Complex * dst){ double x=2.0*M_PI*n/size_n; dst->imagin=-sin(x); dst->real=cos(x); }
void setInput(double * data,int n){ srand((int)time(0)); for(int i=0;i<SIZE;i++){ data[i]=rand()%VALUE_MAX; }
}
void DFT(double * src,Complex * dst,int size){ clock_t start,end; start=clock();
for(int m=0;m<size;m++){ double real=0.0; double imagin=0.0; for(int n=0;n<size;n++){ double x=M_PI*2*m*n; real+=src[n]*cos(x/size); imagin+=src[n]*(-sin(x/size));
} dst[m].imagin=imagin; dst[m].real=real;
} end=clock(); printf("DFT use time :%lf for Datasize of:%d\n",(double)(end-start)/CLOCKS_PER_SEC,size);
}
void IDFT(Complex *src,Complex *dst,int size){ clock_t start,end; start=clock(); for(int m=0;m<size;m++){ double real=0.0; double imagin=0.0; for(int n=0;n<size;n++){ double x=M_PI*2*m*n/size; real+=src[n].real*cos(x)-src[n].imagin*sin(x); imagin+=src[n].real*sin(x)+src[n].imagin*cos(x);
} real/=SIZE; imagin/=SIZE; if(dst!=NULL){ dst[m].real=real; dst[m].imagin=imagin; } if(imagin>=0.0) printf("%lf+%lfj\n",real,imagin); else printf("%lf%lfj\n",real,imagin); } end=clock(); printf("IDFT use time :%lfs for Datasize of:%d\n",(double)(end-start)/CLOCKS_PER_SEC,size);
}
int FFT_remap(double * src,int size_n){
if(size_n==1) return 0; double * temp=(double *)malloc(sizeof(double)*size_n); for(int i=0;i<size_n;i++) if(i%2==0) temp[i/2]=src[i]; else temp[(size_n+i)/2]=src[i]; for(int i=0;i<size_n;i++) src[i]=temp[i]; free(temp); FFT_remap(src, size_n/2); FFT_remap(src+size_n/2, size_n/2); return 1;
}
void FFT(double * src,Complex * dst,int size_n){
FFT_remap(src, size_n); clock_t start,end; start=clock(); int k=size_n; int z=0; while (k/=2) { z++; } k=z; if(size_n!=(1<<k)) exit(0); Complex * src_com=(Complex*)malloc(sizeof(Complex)*size_n); if(src_com==NULL) exit(0); for(int i=0;i<size_n;i++){ src_com[i].real=src[i]; src_com[i].imagin=0; } for(int i=0;i<k;i++){ z=0; for(int j=0;j<size_n;j++){ if((j/(1<<i))%2==1){ Complex wn; getWN(z, size_n, &wn); Multy_Complex(&src_com[j], &wn,&src_com[j]); z+=1<<(k-i-1); Complex temp; int neighbour=j-(1<<(i)); temp.real=src_com[neighbour].real; temp.imagin=src_com[neighbour].imagin; Add_Complex(&temp, &src_com[j], &src_com[neighbour]); Sub_Complex(&temp, &src_com[j], &src_com[j]); } else z=0; }
}
for(int i=0;i<size_n;i++){ dst[i].imagin=src_com[i].imagin; dst[i].real=src_com[i].real; } end=clock(); printf("FFT use time :%lfs for Datasize of:%d\n",(double)(end-start)/CLOCKS_PER_SEC,size_n);
}
int main(int argc, const char * argv[]) { double input[SIZE]; Complex dst[SIZE]; setInput(input,SIZE); printf("\n\n"); DFT(input, dst, SIZE); printf("\n\n"); FFT(input, dst, SIZE); getchar(); }
|