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

设矩形脉冲

da1d56f4fcd5eeb08f0cbeb94131c75a.gif 是脉冲响应 

 的LTI系统的输入,求输出 y(n).

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

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

matlab中两个序列的卷积,【 MATLAB 】两个序列的卷积和运算的MATLAB实现(1)_第1张图片

本文同步分享在 博客“李锐博恩”(CSDN)。

如有侵权,请联系 [email protected] 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

你可能感兴趣的:(matlab中两个序列的卷积)