📄 dft.cpp
字号:
#include"stdio.h"
#include "math.h"
#include "graphics.h"
#include "stdlib.h"
#include <conio.h>
#define PI 3.1416
#define N 256
// DFT using common method
float C[N],S[N];//array for storing cos and sin value
void main()
{
int i,n,k;
unsigned long int t; // this is the key point--unsiged int
float x[N]={0},xkr[N]={0},xki[N]={0},xk[N]={0};
char ch[50],cha;
void draw(float data[],int num,int ratio,char ch[]);
void DFT(float x[],float xkr[],float xki[],float xk[]);
for(i=0;i<N;i++)
{
C[i]=cos(2*PI*i/N);
S[i]=sin(2*PI*i/N);
}
for(i=0;i<200;i++)
{
x[i]=sin(PI*i/N);
}
DFT(x,xkr,xki,xk);
sprintf(ch,"x[n]");
draw(x,N,50,ch);
sprintf(ch,"|xk[N]|");
draw(xk,N,2,ch);
}
void DFT(float x[],float xkr[],float xki[],float xk[])
{
int n,k;
unsigned long int t;
for(k=0;k<N;k++)
{
for(n=0;n<N;n++)
{
t=n*k;
xkr[k]=xkr[k]+x[n]*C[t%N];
xki[k]=xki[k]+x[n]*S[t%N];
}
xk[k]=sqrt(xkr[k]*xkr[k]+xki[k]*xki[k]);
xkr[k]=0; xki[k]=0;// clear intermedia value
}
}
void draw(float data[],int num,int ratio,char ch[])
{
int i,xb,xe,yb,ye,oy;
int gdriver = DETECT,gmode;
char buff[50];
initgraph(&gdriver,&gmode,"d:\\tc30\\bgi");
cleardevice();
setbkcolor(0); //set the background black
setcolor(30); //set the axis yellow
xb=20;xe=630;yb=50; ye=450; oy=(yb+ye)/2;
yb=20;
line(xb,oy,xe,oy); //draw x axis
outtextxy(xe,oy-2,">");
outtextxy(xe-5,oy+3,"n");
line(xb,yb,xb,ye); //dray Y axis
outtextxy(xb-3,yb-2,"^");
outtextxy(xb+3,yb-2,ch);
for(i=0;i<4;i++)
{
line(xb,oy-i*50,xb+3,oy-i*50);
sprintf(buff,"%d",i);
outtextxy(xb-10,oy-i*50,buff);
}
for(i=0;i<610;i++)
{
if(i==40*int(i/40))
{
line(xb+i,oy+5,xb+i,oy);
sprintf(buff,"%d",i/2);
if(i!=0)
outtextxy(xb+i-5,oy+10,buff);
}
}
for(i=1;i<num;i++)
{
setcolor(12);
// line(xb+(i*2-1), oy-data[i-1]*ratio,xb+i*2,oy-data[i]*ratio);
line(xb+(i*2-2), oy,xb+i*2-2,oy-data[i]*ratio); //line graph
}
getchar();
closegraph();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -