Sunteți pe pagina 1din 5

The convolution and correlation of nonperiodic signals

1. Convolution of nonperiodic signals


For two analog nonperiodic signals f (t ) and g (t ) we define the convolution operation as:

 f  g  (t )   f  g  t    d (1.1)


In Matlab in order to implement the convolution first we must define the two signals as
functions. Let us consider the following signals:

 2, for t  (0,3)
f (t )   (1.2)
0, otherwise

 3, for t  (0,5)
g (t )   (1.3)
0, otherwise

First we will implement the functions for the two signals:


File f.m :
function y=f(t)
% Returns a gate function with the amplitude 2 between 0 and 3
y = zeros(1,length(t));
% In Matlab we use numbers reprezented by bits, which means that
% sometimes the values for the numbers are not exact. For example
% even though the value for a number should be 3, in reality
% it might be equal to 2.9999999999, or 3.0000000001. Therefore we
% will use a threshold to account for these errors
thr = 0.00001;
for i=1:length(t)
if (t(i) - thr >0) && (t(i) + thr <3)
y(i) = 2;
end
end
File g.m:
function y=g(t)
% Returns a gate function with the amplitude 3 between 0 and 5
y = zeros(1,length(t));

thr = 0.00001;
for i=1:length(t)
if (t(i) - thr > 0)&&(t(i) + thr < 5)
y(i) = 3;
end
end
Next we will represent the two functions according to    6,10 , so that we display the
whole function:
step =0.1;
tau=-6:step:10;
figure(1);
subplot(2,1,1);
plot(tau,f(tau));
% With axis(xmin, xmax, ymin, ymax) we establish the limits for
% displaying the graphs so that we see the whole signal
axis([min(tau), max(tau), -1, 4]);
xlabel('Time \tau [s]'), ylabel('Amplitude'), title('f(\tau)');
% We use the “grid” command to display a grid on the graph so that we visualize
the graphs easier
grid;

subplot(2,1,2);
plot(tau,g(tau));
axis([min(tau), max(tau), -1, 4]);
xlabel('Time \tau [s]'), ylabel('Amplitude'), title('g(\tau)');
grid;

Figure 1. The two functions

Next we will determine the convolution by computing the integral (1.1) for every t   tmin , tmax 
. The values of tmin and t max should be chosen so that we include all the values of the convolution.
We know from the seminar that for the signals considered in the example above the convolution will
have the support  0,8 , therefore we will choose tmin  1 , tmax  9 and determine the convolution:

tmin = -1;
tmax = 9;
t = tmin:step:tmax;
for index_t=1:length(t)
% We display on the first plot the 3 signals f(tau), g(t-tau) and
% f(tau)*g(t-tau)
figure(2);
subplot(211);
plot(tau,f(tau),'b');
xlabel('Time \tau [s]');
ylabel('Amplitude');
title('Signals f(\tau), g(t-\tau) and their product');
hold on
plot(tau,g(t(index_t)-tau),'r');
prod = f(tau).*g(t(index_t)-tau);
stem(tau, prod,'g');
hold off
% We use axis in order to specify the limits of the figure
axis([min(tau),max(tau),-1,7]);
% With legend() we ca display the name of each graph
legend('f(\tau)', 'g(t-\tau)', 'f(\tau)*g(t-\tau)');
grid;

% We determine the convolution at moment t


cnv(index_t) = integral(@(tau)f(tau).*g(t(index_t)-tau),-10,10);
subplot(212);
plot(t(1:length(cnv)),cnv);
xlabel('Time t [s]');
ylabel('Amplitude');
title('The convolution (f*g)(t)');
axis([min(tau),max(tau),-1,19]);
grid;
pause(0.1);
end

The graphs will display an animation with the calculus of the convolution of the two signals. A
frame from this animation is presented in the following figure:

Figure 2. The convolution of the two signals

Note: at the beginning of the script we should use the following commands in order to erase
the variables from previous runs of the script:
close all;
clear;
clc;

2. The correlation of nonperiodic signals


For two analog nonperiodic signals f (t ) and g (t ) we define the correlation operation as:

 f ˆ g  (t )   f  g   t  d (1.4)


Since the two operations are very similar we can modify the previous program to display the
correlation of signals f ( ) and g ( ) as follows:
tmin = -4;
tmax = 6;
t = tmin:step:tmax;
for index_t=1:length(t)
% We display on the first subplot the 3 signals f(tau), g(t-tau) and
% f(tau)*g(t-tau)
figure(2);
subplot(211);
plot(tau,f(tau),'b');
xlabel('Time \tau [s]');
ylabel('Amplitude');
title('Signals f(\tau), g(\tau+t) and their product');
hold on
plot(tau,g(tau + t(index_t)),'r');
prod = f(tau).*g(tau + t(index_t));
stem(tau, prod,'g');
hold off
% We use axis in order to specify the limits of the figure
axis([min(tau),max(tau),-1,7]);
% With legend() we ca display the name of each graph
legend('f(\tau)', 'g(\tau+t)', 'f(\tau)g(\tau+t)');
grid;

% We determine the convolution at moment t


cnv(index_t) = integral(@(tau)f(tau).*g(tau + t(index_t)),-10,10);
subplot(212);
plot(t(1:length(cnv)),cnv);
xlabel('Time t [s]');
ylabel('Amplitude');
% The 'Interpreter', 'latex' dirrective are used only to display
% the proper symbol for correlation ( a * with ^ on top)
title('The correlation (f$\hat{*}$g)(t)','Interpreter','latex');
axis([min(tau),max(tau),-1,19]);
grid;
pause(0.1);
end

The displayed figures will be similar to the ones bellow:


Figure 3. The correlation of the two signals

3. Homework
Given two non periodic signals:

t 2  6t  6, t   5, 1


f (t )   (1.5)
 0, otherwise

 t  5, t   3,8 
g (t )   (1.6)
0, otherwise

using Matlab, compute the following operations and display the corresponding results:

a)  f ˆ g  (t ) d)  f  g  (t ) g)  f ˆ g  (t )

b)  g ˆ f  (t ) 
e) f  g (t ) h)  f3 ˆ g  (t )

c)  f  g  (t ) f)  f  g3  (t )  
i) f  g 5 (t )