【 MATLAB 】两个序列的卷积和运算的MATLAB实现(1)

设矩形脉冲 x(n) = u(n) - u(n -10) 是脉冲响应 h(n) = (0.9)^nu(n) 的LTI系统的输入,求输出 y(n).

下面的脚本中用到了一个自定义的函数,也就是两个信号相加的函数:

相关博文:【 MATLAB 】基本序列运算及其MATLAB的等效表示

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

直接给出MATLAB脚本:

clc
clear
close all

% help stepseq
%   generate x(n) = u(n - n0); n1 <= n <= n2
%  _____________________________________________
%  [x,n] = stepseq(n0, n1, n2);
[u1,n1] = stepseq(0,-5,45);
[u2,n2] = stepseq(10,-5,45);

% generate signal x(n)
[x,n] = sigadd(u1,n1,-u2,n2);

% generate signal h(n)
m = -5:45;
h = ( (0.9).^m ).* u1;


% the convolution of x(h) and h(n)
y = conv(x,h);
% ensure the index
nyb = n(1)+ m(1);
nye = n(length(x)) +n(length(h));
ny = nyb:nye;


subplot(3,1,1);
stem(n,x);
title('x(n)');
xlabel('n')

subplot(3,1,2);
stem(m,h);
title('h(n)');
xlabel('n')

subplot(3,1,3);
stem(ny,y);
title('the conv of x(n) and h(n)');
xlabel('n')
xlim([-5,45]);

 

 

  • 15
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李锐博恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值