You are on page 1of 3

void CFourierTransform::FFT(BYTE *inputimage, BYTE *resultimage) { int index=0,i=0,j=0,k=0; COMPLEX col_data[256]; // storage for the columns COMPLEX row_data[256];

// storage for the rows for(i=0;i<256;i++) { col_data[i].re=0; col_data[i].im=0; row_data[i].re=0; row_data[i].im=0; for(j=0;j<256;j++) { complex_data[k+j].re=inputimage[i*256+j]; complex_data[k+j].im=0; } //for(j) k+=256; } // for(i) k=0; for(int y=0;y<256;y++) // FFT on all rows { index=y*256; for(int x=0;x<256;x++) { row_data[x].re=complex_data[index].re; row_data[x].im=complex_data[index++].im; } //for(x) fftproc(row_data,8,256,1); // compute forward FFT index=y*256; for(x=0; x<256; x++) { complex_data[index].re=row_data[x].re; complex_data[index++].im=row_data[x].im; } //for(x) } // for(y) for(int x=0;x<256;x++) // FFT on all columns { index=x; for(int y=0;y<256;y++) { col_data[y].re=complex_data[index].re; col_data[y].im=complex_data[index].im; index += 256; } // for(y) fftproc(col_data,8,256,1); // computer forward FFT index=x; for(y=0;y<256;y++) { complex_data[index].re=col_data[y].re; complex_data[index].im=col_data[y].im; index += 256;

} //for(y) } // for(x) } void CFourierTransform::fftproc(COMPLEX *f, int logN, int numpoints, int dir) { scramble(numpoints,f); butterflies(numpoints,logN,dir,f); // if dir==1 : forward FFT , dir==-1 : inverse FFT } void CFourierTransform::scramble(int numpoints, COMPLEX *f) { int i,j,m; // loop variables double temp; // temporary storage during swapping j=0; for(i=0;i<numpoints;i++) { if(i>j) { // swap f[j] and f[i] //swap real temp = f[j].re; f[j].re = f[i].re; f[i].re = (float)temp; //swap imagenary temp = f[j].im; f[j].im = f[i].im; f[i].im = (float)temp; } //if() m = numpoints>>1; while((j>=m) & (m>=2)) { j -= m; m = m>>1; } //while() j += m; } //for(i) } void CFourierTransform::butterflies(int numpoints, int logN, int dir, COMPLEX *f ) { double angle; // polar angle COMPLEX w, wp, temp; // intermediat complex numbers int i,j,k,offset; // loop variables int N, half_N; // storage for powers of 2 double wtemp; // temporary storage for w.re N=1; for(k=0; k<logN ; k++) { half_N=N; N <<=1; // multiply N by 2 angle = -2.0 * PI / N * dir; wtemp = sin(0.5*angle); //wp.re = (float)sin(0.5*angle); wp.re = (float)(-2.0*wtemp*wtemp);

wp.im = (float)sin(angle); w.re = 1.0; w.im = 0.0; for(offset=0 ; offset<half_N ; offset++) { for(i=offset ; i<numpoints ; i += N) { j=i + half_N; temp.re = (w.re * f[j].re) - (w.im * f[j].im); temp.im = (w.im * f[j].re) + (w.re * f[j].im); f[j].re = f[i].re - temp.re; f[i].re += temp.re; f[j].im = f[i].im - temp.im; f[i].im += temp.im; } //for(i) wtemp = w.re; w.re = (float)(wtemp * wp.re - w.im * wp.im + w.re); w.im = (float)(w.im * wp.re + wtemp * wp.im + w.im); } //for(offset) } //for(k) if(dir == -1) // if inverse fft, divide by numpoints for(i=0;i<numpoints;i++) { f[i].re /= numpoints; f[i].im /= numpoints; } // for(i) }

You might also like