Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.20 KB | None | 0 0
  1.  
  2. clear all;
  3. close all;
  4. clc;
  5.  
  6.  
  7. % Regular Gradient descent Synapses
  8. NumberOfBits = 8;
  9. R = 1000*[0 30 30 30 30 30 30 30 ; 0 0 30 30 30 30 30 30 ; 0 0 0 30 30 30 30 30 ;0 0 0 0 30 30 30 30 ;0 0 0 0 0 30 30 30; 0 0 0 0 0 0 30 30; 0 0 0 0 0 0 0 30; 0 0 0 0 0 0 0 0];
  10.  
  11. R12 = 30000;
  12. R13 = 30000;
  13. R14 = 30000;
  14. R15 = 30000;
  15. R16 = 30000;
  16. R17 = 30000;
  17. R18 = 30000;
  18. R23 = 30000;
  19. R24 = 30000;
  20. R34 = 30000;
  21. R38 = 30000;
  22. R58 = 30000;
  23. R37 = 30000;
  24. R57 = 30000;
  25. R36 = 30000;
  26. R35 = 30000;
  27.  
  28.  
  29. % Define constants
  30. tau = 400e-12; % RC constant
  31. fs=100e3;
  32. fi = 1/(160e-6);
  33. cycles=512;
  34. N=fs/fi*cycles;
  35. ts = (0:1/fs:(50e-3)-1/fs);
  36. Ns= 5e3;
  37.  
  38. % CLK
  39. tr=1/fs*(1:N);
  40. tf=tr+1/(2*fs);
  41. dt=1/(2*fs);
  42.  
  43.  
  44. prompt = 'Enter "1" for fixed wieghts equations ';
  45. fixed = input(prompt);
  46.  
  47. prompt = 'Enter "1" for non_ideal equations ';
  48. non_ideal = input(prompt);
  49.  
  50.  
  51. if non_ideal == 1
  52.  
  53. %%%%%%%%%%%%%%%%
  54. % Noise sources
  55. %%%%%%%%%%%%%%%%
  56. % Comparator mismatch
  57. kVth=5e-3;
  58. area_tran=100;
  59. sVth=kVth/sqrt(area_tran);
  60.  
  61. % There are two transistors in a differential comparator
  62. sVth=sqrt(2*sVth^2);
  63. offset=random('norm',0,sVth,1,length(ts));
  64.  
  65. % Comparator input noise
  66. sNoise=(10e-9)*sqrt(6.25e3);
  67. %noise=random('norm',0,sNoise,1,length(ts));
  68.  
  69. %Thermal noise
  70. thNoise=10e-16;
  71.  
  72. % Resistance mismatch
  73. sR = 0.05/sqrt(32);
  74.  
  75. frec=1;
  76. % Jitter
  77. sJit=20e-3; %frequency dependent
  78. vJit=random('norm',0,sJit,1,length(ts));
  79.  
  80.  
  81. % Resistance ladder
  82. Rf= normrnd(45e3,sR*(45e3),1,8);
  83. %Rf = 90e3;
  84. Rin= normrnd(45e3,sR*(45e3),1,8);
  85. RRef = normrnd([45e3 22.5e3 11.25e3 5.625e3], sR*(22.5e3),1,4);
  86. else
  87. %%%%%%%%%%%%%%%%
  88. % Noise sources
  89. %%%%%%%%%%%%%%%%
  90. % Comparator mismatch
  91. kVth= 0;%5e-3;
  92. area_tran=100;
  93. sVth=kVth/sqrt(area_tran);
  94.  
  95. % There are two transistors in a differential comparator
  96. sVth=0;%sqrt(2*sVth^2);
  97. offset=0;%random('norm',0,sVth,1,length(ts));
  98.  
  99. % Comparator input noise
  100. sNoise=0;%(10e-9)*sqrt(6.25e3);
  101.  
  102. %Thermal noise
  103. thNoise=0;%10e-16;
  104.  
  105. % Resistance mismatch
  106. sR = 0;%0.05/sqrt(32);
  107.  
  108. frec=1;
  109. % Jitter
  110. sJit=0;%20e-3; %frequency dependent
  111. vJit=0;%random('norm',0,sJit,1,length(ts));
  112.  
  113. % Resistance ladder
  114. Rf = 45e3*ones(1,10);%normrnd(45e3,sR*(45e3),1,6);
  115. Rf_b = 45e3*ones(1,6);%normrnd(45e3,sR*(45e3),1,6);
  116. Rf= 45e3;%normrnd(45e3,sR*(45e3)); % for dac
  117. %Rf = 90e3;
  118. Rin= normrnd(45e3,sR*(45e3),1,4);
  119. RRef = normrnd([45e3 22.5e3 11.25e3 5.625e3], sR*(22.5e3),1,4);
  120.  
  121. %learning parameters
  122.  
  123. RMOS_a = 700*ones(1,100);%normrnd(700,sR*(700),1,100);
  124. RMOS_b = 700*ones(1,100);%normrnd(700,sR*(700),1,100);
  125. RMOS = 700*ones(1,700); %normrnd(700,sR*(700),1,6); %for dac
  126. Yvar_a = ones(1,4);%normrnd(1,0.1*(1),1,4);
  127. Yvar_b = ones(1,4);%normrnd(1,0.1*(1),1,4);
  128. Yvar = ones(1,8);%normrnd(1,0.1*(1),1,4);
  129. Kon_a = (-4.8e-3)*ones(1,100);%normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
  130. Kon_b = (-4.8e-3)*ones(1,100);%normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
  131. Koff_a = (2.8125e-3)*ones(1,100);%normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
  132. Koff_b = (2.8125e-3)*ones(1,100);%normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
  133. Kon = (-4.8e-3)*ones(1,6);%normrnd(-4.8e-3,0.1*(4.8e-3),1,6); %for dac
  134. Koff = (2.8125e-3)*ones(1,6);%normrnd(2.8125e-3,0.1*(2.8125e-3),1,6);
  135. Von = -0.3;
  136. Voff = 0.4;
  137.  
  138. Roff_a = (100e3)*ones(1,100);%normrnd(100e3,sR*(100e3),1,100);
  139. Roff_b = (100e3)*ones(1,100);%normrnd(100e3,sR*(100e3),1,100);
  140. Ron_a = (2000)*ones(1,100);%normrnd(2000,sR*(2000),1,100);
  141. Ron_b = (2000)*ones(1,100);%normrnd(2000,sR*(2000),1,100);
  142. Roff = (100e3)*ones(1,6);%normrnd(100e3,sR*(100e3),1,6); %for dac
  143. Ron = (2000)*ones(1,6);%normrnd(2000,sR*(2000),1,6);
  144. VR_parasite = 0;%0.015; %frequency dependent
  145. end
  146.  
  147. %Input
  148. %vin = 1.8/2*(1 + sawtooth(ts*fi*(2*pi)));
  149. alpha = 1.8/256;
  150. %vin=linspace(0,1.8-alpha, length(tr));
  151. vin = [0:alpha:1.8 - alpha];
  152.  
  153.  
  154. % Cap response
  155. vC=zeros(1,length(ts));
  156. %for i=1:length(ts)
  157. % vC(i)=vin(i)*(1-exp(-dt/tau))+noise(i);
  158. %end
  159.  
  160. %learning parameters
  161. Vref = 0.00703125;
  162. Y = zeros(1,8);
  163. E_threshold = 1e-8;
  164. T_write = 5e-6;
  165. dt = 0.54e-8;
  166. %RMOS = 450;
  167. %K = 1e-3;
  168. Vp = 0.5;
  169. %Ron = 100;
  170. %Roff = 200e3;
  171. RMOS = normrnd(700,sR*(700),1,100);
  172. Yvar = normrnd(1,0.1*(1),1,8);
  173. Kon = normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
  174. Koff = normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
  175. Von = -0.3;
  176. Voff = 0.4;
  177. %Von = normrnd(0.3,0.033*(0.3),1,6);
  178. %Vp = normrnd(0.5,0.1*0.5);
  179. %Vpgd = normrnd(0.4,0.1*0.4);
  180. Roff = normrnd(100e3,sR*(100e3),1,100);
  181. Ron = normrnd(2000,sR*(2000),1,100);
  182. VR_parasite = 0.015; %frequency dependent
  183.  
  184. E = 1 ;
  185. E_rate = 1;
  186.  
  187.  
  188. %Digital data set
  189. D = fliplr(1.8*[0 0 0 0 0 0 0 0;1 0 0 0 0 0 0 0;0 1 0 0 0 0 0 0;1 1 0 0 0 0 0 0; 0 0 1 0 0 0 0 0; 1 0 1 0 0 0 0 0;
  190. 0 1 1 0 0 0 0 0;
  191. 1 1 1 0 0 0 0 0;
  192. 0 0 0 1 0 0 0 0;
  193. 1 0 0 1 0 0 0 0;
  194. 0 1 0 1 0 0 0 0;
  195. 1 1 0 1 0 0 0 0;
  196. 0 0 1 1 0 0 0 0;
  197. 1 0 1 1 0 0 0 0;
  198. 0 1 1 1 0 0 0 0;
  199. 1 1 1 1 0 0 0 0;
  200. 0 0 0 0 1 0 0 0;
  201. 1 0 0 0 1 0 0 0;
  202. 0 1 0 0 1 0 0 0;
  203. 1 1 0 0 1 0 0 0;
  204. 0 0 1 0 1 0 0 0;
  205. 1 0 1 0 1 0 0 0;
  206. 0 1 1 0 1 0 0 0;
  207. 1 1 1 0 1 0 0 0;
  208. 0 0 0 1 1 0 0 0;
  209. 1 0 0 1 1 0 0 0;
  210. 0 1 0 1 1 0 0 0;
  211. 1 1 0 1 1 0 0 0;
  212. 0 0 1 1 1 0 0 0;
  213. 1 0 1 1 1 0 0 0;
  214. 0 1 1 1 1 0 0 0;
  215. 1 1 1 1 1 0 0 0;
  216. 0 0 0 0 0 1 0 0;
  217. 1 0 0 0 0 1 0 0;
  218. 0 1 0 0 0 1 0 0;
  219. 1 1 0 0 0 1 0 0;
  220. 0 0 1 0 0 1 0 0;
  221. 1 0 1 0 0 1 0 0;
  222. 0 1 1 0 0 1 0 0;
  223. 1 1 1 0 0 1 0 0;
  224. 0 0 0 1 0 1 0 0;
  225. 1 0 0 1 0 1 0 0;
  226. 0 1 0 1 0 1 0 0;
  227. 1 1 0 1 0 1 0 0;
  228. 0 0 1 1 0 1 0 0;
  229. 1 0 1 1 0 1 0 0;
  230. 0 1 1 1 0 1 0 0;
  231. 1 1 1 1 0 1 0 0;
  232. 0 0 0 0 1 1 0 0;
  233. 1 0 0 0 1 1 0 0;
  234. 0 1 0 0 1 1 0 0;
  235. 1 1 0 0 1 1 0 0;
  236. 0 0 1 0 1 1 0 0;
  237. 1 0 1 0 1 1 0 0;
  238. 0 1 1 0 1 1 0 0;
  239. 1 1 1 0 1 1 0 0;
  240. 0 0 0 1 1 1 0 0;
  241. 1 0 0 1 1 1 0 0;
  242. 0 1 0 1 1 1 0 0;
  243. 1 1 0 1 1 1 0 0;
  244. 0 0 1 1 1 1 0 0;
  245. 1 0 1 1 1 1 0 0;
  246. 0 1 1 1 1 1 0 0;
  247. 1 1 1 1 1 1 0 0;
  248. 0 0 0 0 0 0 1 0;
  249. 1 0 0 0 0 0 1 0;
  250. 0 1 0 0 0 0 1 0;
  251. 1 1 0 0 0 0 1 0;
  252. 0 0 1 0 0 0 1 0;
  253. 1 0 1 0 0 0 1 0;
  254. 0 1 1 0 0 0 1 0;
  255. 1 1 1 0 0 0 1 0;
  256. 0 0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  
  283.  
  284. n = 0;
  285.  
  286. t = 1;
  287. A = 0;
  288. DAC = zeros(1, length(vin));
  289. Neurons = zeros(8, length(vin));
  290. vout = zeros(1, length(vin));
  291.  
  292.  
  293. %binary-weighted gradient descent Training loop
  294. W_prev = 1/2.+zeros(1,100);
  295. gamma = 1;
  296. for l = 1:40 %2000 samples 20ms
  297. Ptot_neuron = 0;
  298. Ptot_net = 0;
  299. Ptot_comparator = 0;
  300. for x = 1: length(vin)
  301. %k = randi([1,length(vin)]); %random dataset
  302. k = x;
  303.  
  304. noise=random('norm',0,sNoise);
  305. thermnoise = random('norm',0,thNoise);
  306. vJit=random('norm',0,sJit);
  307.  
  308. if vin(k)> 1.793
  309. vin(k)=0;
  310. end
  311. indx = int16(vin(k)/alpha) + 1;
  312.  
  313. n = n+1;
  314. vC(n)=vin(k)*(1-exp(-dt/tau));
  315. if fixed == 1
  316.  
  317. [Y(8),Y(7)] = activation_flash_adc( vin(k) , 1.8);
  318. [Y(6),Y(5)] = activation_flash_adc( (vin(k) - 64*Y(7)/256 - 128*Y(8)/256) , 1.8/4);
  319. [Y(4),Y(3)] = activation_flash_adc( (vin(k) - 16*Y(5)/256 - 32*Y(6)/256 - 64*Y(7)/256 - 128*Y(8)/256) , 1.8/16);
  320. [Y(2),Y(1)] = activation_flash_adc( (vin(k) - 4*Y(3)/256 - 8*Y(4)/256 - 16*Y(5)/256 - 32*Y(6)/256 - 64*Y(7)/256 - 128*Y(8)/256) , 1.8/64);
  321.  
  322.  
  323. else
  324.  
  325. [Y(8),Y(7)] = activation_flash_adc( vin(k) , 1.8);
  326. [Y(6),Y(5)] = activation_flash_adc( (vin(k) - (Rf(5)/(R(5,8)+RMOS(5*10+8)+ thermnoise))*Y(8)*Yvar(8)/18 - (Rf(5)/(R(5,7)+RMOS(5*10+7)+ thermnoise))*Y(7)*Yvar(7)/18 ) , 1.8/4);
  327. [Y(4),Y(3)] = activation_flash_adc( (vin(k) - (Rf(3)/(R(3,8)+RMOS(3*10+8)+ thermnoise))*Y(8)*Yvar(8)/18 - (Rf(3)/(R(3,7)+RMOS(3*10+7)+ thermnoise))*Y(7)*Yvar(7)/18 - (Rf(3)/(R(3,6)+RMOS(3*10+6)+ thermnoise))*Y(6)*Yvar(6)/18 - (Rf(3)/(R(3,5)+RMOS(3*10+5)+ thermnoise))*Y(5)*Yvar(5)/18 ) , 1.8/16);
  328. [Y(2),Y(1)] = activation_flash_adc( (vin(k) - (Rf(1)/(R(1,8)+RMOS(1*10+8)+ thermnoise))*Y(8)*Yvar(8)/18 - (Rf(1)/(R(1,7)+RMOS(1*10+7)+ thermnoise))*Y(7)*Yvar(7)/18 - (Rf(1)/(R(1,6)+RMOS(1*10+6)+ thermnoise))*Y(6)*Yvar(6)/18 - (Rf(1)/(R(1,5)+RMOS(1*10+5)+ thermnoise))*Y(5)*Yvar(5)/18 - (Rf(1)/(R(1,4)+RMOS(1*10+4)+ thermnoise))*Y(4)*Yvar(4)/18 - (Rf(1)/(R(1,3)+RMOS(1*10+3)+ thermnoise))*Y(3)*Yvar(3)/18 ) , 1.8/64);
  329.  
  330. end
  331.  
  332. Rfed=45e3;
  333. P1_neuron = (vin(k) - 8*Vref )^2/Rfed ;
  334. P1_net = vin(k)^2/Rfed + Vref^2/Rfed/8;
  335. P2_neuron = (vin(k) - 4*Vref - (Rfed/(R(3,4)+450))*Y(4)/18)^2/Rfed ;
  336. P2_net = vin(k)^2/Rfed + Vref^2/Rfed/4 + (Y(4)/18)^2/(R(3,4)+450);
  337. P3_neuron = (vin(k) - 2*Vref - (Rfed/(R(2,4)+450))*Y(4)/18 - (Rfed/(R(2,3)+450))*Y(3)/18)^2/Rfed ;
  338. 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);
  339. 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 ;
  340. 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);
  341. %Ptot_net = Ptot_net + P1_net + P2_net + P3_net + P4_net ;
  342. Ptot_neuron = Ptot_neuron + P1_neuron + P2_neuron + P3_neuron + P4_neuron ;
  343. %Ptot_comparator = Ptot_comparator + 4*(1e-8);
  344.  
  345. fliplr(Y);
  346. D(ceil(indx),:);
  347. E(end + 1) = (0.125/(1.8^2))*((Y(1) - D(ceil(indx),8))^2 + (Y(2) - D(ceil(indx),7))^2 + (Y(3) - D(ceil(indx),6))^2 + (Y(4) - D(ceil(indx),5))^2 + (Y(5) - D(ceil(indx),4))^2 + (Y(6) - D(ceil(indx),3))^2 +(Y(7) - D(ceil(indx),2))^2 + (Y(8) - D(ceil(indx),1))^2 + 0.01*rand()^2) + E(end);
  348. if E(end) == 0 %false positive case
  349. continue;
  350. end
  351. DAC(n) = 1/256*(1*Y(1)+ 2*Y(2) + 4*Y(3) + 8*Y(4) + 16*Y(5) + 32*Y(6) + 64*Y(7) + 128*Y(8));
  352.  
  353. Neurons(1,n) = Y(8);
  354. Neurons(2,n) = Y(7);
  355. Neurons(3,n) = Y(6);
  356. Neurons(4,n) = Y(5);
  357. Neurons(5,n) = Y(4);
  358. Neurons(6,n) = Y(3);
  359. Neurons(7,n) = Y(2);
  360. Neurons(8,n) = Y(1);
  361. vout(n) = vin(k);
  362.  
  363. E_rate(end+1) = (1/n)*E(end);
  364. t(end+1) = t(end) + 1;
  365. if E_rate(end) < E_threshold
  366. break;
  367. end
  368.  
  369. for i = 1:8
  370. for j = 1:8
  371. if i >= j
  372. R(i,j) = 0;
  373. else
  374. x = 10*i +j;
  375. W_f = W_prev(x)*(1-W_prev(x));
  376. if (D(ceil(indx),9-i) - Y(i))>=0
  377. Vw = -1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
  378. dW = -1*Kon(x)*((Vw/Von -1)^3)*W_f;
  379. else
  380. Vw = 1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
  381. dW = Koff(x)*((Vw/Voff -1)^1)*W_f;
  382. end
  383.  
  384. if n < 1600
  385. gamma = 1;
  386. elseif n>=1600 && n<2400
  387. gamma = 0.5;
  388. elseif n>=2800 && n<3200
  389. gamma = 0.25;
  390. else
  391. gamma = 0.03;
  392. end
  393.  
  394. beta = gamma*0.09*(Roff(x)-Ron(x))*dW*T_write/dt;
  395.  
  396. %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 >= Roff(x)
  397. % continue
  398. %end
  399. %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 <= Ron(x)
  400. % continue
  401. %end
  402.  
  403. %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;
  404. R(i,j) = R(i,j) + beta*(D(ceil(indx),9-i) - Y(i)+ 0.0001*rand())*D(ceil(indx),9-j)/(1.8^2);
  405. W_prev(x) = R(i,j)/(Roff(x) - Ron(x));
  406. end
  407. end
  408. end
  409. R12(end+1) = R(1,2);
  410. R13(end+1) = R(1,3);
  411. R14(end+1) = R(1,4);
  412. R23(end+1) = R(2,3);
  413. R24(end+1) = R(2,4);
  414. R34(end+1) = R(3,4);
  415.  
  416.  
  417. end
  418. if E_rate(end) < E_threshold
  419. break;
  420. end
  421. P(l) = (Ptot_comparator + Ptot_net + Ptot_neuron )/2^4;
  422. end
  423.  
  424.  
  425. %%
  426. figure(1);
  427. hold on;
  428. ylabel('Training Error','FontSize',18);
  429. xlabel('#Samples','FontSize',18);
  430.  
  431.  
  432. plot(t,E_rate,'LineWidth',4);
  433. grid on;
  434. leg = legend('Algorithm');
  435. set(leg,'FontSize',12);
  436. set(gca,'FontSize',12);
  437.  
  438.  
  439. figure(3);
  440. hold on;
  441. plot(t,(45e3)./R12);
  442. plot(t,(45e3)./R13);
  443. plot(t,(45e3)./R14);
  444. plot(t,(45e3)./R23);
  445. plot(t,(45e3)./R24);
  446. plot(t,(45e3)./R34);
  447.  
  448. figure;
  449. plot(P);
  450.  
  451. % vout = vout*1.8/28.8;
  452. % DAC = DAC*1.8/28.8;
  453. tf = t(1:length(t)-1);
  454. figure(4);
  455. hold on;
  456. stairs(tf,DAC,'-or'), xlim([n-300 n]);
  457. stairs(tf,vout), xlim([n-300 n]);
  458.  
  459. figure(5);
  460. subplot(8,1,1);
  461. stairs((n-48:n),Neurons(1,n-48:n),'b','linewidth',2);
  462. subplot(8,1,2);
  463. stairs((n-48:n),Neurons(2,n-48:n),'r','linewidth',2);
  464. subplot(8,1,3);
  465. stairs((n-48:n),Neurons(3,n-48:n),'g','linewidth',2);
  466. subplot(8,1,4);
  467. stairs((n-48:n),Neurons(4,n-48:n),'y','linewidth',2);
  468. subplot(8,1,5);
  469. stairs((n-48:n),Neurons(5,n-48:n),'b','linewidth',2);
  470. subplot(8,1,6);
  471. stairs((n-48:n),Neurons(6,n-48:n),'r','linewidth',2);
  472. subplot(8,1,7);
  473. stairs((n-48:n),Neurons(7,n-48:n),'g','linewidth',2);
  474. subplot(8,1,8);
  475. stairs((n-48:n),Neurons(8,n-48:n),'y','linewidth',2);
  476. % subplot(4,1,5);
  477. % stairs((n-48:n),Neurons(5,n-48:n),'y','linewidth',2);
  478.  
  479.  
  480. figure(6);
  481. subplot(4,1,1);
  482. stairs((n-3848:n-3800),Neurons(1,n-3848:n-3800),'b','linewidth',2);
  483. subplot(4,1,2);
  484. stairs((n-3848:n-3800),Neurons(2,n-3848:n-3800),'r','linewidth',2);
  485. subplot(4,1,3);
  486. stairs((n-3848:n-3800),Neurons(3,n-3848:n-3800),'g','linewidth',2);
  487. subplot(4,1,4);
  488. stairs((n-3848:n-3800),Neurons(4,n-3848:n-3800),'y','linewidth',2);
  489. % subplot(4,1,5);
  490. stairs((n-3848:n-3800),Neurons(5,n-3848:n-3800),'y','linewidth',2);
  491.  
  492. figure(7);
  493. subplot(4,1,1);
  494. stairs((1:48),Neurons(1,1:48),'b','linewidth',2);
  495. subplot(4,1,2);
  496. stairs((1:48),Neurons(2,1:48),'r','linewidth',2);
  497. subplot(4,1,3);
  498. stairs((1:48),Neurons(3,1:48),'g','linewidth',2);
  499. subplot(4,1,4);
  500. stairs((1:48),Neurons(4,1:48),'y','linewidth',2);
  501. % subplot(4,1,5);
  502. % stairs((1:48),Neurons(5,1:48),'y','linewidth',2);
  503.  
  504.  
  505.  
  506.  
  507. %% %% Static Evaluation
  508.  
  509. %maxDNL =zeros(1,length(R12));
  510. %maxINL =zeros(1,length(R12));
  511. %for id = 1:length(R12)
  512. %Input
  513.  
  514. VFS = 1.8;
  515. vin = [0:0.001:15/16*VFS];
  516.  
  517.  
  518. index = length(R12);
  519. 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))];
  520. vout_f=zeros(length(tr),4);
  521. vout_c=zeros(length(tr),4);
  522. vout_i=zeros(length(tr),4);
  523. out_f = zeros(length(vin),1);
  524. out_i = zeros(length(vin),1);
  525. out_c = zeros(length(vin),1);
  526. for i=1:length(vin)
  527. for j=1:1
  528. noise=random('norm',0,sNoise);
  529. thermnoise = random('norm',0,thNoise);
  530. vJit=random('norm',0,sJit);
  531. vout_f(i,1) = activation(vin(i) - 8*Vref);
  532. 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);
  533. 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);
  534. 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);
  535.  
  536. for j=1:4
  537. out_f(i) = out_f(i) + (vout_f(i,j)/1.8)*power(2,4-j);
  538. end
  539. end
  540. end
  541.  
  542.  
  543. 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))];
  544. for i=1:length(vin)
  545. for j=1:1
  546. noise=random('norm',0,sNoise);
  547. thermnoise = random('norm',0,thNoise);
  548. vJit=random('norm',0,sJit);
  549. vout_c(i,1) = activation(vin(i) - 8*Vref);
  550. 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);
  551. 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);
  552. 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);
  553.  
  554. for j=1:4
  555. out_c(i) = out_c(i) + (vout_c(i,j)/1.8)*power(2,4-j);
  556. end
  557. end
  558. end
  559.  
  560.  
  561. for i=1:length(vin)
  562. for j=1:1
  563. noise=random('norm',0,sNoise);
  564. thermnoise = random('norm',0,thNoise);
  565. vJit=random('norm',0,sJit);
  566. vout_i(i,1) = activation(vin(i) - 8*Vref);
  567. 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);
  568. 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);
  569. 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);
  570.  
  571. for j=1:4
  572. out_i(i) = out_i(i) + (vout_i(i,j)/1.8)*power(2,4-j);
  573. end
  574. end
  575. end
  576.  
  577.  
  578. LSB=1/16;
  579. out_step_unique = unique(out_f);
  580. out_step = zeros(1,16);
  581. for j=1:length(out_step_unique)
  582. out_step(out_step_unique(j)+1) = out_step_unique(j);
  583. end
  584.  
  585. w = length(out_step);
  586. s=zeros(1,w);
  587. in=zeros(1,w);
  588. for i=2: w+1
  589. s(i-1) = find(out_f == out_step(i-1), 1, 'first');
  590. in(i-1) = vin(s(i-1));
  591. end
  592. %x= diff(in);
  593. steps =zeros(1,15);
  594. last = 0;
  595. for k=1:15
  596. if in(k) == 0
  597. steps(k) = 0;
  598. else
  599. steps(k) = in(k) - last;
  600. last = in(k);
  601. end
  602. end
  603. step_av=mean(steps)
  604. DNL_f= steps/step_av - 1;
  605. DNL_f(1) = 0;
  606. maxDNL_f=max(DNL_f)
  607.  
  608. INL_f=zeros(1,length(out_step));
  609. for i = 1:length(steps)
  610. INL_f(i) = sum(DNL_f(1:i-1));
  611. end
  612.  
  613.  
  614. maxINL_f=max(INL_f);
  615.  
  616.  
  617. out_step_unique = unique(out_c);
  618. out_step = zeros(1,16);
  619. for j=1:length(out_step_unique)
  620. out_step(out_step_unique(j)+1) = out_step_unique(j);
  621. end
  622.  
  623. w = length(out_step);
  624. s=zeros(1,w);
  625. in=zeros(1,w);
  626. for i=2: w+1
  627. s(i-1) = find(out_f == out_step(i-1), 1, 'first');
  628. in(i-1) = vin(s(i-1));
  629. end
  630. %x= diff(in);
  631. steps =zeros(1,15);
  632. last = 0;
  633. for k=1:15
  634. if in(k) == 0
  635. steps(k) = 0;
  636. else
  637. steps(k) = in(k) - last;
  638. last = in(k);
  639. end
  640. end
  641. step_av=mean(steps)
  642. DNL_c= steps/step_av - 1;
  643. DNL_c(1) =0;
  644. maxDNL_f=max(DNL_c)
  645.  
  646. INL_c=zeros(1,length(out_step));
  647. for i = 1:length(steps)
  648. INL_c(i) = sum(DNL_c(1:i-1));
  649. end
  650.  
  651.  
  652. maxINL_c=max(INL_c);
  653.  
  654.  
  655.  
  656. out_step_unique = unique(out_i);
  657. out_step = zeros(1,16);
  658. for j=1:length(out_step_unique)
  659. out_step(out_step_unique(j)+1) = out_step_unique(j);
  660. end
  661.  
  662. w = length(out_step);
  663. s=zeros(1,w);
  664. in=zeros(1,w);
  665. for i=2: w+1
  666. s(i-1) = find(out_i == out_step(i-1), 1, 'first');
  667. in(i-1) = vin(s(i-1));
  668. end
  669. %x= diff(in);
  670. steps =zeros(1,15);
  671. last = 0;
  672. for k=1:15
  673. if in(k) == 0
  674. steps(k) = 0;
  675. else
  676. steps(k) = in(k) - last;
  677. last = in(k);
  678. end
  679. end
  680. step_av=mean(steps)
  681. DNL_i= steps/step_av - 1;
  682. DNL_i(1) =0;
  683. maxDNL_i=max(DNL_i)
  684.  
  685. INL_i=zeros(1,length(out_step));
  686. for i = 1:length(steps)
  687. INL_i(i) = sum(DNL_i(1:i-1));
  688. end
  689.  
  690.  
  691. maxINL_i=max(INL_i);
  692.  
  693.  
  694.  
  695.  
  696.  
  697. %end
  698. %close all
  699. figure
  700. hold on
  701. plot((0:14),DNL_f,'b-*','linewidth',2);
  702. plot((0:14),DNL_c,'r--','linewidth',2);
  703. plot((0:14),DNL_i,'g--','linewidth',2);
  704. hold off
  705. ylabel('DNL (LSB)');
  706. xlabel('Input code')
  707. legend('Gradient Descent')
  708. xlim([0 14])
  709. box on
  710. set(gca,'linewidth',3)
  711. set(findall(gcf,'-property','FontSize'),'FontSize',44)
  712. set(findall(gcf,'-property','FontName'),'FontName','Calibri')
  713. set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
  714.  
  715. figure
  716. hold on
  717. plot((0:15),INL_f,'b-*','linewidth',2);
  718. plot((0:15),INL_c,'r--','linewidth',2);
  719. plot((0:15),INL_i,'g--','linewidth',2);
  720. ylabel('INL (LSB)');
  721. xlabel('Input code')
  722. hold off
  723. box on
  724. xlim([0 15])
  725. legend('Gradient Descent')
  726. set(gca,'linewidth',3)
  727. set(findall(gcf,'-property','FontSize'),'FontSize',44)
  728. set(findall(gcf,'-property','FontName'),'FontName','Calibri')
  729. set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement