Effacer les filtres
Effacer les filtres

Convolution of two Gaussian functions without conv

8 vues (au cours des 30 derniers jours)
cg
cg le 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

Réponse acceptée

Star Strider
Star Strider le 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.
.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by