%*************************************************************************%
% Filename   TEST.M                                                       %
%            This file tests a phase-locked loop by creating an FM signal %
%            and demodulation the FM signal using an SPLL.                %
%                                                                         %
% Date   2/4/06                                                           %
% Author  Paul Sand, PE-I                                                 %
% References                                                              %
%           R. Best                                                       %
%*************************************************************************%
clear all;

N  = 5000;                                       % total # of samples     %
Fs = 40000;                                      % sampling rate          %
Ts = 1/Fs;                                       % sampling interval      %
fc = 10000;                                      % carrier frequency      %
t = [0:Ts:(N*Ts)- Ts];                           % time index for samples %

% Initialize the variables.                                               %
PD_GAIN  = 2.0;                                  % Phase detector gain    %
LF_GAIN0 = 1.0;                                  % LPF gain               %
LF_GAIN1 = 1.0;                                  % not really needed      %
VCO_FC   = fc;                                   % VCO center frequency   %
VCO_GAIN = 50000;                                % (rad/sec)/volts        %
VCO_AMP  = 1.0;                                  % amplitude of VCO freq  %

% FM modulate a test signal. When choosing a modulating frequency one has %
% to make sure the LPF in the PLL can handle it. To use a higher frequency%
% one may have to redesign the LPF. A simple task.                        %
TestFreq = 500;                                  % Modulating frequency   %
msg = sin(2*pi*TestFreq*t);                      % compute vector         %
fmmsg = fmod(msg, fc, Fs, 1.0, 0.05);            % modulate the carrier   %

% Recover the 500 Hz signal using an SPLL.                                %
[pd_out,lfsum,lf_out,vco_phase,vco_out] = spll(fmmsg,Fs,PD_GAIN,LF_GAIN0,LF_GAIN1,VCO_FC,VCO_GAIN,VCO_AMP);
%-------------------------------------------------------------------------%

% PLOTS                                                                   %
startplot = 1;
endplot = 300;

figure(101);
subplot(2,1,1);
plot(t(startplot:endplot), fmmsg(startplot:endplot));
title('FM 10 kHz carrier modulated with a 500 Hz message signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;

subplot(2,1,2);
plot(t(startplot:endplot), lf_out(startplot:endplot));
title('PLL Loop filter output');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;
%-------------------------------------------------------------------------%

figure(102)
subplot(2,1,1);
plot(t(startplot:endplot), fmmsg(startplot:endplot));
title('FM 10 kHz carrier modulated with a 500 Hz message signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;

subplot(2,1,2);
plot(t(startplot:endplot), pd_out(startplot:endplot));
title('PLL Phase Detector output');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;
%-------------------------------------------------------------------------%

figure(103)
subplot(2,1,1);
plot(t(startplot:endplot), fmmsg(startplot:endplot));
title('FM output');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;

subplot(2,1,2);
plot(t(startplot:endplot), vco_out(startplot:endplot), 'r');
title('VCO output (PLL tracking the input signal)');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;
%-------------------------------------------------------------------------%

% Get rid of DC and normalize                                             %
lf_out = lf_out - mean(lf_out);                 % elimninate any DC bias  %
yn = lf_out/max(lf_out);                        % normalize & return  
%soundsc(yn, Fs);

figure(104)
plot(t(startplot:endplot), lfsum(startplot:endplot), 'r');
title('PLL Integrator output');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid;
%-------------------------------------------------------------------------%

