常用序列的MATLAB代码(一)

1、典型序列函数

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];

2、基本运算函数

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))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1+y2;
% sequence addition

2)反折函数
function [y,n]=sigfold(x,n)
% implements y(n) = x(-n)
% -----------------------------
% [y,n] = sigfold(x,n)
y=fliplr(x);
n=-fliplr(n);

3)乘法函数
function [y,n] = sigmult(x1,n1,x2,n2)
% implements y(n) = x1(n)*x2(n)
% -----------------------------
% [y,n] = sigmult(x1,n1,x2,n2)
% y = produce 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))&(n<=max(n1))==1))=x1; % x1 with duration of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration of y
y = y1.*y2;
% sequence multiplication

4)移位函数
function [y,n]=sigshift(x,m,n0)
% implements y(n) = x(n-n0)
% -----------------------------
% [y,n] = sigshift(x,m,n0)
n=m+n0;
y=x;

///************************************************************

例1:

在这里插入图片描述
代码:

%1.1
clc
clear
close all

n = [0:25];
x=0;
for m=0:10
    x=x+(m+1)*(impseq(2*m,0,25)-impseq(2*m+1,0,25));
end
stem(n,x);
xlabel('n');ylabel('x(n)');
%ylim([-2,3]);

运行结果:
在这里插入图片描述

例2:在这里插入图片描述

代码:

clc
clear
close all
 
n = [-10:10];
x1 = n.*n.*(stepseq(-5,-10,10) - stepseq(6,-10,10));
x2=10*impseq(0,-10,10);
x3 = 20*0.5.^n.*(stepseq(4,-10,10) - stepseq(10,-10,10));
x = x1+x2+x3;
stem(n,x);
xlabel('n');ylabel('x(n)');
%ylim([0,50]);

运行结果:
在这里插入图片描述

例3:

在这里插入图片描述

代码:

clc
clear
close all
 
n = [0:20];
x =0.9.^n.*(cos(0.2 * pi * n+pi/3));
stem(n,x,'r');
xlabel('n');
ylabel('x(n)');

运行结果:
在这里插入图片描述
注:

1、clc:清除命令窗口的内容,对工作环境中的全部变量无任何影响
2、clear:清除工作空间的所有变量
3、close all:关闭所有的Figure窗口
4、stem(n,x,‘r’);函数的第三个入口参数为绘制线的颜色,“r”表示红色
“k”为黑色,默认浅蓝色。

  • 24
    点赞
  • 200
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值