1、数字信号处理上机实验MATLAB程序及结果研究生数字信号处理(dsp)上机MATLAB实验实验1熟悉MATLAB环境,会用FFT求信号频谱,相关方法去除噪声。%生成一个带有噪声的正弦信号clear;t=linspace(-2*pi,2*pi,1000);y=sin(t)+rand(1,1000);plot(y);波形图:%把带有噪声的正弦信号进行FFT分解z=fft(y); plot(t,z);波形图:%用相关方法除噪声f=xcorr(y,y);plot(f);波形图:实验2利用相关算法求信号的幅值和相位%用相关性求信号的幅值与相位clear;t=linspace(-2*pi,2*pi,100
2、0);s=5*sin(t+pi/6)+rand(1,1000);y=3*sin(t)+rand(1,1000);ss=s.*s;sy=s.*y;yy=y.*y;x1=length(ss);x2=length(yy);x3=length(sy);rx0=sum(ss)/x1;ry0=sum(yy)/x2;rxy0=sum(sy)/x3;A=sqrt(2*rx0);B=sqrt(2*ry0);C=acos(2*rxy0/(A*B);sprintf(A=%d,B=%d,C=%d,A,B,C)命令窗口输出结果:A=5.086632e+00,B=3.109529e+00,C=5.355640e-01%用
3、改进的相关算法求幅值和相位clear;t=linspace(-2*pi,2*pi,1000);s=5*sin(t+pi/6)+rand(1,1000);z=3*sin(t);z1=3*cos(t);zz=z.*z;sz=s.*z;sz1=s.*z1;x1=length(sz);x2=length(zz);x3=length(sz1);rz=sum(zz)/x2;rsz=sum(sz)/x1;rsz1=sum(sz1)/x3;B=sqrt(2*rz);C=atan(rsz1/rsz);A=2*rsz/(B*cos(C);sprintf(A=%d,B=%d,C=%d,A,B,C)命令窗口输出结果:
4、A=4.986266e+00,B=2.998500e+00,C=5.264407e-01实验三设计一个巴尔低通滤波器%设计一个巴尔沃特低通滤波器,fp=1khz,fs=2khz,ap=1db,as=40db;fp=1000;fs=2000;wp=2*pi*fp;ws=2*pi*fs;ap=1;as=40;n,wc=buttord(wp,ws,ap,as,s);fprintf(阶数为%dn,n);num,den=butter(n,wc,s);%z,p,k=buttap(n);disp(分子多项式系数分别为);fprintf(%.4en,num);disp(分母多项式系数分别为);fprintf(
5、%.4en,den);w=wp ws;h1=freqs(num,den,w);fprintf(Ap=%.4en,-20*log10(abs(h1(1);fprintf(As=%.4en,-20*log10(abs(h1(2);w1=0:200:12000*pi;h=freqs(num,den,w1);gain=20*log10(abs(h);plot(w1/(2*pi),gain);xlabel(Hz);ylabel(dB);title(幅频特性);输出结果:阶数为8分子多项式系数分别为0.0000e+000.0000e+000.0000e+000.0000e+000.0000e+000.00
6、00e+000.0000e+000.0000e+006.2187e+30分母多项式系数分别为1.0000e+003.6222e+046.5603e+087.7093e+126.4060e+163.8498e+201.6360e+244.5108e+276.2187e+30Ap=6.1668e-01As=4.0000e+01图形为实验四 设计一个高通滤波器%设计一个高通滤波器fp=5khz,fs=1khz,ap=40db;fp=5000;fs=1000;ap=1;as=40;wp=1/(2*pi*fp);ws=1/(2*pi*fs);n,wc=buttord(wp,ws,ap,as,s);dis
7、p(阶数为);disp(n);num,den=butter(n,wc,s);numt,dent=lp2hp(num,den,1);disp(高通滤波器分子多项式系数);fprintf(%.4en,numt)disp(高通滤波器分母多项式系数);fprintf(%.4en,dent)w=1/wp 1/ws;h1=freqs(numt,dent,w);fprintf(Ap=%.4en,-20*log10(abs(h1(1)fprintf(Bp=%.4en,-20*log10(abs(h1(2)w1=0:200:12000*pi;h=freqs(numt,dent,w1);gain=20*log10
8、(abs(h);plot(w1/(2*pi),gain);xlabel(频率);ylabel(幅值);输出结果:阶数为 4高通滤波器分子多项式系数1.0000e+000.0000e+000.0000e+000.0000e+000.0000e+00高通滤波器分母多项式系数1.0000e+005.1920e+041.3478e+092.0497e+131.5585e+17Ap=1.0977e-01Bp=4.0000e+01 图形实验5 用FFT设计低通滤波器%用fft变换设计低通滤波器clear;N=100;n=0:(N-1);fs=100;%采样频率f1=1;f2=10;y=sin(2*pi*f
9、1/fs*n)+sin(2*pi*f2/fs*n);%原始信号subplot(211);plot(2*pi*f2/fs*n,y,k);%显示波形xlabel(时间);ylabel(幅值);title(原始信号);ffty=fft(y);%进行快速傅里叶变换ffty1=ffty;ffty1(f2/(fs/N)+1)=0;%f2频率对应的幅值为零ffty1(N-f2)/(fs/N)+1)=0;%f2/2频率对应f2的对称频率值对应幅值为零yy=ifft(ffty1);%逆fft变换subplot(212);plot(2*pi*f2/fs*n,yy);xlabel(时间);ylabel(幅值);ti
10、tle(滤波以后的低频信号);输出图形为实验六 用FFT高通滤波器设计%用fft变换设计低通滤波器clear;N=100;n=0:(N-1);fs=100;%采样频率f1=1;f2=10;y=sin(2*pi*f1/fs*n)+sin(2*pi*f2/fs*n);%原始信号subplot(211);plot(2*pi*f2/fs*n,y,k);%显示波形xlabel(时间);ylabel(幅值);title(原始信号);ffty=fft(y);%进行快速傅里叶变换ffty1=ffty;ffty1(f1/(fs/N)+1)=0;%f2频率对应的幅值为零ffty1(N-f1)/(fs/N)+1)=
11、0;%f2/2频率对应f2的对称频率值对应幅值为零yy=ifft(ffty1);%逆fft变换subplot(212);plot(2*pi*f2/fs*n,yy);xlabel(时间);ylabel(幅值);title(滤波以后的高频信号);输出图形为实验7 用双线性设计数字滤波器%用双线性设计数字滤波器,wp=0.2pi,ws=0.6Pi,ap=2db,as=15db;wp=0.2*pi;ws=0.6*pi;fs=1/2;ap=2;as=15;Wp=2*fs*tan(wp/2);Ws=2*fs*tan(ws/2);n,wc=buttord(Wp,Ws,ap,as,s);num,den=but
12、ter(n,wc,s);numz,denz=bilinear(num,den,fs);disp(分子多项式的系数);fprintf(%.4en,numz);disp(分母多项式的系数);fprintf(%.4en,denz);w=wp ws;h=freqz(numz,denz,w);fprintf(ap=%.4fn,-20*log10(abs(h(1);fprintf(as=%.4fn,-20*log10(abs(h(2);w=linspace(0,2*pi,1024);h=freqz(numz,denz,w);gain=20*log10(abs(h);plot(w/pi,gain);xlab
13、el(Normalized frequency);ylabel(Gain,dB);axis(0 1 -50 0);grid;输出结果分子多项式的系数1.5777e-013.1555e-011.5777e-01分母多项式的系数1.0000e+00-6.0620e-012.3730e-01ap=0.3945as=15.0000图形为实验8切比雪夫1型设计低通和高通滤波器%用切比雪夫1型设计低通滤波器%wp=2*pi*1000,ws=2*pi*2000,ap=1,as=40;clear;wp=2*pi*1000,ws=2*pi*2000,ap=1,as=40;n,wc=cheb1ord(wp,ws,
14、ap,as,s);num,den=cheby1(n,ap,wc,s);disp(分子多项式的系数);fprintf(%.4fn,num)disp(分母多项式的系数);fprintf(%.4fn,den)w=wp ws;h=freqs(num,den,w);fprintf(ap=%.4fn,-20*log10(abs(h(1);fprintf(as=%.4fn,-20*log10(abs(h(2);w=0:200:12000*pi;h=freqs(num,den,w);gain=20*log10(abs(h);plot(w/(2*pi),gain);输出结果分子多项式的系数0.00000.000
15、00.00000.00000.00001202796127888840400.0000分母多项式的系数1.00005886.214566671782.4829241699153902.8650904788863911639.00001202796127888840400.0000ap=1.0000as=45.3060图形%用切比雪夫1型设计高通滤波器wp=2*pi*5000,ws=2*pi*1000,ap=1,as=40;wp=1/wp;ws=1/ws;n,wc=cheb1ord(wp,ws,ap,as,s);num,den=cheby1(n,ap,wc,s);numz,denz=lp2hp(
16、num,den,1);w=1/wp 1/ws;h=freqs(numz,denz,w);fprintf(ap=%.4en,-20*log10(abs(h(1);fprintf(as=%.4en,-20*log10(abs(h(2);w=0:200:12000*pi;h=freqs(numz,denz,w);gain=20*log10(abs(h);plot(w/(2*pi),gain);输出结果ap=1.0000e+00as=4.7847e+01输出图形实验9 用切比雪夫2型设计低通和高通滤波器%用切比雪夫2型设计低通滤波器%wp=2*pi*1000,ws=2*pi*2000,ap=1,as=
17、40;clear;wp=2*pi*1000,ws=2*pi*2000,ap=1,as=40;n,wc=cheb2ord(wp,ws,ap,as,s);num,den=cheby2(n,as,wc,s);disp(分子多项式的系数);fprintf(%.4fn,num)disp(分母多项式的系数);fprintf(%.4fn,den)w=wp ws;h=freqs(num,den,w);fprintf(ap=%.4fn,-20*log10(abs(h(1);fprintf(as=%.4fn,-20*log10(abs(h(2);w=0:200:12000*pi;h=freqs(num,den,w
18、);gain=20*log10(abs(h);plot(w/(2*pi),gain);输出结果分子多项式的系数0.0000566.39190.0000290688415499.92920.000029837909134910329000.0000分母多项式的系数1.000024344.5782296168842.79422252800523365.803210820843108280358.000029837909134910292000.0000ap=1.0000as=44.1570图形%用切比雪夫1型设计高通滤波器wp=2*pi*5000;ws=2*pi*1000; ap=1;as=40;
19、wp=1/wp;ws=1/ws;n,wc=cheb2ord(wp,ws,ap,as,s);num,den=cheby2(n,as,wc,s);numz,denz=lp2hp(num,den,1);w=1/wp 1/ws;h=freqs(numz,denz,w);fprintf(ap=%.4en,-20*log10(abs(h(1);fprintf(as=%.4en,-20*log10(abs(h(2);w=0:200:12000*pi;h=freqs(numz,denz,w);gain=20*log10(abs(h);plot(w/(2*pi),gain);xlabel(频率);ylabel(
20、幅值);title(幅频特性曲线);输出结果ap=1.0000e+00as=4.4779e+01输出图形实验10画一个信号的频谱图%画频谱图clear;fs=1000;f1=50;f2=100;f3=200;N=100t=0:N-1;data=25+22*sin(2*pi*f1/fs*t)+30*sin(2*pi*f2/fs*t)+50*sin(2*pi*f3/fs*t);%原始信号y = fft(data);yabs =abs(y);n = 0:N/2;yabs(1)=yabs(1)/2;stem(n*fs/N,2*yabs(1:N/2+1)/N);频谱图实验11 数字高通滤波器的设计%数字
21、高通滤波器的设计,ws=0.2pi,wp=0.6pi,as=15,ap=2;用巴特沃斯滤波器设计。wp=0.6*pi;ws=0.2*pi;ap=2;as=15;t=2;fs=1/t;wp=2/t*tan(wp/2);ws=2/t*tan(ws/2);wp=1/wp;ws=1/ws;n,wc=buttord(wp,ws,ap,as,s);num,den=butter(n,wc,s);numg,deng=lp2hp(num,den,1);numz,denz=bilinear(numg,deng,fs);disp(分子多项式的系数为);fprintf(%.4en,numz);disp(分母多项式的系
22、数为);fprintf(%.4en,denz);wp=0.6*pi;ws=0.2*pi;w=wp ws;h=freqz(numz,denz,w);fprintf(ap=%fn,-20*log10(abs(h(1);fprintf(as=%fn,-20*log10(abs(h(2);w=0:0.01:1.2*pi;h=freqz(numz,denz,w);plot(w/(2*pi),20*log10(abs(h);输出结果分子多项式的系数为3.7521e-01-7.5042e-013.7521e-01分母多项式的系数为1.0000e+00-3.1201e-011.8884e-01ap=0.394
23、549as=15.000000输出图像%数字高通滤波器的设计,ws=0.2pi,wp=0.6pi,as=15,ap=2;用切比雪夫1型滤波器设计。wp=0.6*pi;ws=0.2*pi;ap=2;as=15;t=2;fs=1/t;wp=2/t*tan(wp/2);ws=2/t*tan(ws/2);wp=1/wp;ws=1/ws;n,wc=cheb1ord(wp,ws,ap,as,s);num,den=cheby1(n,ap,wc,s);numg,deng=lp2hp(num,den,1);numz,denz=bilinear(numg,deng,fs);disp(分子多项式的系数为);fpri
24、ntf(%.4en,numz);disp(分母多项式的系数为);fprintf(%.4en,denz);wp=0.6*pi;ws=0.2*pi;w=wp ws;h=freqz(numz,denz,w);fprintf(ap=%fn,-20*log10(abs(h(1);fprintf(as=%fn,-20*log10(abs(h(2);w=0:0.01:1.2*pi;h=freqz(numz,denz,w);plot(w/(2*pi),20*log10(abs(h);输出结果:分子多项式的系数为1.7097e-01-3.4195e-011.7097e-01分母多项式的系数为1.0000e+00
25、5.6036e-014.2134e-01ap=2.000000as=28.530518输出图像%数字高通滤波器的设计,ws=0.2pi,wp=0.6pi,as=15,ap=2;用切比雪夫2型滤波器设计。clear;wp=0.6*pi;ws=0.2*pi;ap=2;as=15;t=2;fs=1/t;wp=2/t*tan(wp/2);ws=2/t*tan(ws/2);wp=1/wp;ws=1/ws;n,wc=cheb2ord(wp,ws,ap,as,s);num,den=cheby2(n,as,wc,s);numg,deng=lp2hp(num,den,1);numz,denz=bilinear(
26、numg,deng,fs);disp(分子多项式的系数为);fprintf(%.4en,numz);disp(分母多项式的系数为);fprintf(%.4en,denz);wp=0.6*pi;ws=0.2*pi;w=wp ws;h=freqz(numz,denz,w);fprintf(ap=%fn,-20*log10(abs(h(1);fprintf(as=%fn,-20*log10(abs(h(2);w=0:0.01:1.2*pi;h=freqz(numz,denz,w);plot(w/(2*pi),20*log10(abs(h);输出结果分子多项式的系数为3.2784e-01-4.1044
27、e-013.2784e-01分母多项式的系数为1.0000e+001.5646e-012.2258e-01ap=2.000000as=20.237224输出图像%模拟带通滤波器的设计,wp1=6,wp2=8,ws1=4,ws2=11,ap=1.as=32;clear;wp1=6,wp2=8,ws1=4,ws2=11,ap=1,as=32;b=wp2-wp1;w02=wp1*wp2;ws1=(ws1*ws1-w02)/(b*ws1);ws2=(ws2*ws2-w02)/(b*ws2);ws=min(abs(ws1),abs(ws2);wp=1;n,wc=buttord(wp,ws,ap,as,s
28、);num,den=butter(n,wc,s);num,den=lp2bp(num,den,sqrt(w02),b);disp(分子多项式的系数);fprintf(%.4en,num);disp(分母多项式的系数);fprintf(%.4en,den);w=wp ws;h=freqs(num,den,w);fprintf(ap=%fn,-20*log10(abs(h(1);fprintf(as=%fn,-20*log10(abs(h(2);w=3:0.1:12;h=freqs(num,den,w);plot(w,20*log10(abs(h);wp1=6,wp2=8,ws1=4,ws2=11;w=ws1 wp1 wp2 ws2;set(gca,xti
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1