Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Problem Convolution 2(a) Write a Matlab function contconv that computes a

ID: 2079555 • Letter: M

Question

MATLAB Problem

Convolution 2(a) Write a Matlab function contconv that computes an approximation to continuous time convolution as follows. Inputs: Vectors x1 and x2 representing samples of two signals to be convolved. Scalars ti, t2 and dt, representing the starting time for the samples of x1, the starting time for the samples in x2, and the spacing of the samples. Outputs: Vectors y and t, corresponding to the samples of the convolution output and the sampling times. (b) Check that your function works by using it to convolve two boxes, 3I 2-11 and 4I to get [1,3] a trapezoid (e.g., using the following code fragment): dt 0.01 sample spacing si -2:dt -1; %sampling times over the interval C-2,-1] s23 1:dt :3; %sampling times over the interval [1,3]

Explanation / Answer

For the convolution of two signals, we use the inbuilt function conv.

Also, if the starting point of signal a is at m and starting point of signal b is at n,

Then the starting point of the convolution of a and b is m+n.

%MATLAB SCRIPT FOR FUNCTION

function [out1,out2] = contconv(x1,x2,x,y,dt);
    out1 = conv(x1,x2);
    result_start_point = x+y;
    LEN = length(y);
    %display(length(s1));
    T = length(x1)+length(x2)-2;
    out2 = result_start_point : dt : result_start_point + [(length(out1)-1)*dt];
end


Save it as contconv.m in the working directory.

%MATLAB SCRIPT FOR CALLING THE FUNCTION

clc;
close all;
clear all;

dt = 0.01; %sample spacing
s1 = -2:dt:-1; %sampling times over the interval [-2,-1]
s2 = 1:dt:3;   %sampling times over the interval [+1,+3]

x1 = 3*ones(length(s1),1);
x2 = 4*ones(length(s2),1);

figure(2);
subplot(2,1,1);
plot(s1,x1);

subplot(2,1,2);
plot(s2,x2);

figure(1);
%subplot(1,1,1);
[Y,t] = contconv(x1,x2,s1(1),s2(1),dt);
plot(t,Y);


Save it as trap.m