📄 analyze_ddc.m
字号:
ShowPlots=4;
ClearVars = 0;
spec_win = 1024; % Window for Welch spectral estimation
addpath ../../model
%% Check input sequences match, to detect any corruption through interpolation
if exist('sg_ddc_input')
if (ddc_input_i(1:size(sg_ddc_input,1)-1,2) == sg_ddc_input(2:end,1))
if (ddc_input_q(1:size(sg_ddc_input,1)-1,2) == sg_ddc_input(2:end,2))
display('Matched input data to file data!');
else
display('Mismatch on imaginary input data vs. file data');
end
else
error('Mismatch on real input data vs. file data!');
end
end
%% Clear variables
if ClearVars, clear sg_ddc_input; end
%% Check ADC output values match, to detect quantization errors
if exist('sg_adc_output')
sg_adc_start = 6; % 5 sample offset from model
load results/adc_output.mat
if real(adc_output(1:size(sg_adc_output,1)-sg_adc_start)) == sg_adc_output(sg_adc_start:end-1,1)
display('Matched real ADC data to model!')
else
error('Mismatch between real ADC data and model!')
end
if imag(adc_output(1:size(sg_adc_output,1)-sg_adc_start)) == sg_adc_output(sg_adc_start:end-1,2)
display('Matched imaginary ADC data to model!')
else
error('Mismatch between imaginary ADC data and model!')
end
end
%% Clear variables
if ClearVars, clear adc_output sg_adc_output; end
%% Analyze DDS against model sinusoid
if exist('sg_dds')
load results/sinusoid.mat
sg_dds_start = dds_start; % Must match model start point
sinusoid_start = 1; % Model sinusoid is already trimmed
chk_length=min((size(sg_dds,1)-sg_dds_start)/n_carr-n_carr,size(sinusoid,1)-1);
dds_compare_i=zeros(chk_length,n_carr);
dds_compare_q=zeros(chk_length,n_carr);
for ii=1:n_carr
dds_compare_i(:,ii)=sg_dds(sg_dds_start+ii-1:n_carr:sg_dds_start+ii-1+n_carr*chk_length-1,1)==real(sinusoid(sinusoid_start:sinusoid_start+chk_length-1,ii));
dds_compare_q(:,ii)=sg_dds(sg_dds_start+ii-1:n_carr:sg_dds_start+ii-1+n_carr*chk_length-1,2)==imag(sinusoid(sinusoid_start:sinusoid_start+chk_length-1,ii));
end
if ShowPlots>=0
for ii=1:n_carr
figure; plot(sg_dds(sg_dds_start+ii-1:n_carr:sg_dds_start+ii-1+n_carr*chk_length-1,1));
hold on; plot(real(sinusoid(sinusoid_start:sinusoid_start+chk_length-1,ii)),'r:'); hold off;
figure; plot(sg_dds(sg_dds_start+ii-1:n_carr:sg_dds_start+ii-1+n_carr*chk_length-1,2));
hold on; plot(imag(sinusoid(sinusoid_start:sinusoid_start+chk_length-1,ii)),'r:'); hold off;
end
end
if size(find(dds_compare_i==0),1)>0 || size(find(dds_compare_i==0),1)>0
error('DDS output does not match Matlab model sinusoid!')
else
display(sprintf('Matched DDS output to Matlab model sinusoid! [Samples=%d]',size(sg_dds,1)))
end
end
%% Clear variables
if ClearVars, clear sinusoid sg_dds dds_compare_i dds_compare_q ii chk_length; end
%% Analyze mixer output
mixer_delay = 4; % 4-cycle delay through mixer
if exist('sg_downmixed')
load results/downmixed.mat
% Find first valid sample and add 3 samples for data sample loading
% latency of Sysgen model compared to Matlab model
sg_downmixed_start = min(find(sg_downmixed(:,3)==1)) + 3*clk_os;
downmixed_start = 1; % Already matched up
%% Chop preceding zeroes and tail, and de-integerize
sg_downmixed_temp = (sg_downmixed(sg_downmixed_start:end-2,1)+j*sg_downmixed(sg_downmixed_start:end-2,2));
%% Split CIC dout into channels and analyze
sg_downmixed_chan = zeros(ceil(length(sg_downmixed_temp)/n_carr),n_carr);
sg_downmixed_temp = [ sg_downmixed_temp ; zeros(numel(sg_downmixed_chan)-length(sg_downmixed_temp),1) ];
for ii=1:n_carr
sg_downmixed_chan(:,ii) = sg_downmixed_temp(ii:n_carr:end);
end
clear sg_downmixed_temp;
sg_downmixed_chan = sg_downmixed_chan(1:end-1,:); % Drop end row, may hold filler zeros
if ShowPlots>=0
for ii=1:n_carr
figure; plot(real(downmixed(downmixed_start:downmixed_start-1+size(sg_downmixed_chan(1:end,ii),1),ii)));
hold on; plot(real(sg_downmixed_chan(:,ii)),'r:'); hold off;
figure; plot(imag(downmixed(downmixed_start:downmixed_start-1+size(sg_downmixed_chan(1:end,ii),1),ii)));
hold on; plot(imag(sg_downmixed_chan(:,ii)),'r:'); hold off;
end
end
%% Check samples match
failed=0;
for ch=1:n_carr
i_mismatches = 0; q_mismatches = 0;
display(sprintf('Checking Carrier %d',ch));
%for ii=1:size(cic_i_scaled_temp,1)
for ii=1:size(sg_downmixed_chan,1)
if real(downmixed(ii+downmixed_start-1,ch)) ~= real(sg_downmixed_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('I value mismatch on Channel %d at position %d',ch,ii));
end
i_mismatches = i_mismatches+1;
end
if imag(downmixed(ii+downmixed_start-1,ch)) ~= imag(sg_downmixed_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('Q value mismatch on Channel %d at position %d',ch,ii));
end
q_mismatches = q_mismatches+1;
end
end
display(sprintf('Carrier %d: I mismatches = %d',ch,i_mismatches));
display(sprintf('Carrier %d: Q mismatches = %d',ch,q_mismatches));
display(sprintf('Carrier %d: Total mismatches = %d',ch,i_mismatches+q_mismatches));
if (i_mismatches+q_mismatches)>0
failed=1;
end
end
if failed==1
error('Output from mixer does not match Matlab model mixer output!')
else
display(sprintf('Matched mixer output to Matlab model mixer output! [Samples=%d]',size(sg_downmixed,1)))
end
end
%% Clear variables
if ClearVars, clear downmixed sg_downmixed sg_downmixed_chan failed ii ch; end
%% Analyze CIC output (pre-normalization)
if exist('sg_ycic')
if ~exist('ycic_ddc')
load results/ycic_ddc.mat
end
sg_ycic_start = min(find(sg_ycic(:,3)==1));
ycic_start = 2; % Remove padded zero
%% Chop preceding zeroes and tail, and de-integerize
sg_ycic_temp = [];
for ii=sg_ycic_start:m_cic*clk_os:size(sg_ycic,1)-n_carr
sg_ycic_temp = [ sg_ycic_temp ; (sg_ycic(ii:ii+n_carr-1,1)+j*sg_ycic(ii:ii+n_carr-1,2)) ];
end
%% Split CIC dout into channels and analyze
sg_ycic_chan = zeros(ceil(length(sg_ycic_temp)/n_carr),n_carr);
sg_ycic_temp = [ sg_ycic_temp ; zeros(numel(sg_ycic_chan)-length(sg_ycic_temp),1) ];
for ii=1:n_carr
sg_ycic_chan(:,ii) = sg_ycic_temp(ii:n_carr:end);
end
clear sg_ycic_temp;
sg_ycic_chan = sg_ycic_chan(2:end-1,:); % Drop end row, may hold filler zeros
if ShowPlots>=0
for ii=1:n_carr
figure; plot(real(ycic_ddc(ycic_start:ycic_start-1+size(sg_ycic_chan(1:end,ii),1),ii)));
hold on; plot(2^30*real(sg_ycic_chan(:,ii)),'r:'); hold off;
figure; plot(imag(ycic_ddc(ycic_start:ycic_start-1+size(sg_ycic_chan(1:end,ii),1),ii)));
hold on; plot(2^30*imag(sg_ycic_chan(:,ii)),'r:'); hold off;
end
end
%% Check samples match
failed=0;
display('CIC OUTPUT CHECK :');
for ch=1:n_carr
i_mismatches = 0; q_mismatches = 0;
display(sprintf('Checking Carrier %d',ch));
for ii=1:size(sg_ycic_chan,1)
if real(ycic_ddc(ii+ycic_start-1,ch)) ~= 2^30*real(sg_ycic_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('I value mismatch on Channel %d at position %d',ch,ii));
end
i_mismatches = i_mismatches+1;
end
if imag(ycic_ddc(ii+ycic_start-1,ch)) ~= 2^30*imag(sg_ycic_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('Q value mismatch on Channel %d at position %d',ch,ii));
end
q_mismatches = q_mismatches+1;
end
end
display(sprintf('Carrier %d: I mismatches = %d',ch,i_mismatches));
display(sprintf('Carrier %d: Q mismatches = %d',ch,q_mismatches));
display(sprintf('Carrier %d: Total mismatches = %d',ch,i_mismatches+q_mismatches));
if (i_mismatches+q_mismatches)>0
failed=1;
end
end
if failed==1
error('Output from CIC does not match Matlab model CIC output!')
else
display(sprintf('Matched CIC output to Matlab model CIC output! [Samples=%d]',size(sg_ycic_chan,1)))
end
end
%% Clear variables
if ClearVars, clear ycic_ddc sg_ycic sg_ycic_chan failed ii ch; end
%% Analyze CIC output (post-normalization)
if exist('sg_ycic_norm')
load results/ycic_ddc_norm.mat
sg_ycic_norm_start = min(find(sg_ycic_norm(:,3)==1));
ycic_norm_start = 2; % Remove padded zero
%% Chop preceding zeroes and tail, and de-integerize
sg_ycic_norm_temp = [];
for ii=sg_ycic_norm_start:m_cic*clk_os:size(sg_ycic_norm,1)-n_carr
sg_ycic_norm_temp = [ sg_ycic_norm_temp ; (sg_ycic_norm(ii:ii+n_carr-1,1)+j*sg_ycic_norm(ii:ii+n_carr-1,2)) ];
end
%% Split CIC dout into channels and analyze
sg_ycic_norm_chan = zeros(ceil(length(sg_ycic_norm_temp)/n_carr),n_carr);
sg_ycic_norm_temp = [ sg_ycic_norm_temp ; zeros(numel(sg_ycic_norm_chan)-length(sg_ycic_norm_temp),1) ];
for ii=1:n_carr
sg_ycic_norm_chan(:,ii) = sg_ycic_norm_temp(ii:n_carr:end);
end
clear sg_ycic_norm_temp;
sg_ycic_norm_chan = sg_ycic_norm_chan(2:end-1,:); % Drop end row, may hold filler zeros
if ShowPlots>=0
for ii=1:n_carr
figure; plot(real(ycic_ddc_norm(ycic_norm_start:ycic_norm_start-1+size(sg_ycic_norm_chan(1:end,ii),1),ii)));
hold on; plot(real(sg_ycic_norm_chan(:,ii)),'r:'); hold off;
figure; plot(imag(ycic_ddc_norm(ycic_norm_start:ycic_norm_start-1+size(sg_ycic_norm_chan(1:end,ii),1),ii)));
hold on; plot(imag(sg_ycic_norm_chan(:,ii)),'r:'); hold off;
end
end
%% Check samples match
failed=0;
display('CIC OUTPUT (NORMALIZED) CHECK :');
for ch=1:n_carr
i_mismatches = 0; q_mismatches = 0;
display(sprintf('Checking Carrier %d',ch));
for ii=1:size(sg_ycic_norm_chan,1)
if real(ycic_ddc_norm(ii+ycic_norm_start-1,ch)) ~= real(sg_ycic_norm_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('I value mismatch on Channel %d at position %d',ch,ii));
end
i_mismatches = i_mismatches+1;
end
if imag(ycic_ddc_norm(ii+ycic_norm_start-1,ch)) ~= imag(sg_ycic_norm_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('Q value mismatch on Channel %d at position %d',ch,ii));
end
q_mismatches = q_mismatches+1;
end
end
display(sprintf('Carrier %d: I mismatches = %d',ch,i_mismatches));
display(sprintf('Carrier %d: Q mismatches = %d',ch,q_mismatches));
display(sprintf('Carrier %d: Total mismatches = %d',ch,i_mismatches+q_mismatches));
if (i_mismatches+q_mismatches)>0
failed=1;
end
end
if failed==1
error('Output from CIC (normalized) does not match Matlab model CIC output!')
else
display(sprintf('Matched CIC output (normalized) to Matlab model CIC output! [Samples=%d]',size(sg_ycic_norm_chan,1)))
end
end
%% Clear variables
if ClearVars, clear ycic_ddc_norm sg_ycic_norm sg_ycic_norm_chan failed ii ch; end
%% Analyze TDM CIC output (post-normalization)
if exist('sg_ycic_tdm')
load results/ycic_ddc_norm.mat
sg_ycic_tdm_start = min(find(sg_ycic_tdm(:,2)==1));
ycic_norm_start = 2; % Remove padded zero
%% Chop preceding zeroes and tail
sg_ycic_tdm_temp = [];
for ii=sg_ycic_tdm_start:2:size(sg_ycic_tdm,1)-1
sg_ycic_tdm_temp = [ sg_ycic_tdm_temp ; sg_ycic_tdm(ii,1)+j*sg_ycic_tdm(ii+1,1) ];
end
%% Split CIC dout into channels and analyze
sg_ycic_tdm_chan = zeros(ceil(length(sg_ycic_tdm_temp)/n_carr),n_carr);
sg_ycic_tdm_temp = [ sg_ycic_tdm_temp ; zeros(numel(sg_ycic_tdm_chan)-length(sg_ycic_tdm_temp),1) ];
for ii=1:n_carr
sg_ycic_tdm_chan(:,ii) = sg_ycic_tdm_temp(ii:n_carr:end);
end
clear sg_ycic_tdm_temp;
sg_ycic_tdm_chan = sg_ycic_tdm_chan(2:end-1,:); % Drop end row, may hold filler zeros
if ShowPlots>=0
for ii=1:n_carr
figure; plot(real(ycic_ddc_norm(ycic_norm_start:ycic_norm_start-1+size(sg_ycic_tdm_chan(1:end,ii),1),ii)));
hold on; plot(real(sg_ycic_tdm_chan(:,ii)),'r:'); hold off;
figure; plot(imag(ycic_ddc_norm(ycic_norm_start:ycic_norm_start-1+size(sg_ycic_tdm_chan(1:end,ii),1),ii)));
hold on; plot(imag(sg_ycic_tdm_chan(:,ii)),'r:'); hold off;
end
end
%% Check samples match
failed=0;
display('CIC OUTPUT (NORMALIZED, TDM) CHECK :');
for ch=1:n_carr
i_mismatches = 0; q_mismatches = 0;
display(sprintf('Checking Carrier %d',ch));
for ii=1:size(sg_ycic_tdm_chan,1)
if real(ycic_ddc_norm(ii+ycic_norm_start-1,ch)) ~= real(sg_ycic_tdm_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('I value mismatch on Channel %d at position %d',ch,ii));
end
i_mismatches = i_mismatches+1;
end
if imag(ycic_ddc_norm(ii+ycic_norm_start-1,ch)) ~= imag(sg_ycic_tdm_chan(ii,ch))
if (i_mismatches+q_mismatches)<50
display(sprintf('Q value mismatch on Channel %d at position %d',ch,ii));
end
q_mismatches = q_mismatches+1;
end
end
display(sprintf('Carrier %d: I mismatches = %d',ch,i_mismatches));
display(sprintf('Carrier %d: Q mismatches = %d',ch,q_mismatches));
display(sprintf('Carrier %d: Total mismatches = %d',ch,i_mismatches+q_mismatches));
if (i_mismatches+q_mismatches)>0
failed=1;
end
end
if failed==1
error('Output from CIC (normalized, TDM) does not match Matlab model CIC output!')
else
display(sprintf('Matched CIC output (normalized, TDM) to Matlab model CIC output! [Samples=%d]',size(sg_ycic_tdm_chan,1)))
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -