锁相环 matlab仿真,MATLAB锁相环仿真程序求解

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

%File:c6_nltvde.m

w2b=0;w2c=0; %?initialize?integrators

yd=0;y=0; %?initialize?differential?equation

tfinal=50; %?simulation?time

fs=100; %?sampling?frequency

delt=1/fs; %?sampling?period

npts=1+fs*tfinal; %?number?of?samples?simulated

ydv=zeros(1,npts); %?vector?of?dy/dt?samples?

yv=zeros(1,npts); %?vector?of?y(t)?samples

%

%?beginning?of?simulation?loop

for i=1:npts

t=(i-1)*delt; %?time

if t<20

ydd=4*exp(-t/2)-3*yd*abs(y)-9*y; %?de?for?t<20

else

ydd=4*exp(-t/2)-3*yd-9*y; %?de?for?t>=20

end

w1b=ydd+w2b; %?first?integrator?-?step?1

w2b=ydd+w1b; %?first?integrator?-?step?2?

yd=w1b/(2*fs); %?first?integrator?output

w1c=yd+w2c; %?second?integrator?-?step?1

w2c=yd+w1c; %?second?integrator?-?step?2?

y=w1c/(2*fs); %?second?integrator?output

ydv(1,i)=yd; %?build?dy/dt?vector?

yv(1,i)=y; %?build?y(t)?vector

end %?end?of?simulation?loop

plot(yv,ydv) %?plot?phase?plane?

xlabel('y(t)') %?label?x?axis?

ylabel('dy/dt') %?label?y?zxis

%?End?of?script?file.?

%?File:?pllpost.m

%

kk=0;

while kk==0

k=menu('Phase Lock Loop Postprocessor',...

'Input Frequency and VCO Frequency',...?

'Input Phase and VCO Phase',...

'Frequency Error','Phase Error','Phase Plane Plot',...?

'Phase Plane and Time Domain Plots','Exit Program');

if k==1

plot(t,fin,'k',t,fvco,'k')

title('Input Frequency and VCO Freqeuncy')

xlabel('Time-Seconds');ylabel('Frequency-Hertz');pause

elseif k==2

pvco=phin-phierror;plot(t,phin,t,pvco)

title('Input?Phase?and?VCO?Phase')

xlabel('Time-Seconds');ylabel('Phase-Radians');pause

elseif k==3

plot(t,freqerror);title('Frequency Error')

xlabel('Time-Seconds');ylabel('Frequency Error-Hertz');pause

elseif k==4

plot(t,phierror);title('Phase Error')

xlabel('Time-Seconds');ylabel('Phase Error-Radians');pause

elseif k==5

ppplot

elseif k==6

subplot(211);phierrn=phierror/pi;

plot(phierrn,freqerror,'k');grid;

title('Phase Plane Plot');xlabel('Phase Error/Pi');

ylabel('Frequency Error-Hertz');subplot(212)

plot(t,fin,'k',t,fvco,'k');grid

title('Input Frequency and VCO Freqeuncy')

xlabel('Time-Seconds');ylabel('Frequency-Hertz');subplot(111)

elseif k==7

kk=1;

end

end %?End?of?script?file.?

%?File:?pllpre.m

%

clear all %?be?safe

disp(' ') %?insert?blank?line

fdel=input('Enter the size of the frequency step in Hertz > ');

fn=input('Enter the loop natural frequency in Hertz > ');

lambda=input('Enter lambda,the relative pole offset > ');

disp(' ')

disp('Accept default values:')

disp('zeta=1/sqrt(2)=0.707,')

disp('fs=200*fn,and')

disp('tstop=1')

dtype=input('Enter y for yes or n for no > ','s');

if dtype=='y'

zeta=1/sqrt(2);

fs=200*fn;

tstop=1;

else

zeta=input('Enter zeta, the loop damping factor > ');

Fs=input('Enter the sampling frequency in Hertz > ');

tstop=input('Enter tstop,the simulation runtime > ');

end %

