% Matlab code to model Second-Order Digital PLL 
% Phase noise modeling requires 2 files for the reference clock and VCO
% noise placed in the same directory. These are "ref_clk_data_image.txt"
% and "lc_vco_pn.txt".


clear all;

fref=156.25e6;
delta_tdc=10e-12;
KDCO=2*pi*1e6;  % Hz/LSB
N=90;

alpha=1.49e-0;
beta=7.11e-2;

f_pll=logspace(1,11,1000);
w_pll=2*pi*logspace(1,11,1000);

% Loop Filter
h=(alpha+beta-alpha*exp(-i*2*pi*f_pll/fref))./(1-exp(-i*2*pi*f_pll/fref));

figure;
semilogx(f_pll,20*log10(abs(h))); grid on;

% Loop Gain
LG=(1/(2*pi*fref))*(1/delta_tdc)*h.*(KDCO./(i*2*pi*f_pll))*(1/N);

figure;
subplot(2,1,1); 
A=semilogx(w_pll/(2*pi),20*log10(abs(LG)),'-b'); grid on;
set(A, 'LineWidth', [2.0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [2e4 2e8]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-50 80]); 
set(AX, 'YTick', -40:20:80);
set(AX, 'YColor', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '|LG| (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
subplot(2,1,2); 
B=semilogx(w_pll/(2*pi),(180/pi)*phase(LG),'-b'); grid on;
set(B, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [2e4 2e8]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-180 -60]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Phase(LG) (degrees)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Cross-Correlation Method','Ideal Model'},'Location','northeast');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])



%% Closed-Loop Responses
in=(N/fref)*LG./(1+LG);
tdc=(2*pi*N)*LG./(1+LG);
dco=1./(1+LG);
dac=(KDCO./(i*2*pi*f_pll))*(1/fref)./(1+LG);

in_ct_ct_xfer=20*log10(abs(in))+20*log10(fref);

figure;
semilogx(f_pll,20*log10(abs(in)),'-b'); grid on; hold on;
semilogx(w_pll/(2*pi),in_ct_ct_xfer,'-g');

figure;
A=semilogx(w_pll/(2*pi),in_ct_ct_xfer,'-b'); hold on;
grid on;
set(A, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [1e4 1e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [-200 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Transfer Function (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Input Ref Clk PN','After PLL','After 4MHz CDR'},'Location','northeast');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure;
semilogx(w_pll/(2*pi),20*log10(abs(in)),'-b'); grid on; hold on;
semilogx(w_pll/(2*pi),20*log10(abs(tdc)),'-r');
semilogx(w_pll/(2*pi),20*log10(abs(dac)),'-g');
semilogx(w_pll/(2*pi),20*log10(abs(dco)),'-m');

figure('RendererMode','manual');
A=semilogx(w_pll/(2*pi),20*log10(abs(in)),'-b'); grid on; hold on;
B=semilogx(w_pll/(2*pi),20*log10(abs(tdc)),'-r');
C=semilogx(w_pll/(2*pi),20*log10(abs(dac)),'-g');
D=semilogx(w_pll/(2*pi),20*log10(abs(dco)),'-m');
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
set(D, 'LineWidth', [2.0]);
grid on;
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [-50 10]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '|NTF| (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'Reference','TDC QN','DAC QN','DCO'},'Location','east');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])





load 'ref_clk_data_image.txt';
ref_clk_data=ref_clk_data_image;
ref_clk_freqs=ref_clk_data(:,1);
ref_clk_pn_dBc=ref_clk_data(:,2);

% Integrate phase noise from 10kHz to 1MHz
fstart=1e4;
fstop=1e6;
[~,fstart_i]=min(abs(ref_clk_freqs-fstart))
[~,fstop_i]=min(abs(ref_clk_freqs-fstop))
rms_jitter=sqrt(2/(2*pi*fref)^2*trapz(ref_clk_freqs(fstart_i:fstop_i),10.^(ref_clk_pn_dBc(fstart_i:fstop_i)/10)))

ref_clk_pn_dBc_new=interp1(ref_clk_freqs,ref_clk_pn_dBc,f_pll,'linear','extrap');

figure;
semilogx(ref_clk_freqs,ref_clk_pn_dBc,'-b'); hold on;
semilogx(f_pll,ref_clk_pn_dBc_new,'-r');

fstart=1e4;
fstop=1e6;
[~,fstart_i]=min(abs(f_pll-fstart))
[~,fstop_i]=min(abs(f_pll-fstop))
rms_jitter_new=sqrt(2/(2*pi*fref)^2*trapz(f_pll(fstart_i:fstop_i),10.^(ref_clk_pn_dBc_new(fstart_i:fstop_i)/10)))

% Filter input reference noise
ref_clk_pn_pll=ref_clk_pn_dBc_new+20*log10(abs(in))+20*log10(fref);

figure;
semilogx(f_pll,ref_clk_pn_dBc_new,'-b'); hold on;
semilogx(f_pll,ref_clk_pn_pll,'-r');
xlim([10 7e9]);
ylim([-200 -40]);
grid on;

rms_jitter_ref_clk_out=sqrt(2/(2*pi*fref*N)^2*trapz(f_pll(fstart_i:fstop_i),10.^(ref_clk_pn_pll(fstart_i:fstop_i)/10)))
fstart=1e4;
fstop=10e6;
[~,fstart_i]=min(abs(f_pll-fstart))
[~,fstop_i]=min(abs(f_pll-fstop))
rms_jitter_ref_clk_out_full=sqrt(2/(2*pi*fref*N)^2*trapz(f_pll(fstart_i:fstop_i),10.^(ref_clk_pn_pll(fstart_i:fstop_i)/10)))

figure('RendererMode','manual');
A=semilogx(w_pll/(2*pi),20*log10(abs(in))+20*log10(fref),'-b'); grid on; hold on;
set(A, 'LineWidth', [2.0]);
grid on;
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [-50 10]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '(1/T)^{2}|NTF|^{2} (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Reference','TDC QN','DAC QN','DCO'},'Location','east');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])



%% First-Order CDR Model
wpcdr = 2*pi*4e6;
cdr_num = [1 0];
cdr_den = [1 wpcdr];
cdr_xfer_function=tf(cdr_num,cdr_den);
[cdr_mag,cdr_phase,w1]=bode(cdr_xfer_function,w_pll);
cdr_mag_plot(1,:)=cdr_mag(1,1,:);

% Pass Ref Noise through both PLL and CDR
ref_clk_pn_pll_cdr=ref_clk_pn_pll+20*log10(cdr_mag_plot);

figure;
semilogx(w_pll/(2*pi),ref_clk_pn_dBc_new,'-b'); hold on;
semilogx(w_pll/(2*pi),ref_clk_pn_pll,'-r');
semilogx(w_pll/(2*pi),ref_clk_pn_pll_cdr,'-g');
xlim([10 7e9]);
ylim([-200 -40]);
grid on;

fstart=1e2;
fstop=7e9;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_ref_clk_out_full_cdr=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(ref_clk_pn_pll_cdr(fstart_i:fstop_i)/10)))

