int iter,i,k; int k1,inc1; int k2,j,k3,k4; char *buff1,*buff2,tmp; long loc; buff1=(char *)malloc(N*sizeof(char)); buff2=(char *)malloc(N*sizeof(char)); N1=N/2; inc=1; inc1=2;

10 04 24 34 00 20 30 14 00 01 02 03 04 05 06 07

11 05 25 35 01 21 31 15 10 12 16 11 13 14 15 17

12 06 16 26 36 02 22 32 21 23 25 27 20 22 24 26

13 07 17 27 37 03 23 33 30 31 32 33 34 35 36 37

44 54 64 74 40 50 60 70 41 43 45 47 40 42 44 46

45 55 65 75 41 51 61 71 50 52 51 53 55 57 54 56

46 66 61 63 65 67 56 76 42 52 62 72 60 62 64 66

47 67 57 77 43 53 63 73 70 74 76 71 72 73 75 77

b­íc 0 b­íc 2

10 40 60 00 20 30 50 70 02 06 10 12 16 04 14 00

11 41 61 01 21 31 51 71 03 07 11 13 17 05 15 01

12 42 62 02 22 32 52 72 20 30 22 32 24 34 26 36

13 43 63 03 23 33 53 73 21 31 23 33 25 35 27 37

40 44 54 04 14 24 34 44 54 64 74 42 46 50 52 56

53 57 41 43 47 45 55 05 15 25 35 45 55 65 75 51

60 62 66 64 74 70 72 76 16 46 66 06 26 36 56 76

61 63 67 65 75 71 73 77 17 47 67 07 27 37 57 77

b­íc 1 Ma trËn chuyÓn vÞ

116

Hình 6.10 Thuật toán của Eklundh cho dịch chuyển một ma trận.

*(buff1+k4)=fgetc(fptr); {

for(iter=0;iter

117

for(j=k3;j<(k3+inc);j++) { tmp=*(buff1+j+inc); *(buff1+j+inc)=*(buff2+j); *(buff2+j)=tmp; } k3+=inc1 ; } loc=(long)(N)*(long)i; if(fseek(fptr,loc,SEEK_SET)!=0) { perror("fseek failed"); exit( 1 ) ; } else { gotoxy(1,3); printf("writing row # %4d",i); for(k4=0;k4

118

inc1*=2; N1/=2; } Để kiểm tra chương trình 6.5 chúng ta sẽ áp dụng thuật toán này lên ảnh cho trong hình 6.11. ảnh này chứa trên đĩa đi kèm theo cuốn sách này dưới file có tên là “MOHSEN.IMG”.

Chương trình 6.6 “ FFT2D.C” 2-D FFT

/****************************** * Program developed by: * * * M.A.Sid-Ahmed. * ver. 1.0 1992. * * @ 1994 * ******************************/ /* 2D-FFT - Using Decimation-in-time routine.*/ #define pi 3.141592654 #include #include #include #include #include #include void bit_reversal(unsigned int *, int , int); void WTS(float *, float *, int, int); void FFT(float *xr, float *xi, float *, float *, int, int ) ; void transpose(FILE *, int, int); void FFT2D(FILE *, FILE *, float *, float*, unsigned int *,int,int,int);

119

Hình 6.11 Ảnh đã được dịch chuyển, "MOHSEN.IMG".

void main() { int N,n2,m,k,i; unsigned int *L; float *wr , *wi; char file_name[14]; FILE *fptr,*fptro; double nsq; clrscr(); printf(" Enter name of input file-> "); scanf("%s",file_name); if((fptr=fopen(file_name,"rb"))==NULL) { printf("file %s does not exist.\n"); exit(1); } nsq=(double)filelength(fileno(fptr)); N=sqrt(nsq); m=(int)(log10((double)N)/log10((double)2.0)); k=1 ;

120

for(i=0;i

Hình 6.12 Ảnh dịch chuyển của “MOHSEN.IMG”.

printf(" Enter file name for output file.-->"); scanf("%s",file_name); fptro=fopen(file_name,"wb+"); /* Allocating memory for bit reversal LUT. */ L=(unsigned int *)malloc(N*sizeof(unsigned int)); /* Generate Look-up table for bit reversal. */ bit_reversal(L,m,N); /* Allocating memory for twiddle factors. */ n2=(N>>1)-1; wr=(float *)malloc(n2*sizeof(float));

121

fptr, fptro should be opened in the main

wi=(float *)malloc(n2*sizeof(float)); /*Generating LUT for twiddle factors. */ WTS(wr,wi,N,-1); FFT2D(fptr,fptro,wr,wi,L,N,m,-1); } void FFT2D(FILE *fptr, FILE *fptro, float *wr, float *wi, unsigned int *L, int N, int m, int sign) { /* 2-D FFT Algorithm. */ /* fptr=file pointer to input file. fptro=file pointer to output file. Note: routine. They are closed before return to @he main routine. wr[1,wj[I input arrays for twiddle factors, calculated by calling procedure WTS. L[I look-up table for bit reversal. N input array size ( NxN words.) =2 to the power m. sign =-1 for 2-D FFT, =1 for 2-D IFFT. For FFT (sign= 1) the input data is assumed to be real. The result of the FFT has its origin shifted to (N/2,N/2).*/ int N2,i,j,k,kk; long loc,NB; float *xr,*xi,*buff; N2=N<<1; NB=sizeof(float)*N2; /* Allocating memory for FFT arrays ( real and imag.) */ xr=(float *)malloc(N*sizeof(float)); xi=(float *)malloc(N*sizeof(float)); buff=(float *)malloc(NB);

122