Convolution of two Gaussian functions without conv

I am attempting to perform the convolution of two Gaussian functions, x and h, without using conv and then comparing that with the convolution solved by the built-in conv. I'm stuck setting the parameter for z and the x-axis for the two plots. Below is what I have come up with. Any help would be appreciated!
c1 = 1;
s1 = 1;
c2 = 1;
s2 = 1;
z = %%????
x = exp(-(z-c1).^2/(2*s1.^2));
h = exp(-(z-c2).^2/(2*s2.^2));
y = my_convolution(x, h);
figure;
plot(???,y)
comp = conv(x, h, 'same');
figure;
plot(???,comp)
function y = my_convolution(x, h)
M = length(x);
N = length(h);
X = [x, zeros(1, N)];
H = [h, zeros(1, M)];
for i = 1 : M + N -1
y(i) = 0;
for j = 1:M
if (j < i + 1)
y(i) = y(i) + X(j)*H(i - j + 1);
else
end
end
end
end

 채택된 답변

Star Strider
Star Strider 2021년 10월 12일
Just choose any arbitrary vector for ‘z’ that is long enough to completely support the signals to be convolved. For a Gaussian, that is relatively easy to describe. (For other signals, it may be less obvious.)
c1 = 1;
s1 = 1;
c2 = 1;
s2 = 1;
% z = %%????
z = (min(c1,c2)-10):(max(c1,c2)+10);
x = exp(-(z-c1).^2/(2*s1.^2));
h = exp(-(z-c2).^2/(2*s2.^2));
y = my_convolution(x, h);
same_range = (fix(numel(y)/2)-fix(numel(z)/2))+(1:numel(z)); % Create 'same' Vector
ys = y(same_range); % Part Of 'y' To Plot
figure;
plot(z,ys)
comp = conv(x, h, 'same');
figure;
plot(z,comp)
function y = my_convolution(x, h)
M = length(x);
N = length(h);
X = [x, zeros(1, N)];
H = [h, zeros(1, M)];
for i = 1 : M + N -1
y(i) = 0;
for j = 1:M
if (j < i + 1)
y(i) = y(i) + X(j)*H(i - j + 1);
else
end
end
end
end
The ‘same_range’ index vector appears to work reasonably well.
Experiment to get different results.
.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

cg
2021년 10월 12일

답변:

2021년 10월 12일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by