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.

```function y = overlap_add(x,h,half_segment_length)
% Linear Convolution computation by use of overlap-and-add method.
% Constraint: L1>=L2 in order for this function to work properly

% 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 = half_segment_length;
L2 = min(N,M);   % length(short)
L3 = max(N,M);  % length(long);

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

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

y_current = zeros(1,2*L1);
y_previous = zeros(1,L1);

for k=1:NofBlocks

long_block = [long((k-1)*L1+1:k*L1)   zeros(1,L1)];  % Both blocks are of size 2*L1 = segment_length.
short_block = [short                      zeros(1,2*L1-L2)];

y_current = fastconv(long_block,short_block);           % length(y_current) = 2*L1 = segment_length.

y((k-1)*L1+1:k*L1) = y_previous + y_current(1:L1) ;  % length(y((k-1)*L1+1:k*L1)) = L1.

y_previous = y_current(L1+1:end);                              % length(y_previous) = L1 = half_segment_length.

end

% Finally, select only the valid range of values for output.
y = y(1:N+M-1);
```