// 2d_fourier.c // // Karl's Nifty 2D FFT thingy // Nov 4, 2000 #include #include #include #include #include #include #define MAXSIGNALSIZE 520 #define INSIGNALSOURCE argv[1] //"2D_White_Box.pgm" #define OUTSIGNALDEST argv[2] //"2D_Square_Square.pgm" struct complex { double R,I; }; int MAX(int x, int y); int MIN(int x, int y); int getImageFromFile(char *filename, int signalarray[MAXSIGNALSIZE][MAXSIGNALSIZE], int *sizex, int *sizey, int *sdepth); int writeImageToFile(char *filename, int signalarray[MAXSIGNALSIZE][MAXSIGNALSIZE], int sizex, int sizey, int sdepth); void TwoDUFFT(complex f[MAXSIGNALSIZE][MAXSIGNALSIZE], int sizex, int sizey, int depth); void TwoDFFT(complex f[MAXSIGNALSIZE][MAXSIGNALSIZE], int sizex, int sizey, int depth); void FFT(complex signalarray[], int size); void uFFT(complex signalarray[], int size); void FFTs(complex signalarray[], int size); void uFFTs(complex signalarray[], int size); int maxpix; //Main Program ************************************************** int main(int argc, char *argv[]) { //cout << "Hello\n"; maxpix=0; if (argc<3) { cout << "\nUsage: " << argv[0] << " signal.dat out.dat\n\n"; exit(0); } int insignal[MAXSIGNALSIZE][MAXSIGNALSIZE]; complex sig[MAXSIGNALSIZE][MAXSIGNALSIZE]; //cout << "Allocated\n"; int insizex, insizey, depth; //char *out[5]; if (!getImageFromFile(INSIGNALSOURCE, insignal, &insizex, &insizey, &depth)) { cout << "\nError, could not open file " << INSIGNALSOURCE << "\n\n"; exit(0); } //cout << "Read image " << insizex << " " << insizey << "\n"; for (int x=0; x=insizey) y2=0; if (x1>=insizex) x2=0; sum+=(sig[x2][y2].R*sig[x2][y2].R)+ (sig[x2][y2].I*sig[x2][y2].I); } } float tval = (((sig[x][y].R*sig[x][y].R)+ (sig[x][y].I*sig[x][y].I)+sum)/sum); if (tval > hival) { hival=tval; hix=x; hiy=y; } } } ix=512-hix; iy=512-hiy; //cout << "Removed Interference at: " << hix << "," << hiy << // " & " << ix << "," << iy << "\n"; cout << "(" << sig[hix][hiy].R << "," << sig[hix][hiy].I << ") "; cout << "(" << sig[ix][iy].R << "," << sig[ix][iy].I << ")\n"; sig[hix][hiy].R=0; //(sig[1][1].R+sig[0][1].R+sig[0][511].R+sig[511][0].R)/4; sig[hix][hiy].I=0; //(sig[1][1].I+sig[0][1].I+sig[0][511].I+sig[511][0].I)/4; sig[ix][iy].R=0; sig[ix][iy].I=0; //sig[0][0].R = 0; //sig[0][0].I = 0; //sig[0][511].R = 0; //sig[0][511].I = 0; TwoDUFFT(sig,insizex,insizey,depth); /* out[0] = (char *)malloc(sizeof(char)*20); strcpy(out[0], OUTSIGNALDEST); strcat(out[0],"R.pgm"); out[1] = (char *)malloc(sizeof(char)*20); strcpy(out[1], OUTSIGNALDEST); strcat(out[1],"I.pgm"); out[2] = (char *)malloc(sizeof(char)*20); strcpy(out[2], OUTSIGNALDEST); strcat(out[2],"P.pgm"); out[3] = (char *)malloc(sizeof(char)*20); strcpy(out[3], OUTSIGNALDEST); strcat(out[3],"M.pgm"); out[4] = (char *)malloc(sizeof(char)*20); strcpy(out[4], OUTSIGNALDEST); strcat(out[4],"PO.pgm"); */ for (int y=0; y> image_type >> width >> height >> depth; //cout << "Reading ... size = " << height << ", " << width << "\n"; *sizex = height; *sizey = width; *sdepth = depth; if (image_type!="P5") { cout << "\nError: " << image_type << " is not a valid image type.\n\n"; exit(1); } xin = 0; yin = 0; unsigned char greyscale; while (!Infile.eof()) { Infile.read(&greyscale,1); if (greyscale>depth) { cout << "Greyscale Out of Bounds! = " << greyscale << "\n"; } else { signalarray[xin][yin]=(int)greyscale; } if (xin<(width-1)) { xin++; } else { xin=0; yin++; } } Infile.close(); return 1; } } //Dump array to signal file *************************************** int writeImageToFile(char *filename, int signalarray[MAXSIGNALSIZE][MAXSIGNALSIZE], int sizex, int sizey, int sdepth) { maxpix=0; for (int x=0; xmaxpix) { maxpix=signalarray[x][y]; } } } //cout << "MAxpix image " << maxpix << "\n"; ofstream outfile; outfile.open(filename, ios::out); outfile << "P5\n" << sizex << " " << sizey << "\n" << sdepth << "\n"; if (!outfile) { return 0; } else { outfile.setf(ios::fixed); for (int y=0; y (" << signalarray[0].R << "," << signalarray[0].I // << ") (" << signalarray[1].R << "," << signalarray[1].I << ")\n"; } else { complex g[size/2]; complex h[size/2]; for (int loop=0; loop<(size/2); loop++) { g[loop]=signalarray[2*loop]; h[loop]=signalarray[2*loop + 1]; } FFT(g, size/2); FFT(h, size/2); complex gsum,hsum; //cout << "Sig array tally -> "; for (int loop=0; loop (" << signalarray[0].R << "," << signalarray[0].I // << ") (" << signalarray[1].R << "," << signalarray[1].I << ")\n"; } else { complex g[size/2]; complex h[size/2]; for (int loop=0; loop<(size/2); loop++) { g[loop]=signalarray[2*loop]; h[loop]=signalarray[2*loop + 1]; } uFFT(g, size/2); uFFT(h, size/2); complex gsum,hsum; //cout << "Sig array tally -> "; for (int loop=0; loopy) return x; else return y; } int MIN(int x,int y) { if (x>y) return y; else return x; }