% using Matlab to analyze ADC's static performance when the input is sine wave
% Author: Zhu Kai
% ASIC & System State Key Lab, Fudan University
% Version 20080310
tic;
clear;
close all;
clc;
load('G:\Digit.mat');
code=Digit;
% code = code(1:2^21);
% code = code_A;
clearvars -except code;
fs = 100*10^6;
%Draw = csvread('phil50_540.csv',1,1);
% Draw = csvread('E:\MEASUREMENT_MATLAB\200M14B_PCBv2\data\data_1103\a109.csv',1,1);
%
% %990 FS
%
% D = Draw(nx:nx+n,1:12);
%
% codet = D(end-n+1:1:end,:)*[444 276 144 72 40 20 12 8 4 2 1 0.5]'*2;
% code = codet';
max(code)
min(code)
code = code+16384/2;
max(code)
min(code)
numbit= 12;
numdel = 0;
sign_code= 0 ; % 0 represent 原码, 1 represent 补码
numLSB=2^(numbit-numdel);
num_missingcode=0;
samples = 2^13;
% ---------------------------------------------------
% read data from data file to matrix named "rawdata"
% ---------------------------------------------------
%{
fileName = input('Type data file name: ','s');
if isempty(fileName)
return;
end
fid = fopen(fileName,'r');
if (fid == -1)
fprintf('File not found.');
return;
end
fgetl(fid);
data = fscanf(fid,'%d,%d,%d,%d,%d,%d,%d',[7 samples]);
fclose(fid);
% ---------------------------------------------------
% To acquire sampling frequency and sampling points
% ---------------------------------------------------
code=...
data(1,:)* (2^12)+...
data(2,:)* (2^10)+...
data(3,:)* (2^8)+...
data(4,:)* (2^6)+...
data(5,:)* (2^4)+...
data(6,:)* (2^2)+...
data(7,:)* (2^0);
%}
code=floor(code/(2^numdel))+1;
dPnts=length(code);
clear data;
count=zeros(1,numLSB);
for i=1:dPnts
count(code(i))=count(code(i))+1;
end
% for i=1:numLSB
% temp=find(code==i);
% count(i)=length(temp);
% clear temp;
% end
clear code;
percent=(count/dPnts)*100;
h=zeros(1,3*numLSB);
for i=1:numLSB;
h(3*i-2)=i-1;
h(3*i-1)=count(i);
h(3*i-0)=percent(i);
end
fwriteid=fopen('G:\test.txt1','w');
fprintf(fwriteid, ' Dout count percent\n');
fprintf(fwriteid, '%15d %15d %20.2f \n', h);
status = fclose(fwriteid);
toc;
numbit = 12;
numdel = 0;
sign_code= 0 ; % 0 represent 原码, 1 represent 补码
numLSB=2^numbit;
num_missingcode=0;
% ---------------------------------------------------
% read data from data file to matrix named "rawdata"
% ---------------------------------------------------
%fileName = input('Type data file name: ','s');
fileName = 'E:\MEASUREMENT_MATLAB\200M14B_PCBv2\data\paper_data\test.txt'
if isempty(fileName)
return;
end
fid = fopen(fileName,'r');
if (fid == -1)
fprintf('File not found.');
return;
end
k=1;
for skip=1:k
line = fgetl(fid);
end
for i=1:numLSB
x=fscanf(fid,'%d %d %e',3);
data(i)=x(2);
line = fgetl(fid);
end
fclose(fid);
numbit=numbit-numdel;
numLSB=2^numbit;
for i=1:numLSB
count(i)=sum(data(((2^numdel)*i-(2^numdel)+1):((2^numdel)*i)));
end
% if sign_code==0
% for i=1:numLSB
% count(i)=data(i);
% end
% else if sign_code==1
% for i=1:(numLSB/2)
% count(i)=data(i+numLSB/2);
% count(i+numLSB/2)=data(i);
% end
% end
% end
% ---------------------------------------------------
% To determine the point_start and point_end
% ---------------------------------------------------
a=count(1:(numLSB/2)); % a starts from 1
b=count((numLSB/2+1):numLSB); % b starts from 513
[count_max1,point_start]=max(a); % acquire point_start
[count_max2,point_end]=max(b);
point_end=point_end+numLSB/2; % acquire point_end
% point_start=287;
% point_end=16023;
point_start=point_start +0;
point_end =point_end -0;
fprintf('* Accepted points start from %d to %d\n',point_start,point_end);
%%%%%%%%%%%%%%%%%%%%%%%%
Amp_count=numLSB/2;
dPnts=sum(count(1:numLSB));
fprintf('* Total accepted sampling points : %d\n',dPnts);
fprintf('\n****************Static Performance Testing Results*****************\n\n');
Amp_count(1)=Amp_count;
for i=1:20000
temp=i;
Amp_end(i) =Amp_count(i)*(1-cos((pi*sum(count(point_end:numLSB)))/dPnts));
Amp_start(i)=Amp_count(i)*(1-cos((pi*sum(count(1:point_start)))/dPnts));
Amp_count(i+1)=(point_end+1-point_start)/2+(Amp_end(i)+Amp_start(i)-2)/2;
if abs(Amp_count(i+1)-Amp_count(i))<(1/(2^numbit)/(10^10))
Amp_end =Amp_end(i) ;
Amp_start=Amp_start(i);
Amp_count=Amp_count(i+1);
break;
end
end
% temp
% for i=1:2000
% Amp_end =Amp_count*(1-cos((pi*sum(count(point_end:numLSB)))/dPnts));
% Amp_start=Amp_count*(1-cos((pi*sum(count(1:point_start)))/dPnts));
% Amp_count=(point_end+1-point_start)/2+(Amp_end+Amp_start-2)/2;
% end
% Amp_count=(numLSB/2-1)/(sin((pi/2)*(sum(count((point_start+1):(point_end-1)))/dPnts)))
% if there is offset existing, the upper formula is wrong!!
% Amp_end =Amp_count*(1-cos((pi*sum(count(point_end:numLSB)))/dPnts))
% Amp_start=Amp_count*(1-cos((pi*sum(count(1:point_start)))/dPnts))
OFFSET_count=(point_start+1-Amp_start+point_end+Amp_end)/2;
Offset_Error_LSB=OFFSET_count-numLSB/2-1;
Offset_Error_percent=Offset_Error_LSB/(numLSB);
fprintf(' Offset_Error = %.2fLSB / %.2f%%FSR \n',Offset_Error_LSB,Offset_Error_percent*100);
%%%%%%%%%%%%%%%%%%%%%%%%%%
percent=zeros(1,numLSB);
PT=zeros(1,numLSB);
DNL=zeros(1,numLSB);
INL=zeros(1,numLSB);
% for i=(point_start+1):(point_end-1)
% percent(i)=(count(i)/dPnts)*100;
% PT(i)=(asin((i+1-OFFSET_count)/Amp_count)-asin((i-OFFSET_count)/Amp_count))*(100/pi);
% end
%
% k=(point_start+1):(point_end-1);
% figure('color','w');
% axes('FontSize',12.5);
% box on;
% plot(k-1,percent(k),'b-',k-1,PT(k),'r-');
% title('PERCENT OF OUTPUT', 'color','k','FontSize',12.5,'FontName','Arial','FontWeight','normal');
% xlabel('DIGITAL OUTPUT', 'color','k','FontSize',12.5,'FontName','Arial','FontWeight','normal');
% ylabel('PERCENT OF OUTPUT[%]', 'color','k','FontSize',12.5,'FontName','Arial','FontWeight','normal');
% grid
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=(point_start+1):point_end
H(i)=sum(count(1:(i-1)));
point_esti(i)=OFFSET_count-Amp_count*(cos((pi*H(i))/dPnts));
end
for i=(point_start+1):(point_end-1)
DNL(i)=point_esti(i+1)-point_esti(i)-1; %DNL starts from point_start+1 to point_end-1
if DNL(i)<-0.95
if sign_code==0
% fprintf(' the output %d is a missing code\n',i-1);
else if sign_code==1
% fprintf(' the output %d is a missing code\n',i-1-numLSB/2);
end
end
num_missingcode = num_missingcode+1;
end
end
if num_missingcode==0
fprintf(' There is no missing codes!\n');
else
fprintf(' There is %d missing code(s)!\n',num_missingcode);
end
for i=(point_start+1):point_end
INL(i)=point_esti(i)-i; %INL starts from point_start+1 to point_end
end
[DNL_max,DNL_max_point]=max(DNL);
[DNL_min,DNL_min_point]=min(DNL);
fprintf(' Minimum DNL : DNL(%6d) = %5.2f LSB\n',DNL_min_point-1,DNL_min);
fprintf(' Maximum DNL : DNL(%6d) = %5.2f LSB\n',DNL_max_point-1,DNL_max);
[INL_max,INL_max_point]=max(INL);
[INL_min,INL_min_point]=min(INL);
fprintf(' Minimum INL : INL(%6d) = %5.2f LSB\n',INL_min_point-1,INL_min);
fprintf(' Maximum INL : INL(%6d) = %5.2f LSB\n',INL_max_point-1,INL_max);
fprintf('\n*******************************************************************\n\n');
% k=(point_start+1):point_end;
% figure('color','w');
% axes('FontSize',12.5);
% box on;
% plot(k-1,k-2,'r-');
% hold on;
% plot([0,point_start],[point_start-1,point_start-1],'b-');
% plot([point_esti(point_end)-1,point_esti(point_end)-1],[point_end-2,point_end