npts=fs*tstop+1; %?number?of?simulation?points

t=(0:(npts-1))/fs; %?default?time?vector

nsettle=fix(npts/10); %?set?nsettle?time?as?0.1*npts

tsettle=nsettle/fs; %?set?tsettle

%?The?next?two?lines?establish?the?loop?input?frequency?and?phase

%?deviations.?

fin=[zeros(1,nsettle),fdel*ones(1,npts-nsettle)];

phin=[zeros(1,nsettle),2*pi*fdel*t(1:(npts-nsettle))];

disp(' ') %?insert?blank?line??

%?end?of?script?file?pllpre.m

%?File:?pll2sin.m

w2b=0;w2c=0;s5=0;phivco=0; %initialize

twopi=2*pi; %?define?2*pi

twofs=2*fs; %?define?2*fs

G=2*pi*fn*(zeta+sqrt(zeta*zeta-lambda)); %?set?loop?gain

a=2*pi*fn/(zeta+sqrt(zeta*zeta-lambda)); %?set?filter?parameter?

a1=a*(1-lambda);a2=a*lambda; %?define?constants

phierror=zeros(1,npts); %?initialize?vector

fvco=zeros(1,npts); %?initialize?vector

%?beginning?of?simulation?loop

for i=1:npts

s1=phin(i)-phivco; %?phase?error

s2=sin(s1); %?sinusoidal?phase?detector

s3=G*s2;

s4=a1*s3;

s4a=s4-a2*s5; %?loop?filter?integrator?input

w1b=s4a+w2b; %?filter?integrator?(step?1)?

w2b=s4a+w1b; %?filter?integrator?(step?2)

s5=w1b/twofs; %?generate?fiter?output

s6=s3+s5; %?VCO?integrator?input

w1c=s6+w2c; %?VCO?integrator?(step?1)?

w2c=s6+w1c; %?VCO?integrator?(step?2)?

phivco=w1c/twofs; %?generate?VCO?output

phierror(i)=s1; %?build?phase?error?vector

fvco(i)=s6/twopi; %?build?VCO?input?vector

end

%?end?of?simulation?loop?

freqerror=fin-fvco; %?build?frequency?error?vector

%?End?of?script?file.

function []=pplane(x,y,nsettle)

%?Plots?the?phase?plane?with?phase?in?the?range?(-pi,pi)

ln=length(x);

maxfreq=max(y);

minfreq=min(y);

close %?Old?figure?discarded

axis([-1 1 1.1*minfreq 1.1*maxfreq]); %?Establish?scale

hold on %?Collect?info?for?new?fig

j=nsettle;

while j

i=1;

while x(j)

a(i) = x(j)/pi;

b(i) = y(j);

j = j+1;

i = i+1;

end

plot(a,b,'k')

a=[];

b=[];

x=x-2*pi;

end

hold off

title('Phase-Plane?Plot')

xlabel('Phase?Error/Pi')

ylabel('Frequency?Error?in?Hertz')

grid %?End?of?script?file.

%?File:?ppplot.m

%?ppplot.m?is?the?script?file?for?plotting?phase?plane?plots.?If?the

%?phase?plane?is?constrained?to?(-pi,pi)?ppplot.m?calls?pplane.m.

kz=0;

while kz==0

k=menu('Phase?Plane?Options',...

'Extended?Phase?Plane',...?

'Phase?Plane?mod(2pi)',...

'Exit?Phase?Plane?Menu');

if k==1

phierrn=phierrn/pi;

plot(phierrn,freqerror,'k')

title('Phase?Plane?Plot')

xlabel('Phase?Error/Pi')

ylabel('Frequency?Error-Hertz')

grid

pause

elseif k==2

pplane(phierrn,freqerror,nsettle+1)

pause

elseif k==3

kz=1;

end

end %?End?of?script?file.

运行时遇到如下问题

03417fa6cefaa3bcbcb6309998218eca.png

请问该如何解决?如果要改动程序,请详细指点一下,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值