1. Trang chủ
  2. » Công Nghệ Thông Tin

Giáo trình xử lý ảnh y tế Tập 2 P5 pps

8 351 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 21,44 KB

Nội dung

150 #include <stdio.h> #include <math.h> #include <alloc.h> #include <stdlib.h> #include <io.h> #include <conio.h> #include <ctype.h> #include <conio.h> #include <string.h> 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) ; void main() { int M,M1,m,n2,i,j,ii,jj,k; int Nt,n,M2,M3,N,N1,ind,yt; unsigned int *L; float *wr,*wi,Do,winc,Do1,Do2; float win,T,alpha,beta,sum1,sum2; float nsq,nsqrt; FILE *fptri,*fptro,*fptr; float *buffi,*buffo,R2,H; float xrm,xim,xrn,xin,zrt,zit,mag,theta; float *w,**h,r1,im; unsigned char file_name[14], ch,ch1,choice,file_name1[14]; clrscr() ; printf("Freq. response can be calculated using standard \n"); printf(" functions which you can select from a menu if your\n"); printf(" reply to the following question is negative.\n"); printf("Is freq. response provided in a file? (y or n) >"); while(((ch1=getch())!='y')&&(ch1!='n')); 151 putch(ch1); switch(ch1) { case 'n': printf("\n Enter # of points to be generated (e.g. 32x32) >"); scanf("%dx%d",&M1,&M1); break ; case 'y': printf("\nEnter name of file storing magnitude response >"); scanf("%s",file_name1); fptr=fopen(file_name1,"r"); fscanf(fptr,"%d %d 11,01,&Mi"); } M=M1>>1 ; yt=wherey(); again1 : gotoxy(1,yt); printf( " "); gotoxy(1,yt); printf("Enter file name for storing impulse response >"); scanf("%s",file_name); if(((stricmp("FFT.DAT",file_name))==0)|| ((stricmp("TEMP.DAT",file_name))==0)|| ((stricmp("IFFT.DAT",file_name))==0)) printf("This is a reserved file name. Use some other name."); goto again1; gotoxy(1,yt); printf ( " "); ind=access(file_name,0); while(!ind) { gotoxy(1,yt+1); printf ( " "); gotoxy(1,yt+1); printf("File exists. Wish to overwrite? (y or n)- ->"); while(((ch=tolower(getch()))!='y')&&(ch1='n')); 152 putch(ch); switch(ch) { case 'y' : ind=1 ; break; case 'n' : gotoxy(1,yt+1); printf(" "); gotoxy(1,yt); printf("Enter file name >"); scanf("file_name"); ind=access(file_name,0); } } fptri=fopen("FFT.DAT","wb+"); fptro=fopen("IFFT.DAT","wb+"); buffi=(float *)malloc((M1<<1)*sizeof(float)); switch(ch1) { case 'n': /*Generating data for IFFT.*/ printf("\nEnter choice (1,2 etc.):\n"); printf(" 1. Low-pass.\n"); printf(" 2. High-pass.\n"); printf(" 3. Band-pass.\n"); printf(" 4. Band-reject >"); while(((ch=getche())!='1')&&(ch!='2')&&(ch!='3')&&(c h!='4')); switch(ch) { case '1' : case '2' : printf("\nEnter cut-off freq. in rad./sec.(cut-off <= n.)->"); scanf("%f",&Do); Do=(Do/pi)*(float)M; Do*=Do; printf("Enter choice:\n"); printf(" 1. Abrupt transition.\n"); printf(" 2. Butterworth.\n"); 153 printf(" Enter 1 or 2 >"); while(((choice=getche())!='1')&&(choice!='2')); printf("\n"); break; case '3': case '4': printf ("\nEnter lower cut-off freq. in rad./sec. (cut-off <=n."); scanf("%f",&Do1); Do1=(Do/pi)*(float)M; Do1*=Do1; printf ("\nEnter upper cut-off freq. in rad./sec.cut-off<= n.)->"); scanf("%f",&Do2); Do2=(Do/pi)*(float)M; Do2*=Do2; } for(i=0;i<M1;i++) { ii=(i-M)*(i-M); for(j=0;j<M1;j++) { R2=(float)((j-M)*(j-M)+ii); switch(ch) { case '1': /* low-pass. */ if(choice=='1') { if(R2<Do) H=(float)1.0; else H=(float)0.0; } if(choice=='2') H=0.414*Do/(R2+0.414*Do); break; case '2': /* high-pass. */ if(choice=='1') { if(R2<Do) H=(float)0.0; else H=(float)1.0; } 154 if(choice=='2') H=R2/(R2+0.414*Do); break; case '3': /* Band-pass. */ if(R2<Do1) H=(float)0.0; else if (R2>Do2) H=(float)0.0; else H=(float)1.0; break; case '4': /* Band-reject. */ if(R2<Do1) H=(float)1.0; else if (R2>Do2) H=(float)1.0; else H=(float)0.0; break; } jj=j <<1; buffi[jj]=H; buffi[jj+1]=0.0; } fwrite(buffi,M1,2*sizeof(float),fptri); } break; case 'y' : for(i=0;i<M1;i++) { for(j=0;j<M1;j++) { fscanf(fptr,"%f ",&H); jj=j<<1 ; buffi[jj]=H; buffi[jj+1]=(float)0.0; } fwrite(buffi,M1,2*sizeof(float),fptri); } fclose(fptr); } rewind(fptri); m=(int)(log10((double)M1)/log10((double)2)); /* Allocating memory for bit reversal LUT. */ L=(unsigned int *)malloc(M1*sizeof(unsigned int)); 155 /* Generate Look-up table for bit reversal. */ bit_reversal(L,m,M1); /* Allocating memory for twiddle factors. */ n2=M1-1; wr=(float *)malloc(n2*sizeof(float)); wi=(float *)malloc(n2*sizeof(float)); /* Generating twiddle factor.*/ WTS(wr,wi,M1,1); clrscr(); FFT2D(fptri,fptro,wr,wi,L,M1,m,1); clrscr(); fptri=fopen("IFFT.DAT","rb"); fptro=fopen("temp.dat","wb+"); nsq=(float)(M1*M1); buffo=(float *)malloc(M1*sizeof(float)); for(i=0;i<M1;i++) { fread(buffi,M1,2*sizeof(float),fptri); for(j=0;j<M1;j++) buffo[j]=(float)(buffi[2*j]/nsq); fwrite(buffo,M1,sizeof(float),fptro); } fclose(fptri); rewind(fptro); printf("Enter order of FIR filter (odd numbers)."); printf(" \n >(e.g. 3x3, 5x5) "); scanf("%dx%d",&N,&N); N1=(N-1)/2; M2=M-N1; M3=M+N1; h=(float **)malloc(N*sizeof(float)); for(i=0;i<N;i++) *(h+i)=(float *)malloc(N*sizeof(float)); /* Reading in the impulse response. */ buffo=(float *)malloc(M1*sizeof(float)); fseek(fptro,(long)(M2)*(long)(M1*sizeof(float)),SEEK _SET); 156 for(i=M2;i<=M3;i++) { fread(buffo,M1,sizeof(float),fptro); for(j=M2;j<=M3;j++) h[(i-M2)][j-M2]=buffo[j]; } /* Selection of Window functions. */ printf("\nEnter selection of window function:"); printf("\n 1.Rectangular."); printf("\n 2.Hann."); printf("\n 3.Hamming,"); printf("\n 4.Blackmann."); printf("\n 5.Kaiser."); printf("\nEnter (1,2 etc.) >"); while(((ch=getche())!='1')&&(ch!='2') &&(ch!='3')&&(ch!='4')&&(ch!='5')); yt=wherey(); /* Storing impulse response of FIR filter.*/ again : gotoxy(1,yt+2); printf(" "); gotoxy(1,yt+2); printf("\nEnter file name to store FIR filter coefficients >"); scanf("%s",file_name); if(((stricmp("FFT.DAT",file_name))==0)|| ((stricmp("temp.DAT",file_name))==0)|| ((stricmp("IFFT.DAT",file_name))==0)) { printf("This is a reserved file name. Use some other name."); goto again; } gotoxy(1,yt+2); printf(" "); ind=access(file_name,0); while(!ind) { gotoxy(1,yt+3); 157 printf ( " "); gotoxy(1,yt+3); printf("File exists. Wish to overwrite? (y or n)- ->"); while(((ch1=tolower(getch()))!='y')&&(ch1!='n')); putch(ch1); switch(ch1) { case 'y': ind=1; break; case 'n' : gotoxy(1,yt+3); printf(" "); gotoxy(1,yt+2); printf(" "); gotoxy(1,yt+2); printf("Enter file name >"); scanf("%s",file_name); ind=access(file_name,0); } } fptri=fopen(file_name,"w"); theta=pi/((float)N1*sqrt((double)2.0)); for(i=0;i<N;i++) { ii=(i-N1)*(i-N1); for(j=0;j<N;j++) { jj=(j-N1)-(j-N1); nsq=(float)(ii+jj); nsqrt=sqrt((double)nsq)/sqrt((double)2.0); switch(ch) { case '1': break; case '2': win=0.5+0.5*cos((double)(theta*nsqrt)); . ind=1; break; case 'n' : gotoxy(1,yt+3); printf(" "); gotoxy(1,yt +2) ; printf(" "); gotoxy(1,yt +2) ; printf("Enter file name >");. name."); goto again; } gotoxy(1,yt +2) ; printf(" "); ind=access(file_name,0); while(!ind) { gotoxy(1,yt+3); 157 printf ( " "); gotoxy(1,yt+3); printf("File. gotoxy(1,yt); printf ( " "); ind=access(file_name,0); while(!ind) { gotoxy(1,yt+1); printf ( " "); gotoxy(1,yt+1); printf("File exists. Wish to overwrite? (y

Ngày đăng: 10/07/2014, 22:20

TỪ KHÓA LIÊN QUAN