/***********************************************************************************************/
double *bcg(double ap,double as,double wp,double ws,int *n,double *h,int *type);
struct rptr *bsf(double *c,int ni,double *f1,double *f2,int nf,struct rptr *ptr,int *no);
double *pnpe(double *a,int m,int n,double *b,int *mn);
double *ypmp(double *a,int m,double *b,int n,double *c,int *mn);
void lowpass_input(double *wp,double *ws,double *ap,double *ar,double *f1,double *f2,int *nf);
void highpass_input(double *wp,double *ws,double *ap,double *ar,double *f1,double *f2,int *nf);
void bandpass_input(double *wp,double *ws,double *ap,double *ar,double *f1,double *f2,int *nf);
void draw_image(double *x,int m,char *title1,char *title2,char *xdis1,char *xdis2,int dis_type);
/***********************************************************************************************/
void main(void)
{
double *f1,*f2,*hwdb;
double *h=NULL;
int N,nf,ns,nz,i,j,k,ftype,type;
COMPLEX hwdb1,hwdb2;
double wp,ws,ap,as,jw,amp1,amp2;
char title[80],tmp[20];
struct rptr *ptr=NULL;
/* printf("%lf",PI);
getch();*/
N=500;
f1=(double *)calloc(4,sizeof(double));
f2=(double *)calloc(4,sizeof(double));
hwdb=(double *)calloc(N+2,sizeof(double));
if (hwdb==NULL){
printf("\n Not enough memory to allocate!");
exit(0);
}
printf("\n1.Lowpass 2.Highpass 3.bandpass");
printf("\nPlease select the filter type:");
scanf("%d",&ftype);
switch(ftype){
case 1:lowpass_input(&wp,&ws,&ap,&as,f1,f2,&nf);
break;
case 2:highpass_input(&wp,&ws,&ap,&as,f1,f2,&nf);
break;
case 3:bandpass_input(&wp,&ws,&ap,&as,f1,f2,&nf);
break;
default:lowpass_input(&wp,&ws,&ap,&as,f1,f2,&nf);
}
h=bcg(ap,as,wp,ws,&ns,h,&type);
printf("\nThe analog filter denominator coefficients of Ha(s):");
for(i=0;i<=ns;i++)
printf("\nb[%2d]=%16f",i,h);
ptr=bsf(h,ns,f1,f2,nf,ptr,&nz);
printf("\nThe digital filter coefficients of H(z):");
printf("\n(a is numerator coefficient. b is denominator coefficient.)");
for(i=0;i<=nz;i++)
printf("\na[%2d]=%10f,b[%2d]=%16f",i,ptr->a,i,ptr->b);
printf("\n\nPress any key to calculate the filter response of H(z)...");
getch();
printf("\nWaitting for calculating...");
/* Calculate the magnitude-frequency response*/
for(k=0;k<=N;k++){
jw=k*PI/N;
hwdb1.real=0;hwdb1.image=0;
hwdb2.real=0;hwdb2.image=0;
for(i=0;i<=nz;i++){
hwdb1.real+=ptr->a*cos(i*jw);
hwdb2.real+=ptr->b*cos(i*jw);
hwdb1.image+=ptr->a*sin(i*jw);
hwdb2.image+=ptr->b*sin(i*jw);
}
amp1=(pow(hwdb1.real,2)+pow(hwdb1.image,2));
amp2=(pow(hwdb2.real,2)+pow(hwdb2.image,2));
if(amp1==0) amp1=1e-90;
if(amp2==0) amp2=1e-90;
hwdb[k]=10*log10(amp1/amp2);
if(hwdb[k]<-200) hwdb[k]=-200;
if(k%10==9) printf("*");
}
strcpy(title,"Transfer Property");
if(type==1) strcat(title,"(BW Type) N=");
if(type==2) strcat(title,"(CB Type) N=");
itoa(ns,tmp,10);
strcat(title,tmp);
strcpy(tmp,"PI(rad)");
draw_image(hwdb,N,title,"The Attenuation(dB)","0",tmp,0);
free(ptr->b);
free(ptr->a);
free(h);
free(hwdb);
free(f2);
free(f1);
}
/***************************************************************/
|