Code covered by the BSD License

# MatLab Solutions: "Introduction to Digital Signal Processing: A Computer Laboratory Textbook".

### Ilias Konsoulas (view profile)

29 Oct 2012 (Updated )

These files are the MatLab solutions of exercises contained in the above DSP lab textbook.

overlap_save2(x,h,segment_length)
```function y = overlap_save2(x,h,segment_length)
% Linear Convolution computation via the Overlap-and-Save method. Version 2.
% In order for this function to work, L3 (length(long)) >= L1 ( = segment_length) >= L2 (length(short)).

% Make sure x and h are row vectors for subsequent processing:
x = x(:).';
h = h(:).';

N = length(x);
M = length(h);

if N>M
long = x;
short = h;
else
long = h;
short = x;
end

L1 = segment_length;
L2 = length(short);
L3 = length(long);

m = ceil((N+M-1)/L1);

long = [long  zeros(1, m*L1-L3)];  % Pad long with zeros to the least integer multiple of
% L1 (i.e. segment_length) greater than or equal to N+M-1.
y = zeros(1,m*L1);                         % This is the pre-allocated size of the output sequence
% y[n] where the convolution values will be stored.
y_current = zeros(1,L1+L2);
short_block = short;

for k=0:m-1
if k ==0  % First x_block processing. We introduce some zeros in the beginning of long_block as necessary.
long_block = [zeros(1,L2) long(1:L1)];
% Compute the circular convolution of size L1 + L2.
y_current = cconv(long_block,short_block,L1+L2);
y(1:L1) = y_current(L2+1:end);
else
long_block = long(k*L1- L2+1:(k+1)*L1);      % Successive long_blocks are of size L1+L2 overlapping by L2 samples.
% Compute the circular convolution of size L1 + L2.
y_current = cconv(long_block,short_block,L1+L2);
y(k*L1+1:(k+1)*L1)  = y_current(L2+1:end) ;   % Keep only the last L1 samples of y_current.
end
end

% Finally, keep only the valid entries of y[n]:
y = y(1:N+M-1);
```