Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all;
- close all;
- clc;
- % Regular Gradient descent Synapses
- NumberOfBits = 8;
- 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];
- R12 = 30000;
- R13 = 30000;
- R14 = 30000;
- R15 = 30000;
- R16 = 30000;
- R17 = 30000;
- R18 = 30000;
- R23 = 30000;
- R24 = 30000;
- R34 = 30000;
- R38 = 30000;
- R58 = 30000;
- R37 = 30000;
- R57 = 30000;
- R36 = 30000;
- R35 = 30000;
- % Define constants
- tau = 400e-12; % RC constant
- fs=100e3;
- fi = 1/(160e-6);
- cycles=512;
- N=fs/fi*cycles;
- ts = (0:1/fs:(50e-3)-1/fs);
- Ns= 5e3;
- % CLK
- tr=1/fs*(1:N);
- tf=tr+1/(2*fs);
- dt=1/(2*fs);
- prompt = 'Enter "1" for fixed wieghts equations ';
- fixed = input(prompt);
- prompt = 'Enter "1" for non_ideal equations ';
- non_ideal = input(prompt);
- if non_ideal == 1
- %%%%%%%%%%%%%%%%
- % Noise sources
- %%%%%%%%%%%%%%%%
- % Comparator mismatch
- kVth=5e-3;
- area_tran=100;
- sVth=kVth/sqrt(area_tran);
- % There are two transistors in a differential comparator
- sVth=sqrt(2*sVth^2);
- offset=random('norm',0,sVth,1,length(ts));
- % Comparator input noise
- sNoise=(10e-9)*sqrt(6.25e3);
- %noise=random('norm',0,sNoise,1,length(ts));
- %Thermal noise
- thNoise=10e-16;
- % Resistance mismatch
- sR = 0.05/sqrt(32);
- frec=1;
- % Jitter
- sJit=20e-3; %frequency dependent
- vJit=random('norm',0,sJit,1,length(ts));
- % Resistance ladder
- Rf= normrnd(45e3,sR*(45e3),1,8);
- %Rf = 90e3;
- Rin= normrnd(45e3,sR*(45e3),1,8);
- RRef = normrnd([45e3 22.5e3 11.25e3 5.625e3], sR*(22.5e3),1,4);
- else
- %%%%%%%%%%%%%%%%
- % Noise sources
- %%%%%%%%%%%%%%%%
- % Comparator mismatch
- kVth= 0;%5e-3;
- area_tran=100;
- sVth=kVth/sqrt(area_tran);
- % There are two transistors in a differential comparator
- sVth=0;%sqrt(2*sVth^2);
- offset=0;%random('norm',0,sVth,1,length(ts));
- % Comparator input noise
- sNoise=0;%(10e-9)*sqrt(6.25e3);
- %Thermal noise
- thNoise=0;%10e-16;
- % Resistance mismatch
- sR = 0;%0.05/sqrt(32);
- frec=1;
- % Jitter
- sJit=0;%20e-3; %frequency dependent
- vJit=0;%random('norm',0,sJit,1,length(ts));
- % Resistance ladder
- Rf = 45e3*ones(1,10);%normrnd(45e3,sR*(45e3),1,6);
- Rf_b = 45e3*ones(1,6);%normrnd(45e3,sR*(45e3),1,6);
- Rf= 45e3;%normrnd(45e3,sR*(45e3)); % for dac
- %Rf = 90e3;
- Rin= normrnd(45e3,sR*(45e3),1,4);
- RRef = normrnd([45e3 22.5e3 11.25e3 5.625e3], sR*(22.5e3),1,4);
- %learning parameters
- RMOS_a = 700*ones(1,100);%normrnd(700,sR*(700),1,100);
- RMOS_b = 700*ones(1,100);%normrnd(700,sR*(700),1,100);
- RMOS = 700*ones(1,700); %normrnd(700,sR*(700),1,6); %for dac
- Yvar_a = ones(1,4);%normrnd(1,0.1*(1),1,4);
- Yvar_b = ones(1,4);%normrnd(1,0.1*(1),1,4);
- Yvar = ones(1,8);%normrnd(1,0.1*(1),1,4);
- Kon_a = (-4.8e-3)*ones(1,100);%normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
- Kon_b = (-4.8e-3)*ones(1,100);%normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
- Koff_a = (2.8125e-3)*ones(1,100);%normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
- Koff_b = (2.8125e-3)*ones(1,100);%normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
- Kon = (-4.8e-3)*ones(1,6);%normrnd(-4.8e-3,0.1*(4.8e-3),1,6); %for dac
- Koff = (2.8125e-3)*ones(1,6);%normrnd(2.8125e-3,0.1*(2.8125e-3),1,6);
- Von = -0.3;
- Voff = 0.4;
- Roff_a = (100e3)*ones(1,100);%normrnd(100e3,sR*(100e3),1,100);
- Roff_b = (100e3)*ones(1,100);%normrnd(100e3,sR*(100e3),1,100);
- Ron_a = (2000)*ones(1,100);%normrnd(2000,sR*(2000),1,100);
- Ron_b = (2000)*ones(1,100);%normrnd(2000,sR*(2000),1,100);
- Roff = (100e3)*ones(1,6);%normrnd(100e3,sR*(100e3),1,6); %for dac
- Ron = (2000)*ones(1,6);%normrnd(2000,sR*(2000),1,6);
- VR_parasite = 0;%0.015; %frequency dependent
- end
- %Input
- %vin = 1.8/2*(1 + sawtooth(ts*fi*(2*pi)));
- alpha = 1.8/256;
- %vin=linspace(0,1.8-alpha, length(tr));
- vin = [0:alpha:1.8 - alpha];
- % Cap response
- vC=zeros(1,length(ts));
- %for i=1:length(ts)
- % vC(i)=vin(i)*(1-exp(-dt/tau))+noise(i);
- %end
- %learning parameters
- Vref = 0.00703125;
- Y = zeros(1,8);
- E_threshold = 1e-8;
- T_write = 5e-6;
- dt = 0.54e-8;
- %RMOS = 450;
- %K = 1e-3;
- Vp = 0.5;
- %Ron = 100;
- %Roff = 200e3;
- RMOS = normrnd(700,sR*(700),1,100);
- Yvar = normrnd(1,0.1*(1),1,8);
- Kon = normrnd(-4.8e-3,0.01*(4.8e-3),1,100);
- Koff = normrnd(2.8125e-3,0.01*(2.8125e-3),1,100);
- Von = -0.3;
- Voff = 0.4;
- %Von = normrnd(0.3,0.033*(0.3),1,6);
- %Vp = normrnd(0.5,0.1*0.5);
- %Vpgd = normrnd(0.4,0.1*0.4);
- Roff = normrnd(100e3,sR*(100e3),1,100);
- Ron = normrnd(2000,sR*(2000),1,100);
- VR_parasite = 0.015; %frequency dependent
- E = 1 ;
- E_rate = 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0 1 1 0 1 1 1;0 1 1 1 0 1 1 1;1 1 1 1 0 1 1 1;0 0 0 0 1 1 1 1;1 0 0 0 1 1 1 1;0 1 0 0 1 1 1 1;1 1 0 0 1 1 1 1;0 0 1 0 1 1 1 1;1 0 1 0 1 1 1 1;0 1 1 0 1 1 1 1;1 1 1 0 1 1 1 1;0 0 0 1 1 1 1 1;1 0 0 1 1 1 1 1;0 1 0 1 1 1 1 1;1 1 0 1 1 1 1 1;0 0 1 1 1 1 1 1;1 0 1 1 1 1 1 1;0 1 1 1 1 1 1 1;1 1 1 1 1 1 1 1;]);
- n = 0;
- t = 1;
- A = 0;
- DAC = zeros(1, length(vin));
- Neurons = zeros(8, length(vin));
- vout = zeros(1, length(vin));
- %binary-weighted gradient descent Training loop
- W_prev = 1/2.+zeros(1,100);
- gamma = 1;
- for l = 1:40 %2000 samples 20ms
- Ptot_neuron = 0;
- Ptot_net = 0;
- Ptot_comparator = 0;
- for x = 1: length(vin)
- %k = randi([1,length(vin)]); %random dataset
- k = x;
- noise=random('norm',0,sNoise);
- thermnoise = random('norm',0,thNoise);
- vJit=random('norm',0,sJit);
- if vin(k)> 1.793
- vin(k)=0;
- end
- indx = int16(vin(k)/alpha) + 1;
- n = n+1;
- vC(n)=vin(k)*(1-exp(-dt/tau));
- if fixed == 1
- [Y(8),Y(7)] = activation_flash_adc( vin(k) , 1.8);
- [Y(6),Y(5)] = activation_flash_adc( (vin(k) - 64*Y(7)/256 - 128*Y(8)/256) , 1.8/4);
- [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);
- [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);
- else
- [Y(8),Y(7)] = activation_flash_adc( vin(k) , 1.8);
- [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);
- [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);
- [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);
- end
- Rfed=45e3;
- P1_neuron = (vin(k) - 8*Vref )^2/Rfed ;
- P1_net = vin(k)^2/Rfed + Vref^2/Rfed/8;
- P2_neuron = (vin(k) - 4*Vref - (Rfed/(R(3,4)+450))*Y(4)/18)^2/Rfed ;
- P2_net = vin(k)^2/Rfed + Vref^2/Rfed/4 + (Y(4)/18)^2/(R(3,4)+450);
- P3_neuron = (vin(k) - 2*Vref - (Rfed/(R(2,4)+450))*Y(4)/18 - (Rfed/(R(2,3)+450))*Y(3)/18)^2/Rfed ;
- 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);
- 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 ;
- 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);
- %Ptot_net = Ptot_net + P1_net + P2_net + P3_net + P4_net ;
- Ptot_neuron = Ptot_neuron + P1_neuron + P2_neuron + P3_neuron + P4_neuron ;
- %Ptot_comparator = Ptot_comparator + 4*(1e-8);
- fliplr(Y);
- D(ceil(indx),:);
- 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);
- if E(end) == 0 %false positive case
- continue;
- end
- 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));
- Neurons(1,n) = Y(8);
- Neurons(2,n) = Y(7);
- Neurons(3,n) = Y(6);
- Neurons(4,n) = Y(5);
- Neurons(5,n) = Y(4);
- Neurons(6,n) = Y(3);
- Neurons(7,n) = Y(2);
- Neurons(8,n) = Y(1);
- vout(n) = vin(k);
- E_rate(end+1) = (1/n)*E(end);
- t(end+1) = t(end) + 1;
- if E_rate(end) < E_threshold
- break;
- end
- for i = 1:8
- for j = 1:8
- if i >= j
- R(i,j) = 0;
- else
- x = 10*i +j;
- W_f = W_prev(x)*(1-W_prev(x));
- if (D(ceil(indx),9-i) - Y(i))>=0
- Vw = -1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
- dW = -1*Kon(x)*((Vw/Von -1)^3)*W_f;
- else
- Vw = 1*Vp*(R(i,j)/(R(i,j)+ RMOS(x)));
- dW = Koff(x)*((Vw/Voff -1)^1)*W_f;
- end
- if n < 1600
- gamma = 1;
- elseif n>=1600 && n<2400
- gamma = 0.5;
- elseif n>=2800 && n<3200
- gamma = 0.25;
- else
- gamma = 0.03;
- end
- beta = gamma*0.09*(Roff(x)-Ron(x))*dW*T_write/dt;
- %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 >= Roff(x)
- % continue
- %end
- %if R(i) + beta*(D(ceil(indx),5-i) - Y(i))*D(ceil(indx),5-j)*Yvar(i)/1.8 <= Ron(x)
- % continue
- %end
- %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;
- 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);
- W_prev(x) = R(i,j)/(Roff(x) - Ron(x));
- end
- end
- end
- R12(end+1) = R(1,2);
- R13(end+1) = R(1,3);
- R14(end+1) = R(1,4);
- R23(end+1) = R(2,3);
- R24(end+1) = R(2,4);
- R34(end+1) = R(3,4);
- end
- if E_rate(end) < E_threshold
- break;
- end
- P(l) = (Ptot_comparator + Ptot_net + Ptot_neuron )/2^4;
- end
- %%
- figure(1);
- hold on;
- ylabel('Training Error','FontSize',18);
- xlabel('#Samples','FontSize',18);
- plot(t,E_rate,'LineWidth',4);
- grid on;
- leg = legend('Algorithm');
- set(leg,'FontSize',12);
- set(gca,'FontSize',12);
- figure(3);
- hold on;
- plot(t,(45e3)./R12);
- plot(t,(45e3)./R13);
- plot(t,(45e3)./R14);
- plot(t,(45e3)./R23);
- plot(t,(45e3)./R24);
- plot(t,(45e3)./R34);
- figure;
- plot(P);
- % vout = vout*1.8/28.8;
- % DAC = DAC*1.8/28.8;
- tf = t(1:length(t)-1);
- figure(4);
- hold on;
- stairs(tf,DAC,'-or'), xlim([n-300 n]);
- stairs(tf,vout), xlim([n-300 n]);
- figure(5);
- subplot(8,1,1);
- stairs((n-48:n),Neurons(1,n-48:n),'b','linewidth',2);
- subplot(8,1,2);
- stairs((n-48:n),Neurons(2,n-48:n),'r','linewidth',2);
- subplot(8,1,3);
- stairs((n-48:n),Neurons(3,n-48:n),'g','linewidth',2);
- subplot(8,1,4);
- stairs((n-48:n),Neurons(4,n-48:n),'y','linewidth',2);
- subplot(8,1,5);
- stairs((n-48:n),Neurons(5,n-48:n),'b','linewidth',2);
- subplot(8,1,6);
- stairs((n-48:n),Neurons(6,n-48:n),'r','linewidth',2);
- subplot(8,1,7);
- stairs((n-48:n),Neurons(7,n-48:n),'g','linewidth',2);
- subplot(8,1,8);
- stairs((n-48:n),Neurons(8,n-48:n),'y','linewidth',2);
- % subplot(4,1,5);
- % stairs((n-48:n),Neurons(5,n-48:n),'y','linewidth',2);
- figure(6);
- subplot(4,1,1);
- stairs((n-3848:n-3800),Neurons(1,n-3848:n-3800),'b','linewidth',2);
- subplot(4,1,2);
- stairs((n-3848:n-3800),Neurons(2,n-3848:n-3800),'r','linewidth',2);
- subplot(4,1,3);
- stairs((n-3848:n-3800),Neurons(3,n-3848:n-3800),'g','linewidth',2);
- subplot(4,1,4);
- stairs((n-3848:n-3800),Neurons(4,n-3848:n-3800),'y','linewidth',2);
- % subplot(4,1,5);
- stairs((n-3848:n-3800),Neurons(5,n-3848:n-3800),'y','linewidth',2);
- figure(7);
- subplot(4,1,1);
- stairs((1:48),Neurons(1,1:48),'b','linewidth',2);
- subplot(4,1,2);
- stairs((1:48),Neurons(2,1:48),'r','linewidth',2);
- subplot(4,1,3);
- stairs((1:48),Neurons(3,1:48),'g','linewidth',2);
- subplot(4,1,4);
- stairs((1:48),Neurons(4,1:48),'y','linewidth',2);
- % subplot(4,1,5);
- % stairs((1:48),Neurons(5,1:48),'y','linewidth',2);
- %% %% Static Evaluation
- %maxDNL =zeros(1,length(R12));
- %maxINL =zeros(1,length(R12));
- %for id = 1:length(R12)
- %Input
- VFS = 1.8;
- vin = [0:0.001:15/16*VFS];
- index = length(R12);
- 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))];
- vout_f=zeros(length(tr),4);
- vout_c=zeros(length(tr),4);
- vout_i=zeros(length(tr),4);
- out_f = zeros(length(vin),1);
- out_i = zeros(length(vin),1);
- out_c = zeros(length(vin),1);
- for i=1:length(vin)
- for j=1:1
- noise=random('norm',0,sNoise);
- thermnoise = random('norm',0,thNoise);
- vJit=random('norm',0,sJit);
- vout_f(i,1) = activation(vin(i) - 8*Vref);
- 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);
- 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);
- 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);
- for j=1:4
- out_f(i) = out_f(i) + (vout_f(i,j)/1.8)*power(2,4-j);
- end
- end
- end
- 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))];
- for i=1:length(vin)
- for j=1:1
- noise=random('norm',0,sNoise);
- thermnoise = random('norm',0,thNoise);
- vJit=random('norm',0,sJit);
- vout_c(i,1) = activation(vin(i) - 8*Vref);
- 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);
- 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);
- 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);
- for j=1:4
- out_c(i) = out_c(i) + (vout_c(i,j)/1.8)*power(2,4-j);
- end
- end
- end
- for i=1:length(vin)
- for j=1:1
- noise=random('norm',0,sNoise);
- thermnoise = random('norm',0,thNoise);
- vJit=random('norm',0,sJit);
- vout_i(i,1) = activation(vin(i) - 8*Vref);
- 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);
- 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);
- 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);
- for j=1:4
- out_i(i) = out_i(i) + (vout_i(i,j)/1.8)*power(2,4-j);
- end
- end
- end
- LSB=1/16;
- out_step_unique = unique(out_f);
- out_step = zeros(1,16);
- for j=1:length(out_step_unique)
- out_step(out_step_unique(j)+1) = out_step_unique(j);
- end
- w = length(out_step);
- s=zeros(1,w);
- in=zeros(1,w);
- for i=2: w+1
- s(i-1) = find(out_f == out_step(i-1), 1, 'first');
- in(i-1) = vin(s(i-1));
- end
- %x= diff(in);
- steps =zeros(1,15);
- last = 0;
- for k=1:15
- if in(k) == 0
- steps(k) = 0;
- else
- steps(k) = in(k) - last;
- last = in(k);
- end
- end
- step_av=mean(steps)
- DNL_f= steps/step_av - 1;
- DNL_f(1) = 0;
- maxDNL_f=max(DNL_f)
- INL_f=zeros(1,length(out_step));
- for i = 1:length(steps)
- INL_f(i) = sum(DNL_f(1:i-1));
- end
- maxINL_f=max(INL_f);
- out_step_unique = unique(out_c);
- out_step = zeros(1,16);
- for j=1:length(out_step_unique)
- out_step(out_step_unique(j)+1) = out_step_unique(j);
- end
- w = length(out_step);
- s=zeros(1,w);
- in=zeros(1,w);
- for i=2: w+1
- s(i-1) = find(out_f == out_step(i-1), 1, 'first');
- in(i-1) = vin(s(i-1));
- end
- %x= diff(in);
- steps =zeros(1,15);
- last = 0;
- for k=1:15
- if in(k) == 0
- steps(k) = 0;
- else
- steps(k) = in(k) - last;
- last = in(k);
- end
- end
- step_av=mean(steps)
- DNL_c= steps/step_av - 1;
- DNL_c(1) =0;
- maxDNL_f=max(DNL_c)
- INL_c=zeros(1,length(out_step));
- for i = 1:length(steps)
- INL_c(i) = sum(DNL_c(1:i-1));
- end
- maxINL_c=max(INL_c);
- out_step_unique = unique(out_i);
- out_step = zeros(1,16);
- for j=1:length(out_step_unique)
- out_step(out_step_unique(j)+1) = out_step_unique(j);
- end
- w = length(out_step);
- s=zeros(1,w);
- in=zeros(1,w);
- for i=2: w+1
- s(i-1) = find(out_i == out_step(i-1), 1, 'first');
- in(i-1) = vin(s(i-1));
- end
- %x= diff(in);
- steps =zeros(1,15);
- last = 0;
- for k=1:15
- if in(k) == 0
- steps(k) = 0;
- else
- steps(k) = in(k) - last;
- last = in(k);
- end
- end
- step_av=mean(steps)
- DNL_i= steps/step_av - 1;
- DNL_i(1) =0;
- maxDNL_i=max(DNL_i)
- INL_i=zeros(1,length(out_step));
- for i = 1:length(steps)
- INL_i(i) = sum(DNL_i(1:i-1));
- end
- maxINL_i=max(INL_i);
- %end
- %close all
- figure
- hold on
- plot((0:14),DNL_f,'b-*','linewidth',2);
- plot((0:14),DNL_c,'r--','linewidth',2);
- plot((0:14),DNL_i,'g--','linewidth',2);
- hold off
- ylabel('DNL (LSB)');
- xlabel('Input code')
- legend('Gradient Descent')
- xlim([0 14])
- box on
- set(gca,'linewidth',3)
- set(findall(gcf,'-property','FontSize'),'FontSize',44)
- set(findall(gcf,'-property','FontName'),'FontName','Calibri')
- set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
- figure
- hold on
- plot((0:15),INL_f,'b-*','linewidth',2);
- plot((0:15),INL_c,'r--','linewidth',2);
- plot((0:15),INL_i,'g--','linewidth',2);
- ylabel('INL (LSB)');
- xlabel('Input code')
- hold off
- box on
- xlim([0 15])
- legend('Gradient Descent')
- set(gca,'linewidth',3)
- set(findall(gcf,'-property','FontSize'),'FontSize',44)
- set(findall(gcf,'-property','FontName'),'FontName','Calibri')
- set(findall(gcf,'-property','FontWeight'),'FontWeight','bold')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement