#include "math.h" #include "graphics.h" #include "stdlib.h" #include "dos.h" #include "ctype.h" #define PI 3.1415926 #define TRUE 1 #define FALSE 0 #define KB_S_N_A 30 #define KB_S_N_B 48 #define KB_S_N_C 46 #define KB_S_N_D 32 #define KB_S_N_X 45 #define KB_S_N_ESC 1 #define N1 32 #define N2 128 /* DEFINE TYPES */ typedef struct { float (*f_name)(); /* Fuction pointer */ } fuction; /* DEFINE FUCTIONS */ float temp(float t); float h(float t,float n_max); float kaiser(float t,float n_max); float biser(float t); float lpf(float t,float n_max); double power(double x,int y); int get_key(); fuction get_fuction_name(int key,float *p_n_max); /* n_max is the range of x */ void show_face(); void draw_fuction(fuction f,float n_max,int flag); int distance_caculate(float n_max,int *add_on,int *distance);/* caculat the distance */ int quit=FALSE; char name_str[50]; /*/////////////////////////////////////////////////////////////////////////*/ /* Main Program */ /*/////////////////////////////////////////////////////////////////////////*/ main() { int graphdriver=VGA; int graphmode=VGAHI; int key,flag=FALSE; float n_max=0; fuction fuction_name; /* fuction pointer */ fuction_name.f_name=NULL; /* Install graph driver */ initgraph(&graphdriver,&graphmode,""); cleardevice(); setviewport(0,0,640,480,1); setbkcolor(7); show_face(); clearviewport(); do { outtextxy(60,420,"Press to view fuction H(n) in N1=32 to H(n) in N2=128"); outtextxy(100,450,"Press to change view type. Press to quit."); do {key=get_key();} while(!(key==KB_S_N_A||key==KB_S_N_B||key==KB_S_N_ESC||key==KB_S_N_D)); if(key==KB_S_N_ESC) {quit=TRUE;} else { if(key==KB_S_N_D) { flag=!flag; clearviewport(); draw_fuction(fuction_name,n_max,flag); } else { fuction_name=get_fuction_name(key,&n_max); clearviewport(); draw_fuction(fuction_name,n_max,flag); } } } while(quit==FALSE); closegraph(); } /*/////////////////////////////////////////////////////////////////////////*/ /* Main Program end. */ /*/////////////////////////////////////////////////////////////////////////*/ void show_face() { clearviewport(); outtextxy(240,140,"DSP LPF Viewer Demo"); outtextxy(270,180,"Version 1.0"); outtextxy(290,300,"UESTC"); outtextxy(265,340,"97014-1 No.13"); outtextxy(282,320,"Peng Tao"); outtextxy(210,400,"Press any key to continue..."); getch(); } /*////////////////////////////////////////////////////////////////////*/ void draw_fuction(fuction f,float n_max,int flag) /* f is some fuction's*/ { /* pointer ;flag is to*/ char s[10]; /*change the view type*/ int i,j,add_on,x_base=80,y_base=220,distance; /*and n_max is the range*/ float k,time; /*of the (t) */ int x,y; char str[20]; /*///////////////////////////////////////*/ /* Draw surroundings*/ /*///////////////////////////////////////*/ setcolor(WHITE); rectangle(x_base,y_base-200,x_base+480,y_base+200); line(x_base,y_base,x_base+480,y_base); distance_caculate(n_max,&add_on,&distance); /* caculate the draw distance */ for(i=x_base+distance,j=0;i<=x_base+480;i=i+distance) { line(i,y_base,i,y_base+10); sprintf(s,"%d",j+add_on); outtextxy(i-4,y_base+15,s); j=j+add_on; } k=-2.0; /* k is Y_max */ for(j=-200;j<=200;j=j+20) { line(x_base-10,y_base-j,x_base,y_base-j); if(k>=0) { sprintf(s,"%2.1f",k); outtextxy(x_base-35,y_base-j-3,s); } else { sprintf(s,"-%2.1f",(float)fabs(k)); outtextxy(x_base-43,y_base-j-3,s); } k=k+0.2; } outtextxy(x_base+490,y_base,"Times"); settextstyle(0,1,1); outtextxy(30,40,"Values"); settextstyle(0,0,1); /*//////////////////////////////////////////*/ /* Draw fuction points */ /*//////////////////////////////////////////*/ /* n_max is the unmber of points be drawn*/ if(!f.f_name==NULL) { for(time=0;time<=n_max;time++) { x=x_base; y=y_base; x=x+(int)(time*distance/add_on); y=y-(int)((*f.f_name)(time)*100); if(!flag==FALSE) { setcolor(12); line(x,y,x,y_base); } setcolor(1); setfillstyle(1,1); circle(x,y,2); floodfill(x,y,1); } } } /*//////////////////////////////////////////////////////////////////*/ int get_key() { union REGS rg; rg.h.ah=0; int86(0x16,&rg,&rg); return rg.h.ah; } /*//////////////////////////////////////////////////////////////////*/ fuction get_fuction_name(int key,float *p_n_max) { fuction f; switch(key) { case KB_S_N_A: /* PRESS A KEY */ f.f_name=temp; *p_n_max=(N1-1); break; case KB_S_N_B: f.f_name=temp; *p_n_max=(N2-1); break; case KB_S_N_C: /* No codes here. */ break; default: break; }; return f; } /*////////////////////////////////////////////////////////////////*/ int distance_caculate(float n_max,int *add_on,int *distance) { /* caculate the distance etc. */ int success=TRUE; if((n_max<0)||(n_max>168)) { success=FALSE;return success; } else { if(n_max<=24) { *add_on=1;*distance=20; } else { if(n_max<=(24*2)) { *add_on=2;*distance=20; } else { if(n_max<=(24*3)) { *add_on=3;*distance=20; } else { if(n_max<=(24*4)) { *add_on=4;*distance=20; } else { if(n_max<=(24*5)) { *add_on=5;*distance=20; } else { *add_on=10;*distance=25; } } } } } } return success; } /*////////////////////////////////////////////////////////////////*/ /* Math Fuctions to run */ /*////////////////////////////////////////////////////////////////*/ float lpf(float t,float n_max) { float wc=PI/8; return( (float)( (sin(wc*(t-n_max/2)))/(PI*(t-n_max/2)) ) ); } float biser(float x) { int m=10,k; float i=1,b=1.0; for(k=1;k<=m;k++) { i=i*k; b=b+(float)power(x/2,2*k)/i/i; } return b; } float kaiser(float t,float n_max) { float s,beta=5.44; s=beta*(float)sqrt(1-(1-2*t/n_max)*(1-2*t/n_max)); return(biser(s)/biser(beta)); } float h(float t,float n_max) { return(kaiser(t,n_max)*lpf(t,n_max)); } double power(double x,int y) { double p=1.0; int i; for(i=1;i<=y;i++) { p=p*x; } return p; } float temp(float t) { return(t-1.5); }