matlab实验指导解读.docx
《matlab实验指导解读.docx》由会员分享,可在线阅读,更多相关《matlab实验指导解读.docx(22页珍藏版)》请在冰豆网上搜索。
matlab实验指导解读
《数字信号处理》MATLAB实验指导
实验一:
离散时间信号和离散时间系统
一、实验目的:
1、以MATLAB为工具学习离散时间信号的图形表示;
2、以课本提供的例程,练习、理解典型离散信号及其基本运算;
3、通过MATLAB仿真简单的离散时间系统,研究其时域特性;
4、加深对离散系统的差分方程、冲激响应和卷积分析方法的理解。
二、实验内容:
1、典型序列的产生与显示;
2、序列的简单运算;
3、复合和复杂信号的产生与显示;
4、离散时间系统的仿真:
线性和非线性系统、时变和非时变系统的仿真;
5、线性时不变离散时间系统:
系统冲激响应、卷积运算、系统的级联、系统的稳定性;
三、实验例程:
1、参照课本例程产生下列序列,并用plot、stem好人subplot命令绘出其图形:
单位取样序列
;
单位阶跃序列
;
矩形序列RN(n),④斜变序列
。
所需输入的数据是产生序列的长度L和抽样频率FT。
%ProgramP1_1
%GenerationofaUnitSampleSequence
clf;
%Generateavectorfrom-10to20
n=-10:
20;
%Generatetheunitsamplesequence
u=[zeros(1,10)1zeros(1,20)];
%Plottheunitsamplesequence
stem(n,u);
xlabel('Timeindexn');ylabel('Amplitude');
title('UnitSampleSequence');
axis([-102001.2]);
2、编写MATLAB实指数序列程序,
%ProgramP1_3
%Generationofarealexponentialsequence
clf;
n=0:
35;a=1.2;K=0.2;
x=K*a.^n;
stem(n,x);
xlabel('Timeindexn');ylabel('Amplitude');
3、编写产生sweptfrequencysinusoidal序列的程序。
%ProgramP1_7
%Generationofasweptfrequencysinusoidalsequence
n=0:
100;
a=pi/2/100;
b=0;
arg=a*n.*n+b*n;
x=cos(arg);
clf;
stem(n,x);
axis([0,100,-1.5,1.5]);
title('Swept-FrequencySinusoidalSignal');
xlabel('Timeindexn');
ylabel('Amplitude');
grid;axis;
>>
4、产生正弦振幅调制序列
%Generationofamplitudemodulatedsequence
clf;
n=0:
100;
m=0.4;fH=0.1;fL=0.01;
xH=sin(2*pi*fH*n);
xL=sin(2*pi*fL*n);
y=(1+m*xL).*xH;
stem(n,y);grid;
xlabel('Timeindexn');ylabel('Amplitude');
5、用滑动平均滤波器平滑带噪信号,讨论滤波器长度对平滑效果、输出平滑后信号与输入带噪信号之间延时的影响。
%ProgramP1_5
%SignalSmoothingbyAveraging
clf;
R=51;
d=0.8*(rand(R,1)-0.5);%Generaterandomnoise
m=0:
R-1;
s=2*m.*(0.9.^m);%Generateuncorruptedsignal
x=s+d';%Generatenoisecorruptedsignal
subplot(2,1,1);
plot(m,d','r-',m,s,'g--',m,x,'b-.');
xlabel('Timeindexn');ylabel('Amplitude');
legend('d[n]','s[n]','x[n]');
x1=[00x];x2=[0x0];x3=[x00];
y=(x1+x2+x3)/3;
subplot(2,1,2);
plot(m,y(2:
R+1),'r-',m,s,'g--');
legend('y[n]','s[n]');
xlabel('Timeindexn');ylabel('Amplitude');
6、编写输入序列、计算输出序列、差分输出并画出输出序列。
%ProgramP2_4
%Generatetheinputsequences
clf;
n=0:
40;D=10;a=3.0;b=-2;
x=a*cos(2*pi*0.1*n)+b*cos(2*pi*0.4*n);
xd=[zeros(1,D)x];
num=[2.24032.49082.2403];
den=[1-0.40.75];
ic=[00];%Setinitialconditions
%Computetheoutputy[n]
y=filter(num,den,x,ic);
%Computetheoutputyd[n]
yd=filter(num,den,xd,ic);
%Computethedifferenceoutputd[n]
d=y-yd(1+D:
41+D);
%Plottheoutputs
subplot(3,1,1)
stem(n,y);
ylabel('Amplitude');
title('Outputy[n]');grid;
subplot(3,1,2)
stem(n,yd(1:
41));
ylabel('Amplitude');
title(['OutputduetoDelayedInputx[n?
',num2str(D),']']);grid;
subplot(3,1,3)
stem(n,d);
xlabel('Timeindexn');ylabel('Amplitude');
title('DifferenceSignal');grid;
7、编写输入序列和系统单位脉冲响应的卷积程序并画出图形。
%ProgramP2_7
clf;
h=[321-210-403];%impulseresponse
x=[1-23-4321];%inputsequence
y=conv(h,x);
n=0:
14;
subplot(2,1,1);
stem(n,y);
xlabel('Timeindexn');ylabel('Amplitude');
title('OutputObtainedbyConvolution');grid;
x1=[xzeros(1,8)];
y1=filter(h,1,x1);
subplot(2,1,2);
stem(n,y1);
xlabel('Timeindexn');ylabel('Amplitude');
title('OutputGeneratedbyFiltering');grid;
8、编写输入信号经滤波形成的系统输出信号。
%ProgramP2_9
%Generatetheinputsequence
clf;
n=0:
299;
x1=cos(2*pi*10*n/256);
x2=cos(2*pi*100*n/256);
x=x1+x2;
%Computetheoutputsequences
num1=[0.50.270.77];
y1=filter(num1,1,x);%OutputofSystem#1
den2=[1-0.530.46];
num2=[0.450.50.45];
y2=filter(num2,den2,x);%OutputofSystem#2
%Plottheoutputsequences
subplot(2,1,1);
plot(n,y1);axis([0300-22]);
ylabel('Amplitude');
title('OutputofSystem#1');grid;
subplot(2,1,2);
plot(n,y2);axis([0300-22]);
xlabel('Timeindexn');ylabel('Amplitude');
title('OutputofSystem#2');grid;
9、
四、本实验用到的matlab命令
Stemplotsinabscosconvclfsubplotfilterimpz
实验二:
时域连续时间信号和频域抽样理论的验证与观察
一、实验目的:
1、理解并掌握信号时域采样和频率抽样理论涉及的过程和效果;
2、通过编程加深理解奈奎斯特采样定理,理解不满足采样条件的对时域和频域采样造成的混叠现象。
二、实验内容:
1、时域的采样过程、采样定理和混叠效果;
2、频域中的采样效果;
3、学习buttworth模拟低通滤波器的设计命令;
三、实验例程
1、连续时间信号的理想抽样及其混叠效果
clf;
T=0.4;f=25;
n=(0:
T:
1)';
xs=cos(2*pi*f*n);
t=linspace(-0.5,1.5,500)';
ya=sinc((1/T)*t(:
ones(size(n)))-(1/T)*n(:
ones(size(t)))')*xs;
plot(n,xs,'o',t,ya);grid;
xlabel('Time,msec');ylabel('Amplitude');
title('Reconstructedcontinuous-timesignaly_{a}(t)');
axis([01-1.21.2])
2、.频率抽样及其混叠效果
clf;
t=0:
0.002:
50;
xa=2*t.*exp(-t);
subplot(4,2,1)
plot(t,xa);grid
xlabel('Time,msec');ylabel('Amplitude');
title('Continuous-timesignalx_{a}(t)');
subplot(4,2,2)
wa=0:
10/511:
10;
ha=freqs(2,[121],wa);
plot(wa/(2*pi),abs(ha));grid;
xlabel('Frequency,kHz');ylabel('Amplitude');
title('|X_{a}(j\Omega)|');
axis([05/pi02]);
subplot(4,2,3)
T=1;
n=0:
T:
10;
xs=2*n.*exp(-n);
k=0:
length(n)-1;
stem(k,xs);grid;
xlabel('Timeindexn');ylabel('Amplitude');
title('Discrete-timesignalx[n]');
subplot(4,2,4)
wd=0:
pi/255:
pi;
hd=freqz(xs,1,wd);
plot(wd/(T*pi),T*abs(hd));grid;
xlabel('Frequency,kHz');ylabel('Amplitude');
title('|X(e^{j\omega})|');
axis([01/T02])
3、buttworth模拟低通滤波器的设计命令并画出该滤波器图形。
clf;
Fp=3500;Fs=4500;
Wp=2*pi*Fp;Ws=2*pi*Fs;
[N,Wn]=buttord(Wp,Ws,0.5,30,'s');
[b,a]=butter(N,Wn,'s');
wa=0:
(3*Ws)/511:
3*Ws;
h=freqs(b,a,wa);
plot(wa/(2*pi),20*log10(abs(h)));grid
xlabel('Frequency,Hz');ylabel('Gain,dB');
title('Gainresponse');
axis([03*Fs-605]);
四、本实验用到的matlab命令
Butterbuttordfreqsfreqzsinchist
实验三:
离散时间信号与离散时间系统系统的频域分析
一、实验目的:
1、掌握matlab编写基于离散时间傅立叶变换(DTFT)、z变换和离散傅立叶变换(DFT)的程序,并通过本训练加深对这些概念和算法的理解;
2、理解DTFT、ZT和DFT的相互关系。
3、掌握离散系统的频率响应分析;
4、理解零、极点分布的概念。
5、理解帕斯瓦尔定理。
二、实验内容:
1、离散时间傅立叶变换(DTFT)的定义、计算和基本性质;
2、Z变换分析;
3、系统传输函数和频率响应;
4、系统传输函数的类型;
5、系统稳定性测试;
6、离散傅立叶变换(DFT)的计算和基本性质;
7、利用FFT实现线性卷积;
8、利用FFT显示理解帕斯瓦尔定理。
三、实验例程
1、用MATLAB编写离散时间信号的傅里叶变换(DTFT)并绘出8点幅频和相频曲线。
%ProgramP3_1
%EvaluationoftheDTFT
clf;
%ComputethefrequencysamplesoftheDTFT
w=-4*pi:
8*pi/511:
4*pi;
num=[21];den=[1-0.6];
h=freqz(num,den,w);
%PlottheDTFT
subplot(2,1,1)
plot(w/pi,real(h));grid
title('RealpartofH(e^{j\omega})')
xlabel('\omega/\pi');
ylabel('Amplitude');
subplot(2,1,2)
plot(w/pi,imag(h));grid
title('ImaginarypartofH(e^{j\omega})')
xlabel('\omega/\pi');
ylabel('Amplitude');
pause
subplot(2,1,1)
plot(w/pi,abs(h));grid
title('MagnitudeSpectrum|H(e^{j\omega})|')
xlabel('\omega/\pi');
ylabel('Amplitude');
subplot(2,1,2)
plot(w/pi,angle(h));grid
title('PhaseSpectrumarg[H(e^{j\omega})]')
xlabel('\omega/\pi');
ylabel('Phaseinradians');
2、MATLAB验证实序列的离散时间傅立叶变换的对称关系。
N=8;
k=0:
N-1;
gamma=0.5;
x=exp(gamma*k);
y=exp(gamma*fliplr(k));
xev=0.5*([zeros([1,N-1])x]+[yzeros([1,N-1])]);
xod=0.5*([zeros([1,N-1])x]-[yzeros([1,N-1])]);
[X,w]=freqz(x,1,512);
[Xev,w]=freqz(xev,1,512);
[Xod,w]=freqz(xod,1,512);
Xev=exp(j*w*(N-1)).*Xev;
Xod=exp(j*w*(N-1)).*Xod;
%Verifyreal(X)=Xev,andimag(X)=Xod
r=0:
511;
w0=-pi*r/512;
X1=freqz(x,1,w0');
%VerifyX=conj(X1)
%real(X)=real(X1)andimag(X)=-imag(X1)
%abs(X)=abs(X1)andangle(X)=-angle(X1)
3、用MATLAB计算序列ModulationPropertyofDTFT并给出其调制图形。
%ProgramP3_5
%ModulationPropertyofDTFT
clf;
w=-pi:
2*pi/255:
pi;
x1=[1357911131517];
x2=[1-11-11-11-11];
y=x1.*x2;
h1=freqz(x1,1,w);
h2=freqz(x2,1,w);
h3=freqz(y,1,w);
subplot(3,1,1)
plot(w/pi,abs(h1));grid
title('MagnitudeSpectrumofFirstSequence')
subplot(3,1,2)
plot(w/pi,abs(h2));grid
title('MagnitudeSpectrumofSecondSequence')
subplot(3,1,3)
plot(w/pi,abs(h3));grid
title('MagnitudeSpectrumofProductSequence')
4、用MATLAB计算序列
的N点离散傅立叶变换,N取3,5,7,9。
比较在
时计算得到的离散时间傅立叶变换的结果。
N=16;
clf;
N=input('ThevalueofN=');
k=-N:
N;
y1=ones([1,2*N+1]);
y2=y1-abs(k)/N;
w=0:
2*pi/255:
2*pi;
Y2=freqz(y2,1,w);
Y2dft=fft(y2);
k=0:
1:
2*N;
plot(w/pi,abs(Y2),k*2/(2*N+1),abs(Y2dft),'o');
xlabel('Normalizedfrequency');ylabel('Amplitude');
4、用MATLAB研究滤波器的系统函数及其冲激响应,产生零极点图,最终显示出零点的位置。
clf;
b=[1-8.530.5-63];
num1=[b81fliplr(b)];
num2=[b8181fliplr(b)];
num3=[b0-fliplr(b)];
num4=[b81-81-fliplr(b)];
n1=0:
length(num1)-1;
n2=0:
length(num2)-1;
subplot(2,2,1);stem(n1,num1);
xlabel('Timeindexn');ylabel('Amplitude');grid;
title('Type1FIRFilter');
subplot(2,2,2);stem(n2,num2);
xlabel('Timeindexn');ylabel('Amplitude');grid;
title('Type2FIRFilter');
subplot(2,2,3);stem(n1,num3);
xlabel('Timeindexn');ylabel('Amplitude');grid;
title('Type3FIRFilter');
subplot(2,2,4);stem(n2,num4);
xlabel('Timeindexn');ylabel('Amplitude');grid;
title('Type4FIRFilter');
pause
subplot(2,2,1);zplane(num1,1);
title('Type1FIRFilter');
subplot(2,2,2);zplane(num2,1);
title('Type2FIRFilter');
subplot(2,2,3);zplane(num3,1);
title('Type3FIRFilter');
subplot(2,2,4);zplane(num4,1);
title('Type4FIRFilter');
disp('ZerosofType1FIRFilterare');
disp(roots(num1));
disp('ZerosofType2FIRFilterare');
disp(roots(num2));
disp('ZerosofType3FIRFilterare');
disp(roots(num3));
disp('ZerosofType4FIRFilterare');
disp(roots(num4));
4.用MATLAB产生序列的圆周移位,并画图观察该现象,
clf;
x=[0246810121416];
N=length(x)-1;n=0:
N;
y=circshift(x,5);
XF=fft(x);
YF=fft(y);
subplot(2,2,1)
stem(n,abs(XF));grid
title('MagnitudeofDFTofOriginalSequence');
subplot(2,2,2)
stem(n,abs(YF));grid
title('MagnitudeofDFTofCircularlyShiftedSequence');
subplot(2,2,3)
stem(n,angle(XF));grid
title('PhaseofDFTofOriginalSequence');
subplot(2,2,4)
stem(n,angle(YF));grid
title('PhaseofDFTofCircularlyShift