⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pll.m

📁 锁相环的仿真以及含有文本
💻 M
字号:
clc;
close all;
clear all;

file='CW_data.dat';
fid=fopen(file,'r');
sig=fread(fid,'float32');

fclose(fid); 

% Initialize signal parameters

U10 = 1; % Input Reference signal amplitude
U20 = 1; % Output NCO signal amplitude
F0 = 60; % Center Frequency of NCO
Fs = 200 % Sampling freqency
Ts = 1/Fs; % Sampling duration


lpCoeff=importdata('loCoeff.txt');
lpCoeff=lpCoeff';
firOrder=length(lpCoeff);

waitFilBufQ=zeros(firOrder,1); 
waitFilBufI=zeros(firOrder,1); 


% Calculation of PLL parameters
zeta = 0.707; % Damping factor
Wl = 10*pi; % Lock range defined according to expected
% input characteristics
Wn = Wl/2/zeta % Natural frequency calculated from lock range
Kd = 1; % Phase detector gain
Ko = 1; % Loop filter gain

% Loop filter constants and lead-lag gain constants calculated
tau1 = Ko*Kd/(Wn^2);
tau2 = 2*zeta/Wn;
a1 = -1;
b0 = (Ts/(2*tau1))*(1+1/(tan(Ts/(2*tau2)))); % Lead constant gain
b1 = (Ts/(2*tau1))*(1-1/(tan(Ts/(2*tau2)))); % Lag constant gain

% Initialization for first cycle of feedback loop
phi = 0; % Output phase of the NCO
theta = 0; % Phase error of the two signals
Uf=0; % Output signal of the loop filter

theta=0;
F1 = 2*pi*F0 % Output frequency of NCO

% General case phase lock loop
for n = 2:1:length(sig)
    
   phi(n) = phi(n-1)+F1(n-1)*Ts; % Phase error from current cycle   
   loI=cos(phi(n)); %NCO同相输出
   loQ=sin(phi(n)); %NCO正交输出
    
    Imix=sig(n)*loI;
    Qmix=sig(n)*loQ;    
    mix=Imix*Qmix;
    
    %fir低通滤波
    for j=firOrder:-1:2
        waitFilBufI(j)=waitFilBufI(j-1);
    end;
    waitFilBufI(1)=Imix;    
    Ilp(n)=lpCoeff*waitFilBufI;
    
    for j=firOrder:-1:2
        waitFilBufQ(j)=waitFilBufQ(j-1);
    end;
    waitFilBufQ(1)=Qmix;
    Qlp(n)=lpCoeff*waitFilBufQ;

   %theta(n)=atan(-Qlp(n)/Ilp(n));    %相位误差  
   ;
   theta(n)=Qlp(n);    %相位误差   
   Uf(n) = -a1*Uf(n-1)+b0*theta(n)+b1*theta(n-1);    
   F1(n) = 2*pi*F0+Uf(n);
end 

figure;
plot(theta);

figure;
plot(Uf);








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -