【 MATLAB 】两个序列的卷积和运算的MATLAB实现(2)
已知下面两个序列:
求这两个序列的卷积。
求卷积的函数是conv,但是使用这个函数有个问题,就是下标问题,也就是求卷积之后的元素值的位置。因此,我们必须要定一个起始点和一个结束点。
方法:
是两个有限长序列,二者卷积的起始点和结束点定义为:
测试脚本:
clc
clear
close all
nx = -3:3;
x = [3,11,7,0,-1,4,2];
nh = -1:4;
h = [2,3,0,-5,2,1];
nyb = nx(1) + nh(1);
nye = nx(length(x)) + nh(length(h));
ny = nyb:nye;
y = conv(x,h);
subplot(3,1,1);
stem(nx,x);
title('x(n)');
subplot(3,1,2);
stem(nh,h);
title('h(n)');
subplot(3,1,3);
stem(ny, y);
title('y(n)');
昨天,这篇博文就到此结束了,可是呢?你不觉得每次卷积时候都要进行求卷积之后得到的卷积值的位置麻烦吗?
包括上篇博文:【 MATLAB 】两个序列的卷积和运算的MATLAB实现(1)
那我们考虑下把两个信号的卷积简单扩展为一个函数conv_m。
如下:
function [y,ny] = conv_m(x,nx,h,nh)
% Modified convolution routine for signal processing
%___________________________________________________
% [y,ny] = conv_m(x,nx,h,nh)
% [y,ny] = convolution result
% [x,nx] = first signal
% [h,nh] = second signal
%
nyb = nx(1) + nh(1);
nye = nx(length(x)) + nh(length(h));
ny = nyb:nye;
y = conv(x,h);
我们在验证下:
clc
clear
close all
nx = -3:3;
x = [3,11,7,0,-1,4,2];
nh = -1:4;
h = [2,3,0,-5,2,1];
[y,ny]=conv_m(x,nx,h,nh);
subplot(3,1,1);
stem(nx,x);
title('x(n)');
subplot(3,1,2);
stem(nh,h);
title('h(n)');
subplot(3,1,3);
stem(ny, y);
title('y(n)');