#include "graphics.h"
#include "math.h"
#include "dos.h"
#include "conio.h"
#include "stdio.h"
#include "mydefine.h"
#include "msp.h"
#include "keys.h"
#include "mouse.h"
#define SIZE 512
complex data[SIZE];
double sdata[SIZE],x_max=-5.0,y_max=-5.0;
int arrysize=SIZE,y_location[SIZE],x_location[SIZE],gridflag=0,fftflag;
void showdataf(double *data,int size);
void setupAxis()
{
int i,j;
setcolor(AXISCOLOR);
line(0,X_AXIS,X_MAX,X_AXIS);
i=Y_AXIS;
for(j=1;i<X_MAX;j++)
{
i+=JIANGE;
if(j%5!=0)
{
line(i,X_AXIS,i,X_AXIS-3);
}
else
{
line(i,X_AXIS,i,X_AXIS-5);
}
}
line(Y_AXIS,0,Y_AXIS,HELPLINE);
j=X_AXIS;
for(i=1;j<HELPLINE;i++)
{
j-=JIANGE;
if(i%5!=0)
{
line(Y_AXIS,j,Y_AXIS+3,j);
}
else
{
line(Y_AXIS,j,Y_AXIS+5,j);
}
}
}
void clc()
{
int x1=Y_AXIS,x2=Y_AXIS+DAJIANGE*XNUM,y1=X_AXIS-DAJIANGE*YNUM,y2=X_AXIS;
setfillstyle(1, BGCOLOR);
bar(x1-5,y1-5,x2+5,y2+5);
setupAxis();
}
void Gridon()
{
int i,j;
setcolor(GRIDCOLOR);
setlinestyle(DASHED_LINE,NORM_WIDTH,0);
i=Y_AXIS;
for(j=1;j<=XNUM*5;j++)
{
i+=DAJIANGE;
if(j%5!=0)
{
line(i,X_AXIS-5,i,X_AXIS-5*JIANGE*YNUM);
}
else
{
line(i,X_AXIS-7,i,X_AXIS-5*JIANGE*YNUM);
}
}
j=X_AXIS;
for(i=1;i<=YNUM*5;i++)
{
j-=DAJIANGE;
if(i%5!=0)
{
line(Y_AXIS+5,j,Y_AXIS+5*JIANGE*XNUM,j);
}
else
{
line(Y_AXIS+7,j,Y_AXIS+5*JIANGE*XNUM,j);
}
}
}
int datamax(double *data,int size)
{
int i;
double m=0;
if(-1==fftflag)
{
for(i=0;i<size;i++)
{
if(data[i]>data[m])
m=i;
}
}
else
{
for(i=0;i<size;i+=4)
{
if(data[i]>data[m])
m=i;
}
}
return m;
}
void setYAxisText(double max)
{
int i,y,x;
char s[6];
double data;
if(fabs(y_max-max)<1)
return;
y_max=max;
setcolor(BGCOLOR);
bar(0,0,Y_AXIS-3,X_AXIS-3);
setcolor(GREEN);
x=Y_AXIS-55;
for(i=1;i<=YNUM;i++)
{
y=X_AXIS-i*DAJIANGE;
data=max*i/YNUM;
sprintf(s,"%5.2f",data);
outtextxy(x,y,s);
}
}
void setXAxisText(double max)
{
int i,y,x;
char s[6];
double data;
if(fabs(x_max-max)<1)
return;
x_max=max;
setcolor(BGCOLOR);
bar(Y_AXIS+3,X_AXIS+3,X_MAX,HELPLINE);
setcolor(GREEN);
outtextxy(Y_AXIS-10,X_AXIS+5,"O");
y=X_AXIS+10;
for(i=1;i<=XNUM;i++)
{
x=Y_AXIS+i*DAJIANGE-20;
data=max*i/XNUM;
if(max<10)
sprintf(s,"%3.2f",data);
else
sprintf(s,"%d",(int)data);
outtextxy(x,y,s);
}
}
void showdatat(double *data,int size)
{
int jiange=(Y_AXIS+5*JIANGE*XNUM)/size;
int lx,ly,i,Y;
double data_max=data[datamax(data,size)];
setYAxisText(data_max);
Y=5*JIANGE*YNUM;
setlinestyle(0,1,0);
for(i=0;i<size;i+=4)
{
lx=Y_AXIS+i*jiange;
ly=X_AXIS-Y*data[i]/y_max;
x_location[i]=lx;
y_location[i]=ly;
/*line*/
setcolor(GREEN);
line(lx,X_AXIS,lx,ly-1);
/*circle*/
setcolor(12);
circle(lx,ly,1);
}
}
void showdataf(double *data,int size)
{
int jiange=(Y_AXIS+5*JIANGE*XNUM)/size;
int x,y,lx,ly,i,Y;
double data_max=data[datamax(data,size)];
setYAxisText(data_max);
Y=5*JIANGE*YNUM;
lx=Y_AXIS;
ly=X_AXIS-Y*data[0]/data_max;
y_location[0]=ly;
x_location[0]=lx;
setcolor(RED);
setlinestyle(0,1,0);
for(i=1;i<size;i++)
{
x=lx+jiange;
y=X_AXIS-Y*data[i]/data_max;
line(lx,ly,x,y);
lx=x;
ly=y;
x_location[i]=lx;
y_location[i]=ly;
}
}
void showval(int x,int y,int seq,int size)
{
int x1,y1,x2,y2;
char s[12];
float data;
if(sdata[seq]<2*y_max/3)
x1=x+3,y1=y-33,x2=x+83+12,y2=y-3;
else
x1=x+5,y2=y+33,x2=x+83+12,y1=y+5;
bar(x1,y1,x2,y2);
setcolor(LIGHTCYAN);
rectangle(x1,y1,x2,y2);
setcolor(GREEN);
data=seq*x_max/size;
sprintf(s," x = %3.2f",data);
outtextxy(x1+2,y1+5,s);
data=sdata[seq];
sprintf(s," y = %3.2f",data);
outtextxy(x1+2,y2-10,s);
}
int showval_m()
{
int x_jiange,i,j,x,y,size;
if(-1==fftflag)
{
size=SIZE/2;
for(i=0;i<SIZE;i++)
{
if(x_location[i]>=MouseX)
{
x=x_location[i];
y=y_location[i];
break;
}
}
}
else
{
size=SIZE;
for(i=0;i<SIZE;i+=4)
{
if(x_location[i]>=MouseX)
{
x=x_location[i];
y=y_location[i];
break;
}
}
}
clc();
if(gridflag)
Gridon();
if(-1==fftflag)
{
showdataf(sdata,size);
}
else
{
showdatat(sdata,size);
}
setcolor(BLUE);
circle(x,y,3);
showval(x,y,i,size);
return i;
}
void backward(int *seq,int size,int n)
{
int x_jiange,x,y;
clc();
if(gridflag)
Gridon();
if(-1==fftflag)
{
showdataf(sdata,size);
*seq=*seq-n;
}
else
{
showdatat(sdata,size);
*seq=*seq-4*n;
}
x_jiange=XNUM*DAJIANGE/size;
x=Y_AXIS+(*seq)*x_jiange;
y=y_location[*seq];
setcolor(BLUE);
circle(x,y,3);
showval(x,y,*seq,size);
}
void forward(int *seq,int size,int n)
{
int x_jiange,x,y;
clc();
if(gridflag)
Gridon();
if(-1==fftflag)
{
showdataf(sdata,size);
*seq=*seq+n;
}
else
{
showdatat(sdata,size);
*seq=*seq+4*n;
}
x_jiange=XNUM*DAJIANGE/size;
x=Y_AXIS+(*seq)*x_jiange;
y=y_location[*seq];
setcolor(BLUE);
circle(x,y,3);
showval(x,y,*seq,size);
}
void getsystem()
{
double numd[10],dend[10],a;
int i,j,num_size=2,den_size=3,hsize=SIZE/2;
complex num,den,ct,ct2;
numd[0]=2;
numd[1]=4;
dend[0]=1;
dend[1]=2;
dend[2]=3;
num.real=0;
num.imag=0;
den.real=0;
den.imag=0;
ct2.real=0;
printf("please input the length of your numerator\n");
scanf("%d",&num_size);
printf("please input the length of your denominator\n");
scanf("%d",&den_size);
for(i=0;i <num_size;i++)
{
printf("please input your %dth numerator\n",i+1);
scanf( "%lf",&numd[i]);
}
for(i=0;i <den_size;i++)
{
printf("please input your %dth denominator\n",i+1);
scanf( "%lf",&dend[i]);
}
for(i=0;i<SIZE;i++)
{
den.real=0;
den.imag=0;
num.real=0;
num.imag=0;
for(j=0;j<num_size;j++)
{
ct2.imag=-2*j*PI*i/SIZE;
ct=cexp(ct2);
num.real+=numd[j]*ct.real;
num.imag+=numd[j]*ct.imag;
}
for(j=0;j<den_size;j++)
{
ct2.imag=-2*j*PI*i/SIZE;
ct=cexp(ct2);
den.real+=dend[j]*ct.real;
den.imag+=dend[j]*ct.imag;
}
a=den.real*den.real+den.imag*den.imag;
if(a)
{
data[i].real=(num.real*den.real+num.imag*den.imag)/a;
data[i].imag=(den.real*num.imag-den.imag*num.real)/a;
}
else
{
data[i].real=0;
data[i].imag=0;
}
}
}
void getdata()
{
int i;
double y;
FILE *f;
f= fopen( "data.txt", "r" );
for(i=0;i<SIZE;i++)
{
fread(&y,sizeof(y),1,f);
data[i].real=y;
data[i].imag=0;
}
fclose(f);
}
void main()
{
int i,j,x,y,driver=DETECT,mode,data_seq=4*3;
int key=0,loops=0,direction=0,bk=BGCOLOR;
char c;
CTLKEYS ck;
char *help="F1->grid ON/OFF F2->FFT F3->restart L/R->shift TAB->accelerate";
initgraph(&driver,&mode,"");
setbkcolor(BGCOLOR);
cleardevice();
MouseMath();/*计算鼠标形状,一开始必须使用,后面就不用了*/
MouseSetY(0,479);
MouseSetX(0,639);
MouseSetXY(100,100);
MouseOn(MouseX,MouseY);
init:
setfillstyle(1,BGCOLOR);
printf("which mod would you like to choose:\n 1 for system analy