📄 digital filter code.txt
字号:
#include "iostream.h"
#include "fstream.h"
#include "string.h"
#include "math.h"
void DFB(int N, double signal[],double Xr[],double Xi[],double spec[]);
void bandf(double h[],int Nh);
void conv(double xin[], double h[], int Nh, double xout[]);
void main()
{
ofstream myfile;
char filename[20];
char extname[]=".dat";
cout<<"请输入保存文件名:";
cin>>filename;
strcat(filename,extname);
myfile.open(filename);
const int N=631;
const int Nh=631;
const int lout=631;
double pi=3.1415926;
double /*signal[N],*/Xr[lout],Xi[lout],spec[lout];
double h[Nh];
double xin[lout]; double xout[lout];
int i,m;
double Tp,fs;
Tp=0.01;fs=300;
//for(i=0;i<N;i++)
/*{1,2,0,1,2,3,2,1,2,1,2,3,2,
2,3,2,1,3,3,2,1,3,2,1,3,2,3,2,3,2,1,3,2,
0,3,2,0,3,2,1,0,3,2,0,3,2,1,3,2,1,3,2,1,
3,2,1,3,2,3,0};*/
double signal[631]=/*{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};*/
{3.7493, 4.6798, 3.8025, 4.6638, 1.2380, 3.4825 , 1.4487 , 3.2318 , 2.2772, 4.8320, 1.0516, 2.3844, 4.3789, 4.9308, 3.4079, 1.3558, 4.8068, 2.7732, 1.0569, 2.4996 , 4.0662, 2.2416, 1.6641, 2.4710, 4.2266, 2.0142, 2.0852, 3.5600, 2.8671 , 3.3847 , 4.4712 , 3.6645, 4.1163,
1.6225, 3.4832 , 3.8143 , 4.4938, 2.6363 , 1.9893 , 1.0587 , 4.2648 , 2.4887 , 1.5238 , 2.2292,
1.7645, 3.9251 , 2.9399 , 3.0536 , 2.8962 , 2.4108 , 3.6562 , 4.9084 , 1.2948, 1.3817 , 4.7067,
2.6898 , 1.7756 , 1.4585 , 3.9306 , 4.6360 , 1.7514 , 3.8962 , 1.8876, 1.7994 , 1.0595 , 3.7149,
4.4239, 4.6192 , 3.6594 , 2.6889 , 3.3850 , 2.9626 , 2.1265 , 3.8147 , 1.1980 , 2.1528 , 1.2973,
2.9610 , 3.2768 , 2.4615 , 4.8455, 2.3158 , 2.6371 , 2.0473 , 3.0882 , 3.2668 , 4.2669 , 1.2827,
4.2637 , 3.5272 , 1.5602, 1.2882 , 2.9128 , 2.8541 , 3.8339, 4.7316 , 1.4877 , 4.9419 , 1.0477,
2.8431 , 1.9377, 3.2671 , 3.2136, 3.3887, 3.4438, 4.1354 , 3.8534, 3.0884 , 1.0695 , 1.9086,
2.8294 , 3.1951 , 4.2920 , 2.1679 , 1.6458 , 1.2847 , 4.9446 , 1.9122 , 1.4682, 4.2776 , 3.0650,
2.8028 , 4.7263 , 3.6958 , 4.4319 , 4.3179 , 2.2571 , 2.8934 , 2.7986 , 4.0797 , 3.4846, 2.8328,
2.6489, 2.3408 , 4.9978, 2.3430 , 4.8245 , 3.4335 , 4.6113 , 1.6888 , 2.5002 , 3.2409 , 3.8128,
4.6064, 3.6221 , 4.8465 , 3.7208 , 3.3822 , 1.7001 , 2.8042 , 4.8753 , 4.2935 , 1.9761 , 3.3299,
1.0223, 2.5676 , 1.2354 , 1.2138 , 1.1150 , 3.4841 , 4.2181 , 2.4229 , 1.1865, 4.2880 , 3.0368,
2.1896 , 3.5093 , 2.4412 , 2.4266, 4.2485 , 1.9838 , 4.3155 , 1.1962 , 3.3917, 2.0528 , 1.2972,
1.1966 , 3.7963 , 3.1941 , 2.9932, 3.4405 , 3.3494 , 1.6651 , 4.0214 , 4.7966, 4.0145, 1.7729,
3.7727, 2.5887, 2.0471 , 2.7378 , 3.8060 , 3.0242 , 2.5756 , 4.5793 , 2.1552, 3.6386 , 2.5184,
3.6004, 2.6545, 3.3894, 3.2498 , 1.3688 , 2.8591 , 3.0830 , 2.1446 , 4.5553 , 1.8563 , 2.1057,
4.9320 , 3.6209, 1.1971, 3.4665 , 2.6996 , 3.1657 , 3.8725 , 2.0048 , 1.4063 , 3.4085 ,4.0835,
3.2107 , 4.3503, 3.2842 , 1.4534 , 2.5023 , 4.7693 , 3.2768 , 4.7309 , 1.2613 , 3.4197 , 2.2557,
2.6003 , 2.4864, 3.8034 , 4.5930 , 1.6646 , 2.3670 , 2.8432 , 1.5239 , 1.9372 ,3.6380 ,3.5528,
1.7952 , 2.7010, 4.8492, 4.0182 , 4.3326 , 2.6072 , 2.7812 , 4.7633 , 4.7324 ,1.7335 , 4.9463,
3.5008 , 3.3787, 4.0021 , 4.1645 , 4.3546 , 2.2308 , 1.3510 , 3.8074 , 1.2525 ,3.5462 ,3.0115,
3.9335 , 3.2630, 3.9600 , 4.2598 , 2.8065 , 2.6463 , 2.7739 , 4.3907 , 2.0569 , 1.6812 ,4.7908,
2.5035 , 3.8662, 2.7275 , 3.6800 , 4.8264 , 2.1438 , 2.4652 , 1.8371 , 4.9981 , 3.1584 , 4.3121,
1.0395 , 3.0452, 3.5371 , 1.8035 , 1.5886 , 2.5765 , 2.2101 , 2.8204 , 1.8480 , 3.4936 ,4.6702,
2.6794 , 4.1056, 4.2121 , 2.0924 , 4.4797 , 3.0121 , 4.4074 , 1.3243 , 2.9936 , 3.7436 , 1.4523,
4.0147 , 2.9574, 1.3355 , 3.5049 , 4.0777 ,3.8879 , 4.0379 , 4.4045 , 2.1620 ,3.7094 ,4.2485,
4.1755 , 1.7436, 4.7819 , 3.1474 , 2.7766 , 2.2248 , 4.7990 , 3.2482 , 3.6910 , 4.5073 ,4.6331,4.6331};
/*{-1,-2,0,1,2,3,2,1,-2,1,2,3,2,-2,3,2,1,3,-3,2,1,-3,2,1,3,2,3,2,3,2,1,3,2,
0,3,2,0,3,2,1,0,-3,2,0,-3,2,1,3,-2,1,3,-2,1,3,-2,1,3,2,-3,0};*/
/*{1,2,0,1,2,3,2,1,2,1,2,3,2,
2,3,2,1,3,3,2,1,3,2,1,3,2,3,2,3,2,1,3,2,
0,3,2,0,3,2,1,0,3,2,0,3,2,1,3,2,1,3,2,1,
3,2,1,3,2,3,0}*/;/*{1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,
1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,
1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0};*/
//signal[i]=sin((2*pi)/(Tp*fs)*i);//signal[i]=sin((2*pi/N)*n)
//DFB(N,signal,Xr,Xi,spec);
/*for(j=0;j<N;j++)
{
cout<<"spec["<<j<<"]="<<spec[j]<<endl;
myfile<<j<<" "<<spec[j]<<endl;
}*/
bandf(h,Nh);
for(m=0;m<Nh;m++)
{
myfile<<m<<" "<<h[m]<<endl;
}
conv(signal,h,Nh,xout);
for(i=0;i<lout;i++)
{
cout<<"signal[n]*h[n]="<<xout[i]<<endl;
myfile<<i<<" "<<xout[i]<<endl;
xin[i]=xout[i];
}
DFB(lout,xin,Xr,Xi,spec);
for(m=0;m<lout;m++)
{
cout<<"spec["<<m<<"]="<<spec[m]<<endl;
myfile<<m<<" "<<spec[m]<<endl;
}
}
void DFB(int N, double x[],double Xr[],double Xi[],double spec[])
{
double pi=3.1415926;
int n,k;
double realsum,imagsum;
for(k=0;k<N;k++)
{
realsum=0.0;
imagsum=0.0;
for(n=0;n<N;n++)
{
realsum+=x[n]*cos((2*pi/N)*n*k);
imagsum+=x[n]*sin((2*pi/N)*n*k);
}
Xr[k]=realsum;
Xi[k]=imagsum;
spec[k]=sqrt(Xr[k]*Xr[k]+Xi[k]*Xi[k]);
}
}
//////
void bandf(double h[],int Nh)
{
int n,a;
double pi=3.1415926;
double wc,wo;
a=(Nh-1)/2;
wc=0.2*pi;
wo=0.5*pi;
for(n=0;n<Nh;n++)
{
if(n!=a)
h[n]=(0.54-0.46*cos(2*pi*n/(Nh-1)))*(2/(pi*(n-a)))*sin((n-a)*wc)*cos((n-a)*wo);
else
h[n]=(0.54-0.46*cos(2*pi*n/(Nh-1)))*(2*wc/pi)*cos((n-a)*wo);
cout<<"h["<<n<<"]="<<h[n]<<endl;
}
}
/////
void conv(double xin[], double h[], int Nh, double xout[])
{
int n,m;
double sum;
for (n = 0; n <Nh; n++)
{
sum = 0.0;
for(m = 0; m <Nh; m++)
{
if(n-m>=0)
sum+= xin[m]*h[n-m];
else
sum+=xin[m]*h[Nh+n-m];
}
xout[n]=sum;
}
for(n=0;n <Nh; n++)
{
cout<<"xin[n]*h[n]="<<xout[n]<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -