-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpll.m
61 lines (46 loc) · 1.13 KB
/
pll.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
clear all;
close all;
Fs = 10000;
Fc_tx = 400;
N = 100;
NCO(1) = sqrt(pi)*randn;
Vi = 0;
%% To fine tune
Kp = 0.6;
Ki = 0.01;
f_offset = 0.02; % Absolute freq error (in Hz)
delta_phi_in = 2*pi*(Fc_tx + f_offset);
phase_tx = 0;
pn_var = 1e-9;
phase_noise = sqrt(pn_var)*randn(N, 1);
phase = phase_tx + phase_noise;
Tx_I_BB = zeros(1, N);
dt = 1/Fs;
t = 0 : dt : (length(Tx_I_BB) - 1) * dt;
in = t.' * delta_phi_in + phase;
s_in = exp(1j * in);
Tx_I = 1 .* (sqrt(2)*cos(2*pi*Fc_tx*t+phase));
Tx_Q = 1 .* (sqrt(2)*sin(2*pi*Fc_tx*t+phase));
Tx = Tx_I - Tx_Q;
s_in = Tx;
for k = 1 : N
% Phase error detector
## e = sin(in(k) - NCO(k)); % phase error between input and nco
## err(k) = e;
s_loop(k) = exp(1j*NCO(k));
dds_mult(k) = s_in(k) * conj(s_loop(k));
err(k) = imag(dds_mult(k));
% Loop filter
Vp = Kp * err(k);
Vi = Vi + Ki * err(k);
V(k) = Vp + Vi;
% Numerically Controlled Oscillator
NCO(k+1) = NCO(k) + 2*pi*Fc_tx/Fs + V(k);
endfor
figure(1)
plot(err);
figure(2)
plot(sin(in));
hold on
plot(sin(NCO));
legend("input", "output");