Convolution of two Gaussian functions without conv

9 views (last 30 days)
cg on 12 Oct 2021
Answered: Star Strider on 12 Oct 2021
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 on 12 Oct 2021
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.
.