% Linear convolution using the DFT.
clear all; % clears all variables and functions 
%
x=[2 3]; % signal x(n)
h=[4 1]; % signal h(n)
L=length(x)+length(h)-1;
disp('min length of the zero-padded signals required, L=')
disp(L)
% convolution with minimum zero-padding
X=fft(x,L); % l-point dft of x(n) with sufficient zeros appended
H=fft(h,L); % l-point dft of h(n) with sufficient zeros appended
disp('convolution output with minimum zero-padding')
y=ifft(X.*H); % IDFT of the pointwise product of X and H
disp(real(y))
% convolution with L increased to the nearest power of 2
L=2^(ceil(log2(L))); % L is made equal to the nearest power of 2
X=fft(x,L); % L-point dft of x(n) with sufficient zeros appended
H=fft(h,L); % L-point dft of h(n) with sufficient zeros appended
disp('output with L increased to the nearest power of 2. L=')
disp(L)
y=ifft(X.*H); % IDFT of the pointwise product of X and H
disp(real(y))
%
disp('direct convolution output')
p=conv(x,h);
disp(p)

