(pllph.m code) % model an analog pll (phase step error) fs = 1000; ts = 1/fs; ps
ID: 3736417 • Letter: #
Question
(pllph.m code)
% model an analog pll (phase step error)
fs = 1000;
ts = 1/fs;
pstep = 160*pi/180; % phase step
wn = 2*pi*10;
zt = 0.707;
w0 = 2*pi*100; % quiescent frequency
filti_old = 0;
int2_old = 0;
vcoi_old = 0;
vcoo_old = 0;
mo = 0;
kp = 0.5; % this is actually intrinsic to mixer
% must be computed from signal amplitudes
% kp = AB/2, but our A and B are both 1
k0= 1; % radians/volt
% add octave statements to find k1, k2
k2 = XXX;
k1 = XXX;
% print k1 and k2 out on scren
% nset = 400; % number of points to settle
ntot = 2000; % total number of points -1
perror = zeros(1,ntot+1);
vcos = zeros(1,ntot+1);
filts = zeros(1,ntot+1);
inv = zeros(1,ntot+1);
outv = zeros(1,ntot+1);
for i=0:ntot
% first get input
if (i<=nset)
mi = sin(w0*ts*i);
else
mi = sin(w0*ts*i+pstep);
end
inv(i+1) = mi; % this is the input sinusoid
pe = mi * mo; % phase detection is the product of 2 sines
perror(i+1) = pe; % save the phase error term
filti = kp*pe; % include phase detector gain
out1 = filti * k1; % proportional
int2 = int2_old + (ts/2)*(filti_old + filti); %integral
out2 = int2 * k2;
filto = out1 + out2;
filti_old = filti;
int2_old = int2;
vcoi = k0*filto;
% add a line here to model the vco integrator
% XXX
vcoi_old = vcoi;
vcoo_old = vcoo;
filts(i+1) = filto;
vcos(i+1) = vcoo; % save to plot vco output
% this is filtered so easier to see transient
mo = cos(w0*ts*i+vcoo);
%
% locks at sin & cos, but to compare, put both phase into a sin
%
outv(i+2) = sin(w0*ts*i+vcoo); % 1 for matlab starts at 1 and
% since this is next sample vco out
end %for i
figure(1) % this is the error but hard to see
% since still has double freq term
clf
plot(perror)
title('Phase error with double freq term')
figure(2) % the two sinusoids, notice that they match over time
clf
plot(inv(nset:nset+300),'b');
hold on
plot(outv(nset:nset+300),'g');
hold off
title('Two sinusoids at beginning of step change')
figure(3) % a closeup of the two sinusoids
clf
plot(inv(1000:1050),'b');
hold on
plot(outv(1000:1050),'g');
hold off
title('Closeup of sinusoids after lock')
figure(4) % The loop filter output, shows most of the
% double freq term going away
clf
plot(filts);
title('Loop filter output')
figure(5) % The VCO output
clf
plot(vcos);
title('VCO output')
Explanation / Answer
% model an analog pll (phase step error)
fs = 1000;
ts = 1/fs;
pstep = 160*pi/180; % phase step
wn = 2*pi*10;
zt = 0.707;
w0 = 2*pi*100; % quiescent frequency
filti_old = 0;
int2_old = 0;
vcoi_old = 0;
vcoo_old = 0;
mo = 0;
kp = 0.5; % this is actually intrinsic to mixer
% must be computed from signal amplitudes
% kp = AB/2, but our A and B are both 1
k0= 1; % radians/volt
% add octave statements to find k1, k2
k2 = XXX;
k1 = XXX;
% print k1 and k2 out on scren
% nset = 400; % number of points to settle
ntot = 2000; % total number of points -1
perror = zeros(1,ntot+1);
vcos = zeros(1,ntot+1);
filts = zeros(1,ntot+1);
inv = zeros(1,ntot+1);
outv = zeros(1,ntot+1);
for i=0:ntot
% first get input
if (i<=nset)
mi = sin(w0*ts*i);
else
mi = sin(w0*ts*i+pstep);
end
inv(i+1) = mi; % this is the input sinusoid
pe = mi * mo; % phase detection is the product of 2 sines
perror(i+1) = pe; % save the phase error term
filti = kp*pe; % include phase detector gain
out1 = filti * k1; % proportional
int2 = int2_old + (ts/2)*(filti_old + filti); %integral
out2 = int2 * k2;
filto = out1 + out2;
filti_old = filti;
int2_old = int2;
vcoi = k0*filto;
% add a line here to model the vco integrator
% XXX
vcoi_old = vcoi;
vcoo_old = vcoo;
filts(i+1) = filto;
vcos(i+1) = vcoo; % save to plot vco output
% this is filtered so easier to see transient
mo = cos(w0*ts*i+vcoo);
%
% locks at sin & cos, but to compare, put both phase into a sin
%
outv(i+2) = sin(w0*ts*i+vcoo); % 1 for matlab starts at 1 and
% since this is next sample vco out
end %for i
figure(1) % this is the error but hard to see
% since still has double freq term
clf
plot(perror)
title('Phase error with double freq term')
figure(2) % the two sinusoids, notice that they match over time
clf
plot(inv(nset:nset+300),'b');
hold on
plot(outv(nset:nset+300),'g');
hold off
title('Two sinusoids at beginning of step change')
figure(3) % a closeup of the two sinusoids
clf
plot(inv(1000:1050),'b');
hold on
plot(outv(1000:1050),'g');
hold off
title('Closeup of sinusoids after lock')
figure(4) % The loop filter output, shows most of the
% double freq term going away
clf
plot(filts);
title('Loop filter output')
figure(5) % The VCO output
clf
plot(vcos);
title('VCO output')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.