Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 15.74 KB | None | 0 0
  1.  
  2. clear all;
  3. close all;
  4. clc;
  5.  
  6.  
  7. % Regular Gradient descent Synapses
  8. R = 1000*[0 30 30 30; 0 0 30 30; 0 0 0 30; 0 0 0 0];
  9.  
  10. R12 = 30000;
  11. R13 = 30000;
  12. R14 = 30000;
  13. R23 = 30000;
  14. R24 = 30000;
  15. R34 = 30000;
  16.  
  17.  
  18.  
  19. % Define constants
  20. tau = 400e-12; % RC constant
  21. fs=100e3;
  22. fi = 1/(160e-6);
  23. cycles=512;
  24. N=fs/fi*cycles;
  25. ts = (0:1/fs:(50e-3)-1/fs);
  26. Ns= 5e3;
  27.  
  28. % CLK
  29. tr=1/fs*(1:N);
  30. tf=tr+1/(2*fs);
  31. dt=1/(2*fs);
  32.  
  33.  
  34. prompt = 'Enter non_ideal parameter value ';
  35. non_ideal = input(prompt);
  36.  
  37.  
  38. %%%%%%%%%%%%%%%%
  39. % Noise sources
  40. %%%%%%%%%%%%%%%%
  41. % Comparator mismatch
  42. kVth=5e-3;
  43. area_tran=100;
  44. sVth=kVth/sqrt(area_tran);
  45.  
  46. % There are two transistors in a differential comparator
  47. sVth=sqrt(2*sVth^2);
  48. offset=random('norm',0,sVth,1,length(ts));
  49.  
  50. % Comparator input noise
  51. sNoise=(10e-9)*sqrt(6.25e3);
  52. %noise=random('norm',0,sNoise,1,length(ts));
  53.  
  54. %Thermal noise
  55. thNoise=10e-16;
  56.  
  57. % Resistance mismatch
  58. sR = 0.05/sqrt(32);
  59.  
  60. frec=1;
  61. % Jitter
  62. sJit=20e-3; %frequency dependent
  63. vJit=random('norm',0,sJit,1,length(ts));
  64.  
  65.  
  66. % Resistance ladder
  67. Rf= normrnd(45e3,sR*(45e3),1,6);
  68. %Rf = 90e3;
  69. Rin= normrnd(45e3,sR*(45e3),1,4);
  70. RRef = normrnd([45e3 22.5e3 11.25e3 5.625e3], sR*(22.5e3),1,4);
  71.  
  72.  
  73. %Input
  74. %vin = 1.8/2*(1 + sawtooth(ts*fi*(2*pi)));
  75. alpha = 1.8/16;
  76. %vin=linspace(0,1.8-alpha, length(tr));
  77. vin = [0:alpha:1.8 - alpha];
  78.  
  79.  
  80. % Cap response
  81. vC=zeros(1,length(ts));
  82. %for i=1:length(ts)
  83. %    vC(i)=vin(i)*(1-exp(-dt/tau))+noise(i);
  84. %end
  85.  
  86. %learning parameters
  87. Vref = 0.1125;
  88. Y = zeros(1,4);
  89. E_threshold = 1e-8;
  90. T_write = 5e-6;
  91. dt = 0.54e-8;
  92. %RMOS = 450;
  93. %K = 1e-3;
  94. Vp = 0.5;
  95. %Ron = 100;
  96. %Roff = 200e3;
  97. RMOS = normrnd(700,sR*(700),1,100);
  98. Yvar = normrnd(1,0.1*(1),1,4);
  99. Kon = normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
  100. Koff = normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
  101. Von = -0.3;
  102. Voff = 0.4;
  103. %Von = normrnd(0.3,0.033*(0.3),1,6);
  104. %Vp = normrnd(0.5,0.1*0.5);
  105. %Vpgd = normrnd(0.4,0.1*0.4);
  106. Roff = normrnd(100e3,sR*(100e3),1,100);
  107. Ron = normrnd(2000,sR*(2000),1,100);
  108. VR_parasite = 0.015; %frequency dependent
  109.  
  110. E = 1 ;
  111. E_rate = 1;
  112.  
  113.  
  114. %Digital data set
  115. D = 1.8*[0 0 0 0; 0 0 0 1; 0 0 1 0; 0 0 1 1; 0 1 0 0; 0 1 0 1; 0 1 1 0; 0 1 1 1;
  116.      1 0 0 0; 1 0 0 1; 1 0 1 0; 1 0 1 1; 1 1 0 0; 1 1 0 1; 1 1 1 0; 1 1 1 1;];
  117.  
  118. n = 0;
  119.  
  120. t = 1;
  121. A = 0;
  122. DAC = zeros(1, length(vin));
  123. Neurons = zeros(4, length(vin));
  124. vout = zeros(1, length(vin));
  125.  
  126.  
  127.  
  128. %binary-weighted gradient descent Training loop
  129. W_prev = 1/2.+zeros(1,100);
  130. gamma = 1;
  131. for l = 1:250 %2000 samples 20ms
  132.     Ptot_neuron = 0;
  133.     Ptot_net = 0;
  134.     Ptot_comparator = 0;
  135. for x = 1: length(vin)
  136.     %k = randi([1,length(vin)]); %random dataset
  137.     k = x;
  138.    
  139.     noise=random('norm',0,sNoise);
  140.     thermnoise = random('norm',0,thNoise);
  141.     vJit=random('norm',0,sJit);
  142.    
  143.     if vin(k)> 1.793
  144.         vin(k)=0;
  145.     end
  146.     indx = int16(vin(k)/alpha) + 1;
  147.    
  148.     n = n+1;
  149.     vC(n)=vin(k)*(1-exp(-dt/tau));
  150.     if non_ideal == 1
  151.         Y(4) = activation(vin(k) - 8*Vref);
  152.         Y(3) = activation(vin(k) +vJit  - 4*Vref - (Rf(3)/(R(3,4)+RMOS(3*10+4) + thermnoise))*Y(4)*Yvar(4)/18 + noise +VR_parasite);
  153.         Y(2) = activation(vin(k) +vJit  - 2*Vref - (Rf(2)/(R(2,4)+RMOS(2*10+4)+ thermnoise))*Y(4)*Yvar(4)/18 - (Rf(2)/(R(2,3)+RMOS(2*10+3)+ thermnoise))*Y(3)*Yvar(3)/18 + noise +VR_parasite);
  154.         Y(1) = activation(vin(k)+vJit - 1*Vref - (Rf(1)/(R(1,4)+RMOS(1*10+4)+ thermnoise))*Y(4)*Yvar(4)/18 - (Rf(2)/(R(1,3)+RMOS(1*10+3)+ thermnoise))*Y(3)*Yvar(3)/18 - (Rf(1)/(R(1,2)+RMOS(1*10+2)+ thermnoise))*Y(2)*Yvar(2)/18 + noise +VR_parasite);
  155.     else
  156. %         Y(5) = activation(vin(k) - 16*Vref);
  157. %         Y(4) = activation(vin(k) - 8*Vref - 16*Y(5)/18);
  158. %         Y(3) = activation(vin(k) - 4*Vref - 8*Y(4)/18 - 16*Y(5)/18);
  159. %         Y(2) = activation(vin(k) - 2*Vref - 4*Y(3)/18 - 8*Y(4)/18 - 16*Y(5)/18);
  160. %         Y(1) = activation(vin(k) - 1*Vref - 2*Y(2)/18 - 4*Y(3)/18 - 8*Y(4)/18 - 16*Y(5)/18);
  161.  
  162.         Y(4) = activation(vin(k) - 8*Vref);
  163.         Y(3) = activation(vin(k) - 4*Vref - 8*Y(4)/16);
  164.         Y(2) = activation(vin(k) - 2*Vref - 4*Y(3)/16 - 8*Y(4)/16);
  165.         Y(1) = activation(vin(k) - 1*Vref - 2*Y(2)/16 - 4*Y(3)/16 - 8*Y(4)/16);
  166.     end
  167.    
  168.     Rfed=45e3;
  169.     P1_neuron = (vin(k) - 8*Vref )^2/Rfed ;
  170.     P1_net =  vin(k)^2/Rfed + Vref^2/Rfed/8;
  171.     P2_neuron = (vin(k) - 4*Vref - (Rfed/(R(3,4)+450))*Y(4)/18)^2/Rfed ;
  172.     P2_net =  vin(k)^2/Rfed + Vref^2/Rfed/4 + (Y(4)/18)^2/(R(3,4)+450);
  173.     P3_neuron = (vin(k) - 2*Vref - (Rfed/(R(2,4)+450))*Y(4)/18 - (Rfed/(R(2,3)+450))*Y(3)/18)^2/Rfed ;
  174.     P3_net =  vin(k)^2/Rfed + Vref^2/Rfed/2 + (Y(4)/18)^2/(R(2,4)+450) + (Y(3)/18)^2/(R(2,3)+450);
  175.     P4_neuron = (vin(k) - 1*Vref - (Rfed/(R(1,4)+450))*Y(4)/18 - (Rfed/(R(1,3)+450))*Y(3)/18 - (Rfed/(R(1,2)+450))*Y(2)/18)^2/Rfed ;
  176.     P4_net =   vin(k)^2/Rfed + Vref^2/Rfed/1 + (Y(4)/18)^2/(R(1,4)+450) + (Y(3)/18)^2/(R(1,3)+450) + (Y(2)/18)^2/(R(1,2)+450);
  177.     %Ptot_net = Ptot_net + P1_net + P2_net + P3_net + P4_net ;
  178.     Ptot_neuron = Ptot_neuron + P1_neuron + P2_neuron + P3_neuron + P4_neuron ;
  179.     %Ptot_comparator = Ptot_comparator + 4*(1e-8);
  180.    
  181.     fliplr(Y);
  182.     D(ceil(indx),:);
  183.     E(end + 1) = (0.125/(1.8^2))*((Y(1) - D(ceil(indx),4))^2 + (Y(2) - D(ceil(indx),3))^2 + (Y(3) - D(ceil(indx),2))^2 + (Y(4) - D(ceil(indx),1))^2 + 0.01*rand()^2) + E(end);
  184.     if E(end) == 0 %false positive case
  185.         continue;
  186.     end
  187.     DAC(n) = 1/16*(1*Y(1)+ 2*Y(2) + 4*Y(3) + 8*Y(4));
  188. %     Neurons(1,n) =Y(5);
  189.     Neurons(1,n) =Y(4);
  190.     Neurons(2,n) =Y(3);
  191.     Neurons(3,n) =Y(2);
  192.     Neurons(4,n) =Y(1);
  193.     vout(n) = vin(k);  
  194.    
  195.     E_rate(end+1) = (1/n)*E(end);
  196.     t(end+1) = t(end) + 1;
  197.     if E_rate(end) < E_threshold
  198.         break;
  199.     end    
  200.     for i = 1:4
  201.         for j = 1:4
  202.             if i >= j
  203.                  R(i,j) = 0;
  204.             else
  205.               x = 10*i +j;  
  206.               W_f = W_prev(x)*(1-W_prev(x));
  207.               if (D(ceil(indx),5-i) - Y(i))>=0
  208.                 Vw = -1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
  209.                 dW = -1*Kon(x)*((Vw/Von -1)^3)*W_f;
  210.               else
  211.                 Vw = 1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
  212.                 dW = Koff(x)*((Vw/Voff -1)^1)*W_f;
  213.               end
  214.                
  215.               if n < 1600
  216.                 gamma = 1;
  217.               elseif n>=1600 && n<2400
  218.                 gamma = 0.5;
  219.               elseif n>=2800 && n<3200
  220.                 gamma = 0.25;    
  221.               else
  222.                 gamma = 0.03;
  223.               end
  224.              
  225.               beta = gamma*0.09*(Roff(x)-Ron(x))*dW*T_write/dt;
  226.  
  227.               %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 >= Roff(x)
  228.               %    continue
  229.               %end
  230.               %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 <= Ron(x)
  231.               %    continue
  232.               %end
  233.            
  234.               %R(i,j) = R(i,j) + beta*random('norm',(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)/(1.8^2), thNoise) + thermnoise;
  235.               R(i,j) = R(i,j) + beta*(D(ceil(indx),5-i) - Y(i)+ 0.0001*rand())*D(ceil(indx),5-j)/(1.8^2);
  236.               W_prev(x) = R(i,j)/(Roff(x) - Ron(x));  
  237.             end
  238.         end
  239.     end
  240.     R12(end+1) = R(1,2);
  241.     R13(end+1) = R(1,3);
  242.     R14(end+1) = R(1,4);
  243.     R23(end+1) = R(2,3);
  244.     R24(end+1) = R(2,4);
  245.     R34(end+1) = R(3,4);
  246.  
  247.  
  248. end
  249. if E_rate(end) < E_threshold
  250.      break;
  251. end
  252. P(l) = (Ptot_comparator + Ptot_net + Ptot_neuron )/2^4;
  253. end
  254.  
  255.  
  256. %%
  257. figure(1);
  258. hold on;
  259. ylabel('Training Error','FontSize',18);
  260. xlabel('#Samples','FontSize',18);
  261.  
  262.  
  263. plot(t,E_rate,'LineWidth',4);
  264. grid on;
  265. leg = legend('Algorithm');
  266. set(leg,'FontSize',12);
  267. set(gca,'FontSize',12);
  268.  
  269.  
  270. figure(3);
  271. hold on;
  272. plot(t,(45e3)./R12);
  273. plot(t,(45e3)./R13);
  274. plot(t,(45e3)./R14);
  275. plot(t,(45e3)./R23);
  276. plot(t,(45e3)./R24);
  277. plot(t,(45e3)./R34);
  278.  
  279. figure;
  280. plot(P);
  281.  
  282.  
  283. tf = t(1:length(t)-1);
  284. figure(4);
  285. hold on;
  286. stairs(tf,DAC), xlim([n-300 n]);
  287. stairs(tf,vout,'-.or'), xlim([n-300 n]);
  288.  
  289. figure(5);
  290. subplot(4,1,1);
  291. stairs((n-48:n),Neurons(1,n-48:n),'b','linewidth',2);
  292. subplot(4,1,2);
  293. stairs((n-48:n),Neurons(2,n-48:n),'r','linewidth',2);
  294. subplot(4,1,3);
  295. stairs((n-48:n),Neurons(3,n-48:n),'g','linewidth',2);
  296. subplot(4,1,4);
  297. stairs((n-48:n),Neurons(4,n-48:n),'y','linewidth',2);
  298. % subplot(4,1,5);
  299. % stairs((n-48:n),Neurons(5,n-48:n),'y','linewidth',2);
  300.  
  301.  
  302. figure(6);
  303. subplot(4,1,1);
  304. stairs((n-3848:n-3800),Neurons(1,n-3848:n-3800),'b','linewidth',2);
  305. subplot(4,1,2);
  306. stairs((n-3848:n-3800),Neurons(2,n-3848:n-3800),'r','linewidth',2);
  307. subplot(4,1,3);
  308. stairs((n-3848:n-3800),Neurons(3,n-3848:n-3800),'g','linewidth',2);
  309. subplot(4,1,4);
  310. stairs((n-3848:n-3800),Neurons(4,n-3848:n-3800),'y','linewidth',2);
  311. % subplot(4,1,5);
  312. % stairs((n-3848:n-3800),Neurons(5,n-3848:n-3800),'y','linewidth',2);
  313. %
  314. figure(7);
  315. subplot(4,1,1);
  316. stairs((1:48),Neurons(1,1:48),'b','linewidth',2);
  317. subplot(4,1,2);
  318. stairs((1:48),Neurons(2,1:48),'r','linewidth',2);
  319. subplot(4,1,3);
  320. stairs((1:48),Neurons(3,1:48),'g','linewidth',2);
  321. subplot(4,1,4);
  322. stairs((1:48),Neurons(4,1:48),'y','linewidth',2);
  323. % subplot(4,1,5);
  324. % stairs((1:48),Neurons(5,1:48),'y','linewidth',2);
  325.  
  326.  
  327.  
  328.  
  329. %% %% Static Evaluation
  330.  
  331. %maxDNL =zeros(1,length(R12));
  332. %maxINL =zeros(1,length(R12));
  333. %for id = 1:length(R12)
  334. %Input
  335.  
  336. VFS = 1.8;
  337. vin = [0:0.001:15/16*VFS];
  338.  
  339.  
  340. index = length(R12);
  341. R_mean_f = [mean(R14(index - 48:index-1)), mean(R24(index - 48:index-1)),mean(R34(index - 48:index-1)),mean(R13(index - 48:index-1)), mean(R23(index - 48:index-1)),mean(R12(index - 48:index-1))];
  342. vout_f=zeros(length(tr),4);
  343. vout_c=zeros(length(tr),4);
  344. vout_i=zeros(length(tr),4);
  345. out_f = zeros(length(vin),1);
  346. out_i = zeros(length(vin),1);
  347. out_c = zeros(length(vin),1);
  348. for i=1:length(vin)
  349.     for j=1:1
  350.        noise=random('norm',0,sNoise);
  351.        thermnoise = random('norm',0,thNoise);
  352.        vJit=random('norm',0,sJit);
  353.        vout_f(i,1) = activation(vin(i) - 8*Vref);
  354.        vout_f(i,2) = activation(vin(i) +vJit  - 4*Vref - (Rf(3)/(R_mean_f(3)+RMOS(3*10+4) + thermnoise))*vout_f(i,1)*Yvar(4)/18 + noise +VR_parasite);
  355.        vout_f(i,3) = activation(vin(i) +vJit  - 2*Vref - (Rf(2)/(R_mean_f(2)+RMOS(2*10+4)+ thermnoise))*vout_f(i,1)*Yvar(4)/18 - (Rf(2)/(R_mean_f(5)+RMOS(2*10+3)+ thermnoise))*vout_f(i,2)*Yvar(3)/18 + noise +VR_parasite);
  356.        vout_f(i,4) = activation(vin(i)+vJit - 1*Vref - (Rf(1)/(R_mean_f(1)+RMOS(1*10+4)+ thermnoise))*vout_f(i,1)*Yvar(4)/18 - (Rf(2)/(R_mean_f(4)+RMOS(1*10+3)+ thermnoise))*vout_f(i,2)*Yvar(3)/18 - (Rf(1)/(R_mean_f(6)+RMOS(1*10+2)+ thermnoise))*vout_f(i,3)*Yvar(2)/18 + noise +VR_parasite);
  357.  
  358.         for j=1:4
  359.             out_f(i) = out_f(i) + (vout_f(i,j)/1.8)*power(2,4-j);
  360.         end
  361.     end
  362. end
  363.  
  364.  
  365. R_mean_c = [mean(R14(index - 3000:index-2600)), mean(R24(index - 3000:index-2600)),mean(R34(index - 3000:index-2600)),mean(R13(index - 3000:index-2600)), mean(R23(index - 3000:index-2600)),mean(R12(index - 3000:index-2600))];
  366. for i=1:length(vin)
  367.     for j=1:1
  368.        noise=random('norm',0,sNoise);
  369.        thermnoise = random('norm',0,thNoise);
  370.        vJit=random('norm',0,sJit);
  371.        vout_c(i,1) = activation(vin(i) - 8*Vref);
  372.        vout_c(i,2) = activation(vin(i) +vJit  - 4*Vref - (Rf(3)/(R34(200)+RMOS(3*10+4) + thermnoise))*vout_c(i,1)*Yvar(4)/18 + noise +VR_parasite);
  373.        vout_c(i,3) = activation(vin(i) +vJit  - 2*Vref - (Rf(2)/(R24(200)+RMOS(2*10+4)+ thermnoise))*vout_c(i,1)*Yvar(4)/18 - (Rf(2)/(R23(200)+RMOS(2*10+3)+ thermnoise))*vout_c(i,2)*Yvar(3)/18 + noise +VR_parasite);
  374.        vout_c(i,4) = activation(vin(i)+vJit - 1*Vref - (Rf(1)/(R14(200)+RMOS(1*10+4)+ thermnoise))*vout_c(i,1)*Yvar(4)/18 - (Rf(2)/(R13(200)+RMOS(1*10+3)+ thermnoise))*vout_c(i,2)*Yvar(3)/18 - (Rf(1)/(R12(200)+RMOS(1*10+2)+ thermnoise))*vout_c(i,3)*Yvar(2)/18 + noise +VR_parasite);
  375.  
  376.         for j=1:4
  377.             out_c(i) = out_c(i) + (vout_c(i,j)/1.8)*power(2,4-j);
  378.         end
  379.     end
  380. end
  381.  
  382.  
  383. for i=1:length(vin)
  384.     for j=1:1
  385.        noise=random('norm',0,sNoise);
  386.        thermnoise = random('norm',0,thNoise);
  387.        vJit=random('norm',0,sJit);
  388.        vout_i(i,1) = activation(vin(i) - 8*Vref);
  389.        vout_i(i,2) = activation(vin(i) +vJit  - 4*Vref - (Rf(3)/(R34(1)+RMOS(3*10+4) + thermnoise))*vout_i(i,1)*Yvar(4)/18 + noise +VR_parasite);
  390.        vout_i(i,3) = activation(vin(i) +vJit  - 2*Vref - (Rf(2)/(R24(1)+RMOS(2*10+4)+ thermnoise))*vout_i(i,1)*Yvar(4)/18 - (Rf(2)/(R23(1)+RMOS(2*10+3)+ thermnoise))*vout_i(i,2)*Yvar(3)/18 + noise +VR_parasite);
  391.        vout_i(i,4) = activation(vin(i)+vJit - 1*Vref - (Rf(1)/(R14(1)+RMOS(1*10+4)+ thermnoise))*vout_i(i,1)*Yvar(4)/18 - (Rf(2)/(R13(1)+RMOS(1*10+3)+ thermnoise))*vout_i(i,2)*Yvar(3)/18 - (Rf(1)/(R12(1)+RMOS(1*10+2)+ thermnoise))*vout_i(i,3)*Yvar(2)/18 + noise +VR_parasite);
  392.  
  393.         for j=1:4
  394.             out_i(i) = out_i(i) + (vout_i(i,j)/1.8)*power(2,4-j);
  395.         end
  396.     end
  397. end
  398.  
  399.  
  400.  
  401.  
  402.  
  403. LSB=1/16;
  404. out_step_unique = unique(out_f);
  405. out_step = zeros(1,16);
  406. for j=1:length(out_step_unique)
  407.     out_step(out_step_unique(j)+1) = out_step_unique(j);
  408. end
  409.    
  410. w = length(out_step);
  411. s=zeros(1,w);
  412. in=zeros(1,w);
  413. for i=2: w+1
  414.     s(i-1) = find(out_f == out_step(i-1), 1, 'first');
  415.     in(i-1) = vin(s(i-1));
  416. end
  417. %x= diff(in);
  418. steps =zeros(1,15);
  419. last = 0;
  420. for k=1:15
  421.     if in(k) == 0
  422.         steps(k) = 0;
  423.     else
  424.         steps(k) = in(k) - last;
  425.         last = in(k);
  426.     end
  427. end
  428. step_av=mean(steps)
  429. DNL_f= steps/step_av - 1;
  430. DNL_f(1) = 0;
  431. maxDNL_f=max(DNL_f)
  432.  
  433. INL_f=zeros(1,length(out_step));
  434. for i = 1:length(steps)
  435.     INL_f(i) = sum(DNL_f(1:i-1));
  436. end
  437.  
  438.  
  439. maxINL_f=max(INL_f);
  440.  
  441.  
  442. out_step_unique  = unique(out_c);
  443. out_step = zeros(1,16);
  444. for j=1:length(out_step_unique)
  445.     out_step(out_step_unique(j)+1) = out_step_unique(j);
  446. end
  447.  
  448. w = length(out_step);
  449. s=zeros(1,w);
  450. in=zeros(1,w);
  451. for i=2: w+1
  452.     s(i-1) = find(out_f == out_step(i-1), 1, 'first');
  453.     in(i-1) = vin(s(i-1));
  454. end
  455. %x= diff(in);
  456. steps =zeros(1,15);
  457. last = 0;
  458. for k=1:15
  459.     if in(k) == 0
  460.         steps(k) = 0;
  461.     else
  462.         steps(k) = in(k) - last;
  463.         last = in(k);
  464.     end
  465. end
  466. step_av=mean(steps)
  467. DNL_c= steps/step_av - 1;
  468. DNL_c(1) =0;
  469. maxDNL_f=max(DNL_c)
  470.  
  471. INL_c=zeros(1,length(out_step));
  472. for i = 1:length(steps)
  473.     INL_c(i) = sum(DNL_c(1:i-1));
  474. end
  475.  
  476.  
  477. maxINL_c=max(INL_c);
  478.  
  479.  
  480.  
  481. out_step_unique  = unique(out_i);
  482. out_step = zeros(1,16);
  483. for j=1:length(out_step_unique)
  484.     out_step(out_step_unique(j)+1) = out_step_unique(j);
  485. end
  486.  
  487. w = length(out_step);
  488. s=zeros(1,w);
  489. in=zeros(1,w);
  490. for i=2: w+1
  491.     s(i-1) = find(out_i == out_step(i-1), 1, 'first');
  492.     in(i-1) = vin(s(i-1));
  493. end
  494. %x= diff(in);
  495. steps =zeros(1,15);
  496. last = 0;
  497. for k=1:15
  498.     if in(k) == 0
  499.         steps(k) = 0;
  500.     else
  501.         steps(k) = in(k) - last;
  502.         last = in(k);
  503.     end
  504. end
  505. step_av=mean(steps)
  506. DNL_i= steps/step_av - 1;
  507. DNL_i(1) =0;
  508. maxDNL_i=max(DNL_i)
  509.  
  510. INL_i=zeros(1,length(out_step));
  511. for i = 1:length(steps)
  512.     INL_i(i) = sum(DNL_i(1:i-1));
  513. end
  514.  
  515.  
  516. maxINL_i=max(INL_i);
  517.  
  518.  
  519.  
  520.  
  521.  
  522. %end
  523. %close all
  524. figure
  525. hold on
  526. plot((0:14),DNL_f,'b-*','linewidth',2);
  527. plot((0:14),DNL_c,'r--','linewidth',2);
  528. plot((0:14),DNL_i,'g--','linewidth',2);
  529. hold off
  530. ylabel('DNL (LSB)');
  531. xlabel('Input code')
  532. legend('Gradient Descent')
  533. xlim([0 14])
  534. box on
  535. set(gca,'linewidth',3)
  536. set(findall(gcf,'-property','FontSize'),'FontSize',44)
  537. set(findall(gcf,'-property','FontName'),'FontName','Calibri')
  538. set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
  539.  
  540. figure
  541. hold on
  542. plot((0:15),INL_f,'b-*','linewidth',2);
  543. plot((0:15),INL_c,'r--','linewidth',2);
  544. plot((0:15),INL_i,'g--','linewidth',2);
  545. ylabel('INL (LSB)');
  546. xlabel('Input code')
  547. hold off
  548. box on
  549. xlim([0 15])
  550. legend('Gradient Descent')
  551. set(gca,'linewidth',3)
  552. set(findall(gcf,'-property','FontSize'),'FontSize',44)
  553. set(findall(gcf,'-property','FontName'),'FontName','Calibri')
  554. set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement