当前位置:文档之家› MATLAB一些函数实例

MATLAB一些函数实例


ws=2*pi/Ts; %采样角频率
wc=ws/2; % 滤波器截止频率
n=-10:10; %采样点的数量
m=fix(length(n)/2);
%单边采样周期 fix为取整
nTs=n*Ts;
%计算每个采样Ts;
f=sinc(nTs/pi);
fa=f*Ts*wc/pi*sinc((wc/pi)*(ones(length(nTs),1)*t-nTs'*ones(1,length(
axis([-m*Ts m*Ts min(error) max(error)+0.1*max(error)]);
xlabel('t');
ylabel(' error(t)');
t))));
f2=sinc(t/pi);
subplot(311);
stem(nTs,f); %绘制采样信号
hold on
plot(t,f2,'r:');
%绘制包络
xlabel('t ');
title('对Sa(t)信号进行采样');
axis([-m*Ts m*Ts -0.5 1.2]);
hold off
plot(i,yy1,'o');
end
xlabel('t ');
title('重构信号');
axis([-m*Ts m*Ts -0.5 1.2]);
legend([h1,h2],'重构信号','分信号')
hold off
subplot(313);
error=abs(fa-f2);
plot(t,error);
p=get(gca,'position'); p(3)=2.4*p(3); set(gca,'position',p) title('f(t)=f1(t)*f2(t)') xlabel('t') 6.周期信号傅里叶级数 display('傅里叶展开的项数')
m=input('m='); t=-2*pi:0.01:2*pi;
stem(n2,x2,'filled') title('x2(n)') xlabel('n') subplot(2,2,3) stem(n,x,'filled') title('x(n)=n1(n)+x2(n)') xlabel('n') p=get(gca,'position'); p(3)=2.4*p(3); set(gca,'position',p)
1.三角波产生器 t=-3:0.01:3; f1=tripuls(t); subplot(3,1,1); plot(t,f1); axis([-3,3,-0.2,1.2]) set(gcf,'color','w'); f2=tripuls(t,4); subplot(3,1,2); plot(t,f2); axis([-3,3,-0.2,1.2]) %set(gcf,'color','w'); f3=tripuls(t,4,-1); subplot(3,1,3); plot(t,f3); axis([-3,3,-0.2,1.2])
subplot(312);
h1=plot(t,fa); %绘制重构信号
hold on
for i = -m*Ts:dt:m*Ts
ft=sinc(i/pi)*sinc(wc/pi*(t-i));
%绘制重构信号的各个风量
h2=plot(t,ft,'m:');
hold on
yy=sinc(i/pi);
yy1=yy*yy;
plot(w/pi,Y2,'r'); axis([-2,2,0,pi*10]); hold off
8.Sa 的采样与重构
%奈奎斯特k=1,0<k<1,过采样,k>1,欠采样
display('Please input the value of k');
k=input('k=');
wm=1;
%信号带宽
Ts=k*pi/wm; %采样间隔
n=round(length(t)/4); f=[ones(n,1);-1*ones(n,1);ones(n,1);-1*ones(n+1,1)]; ones(314,1); y=zeros(m+1,max(size(t))); figure(1); y(m+1,:)=f';
figure(1); plot(t/pi,y(m+1,:));
grid; axis([-2 2 -1.5 1.5]); title('周期方波'); xlabel(size(t)); xlabel('单位:pi','Fontsize',8); x=zeros(size(t)); kk='1' for k =1:2:2*m-1
pause; x=x+sin(k*t)/k; y((k+1)/2,:)=4/pi*x; plot(t/pi,y(m+1,:)); hold on; plot(t/pi,y((k+1)/2,:)); hold off; grid; axis([-2 2 -1.5 1.5]); title(strcat('第',kk,'次谐波叠加')); xlabel('单位:pi','Fontsize',8);
plot(t,y); axis([0 6 -1.1 1.1]); xlabel('t 单位:s','Fontsize',8'); title('f(t)'); line([0 6],[0 0],'color',[0 0 0]); N=300; k=-N:N; W=2*pi*5; w=k*W/N; Y=0.01*y*exp(-j*t'*w); Y=abs(Y); subplot(222); plot(w/pi,Y); axis([-2,2,0,pi*7+0.2]) xlabel(' \omega 单位:s'); title('F(j\omega)'); subplot(223); plot(t,y,'b:'); hold on t2=0:Ts:40; y2=cos(2/3*pi*t2); stem(t2,y2); axis([0 6 -1.1 1.1]); xlabel('t 单位:s','Fontsize',8'); title('fs(s)'); hold off Y2=Ts*y2*exp(-j*t2'*w); Y2=abs(Y2); subplot(224); plot(w/pi,Y,'b') xlabel(' \omega 单位:s'); title('Fs(omega)'); hold on
kk=strcat(kk,',',num2str(k+2)); end pause; plot(t/pi,y(1:m+1,:)); grid; axis([-2 2 -1.5 1.5]); title('各次谐波叠加'); xlabel('单位:pi','Fontsize',8); .
7.cos(2/3pi*t)的采样 display('奈奎斯特周期1.5s,Ts<1.5,过采样,Ts>1.5,欠采样'); display('Please input the value of sample period'); Ts=input('Ts='); t=0:0.01:40; y=cos(2/3*pi*t); subplot(221);
2.离散序列的相加与相乘 function[x,n]=jxl(x1,x2,n1,n2) n=min(min(n1),min(n2)):max(max(n1),max(n2)); s1=zeros(1,length(n));s2=s1; s1(find((n>=min(n1))&(n<=max(n1))==1))=x1; s2(find((n>=min(n2))&(n<=max(n2))==1))=x2; x=s1+s2;//x=s1.*s2:%序列乘 axis([(min(min(n1),min(n2))-1),(max(max(n1),max(n2))+1),(min(x)-0.5), (max(x)+0.5)])
3.序列的反摺 function[x,n]=xlfz(x1,n1) x=fliplr(x1);n=fliplr(n1); stem(n,x,'filled') axis([min(n)-1,max(n)+1,min(x)-0.5,max(x)+0.5])
4.序列的卷积 function[x,n]=gghconv(x1,x2,n1,n2) x=conv(x1,x2) ns=n1(1)+n2(1); leg=length(x1)+length(x2)-2; n=ns:(ns+leg) subplot(2,2,1) stem(n1,x1,'filled') title('x1(n)') xlabel('n') subplot(2,2,2)
相关主题