% Program 8_4
% Linear Convolution Via the DFT
%
% Read in the two sequences
x = [ 2 0 1 1]; %input('Type in the first sequence = ');
h = [1 1 2 2 ]; %input('Type in the second sequence = ');
% Determine the length of the result of convolution
L = length(x)+length(h)-1;
% Compute the DFTs by zero-padding
XE = fft(x,L);  HE = fft(h,L);
% Determine the IDFT of the product
y1 = ifft(XE.*HE);
% Plot the sequence generated by DFT-based convolution and
% the error from direct linear convolution
n = 0:L-1;
subplot(2,1,1)
stem(n,y1)
xlabel('Time index n');ylabel('Amplitude');
title('Result of DFT-based linear convolution')
y2 = conv(x,h);
error = y1-y2;
subplot(2,1,2)
stem(n,error)
xlabel('Time index n');ylabel('Amplitude')
title('Error sequence')
