From: HardySpicer on 16 Apr 2010 16:28
On Apr 16, 10:31 am, "smc123" <xmacleod(a)n_o_s_p_a_m.yahoo.com> wrote:
> I'm looking for guidance on writing a software PLL for a signal acquired
> from a data acquisition board:
> The daq board will be sampling at 10kSPS.
> The signals to which I'd like to lock on to are from 5Hz to 1kHz, sine or
> square wave.
> The output of the PLL will be used to multiply a signal acquired on an
> adjacent channel of the daq board. Lock-in amplifier application.
> The processing of the signals will be done on a PC-based software app.
> Thanks for your help
%Phase Locked Loop
%This simulates an analogue PLL
%The VCO is sinusoidal but can be made a square wave
%Suggested parameters: Sample at 10000Hz,Carrier freq 1000Hz = VCO
free running freq
% Make baseband freq 1Hz and frequency deviation 10Hz.
% Make run time 1 second
%When you run this you will see the demodulated sine wave (1Hz) as
% It will have 2fcarrier (2fc) superimposed on top of it (as in a real
%First Generate Test Signal
%Frequency Modulation of a sine wave is simplest!
fs=input('Input Sampling Frequency (Hz)');
fb=input('Baseband Frequency (FM)');
fc=input('Carrier Frequency (FM)');
fset=input('Free running frequency for VCO(make this normally =
delf=input('Frequency Deviation (Hz)');
%Compute FM Modulation Index
tmax=input('Run Time (Secs)');
%Some arrays inialised
fm(i)=cos(2*pi*i*(fc/fs) + beta* sin(2*pi*i*(fb/fs) ) );
%Unity Gain Crossover Frequency (Hz) for Bode Plot
%Make span ratio 10:1 gives a phase margin of around 57 degrees
%Change this and you change stability phase margin is arcsin[(span-1)/
%This function computes phase advance parameters
%Compute Overall Gain
gainp=2*(1-cos (2*pi*(fcp/fs) ) )/sqrt(spanp);
%Divide up between Integrators
%Initialise just in case..
%Main Loop for Phase Locked Loop
%fu0 is Phase Detector Output
r0=-r1*bp + delp*(q0+ap*q1);
vco(i)=cos( (2*pi*i*(fset/fs) ) + r0 ) ;
%Now Limit VCO Output to +1.0 or - 1.0
%For square wave VCO uncomment the following
%Phase Detector (A simple multiplier)
%PLL Output is first Integrator Output
% Returns zero coefficient a
% Pole coefficient b
% and gain del
%span ratio is span
%fs is sampling frequency
%fc is frenquency for maximum phase advance
den1=sqs*(1-span)*y + 2*span*x;
%Th Thats all folks....