% va_test - MATLAB script for testing Viterbi algorithm (file va.m)

disp(       'Demodulation of BPSK modulated signals');
disp(       '--------------------------------------');
L    = input('length of tapped delay line?    ');
N    = input('number of input bits?           ');
SpS  = input('number of samples per symbol?   ');
SNR  = input('Signal-to-Noise ratio (in dB)?  ');
A = [1 -1];

var  = 0.5*10^(-SNR/10);
f    = randn(1,L) + i*randn(1,L); 		% Generate filter coefficients randomly
f(1) = f(1)/sign(real(f(1)));
disp('Filter Coefficients:')
f   = f/norm(f) 				% Normalize energy of impulse response

I = RandomSymbols( N, A, [0.5 0.5] );
v = tdl(I,f,var,SpS);
 
% "Matched filter" receiver (no equalizer)
for nn=1:N
    I1(nn) = (sum(v((nn-1)*SpS+1:nn*SpS)) >= 0) - (sum(v((nn-1)*SpS+1:nn*SpS)) < 0);
end
P_1 = sum(I ~= I1)/N;
 
% MLSE receiver
[I_hat,d_min] = va(v,f,A,SpS);
P_2 = sum(I ~= I_hat)/N;
 
disp('Estimated bit-error rate with matched filter:');
disp(P_1);
disp(' ');
disp('Estimated bit-error rate with MLSE:         ');
disp(P_2);
