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.

conv_DHT2(x,h)
```% Linear Convolution Calculated via the Discrete Hartley Transform (DHT).
% Pros:  It is good for convolution calculation of real AND complex input sequences.
%Cons: It is slow because it makes use of slower my_DHT2.m routine for DHT calculation.

function y = conv_DHT2(x,h)

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

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

% Handling of even length input sequences x[n] and h[n].
if ~mod(N,2)
N1 = N + 1;
x = [x 0]; % in order to make x[n] of odd length.
else
N1 = N;
end

if ~mod(M,2)
M1 = M + 1;
h = [h 0]; % in order to make h[n] of odd length.
else
M1 = M;
end

% N1 and M1 should always be odd for this to work properly.
h1 = [zeros(1,(N1-1)/2) h zeros(1,(N1-1)/2)]; % M1+N1-1 samples.
time_ind = -(N1+M1)/2+1:(N1+M1)/2-1;

% Calculate the DHT of h1[n]:
H = my_DHT2(h1,time_ind);

% Zero pad both sequences to make their length qual to N+M-1:
x2 = [zeros(1,(M1-1)/2) x zeros(1,(M1-1)/2)]; % N1+M1-1 samples.
X = my_DHT2(x2,time_ind);

% Next calulate the even and odd parts of the impulse response:
Hev  = 1/2*(H + fliplr(H)); % Middle sample corresponds to k=0 i.e. zero Hartley frequency.
Hod  = 1/2*(H - fliplr(H));  % Middle sample corresponds to k=0 i.e. zero Hartley frequency.

%Calculate the DHT of the output using the given formula:
Y = X.*Hev + fliplr(X).*Hod;

freq_ind = time_ind; % frequency indices are common with the time indices.

% Calculate the convolution sequence by taking the "inverse" DHT transform:
y = 1/(N1+M1-1)*my_DHT2(Y,freq_ind);

% Now keep only the valid samples:
y = y(1:N+M-1);
```