figure;
A=semilogx(w_pll/(2*pi),ref_clk_pn_dBc_new,'-b'); hold on;
B=semilogx(w_pll/(2*pi),ref_clk_pn_pll,'-r');
C=semilogx(w_pll/(2*pi),ref_clk_pn_pll_cdr,'-g');
grid on;
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-200 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Phase Noise (dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'Input Ref Clk PN','After PLL','After 4MHz CDR'},'Location','northeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])




%% Model TDC Quantization Noise
% Simple Uniform DAC Quantization Noise
tdc_qn(1:size(f_pll,2))=delta_tdc^2/12;
% TDC Quantization Noise with First-Order Delta-Sigma Modulation
%tdc_qn(1:size(f_pll,2))=delta_tdc^2/12*abs(1-exp(-i*2*pi*f_pll/fref)).^2;
% Filter TDC quantization noise w/ PLL and CDR
tdc_qn_pll=10*log10(tdc_qn)+20*log10(abs(tdc))+10*log10(fref);
tdc_qn_pll_cdr=tdc_qn_pll+20*log10(cdr_mag_plot);

fstart=1e4;
fstop=10e6;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_tdc_out=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(tdc_qn_pll(fstart_i:fstop_i)/10)))

fstart=1e2;
fstop=7e9;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_tdc_out_cdr=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(tdc_qn_pll_cdr(fstart_i:fstop_i)/10)))

figure;
semilogx(w_pll/(2*pi),10*log10(tdc_qn),'-b'); hold on;
semilogx(w_pll/(2*pi),tdc_qn_pll,'-r');
semilogx(w_pll/(2*pi),tdc_qn_pll_cdr,'-g'); grid on;

figure;
A=semilogx(w_pll/(2*pi),10*log10(tdc_qn),'-b'); hold on;
B=semilogx(w_pll/(2*pi),tdc_qn_pll,'-r');
C=semilogx(w_pll/(2*pi),tdc_qn_pll_cdr,'-g'); grid on;
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-250 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Noise/Phase Noise (dB/Hz, dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'TDC Quantization Noise','After PLL','After 4MHz CDR'},'Location','northeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure;
A=semilogx(w_pll/(2*pi),10*log10(tdc_qn),'-b'); hold on; grid on;
set(A, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-240 -220]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'S_{tq} (dB/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'TDC Quantization Noise','After PLL','After 4MHz CDR'},'Location','northeast');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure('RendererMode','manual');
A=semilogx(w_pll/(2*pi),20*log10(abs(tdc))+10*log10(fref),'-b'); grid on; hold on;
set(A, 'LineWidth', [2.0]);
grid on;
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [60 150]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '(1/T)|NTF|^{2} (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Reference','TDC QN','DAC QN','DCO'},'Location','east');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])




