常用序列的MATLAB代码(一)
常用序列的MATLAB代码(一)
1)单位冲激序列
function[x,n] = impseq(n0,ns,nf)
% ns=序列的起点;nf=序列的终点;n0=序列在n0处有一个单位脉冲。
% x=产生的单位采样序列;n=产生序列的位置信息
n = [ns:nf];
x = [(n-n0)==0];
2)单位阶跃序列
function [x,n] = stepseq(n0,n1,n2)
% Generates x(n) = u(n-n0); n1 <= n,n0 <= n2
% ------------------------------------------
% [x,n] = stepseq(n0,n1,n2)
%
if ((n0 < n1) | (n0 > n2) | (n1 > n2))
error('arguments must satisfy n1 <= n0 <= n2')
end
n = [n1:n2];
%x = [zeros(1,(n0-n1)), ones(1,(n2-n0+1))];
x = [(n-n0) >= 0];
1)加法函数
function [y,n] = sigadd(x1,n1,x2,n2)
% implements y(n) = x1(n)+x2(n)
% -----------------------------
% [y,n] = sigadd(x1,n1,x2,n2)
% y = sum sequence over n, which includes n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
%
n = min(min(n1),min(n2)):max(max(n1),max(n2)); % duration of y(n)
y1 = zeros(1,length(n)); y2 = y1;
% initialization
y1(find((n>=min(n1))&