convolution of two function
33 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have two functions as below:
t = 0 : pi/180 : pi;
f = exp(-t/3) .* (sin(2*t) + 2*sin(4*t) + 0.4*sin(2*t).*sin(40*t));
h = 10 * exp(-10*t);
I want to calcluate the convolution of f and h (L = f * h (t)). This means that to filter signal f using h and store results in L. In fact, this is the example 2.21 of "A first course in wavelets with Fourier analysis".
Plot of f is:
Plot of L, that is the filtered signla is:
And this is what MATLAB calculates using conv function
G = conv(f,h,'same');
What is going wrong? G should be same as L as depicted in Figure 15.
0 commentaires
Réponses (2)
sumanth chinna
le 8 Oct 2020
clc
clear all
close all
x=[1 1 1 1 -1 -1 -1 -1]
h=[0 1 2 3 4 3 2 1]
l1=length(x)
l2=length(h)
N=max(l1,l2)
cconv(x,h,N)
disp('without using standard function')
x=[x zeros(1,N-l1)]
h=[h zeros(1,N-l2)]
y=zeros(1,N)
for n=1:N
for m=1:N
j=mod(n-m,N)
j=j+1
y(n)=y(n)+x(m)*h(j)
end
end
1 commentaire
AKASH KUMAR
le 27 Jan 2022
Modifié(e) : AKASH KUMAR
le 27 Jan 2022
%%%%%
% algorithm to compute convolution
clc
clear
close all
x1 = [5,6,1];
h = [10,6,4,8,9,5];
N = length(x1)+length(h)-1;
y=conv(x1,h) % Inbuilt matlab function
x = linearconvolve(x1,h);
x
%% User defined function to find linear convolution
function cnv = linearconvolve(a,b)
L = length(a)+length(b)-1;
cnv = zeros(1,L);
a1=[a,zeros(1,L-length(a))]; % define a new vector of a
b1=[b,zeros(1,L-length(b))];
for i=1:L
c = 0;
for j=1:i
c = c + a1(j)*b1(i-j+1);
end
cnv(i) = c;
end
end
Image Analyst
le 9 Fév 2020
Looks like it's an edge effect where zeros outside the signal are getting convolved in when the signals start to not overlap anymore. Instead of 'same', use 'full' to see the full signal.
2 commentaires
Image Analyst
le 9 Fév 2020
parham, the plots are correct for the filters you gave. You might want to normalize h so that the filtered signal is of the same amplitude as the original signal:
t = 0 : pi/180 : pi;
f = exp(-t/3) .* (sin(2*t) + 2*sin(4*t) + 0.4*sin(2*t).*sin(40*t));
h = 10 * exp(-10*t);
% Normalize h
h = h / sum(h);
plot(t, f, 'b-', 'LineWidth', 2);
grid on;
hold on;
plot(t, h, 'r-', 'LineWidth', 2);
legend('f', 'h');
filtered_f = conv(f, h, 'full');
deltat = t(2)-t(1)
tf = linspace(-length(h)*deltat, deltat * (length(f) + length(h)), length(filtered_f));
plot(tf, filtered_f, 'g-', 'LineWidth', 2);
legend('f', 'h', 'filtered_f', 'Interpreter', 'none', 'Location', 'northwest');
I think that maybe what you aren't realizing is that the convolution flips the filter signal from right ot left, so that the red signal essentially has the blip on the right end as it slides across the blue signal, not the left side like it's plotted. So then the red blip is way off the right end of the blue signal, only the flat, essentially zero, part of the signal is there to multiply by the blue signal, so the output signal is a constant zero on the right.
You'll notice that there are two positive humps and two negative humps, just like your desired output signal.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!