%% Model DAC Quantization Noise
% Simple Uniform DAC Quantization Noise
dac_qn(1:size(f_pll,2))=1/12;
dac_qn_simple(1:size(f_pll,2))=1/12;
% DAC Quantization Noise with First-Order Delta-Sigma Modulation
%dac_qn(1:size(f_pll,2))=1/12*abs(1-exp(-i*2*pi*f_pll/fref)).^2;
% Filter TDC quantization noise w/ PLL and CDR
dac_qn_pll=10*log10(dac_qn)+20*log10(abs(dac))+10*log10(fref);
dac_qn_pll_cdr=dac_qn_pll+20*log10(cdr_mag_plot);

fstart=1e4;
fstop=10e6;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_dac_out=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(dac_qn_pll(fstart_i:fstop_i)/10)))

fstart=1e2;
fstop=7e9;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_dac_out_cdr=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(dac_qn_pll_cdr(fstart_i:fstop_i)/10)))

figure;
semilogx(w_pll/(2*pi),10*log10(dac_qn),'-b'); hold on;
semilogx(w_pll/(2*pi),dac_qn_pll,'-r');
semilogx(w_pll/(2*pi),dac_qn_pll_cdr,'-g'); grid on;

figure;
A=semilogx(w_pll/(2*pi),10*log10(dac_qn),'-b'); hold on;
B=semilogx(w_pll/(2*pi),dac_qn_pll,'-r');
C=semilogx(w_pll/(2*pi),dac_qn_pll_cdr,'-g'); grid on;
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-250 -00]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Noise/Phase Noise (dB/Hz, dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'DAC Quantization Noise','After PLL','After 4MHz CDR'},'Location','southeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure;
A=semilogx(w_pll/(2*pi),10*log10(dac_qn),'-b'); hold on; grid on;
set(A, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-20 0]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'S_{DACq} (dB/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'TDC Quantization Noise','After PLL','After 4MHz CDR'},'Location','northeast');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure;
A=semilogx(w_pll/(2*pi),10*log10(dac_qn_simple),'-b'); hold on; grid on;
B=semilogx(w_pll/(2*pi),10*log10(dac_qn),'-r');
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [-20 0]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'S_{DACq} (dB/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'Raw DAC Quantization Noise','After \Delta\Sigma Modulation'},'Location','southeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])




figure('RendererMode','manual');
A=semilogx(w_pll/(2*pi),20*log10(abs(dac))+10*log10(fref),'-b'); grid on; hold on;
set(A, 'LineWidth', [2.0]);
grid on;
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [60 150]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '(1/T)|NTF|^{2} (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Reference','TDC QN','DAC QN','DCO'},'Location','east');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])




%% Model VCO Phase Noise
load 'lc_vco_pn.txt';
lc_vco_freqs=lc_vco_pn(:,1);
vco_scale=3; % Add additional VCO noise to easily scale to match different VCO designs
lc_vco_pn_dBc=lc_vco_pn(:,2)+vco_scale;

figure;
semilogx(lc_vco_freqs,lc_vco_pn_dBc);
grid on;
%xlim([10 4e7]);

% Resample VCO phase noise to align it with the pll frequency grid
lc_vco_pn_dBc_new=interp1(lc_vco_freqs*2*pi,lc_vco_pn_dBc,w_pll,'linear','extrap');

figure;
semilogx(lc_vco_freqs,lc_vco_pn_dBc,'-b'); hold on;
semilogx(w_pll/(2*pi),lc_vco_pn_dBc_new,'-r');

% Filter VCO noise w/ PLL and CDR
lc_vco_pll=lc_vco_pn_dBc_new+20*log10(abs(dco));
lc_vco_pll_cdr=lc_vco_pll+20*log10(cdr_mag_plot);

fstart=1e4;
fstop=10e6;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_vco_out=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(lc_vco_pll(fstart_i:fstop_i)/10)))

fstart=1e2;
fstop=7e9;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_vco_out_cdr=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(lc_vco_pll_cdr(fstart_i:fstop_i)/10)))

figure;
semilogx(w_pll/(2*pi),lc_vco_pn_dBc_new,'-b'); hold on;
semilogx(w_pll/(2*pi),lc_vco_pll,'-r');
semilogx(w_pll/(2*pi),lc_vco_pll_cdr,'-g'); grid on;

