用x1x2调用函数,然后实现两个函数相乘与相加,并把图形输出:1、建立一个主函数,命名为main.m,程序如下:
n1=-1:4;
n2=-2:5;
x1=[1,2,0,-1,0.5,1.5];
x2=[1,1,-1,0.5,-1,-0.5,0,-0.5];
[y3,n]=sigadd(x1,n1,x2,n2);
[y,n]=sigcheng(x1,n1,x2,n2);
subplot(2,2,1);
stem(n1,x1,'.');
grid;
xlabel('n1');ylabel('x1');
subplot(2,2,2);
stem(n2,x2,'.');
grid;
xlabel('n2');ylabel('x2');
subplot(2,2,3);
stem(n,y3,'.');
grid;
title('序列加');
xlabel('n');ylabel('y3');
subplot(2,2,4);
stem(n,y,'.');
grid;
title('序列乘');
xlabel('n');ylabel('y');
2、建立一个加法函数,命名为sigadd.m;程序如下
function[y3,n]=sigadd(x1,n1,x2,n2)
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));
y2=zeros(1,length(n));
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y3=y1+y2;