数字信号处理实验全部程序MATLAB.doc
《数字信号处理实验全部程序MATLAB.doc》由会员分享,可在线阅读,更多相关《数字信号处理实验全部程序MATLAB.doc(16页珍藏版)》请在冰豆网上搜索。
![数字信号处理实验全部程序MATLAB.doc](https://file1.bdocx.com/fileroot1/2022-10/22/95bed7f1-1891-46c2-a059-f7acd8486e68/95bed7f1-1891-46c2-a059-f7acd8486e681.gif)
实验一熟悉MATLAB环境
一、实验目的
(1)熟悉MATLAB的主要操作命令。
(2)学会简单的矩阵输入和数据读写。
(3)掌握简单的绘图命令。
(4)用MATLAB编程并学会创建函数。
(5)观察离散系统的频率响应。
二、实验内容
认真阅读本章附录,在MATLAB环境下重新做一遍附录中的例子,体会各条命令的含义。
在熟悉了MATLAB基本命令的基础上,完成以下实验。
上机实验内容:
(1)数组的加、减、乘、除和乘方运算。
输入A=[1234],B=[3456],求C=A+B,D=A-B,E=A.*B,F=A./B,G=A.^B并用stem语句画出A、B、C、D、E、F、G。
实验程序:
A=[1234];
B=[3456];
n=1:
4;
C=A+B;D=A-B;E=A.*B;F=A./B;G=A.^B;
subplot(4,2,1);stem(n,A,'fill');xlabel('时间序列n');ylabel('A');
subplot(4,2,2);stem(n,B,'fill');xlabel('时间序列n');ylabel('B');
subplot(4,2,3);stem(n,C,'fill');xlabel('时间序列n');ylabel('A+B');
subplot(4,2,4);stem(n,D,'fill');xlabel('时间序列n');ylabel('A-B');
subplot(4,2,5);stem(n,E,'fill');xlabel('时间序列n');ylabel('A.*B');
subplot(4,2,6);stem(n,F,'fill');xlabel('时间序列n');ylabel('A./B');
subplot(4,2,7);stem(n,G,'fill');xlabel('时间序列n');ylabel('A.^B');
运行结果:
(2)用MATLAB实现以下序列。
a)x(n)=0.8n0≤n≤15
实验程序:
n=0:
15;x=0.8.^n;
stem(n,x,'fill');xlabel('时间序列n');ylabel('x(n)=0.8^n');
b)x(n)=e(0.2+3j)n0≤n≤15
实验程序:
n=0:
15;x=exp((0.2+3*j)*n);
stem(n,x,'fill');xlabel('时间序列n');ylabel('x(n)=exp((0.2+3*j)*n)');
运行结果:
a)的时间序列 b)的时间序列
c)x(n)=3cos(0.125πn+0.2π)+2sin(0.25πn+0.1π)0≤n≤15
实验程序:
n=0:
1:
15;
x=3*cos(0.125*pi*n+0.2*pi)+2*sin(0.25*pi*n+0.1*pi);
stem(n,x,'fill');xlabel('时间序列n');
ylabel('x(n)=3*cos(0.125*pi*n+0.2*pi)+2*sin(0.25*pi*n+0.1*pi)');
运行结果:
d)将c)中的x(n)扩展为以16为周期的函数x16(n)=x(n+16),绘出四个周期
实验程序:
n=0:
1:
63;
x=3*cos(0.125*pi*rem(n,16)+0.2*pi)+2*sin(0.25*pi*rem(n,16)+0.1*pi);
stem(n,x,'fill');xlabel('时间序列n');ylabel('x16(n)');
e)将c)中的x(n)扩展为以10为周期的函数x10(n)=x(n+10),绘出四个周期
实验程序:
n=0:
1:
39;
x=3*cos(0.125*pi*rem(n,10)+0.2*pi)+2*sin(0.25*pi*rem(n,10)+0.1*pi);
stem(n,x,'fill');xlabel('时间序列n');ylabel('x10(n)');
运行结果:
d)的时间序列 e)的时间序列
(3)x(n)=[1,-1,3,5],产生并绘出下列序列的样本。
a)x1(n)=2x(n+2)-x(n-1)-2x(n)
实验程序:
n=0:
3;
x=[1-135];
x1=circshift(x,[0-2]);x2=circshift(x,[01]);x3=2*x1-x2-2*x;
stem(x3,'fill');xlabel('时间序列n');ylabel('x1(n)=2x(n+2)-x(n-1)-2x(n)');
b)
实验程序:
n=0:
3;
x=[1-135];
x1=circshift(x,[01]);x2=circshift(x,[02]);x3=circshift(x,[03]);
x4=circshift(x,[04]);x5=circshift(x,[05]);
xn=1*x1+2*x2+3*x3+4*x4+5*x5;
stem(xn,'fill');xlabel('时间序列n');
ylabel('x2(n)=x(n-1)+2x(n-2)+3x(n-3)+4x(n-4)+5x(n-5)');
运行结果:
a)的时间序列 b)的时间序列
(4)绘出时间函数的图形,对x轴、y轴图形上方均须加上适当的标注。
a)x(t)=sin(2πt)0≤t≤10sb)x(t)=cos(100πt)sin(πt)0≤t≤4s
实验程序:
clc;
t1=0:
0.001:
10;t2=0:
0.01:
4;
xa=sin(2*pi*t1);xb=cos(100*pi*t2).*sin(pi*t2);
subplot(2,1,1);
plot(t1,xa);xlabel('t');ylabel('x(t)');title('x(t)=sin(2*pi*t)');
subplot(2,1,2);
plot(t2,xb);xlabel('t');ylabel('x(t)');title('x(t)=cos(100*pi*t2).*sin(pi*t2)');
运行结果:
(5)编写函数stepshift(n0,n1,n2)实现u(n-n0),n1实验程序:
clc;
n1=input('请输入起点:
');
n2=input('请输入终点');
n0=input('请输入阶跃位置');
n=n1:
n2;
x=[n-n0>=0];
stem(n,x,'fill');xlable('时间序列n');ylable('u(n-n0)');
请输入起点:
2
请输入终点:
8
请输入阶跃位置:
6
运行结果:
(5)运行结果 (6)运行结果
(6)给一定因果系统求出并绘制H(z)的幅频响应与相频响应。
实验程序:
a=[1-0.670.9];
b=[1sqrt
(2)1];
[hw]=freqz(b,a);
fp=20*log(abs(h));
subplot(2,1,1);
plot(w,fp);xlabel('时间序列t');ylabel('幅频特性');
xp=angle(h);
subplot(2,1,2);
plot(w,xp);xlabel('时间序列t');ylabel('相频特性');
运行结果:
(右上图)
常用典型序列
单位采样序列
function[x,n]=impseq(n1,n2,n0)
n=[n1:
n2];
x=[(n-n0)==0];
[x,n]=impseq(-2,8,2);
stem(n,x);
title('电信1201')
n0=-2;
n=[-10:
10];
nc=length(n);
x=zeros(1,nc);
fori=1:
nc
ifn(i)==n0
x(i)=1
end
end
stem(n,x);
title('电信1201采样序列第二种方法')
单位阶跃序列
function[x,n]=stepseq(n1,n2,n0)
n=[n1:
n2];
x=[(n-n0)>=0];
[x,n]=stepseq(-2,8,2);
stem(n,x);
title('电信1201')
实数指数
n=[0:
10];
x=0.9.^n;
stem(n,x);
title('电信1201')
复数指数序列
n=[-10:
10];
alpha=-0.1+0.3*j;
x=exp(alpha*n);
real_x=real(x);image_x=imag(x);
mag_x=abs(x);phase_x=angle(x);
subplot(2,2,1);stem(n,real_x);title('电信1201')
subplot(2,2,2);stem(n,image_x);title('电信1201')
subplot(2,2,3);stem(n,mag_x);title('电信1201')
subplot(2,2,4);stem(n,phase_x);title('电信1201')
正余弦
n=[0:
10];
x=3*cos(0.1*pi*n+pi/3);
stem(n,x);
title('电信1201')
例:
求出下列波形
x1(n)=2x(n-5)-3x(n+4)
function[y,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):
max(max(n1),max(n2));
y1=zeros(1,length(n));
y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y=y1+y2;
function[y,n]=sigshift(x,m,n0)
n=m+n0;
y=x;
n=[-2:
10];
x=[1:
7,6:
-1:
1];
[x11,n11]=sigshift(x,n,5);
[x12,n12]=sigshift(x,n,-4);
[x1,n1]=sigadd(2*x11,n11,-3*x12,n12);
stem(n1,x1);
title('电信1201')
已知某一系统方程为:
y[n]-y[n-1]+0.9y[n-2]=x[n]计算并画出脉冲响应h(n),n=(-20,100)
n=(-20:
100);
num=
(1);den=[1-10.9];
x=impseq(-20,100,0);
h=filter(num,den,x);
stem(n,h)
xlabel('时间序号N');
ylabel('脉冲响应h');
title('