figure;
A=semilogx(w_pll/(2*pi),lc_vco_pn_dBc_new,'-b'); hold on;
B=semilogx(w_pll/(2*pi),lc_vco_pll,'-r');
C=semilogx(w_pll/(2*pi),lc_vco_pll_cdr,'-g'); grid on;
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [1e4 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-170 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Phase Noise (dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'DCO Phase Noise','After PLL','After 4MHz CDR'},'Location','northeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure('RendererMode','manual');
A=semilogx(w_pll/(2*pi),20*log10(abs(dco)),'-b'); grid on; hold on;
set(A, 'LineWidth', [2.0]);
grid on;
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-210 10]); 
%set(AX, 'YTick', -20:5:25);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', '|NTF|^{2} (dB)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
%L=legend({'Reference','TDC QN','DAC QN','DCO'},'Location','east');
%set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])









% Add Individual Phase Noise Contributions
total_pn_pll=10*log10(10.^(ref_clk_pn_pll/10)+10.^(tdc_qn_pll/10)+10.^(dac_qn_pll/10)+10.^(lc_vco_pll/10));
total_pn_pll_cdr=total_pn_pll+20*log10(cdr_mag_plot);

figure;
semilogx(w_pll/(2*pi),ref_clk_pn_pll,'-b'); hold on;
semilogx(w_pll/(2*pi),tdc_qn_pll,'-r');
semilogx(w_pll/(2*pi),dac_qn_pll,'-g');
semilogx(w_pll/(2*pi),lc_vco_pll,'-m');
semilogx(w_pll/(2*pi),total_pn_pll,'-k'); grid on;

fstart=1e4;
fstop=10e6;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_total_out=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(total_pn_pll(fstart_i:fstop_i)/10)))

fstart=1e2;
fstop=7e9;
[~,fstart_i]=min(abs(w_pll-2*pi*fstart))
[~,fstop_i]=min(abs(w_pll-2*pi*fstop))
rms_jitter_total_out_cdr=sqrt(2/(2*pi*fref*N)^2*trapz(w_pll(fstart_i:fstop_i)/(2*pi),10.^(total_pn_pll_cdr(fstart_i:fstop_i)/10)))


figure;
A=semilogx(w_pll/(2*pi),total_pn_pll,'-b'); hold on;
B=semilogx(w_pll/(2*pi),ref_clk_pn_pll,'-r');
C=semilogx(w_pll/(2*pi),tdc_qn_pll,'-g'); grid on;
D=semilogx(w_pll/(2*pi),dac_qn_pll,'-m');
E=semilogx(w_pll/(2*pi),lc_vco_pll,'-k');
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
set(C, 'LineWidth', [2.0]);
set(D, 'LineWidth', [2.0]);
set(E, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
set(AX, 'YLim', [-170 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Phase Noise (dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'Total','Ref Clk','TDC','DAC','DCO'},'Location','northeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

figure;
A=semilogx(w_pll/(2*pi),total_pn_pll,'-b'); hold on;
B=semilogx(w_pll/(2*pi),total_pn_pll_cdr,'-r'); grid on;
set(A, 'LineWidth', [2.0]);
set(B, 'LineWidth', [2.0]);
%set(H, 'Color', [0 0 0]);
AX=gca;
set(AX, 'FontName', 'utopia');
set(AX, 'FontSize', [10]);
%set(AX, 'LineWidth', [2.0]);
set(AX, 'XLim', [10 7e9]);
%set(AX, 'XTick', [0.5 1 5 10]);
%set(AX, 'YLim', [-170 -40]); 
%set(AX, 'YTick', -100:20:60);
set(AX, 'YColor', [0 0 0]);
HX = get(AX, 'xlabel');
set(HX, 'string', 'Frequency (Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
HY = get(AX, 'ylabel');
set(HY, 'string', 'Phase Noise (dBc/Hz)','FontName','utopia', 'FontSize', [16], 'Color', [0 0 0]);
%Htitle = get(AX, 'title');
%set(Htitle, 'string', 'Channel Frequency Responses','FontName','utopia', 'FontSize', [20], 'Color', [0 0 0]);
L=legend({'Total PLL Output','After 4MHz CDR'},'Location','northeast');
set(L, 'FontSize', [10]);
%M=text(20, -7, '26GHz Loss')
%set(M, 'HorizontalAlignment','center','FontSize', [10])

rms_jitter_ref_clk_out_full
rms_jitter_ref_clk_out_full_cdr
rms_jitter_tdc_out
rms_jitter_tdc_out_cdr
rms_jitter_dac_out
rms_jitter_dac_out_cdr
rms_jitter_vco_out
rms_jitter_vco_out_cdr
rms_jitter_total_out
rms_jitter_total_out_cdr

