Overlap Save Convolution
Overlap Save Convolution
%
% Overlap Add Method:
% The overlapadd method is an efficient way to evaluate the discrete convolution of a very long signal with a finite
impulse response
% (FIR) filter where h[m] = 0 for m outside the region [1, M].The concept here is to divide the problem into
multiple convolutions of h[n]
% with short segments of x[n], where L is an arbitrary segment length. Because of this y[n] can be written as a sum
of short convolutions.
%
% Algorithm:
%
% The signal is first partitioned into non-overlapping sequences, then the discrete Fourier transforms of the
sequences are evaluated by
% multiplying the FFT xk[n] of with the FFT of h[n]. After recovering of yk[n] by inverse FFT, the resulting output
signal is reconstructed by
% overlapping and adding the yk[n]. The overlap arises from the fact that a linear convolution is always longer than
the original sequences. In
% the early days of development of the fast Fourier transform, L was often chosen to be a power of 2 for efficiency,
but further development has
% revealed efficient transforms for larger prime factorizations of L, reducing computational sensitivity to this
parameter.
% A pseudo-code of the algorithm is the following:
%
% Algorithm 1 (OA for linear convolution)
% Evaluate the best value of N and L
% H = FFT(h,N)
(zero-padded FFT)
% i=1
% while i <= Nx
%
il = min(i+L-1,Nx)
%
yt = IFFT( FFT(x(i:il),N) * H, N)
%
k = min(i+N-1,Nx)
%
y(i:k) = y(i:k) + yt (add the overlapped output blocks)
%
i = i+L
% end
%
% Circular convolution with the overlapadd method
%
% When sequence x[n] is periodic, and Nx is the period, then y[n] is also periodic, with the same period. To
compute one period of y[n],
% Algorithm 1 can first be used to convolve h[n] with just one period of x[n]. In the region M ? n ? Nx, the
resultant y[n] sequence is correct.
% And if the next M ? 1 values are added to the first M ? 1 values, then the region 1 ? n ? Nx will represent the
desired convolution.
% The modified pseudo-code is:
%
% Algorithm 2 (OA for circular convolution)
% Evaluate Algorithm 1
% y(1:M-1) = y(1:M-1) + y(Nx+1:Nx+M-1)
% y = y(1:Nx)
% end
%
clc;
clear all;
Xn=input('Enter 1st Sequence X(n)= ');
% In this method, the size of the input data blocks is N=L+M-1 and the DFTs and the IDFTs are of length L. Each
Data Block consists of the
% last M-1 data points of the previous block followed by L new data points to form a data sequence of length
N=L+M-1.An N point DFT is computed
% for each data block. The impulse response of the FIR filter is increased in length by appending L-1 zeros and an
N-point DFT of the sequence is
% computed once and stored. The multiplication of the N-point DFTs for the mth block of data yields
%
Ym(k)=h(k)Xm(k).
% Since the data record is of length N, the first M-1 points of Ym(n)are corrupted by aliasing and must be discarded.
The last L points of Ym(n)
% are exactly the same as the result from linear convolution. To avoid loss of data due to aliasing, the last M-1
points of each data record are
% saved and these points become the first M-1 data points of the subsequent record. To begin the processing, the
first M-1 point of the first record
% is set to zero. The resulting data sequence from the IDFT are given where the first M-1 points are discarded due to
aliasing and the remaining L
% points constitute the desired result from the linear convolution. This segmentation of the input data and the fitting
of the output data blocks together
% form the output sequence.
%
%
clc;
clear all;
x=input('Enter 1st sequence X(n)= ');
h=input('Enter 2nd sequence H(n)= ');
L=input('Enter length of each block L = ');
% Code to plot X(n)
subplot (2,2,1);
stem(x);
stem(x,'blue');
xlabel ('n---->');
ylabel ('Amplitude ---->');
title('X(n)');
%Code to plot H(n)
subplot (2,2,2);
stem(h);
stem(h,'black');
xlabel ('n---->');
ylabel ('Amplitude ---->');
title(' H(n)');
% Code to perform Convolution using Overlap Save Method
M=length(h);
lx=length(x);
r=rem(lx,L);
x1=[x zeros(1,L-r)];
nr=(length(x1))/L;
h1=[h zeros(1,L-1)];
for k=1:nr
Ma(k,:)=x1(((k-1)*L+1):k*L)
if k==1
Ma1(k,:)=[zeros(1,M-1) Ma(k,:)];
else
Ma1(k,:)=[Ma(k-1,(L-M+2):L) Ma(k,:)];
end
Ma2(k,:)=ifft(fft(Ma1(k,:)).*fft(h1));
end
Ma3=Ma2(:,M:(L+M-1));
y1=Ma3';
y=y1(:)'
% Representation of the Convoled Signal
subplot (2,2,3:4);
stem(y,'red');
xlabel ('n---->');
ylabel ('Amplitude ---->');
title ('Convolved Signal');
% Add title to the Overall Plot
ha = axes ('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text (0.5, 1,'\bf Convolution using Overlap Save Method ','HorizontalAlignment','center','VerticalAlignment', 'top')