convolution of two function

51 views (last 30 days)
parham kianian
parham kianian on 9 Feb 2020
Edited: AKASH KUMAR on 27 Jan 2022
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:
f.JPG
Plot of L, that is the filtered signla is:
L.JPG
And this is what MATLAB calculates using conv function
G = conv(f,h,'same');
G.JPG
What is going wrong? G should be same as L as depicted in Figure 15.

Answers (2)

sumanth chinna
sumanth chinna on 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 Comment
AKASH KUMAR
AKASH KUMAR on 27 Jan 2022
Edited: AKASH KUMAR on 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

Sign in to comment.


Image Analyst
Image Analyst on 9 Feb 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 Comments
parham kianian
parham kianian on 9 Feb 2020
When I use option 'full', the results is as follow:
S.JPG
The length of output signla is 2*length(f)-1. The first length(f) data points of output signal have exactly the same shape as shown in Figure 15, but its maximum amplitude is about 150. It should be 2.3 as depicted in Figure 15.
How can I solve this problem?
Image Analyst
Image Analyst on 9 Feb 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');
0001 Screenshot.png
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!