-------------------------Model exponenciálne funkcie modelovaný pomocou bilineárnej transfomrácie (BT) a spätnej eulerovej metódy (SEM)------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
final_time = 20E-3; 
T = 4E-4; 		%NVZ = 50
t = 0 : T : (final_time);   	

Tref = 2E-5; 	%100vz.
tref = 0 : Tref : (final_time);

U = 100;
tau = 5E-3; 
X_BT= zeros(length(t),1);
X_SEM = zeros(length(t),1);


U_ref = U-U*exp(-(1/tau)*tref);
for i = 2:length(t)
   	X_BT(i) = U*(T/(T+2*tau)) + U*(T/(T+2*tau)) - X_BT(i-1)*((T-2*tau)/(T+2*tau));
	X_SEM(i) = U*(T/(tau+T)) + X_SEM(i-1)*((tau)/(tau+T));
end

figure;
  plotyy (tref, U_ref, t, X_BT)
  title('Model postupnosti X_BT a U_ref')
  %plotyy (tref, U_ref, t, X_SEM)
  %title('Model postupnosti X_SEM a U_ref')
  %plotyyy (tref, U_ref, t, X_BT, t, X_SEM)
  %title('Model postupností X_BT, X_SEM a U_ref')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


------------------------------------------------Model referenčnej exponenciálnej funkcie pre rôzne vstupné parametre tau a U-----------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
final_time = 40E-3; 
T = 4E-4; 	
t = 0 : T  :  (final_time);   

U_1 = 100; 
U_2 = 50;
U_3 = 30;

tau_1 = 2E-3;
tau_2 = 4E-3;
tau_3 = 1E-2;


U_real_1 = U_1-U_1*exp(-(1/tau_1)*t);
U_real_2 = U_2-U_2*exp(-(1/tau_1)*t);
U_real_3 = U_3-U_3*exp(-(1/tau_1)*t);

figure;
  plotyyy (t, U_real_1, t, U_real_2, t, U_real_3)
  title('Model exponenciálnej funkcie pre rôzne hodnoty parametrov U a t')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




--------------------------------------Chyba bitovej šírky pri danom počte krokov ( ide o porovnávanie s hodnotami z referenčnej funkcie)----------------------------------------------
------------------------------------------------------!Nutný výstup dát zo simulácie vo Visual Studiu importovaný do Matlabu!----------------------------------------------------------
final_time = 20E-3; 		%20ms
T = 4E-4; 			%časový krok pre referenčnú funkciu
t = 0 : T : (final_time); 		%(final_time/T) referenčných vzorkov

U = 100;
tau = 5E-3; 
U_ref = U - U*exp((-1/tau)*t);	%referenčná hodnota exponenciály

krok = 50;			%počet krokov, pre ktorý chybu počítame
konst = uint64((final_time/T)/krok);


for i = 1:krok

y = ((i)*konst);


%Rel_error_BT_16bit(i) = ( abs(output_BT_16bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_16bit(i) = abs(output_BT_16bit(i) - U_ref(y));
%Rel_error_SEM_16bit(i) = ( abs(output_SEM_16bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_16bit(i) = abs(output_SEM_16bit(i) - U_ref(y));

%Rel_error_BT_20bit(i) = ( abs(output_BT_20bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_20bit(i) = abs(output_BT_20bit(i) - U_ref(y));
%Rel_error_SEM_20bit(i) = ( abs(output_SEM_20bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_20bit(i) = abs(output_SEM_20bit(i) - U_ref(y));

%Rel_error_BT_24bit(i) = ( abs(output_BT_24bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_24bit(i) = abs(output_BT_24bit(i) - U_ref(y));
%Rel_error_SEM_24bit(i) = ( abs(output_SEM_24bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_24bit(i) = abs(output_SEM_24bit(i) - U_ref(y));


%Rel_error_BT_28bit(i) = ( abs(output_BT_28bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_28bit(i) = abs(output_BT_28bit(i) - U_ref(y));
%Rel_error_SEM_28bit(i) = ( abs(output_SEM_28bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_28bit(i) = abs(output_SEM_28bit(i) - U_ref(y));

%Rel_error_BT_32bit(i) =( abs(output_BT_32bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_32bit(i) = abs(output_BT_32bit(i) - U_ref(y));
%Rel_error_SEM_32bit(i) = ( abs(output_SEM_32bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_32bit(i) = abs(output_SEM_32bit(i) - U_ref(y));

%Rel_error_BT_48bit(i) =  ( abs(output_BT_48bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_48bit(i) = abs(output_BT_48bit(i) - U_ref(y));
%Rel_error_SEM_48bit(i) = ( abs(output_SEM_48bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_48bit(i) = abs(output_SEM_48bit(i) - U_ref(y));

%Rel_error_BT_56bit(i) = ( abs(output_BT_56bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_56bit(i) =  abs(output_BT_56bit(i) - U_ref(y));
%Rel_error_SEM_56bit(i) = ( abs(output_SEM_56bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_56bit(i) = abs(output_SEM_56bit(i) - U_ref(y));

%Rel_error_BT_64bit(i) = ( abs(output_BT_64bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_BT_64bit(i) = abs(output_BT_64bit(i) - U_ref(y));
%Rel_error_SEM_64bit(i) = ( abs(output_SEM_64bit(i) - U_ref(y)) / U_ref(y) )*100;
%Abs_error_SEM_64bit(i) = abs(output_SEM_64bit(i) - U_ref(y));

end


%MAX_rel_error_BT_16bit = max(Rel_error_BT_16bit);
%MAX_abs_error_BT_16bit = max(Abs_error_BT_16bit);
%Mean_BT_16bit = mean(Rel_error_BT_16bit);
%MAX_rel_error_SEM_16bit = max(Rel_error_SEM_16bit);
%MAX_abs_error_SEM_16bit = max(Abs_error_SEM_16bit);
%Mean_SEM_16bit = mean(Rel_error_SEM_16bit);

%MAX_rel_error_BT_20bit = max(Rel_error_BT_20bit);
%MAX_abs_error_BT_20bit = max(Abs_error_BT_20bit);
%Mean_BT_20bit = mean(Rel_error_BT_20bit);
%MAX_rel_error_SEM_20bit = max(Rel_error_SEM_20bit);
%MAX_abs_error_SEM_20bit = max(Abs_error_SEM_20bit);
%Mean_SEM_20bit = mean(Rel_error_SEM_20bit);

%MAX_rel_error_BT_24bit = max(Rel_error_BT_24bit);
%MAX_abs_error_BT_24bit = max(Abs_error_BT_24bit);
%Mean_BT_24bit = mean(Rel_error_BT_24bit);
%MAX_rel_error_SEM_24bit = max(Rel_error_SEM_24bit);
%MAX_abs_error_SEM_24bit = max(Abs_error_SEM_24bit);
%Mean_SEM_24bit = mean(Rel_error_SEM_24bit);

%MAX_rel_error_BT_28bit = max(Rel_error_BT_28bit);
%MAX_abs_error_BT_28bit = max(Abs_error_BT_28bit);
%Mean_BT_28bit = mean(Rel_error_BT_28bit);
%MAX_rel_error_SEM_28bit = max(Rel_error_SEM_28bit);
%MAX_abs_error_SEM_28bit = max(Abs_error_SEM_28bit);
%Mean_SEM_28bit = mean(Rel_error_SEM_28bit);

%MAX_rel_error_BT_32bit = max(Rel_error_BT_32bit);
%MAX_abs_error_BT_32bit = max(Abs_error_BT_32bit);
%Mean_BT_32bit = mean(Rel_error_BT_32bit);
%MAX_rel_error_SEM_32bit = max(Rel_error_SEM_32bit);
%MAX_abs_error_SEM_32bit = max(Abs_error_SEM_32bit);
%Mean_SEM_32bit = mean(Rel_error_SEM_32bit);

%MAX_rel_error_BT_48bit = max(Rel_error_BT_48bit);
%MAX_abs_error_BT_48bit = max(Abs_error_BT_48bit);
%Mean_BT_48bit = mean(Rel_error_BT_48bit);
%MAX_rel_error_SEM_48bit = max(Rel_error_SEM_48bit);
%MAX_abs_error_SEM_48bit = max(Abs_error_SEM_48bit);
%Mean_SEM_48bit = mean(Rel_error_SEM_48bit);

%MAX_rel_error_BT_56bit = max(Rel_error_BT_56bit);
%MAX_abs_error_BT_56bit = max(Abs_error_BT_56bit);
%Mean_BT_56bit = mean(Rel_error_BT_56bit);
%MAX_rel_error_SEM_56bit = max(Rel_error_SEM_56bit);
%MAX_abs_error_SEM_56bit = max(Abs_error_SEM_56bit);
%Mean_SEM_56bit = mean(Rel_error_SEM_56bit);


%MAX_rel_error_BT_64bit = max(Rel_error_BT_64bit);
%MAX_abs_error_BT_64bit = max(Abs_error_BT_64bit);
%Mean_BT_64bit = mean(Rel_error_BT_64bit);
%MAX_rel_error_SEM_64bit = max(Rel_error_SEM_64bit);
%MAX_abs_error_SEM_64bit = max(Abs_error_SEM_64bit);
%Mean_SEM_64bit = mean(Rel_error_SEM_64bit);
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------






------------------------------------------------Model exponenciálneho priebehu oscilátora  modelovaný využitím S.E.M.--------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
final_time = 4E-2; 	%40ms
T = 6E-5;  		% dĺžka iteračného kroku
t = 0 : T  : (final_time)  	%(final_time/T) vzorkov



U = -15;
tmp_U = abs(U)
tau = 5E-3;
direction = 0;
komp = (tmp_U/2-0.24);

osci_SEM= zeros(length(t),1);


for i = 2:length(t)
	osci_SEM(i) =  U*(T/(tau+T)) + osci_SEM(i-1)*((tau)/(tau+T));

if(direction == 1)
	if(osci_SEM(i) >= komp)
         	     	direction = 0;	
		U = (-15);
	else
            		U = (15);
            		direction = 1;
	end

elseif(direction == 0)
	if(osci_SEM(i) <= -komp)
		direction = 1;
		U = (15);
	else
		U = (-15);
		direction = 0;
	end
end
end

figure;
  plot (t, osci_SEM)
  title('Oscilátor namodelovaný využitím S.E.M.')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



------------------------------------------------Model exponenciálneho priebehu oscilátora  modelovaný využitím B.T.-------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
final_time = 4E-2; 	%40ms
T = 6E-5;  		% dĺžka iteračného kroku
t = 0 : T  : (final_time)  	%(final_time/T) vzorkov



U = -15;
tmp_U = abs(U)
tau = 5E-3;
direction = 0;
komp = (tmp_U/2-0.24);

osci_BT= zeros(length(t),1);


for i = 2:length(t)
	osci_BT(i) = U*(T/(T+2*tau)) + U*(T/(T+2*tau)) + osci_BT(i-1)*((2*tau-T)/(T+2*tau));
if(direction == 1)
	if(osci_BT(i) >= komp)
         	     	direction = 0;	
		U = (-15);
	else
            		U = (15);
            		direction = 1;
	end

elseif(direction == 0)
	if(osci_BT(i) <= -komp)
		direction = 1;
		U = (15);
	else
		U = (-15);
		direction = 0;
	end
end
end

figure;
  plot (t, osci_BT)
  title('Oscilátor namodelovaný využitím B.T.')
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



































--- CHYBA BITOVEJ ŠÍRKY PRI DANOM POČTE KROKOV!---------------------------------
final_time = 20E-3; 		%20ms
T = 2E-3;			%časový krok pre referenčnú funkciu
t = 0 : T : (final_time +T); 	%(final_time/T) referenčných vzorkov

U = 100;
tau = 5E-3; 

U_ref = U - U*exp((-1/tau)*t);	%referenčná hodnota exponenciály

%Abs_error_BT_16bit = zeros(length(t),1);
Rel_error_BT_16bit = zeros(length(t),1);
%Abs_error_SEM_16bit = zeros(length(t),1);
Rel_error_SEM_16bit = zeros(length(t),1);

%Abs_error_BT_24bit = zeros(length(t),1);
%Rel_error_BT_24bit = zeros(length(t),1);
%Abs_error_SEM_24bit = zeros(length(t),1);
Rel_error_SEM_24bit = zeros(length(t),1);

%Abs_error_BT_28bit = zeros(length(t),1);
%Rel_error_BT_28bit = zeros(length(t),1);
%Abs_error_SEM_28bit = zeros(length(t),1);
Rel_error_SEM_28bit = zeros(length(t),1);

%Abs_error_BT_32bit = zeros(length(t),1);
%Rel_error_BT_32bit= zeros(length(t),1);
%Abs_error_SEM_32bit = zeros(length(t),1);
Rel_error_SEM_32bit= zeros(length(t),1);

%Abs_error_BT_48bit = zeros(length(t),1);
%Rel_error_BT_48bit = zeros(length(t),1);
%Abs_error_SEM_48bit = zeros(length(t),1);
Rel_error_SEM_48bit = zeros(length(t),1);

%Abs_error_BT_56bit = zeros(length(t),1);
%Rel_error_BT_56bit = zeros(length(t),1);
%Abs_error_SEM_56bit = zeros(length(t),1);
Rel_error_SEM_56bit = zeros(length(t),1);

%Abs_error_BT_64bit = zeros(length(t),1);
%Rel_error_BT_64bit = zeros(length(t),1);
%Abs_error_SEM_64bit = zeros(length(t),1);
Rel_error_SEM_64bit = zeros(length(t),1);


%zvyšné dáta pre porovnanie sú Importované zo súbor (získané v simulácií) pre jednotlivé bitové šírky
for i = 2:length(t) 
	%Abs_error_BT_16bit(i) =  abs(output_BT_16bit(i) - U_ref(i));
	%Rel_error_BT_16bit(i) = ( abs(output_BT_16bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_16bit(i) = abs(output_SEM_16bit(i) - U_ref(i));
	%Rel_error_SEM_16bit(i) = ( abs(output_SEM_16bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_24bit(i) = abs(output_BT_24bit(i) - U_ref(i));
	%Rel_error_BT_24bit(i) = ( abs(output_BT_24bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_24bit(i) = abs(output_SEM_24bit(i) - U_ref(i));
	%Rel_error_SEM_24bit(i) = ( abs(output_SEM_24bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_28bit(i) = abs(output_BT_28bit(i) - U_ref(i));
	%Rel_error_BT_28bit(i) = ( abs(output_BT_28bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_28bit(i) = abs(output_SEM_28bit(i) - U_ref(i));
	%Rel_error_SEM_28bit(i) = ( abs(output_SEM_28bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_32bit(i) = abs(output_BT_32bit(i) - U_ref(i));
	%Rel_error_BT_32bit(i) =( abs(output_BT_32bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_32bit(i) = abs(output_SEM_32bit(i) - U_ref(i));
	%Rel_error_SEM_32bit(i) = ( abs(output_SEM_32bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_48bit(i) = abs(output_BT_48bit(i) - U_ref(i));
	%Rel_error_BT_48bit(i) =  ( abs(output_BT_48bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_48bit(i) = abs(output_SEM_48bit(i) - U_ref(i));
	%Rel_error_SEM_48bit(i) = ( abs(output_SEM_48bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_56bit(i) = abs(output_BT_56bit(i) - U_ref(i));
	%Rel_error_BT_56bit(i) = ( abs(output_BT_56bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_56bit(i) = abs(output_SEM_56bit(i) - U_ref(i));
	%Rel_error_SEM_56bit(i) = ( abs(output_SEM_56bit(i) - U_ref(i)) / U_ref(i) )*100;

	%Abs_error_BT_64bit(i) = abs(output_BT_64bit(i) - U_ref(i));
	%Rel_error_BT_64bit(i) = ( abs(output_BT_64bit(i) - U_ref(i)) / U_ref(i) )*100;
	%Abs_error_SEM_64bit(i) = abs(output_SEM_64bit(i) - U_ref(i));
	%Rel_error_SEM_64bit(i) = ( abs(output_SEM_64bit(i) - U_ref(i)) / U_ref(i) )*100;
end



%MAX_rel_error_BT_16bit = max(Abs_error_BT_16bit);
%MAX_abs_error_BT_16bit = max(Rel_error_BT_16bit);
%MAX_rel_error_SEM_16bit = max(Abs_error_SEM_16bit);
%MAX_abs_error_SEM_16bit = max(Rel_error_SEM_16bit);


%MAX_rel_error_BT_24bit = max(Abs_error_BT_24bit);
%MAX_abs_error_BT_24bit = max(Rel_error_BT_24bit);
%MAX_rel_error_SEM_24bit = max(Abs_error_SEM_24bit);
%MAX_abs_error_SEM_24bit = max(Rel_error_SEM_24bit);


%MAX_rel_error_BT_28bit = max(Abs_error_BT_28bit);
%MAX_abs_error_BT_28bit = max(Rel_error_BT_28bit);
%MAX_rel_error_SEM_28bit = max(Abs_error_SEM_28bit);
%MAX_abs_error_SEM_28bit = max(Rel_error_SEM_28bit);


%MAX_rel_error_BT_32bit = max(Abs_error_BT_32bit);
%MAX_abs_error_BT_32bit = max(Rel_error_BT_32bit);
%MAX_rel_error_SEM_32bit = max(Abs_error_SEM_32bit);
%MAX_abs_error_SEM_32bit = max(Rel_error_SEM_32bit);

%MAX_abs_error_BT_48bit = max(Abs_error_BT_48bit);
%MAX_rel_error_BT_48bit = max(Rel_error_BT_48bit);
%MAX_rel_error_SEM_48bit = max(Abs_error_SEM_48bit);
%MAX_abs_error_SEM_48bit = max(Rel_error_SEM_48bit);


%MAX_rel_error_BT_56bit = max(Abs_error_BT_56bit);
%MAX_abs_error_BT_56bit = max(Rel_error_BT_56bit);
%MAX_rel_error_SEM_56bit = max(Abs_error_SEM_56bit);
%MAX_abs_error_SEM_56bit = max(Rel_error_SEM_56bit);


%MAX_rel_error_BT_64bit = max(Abs_error_BT_64bit);
%MAX_abs_error_BT_64bit = max(Rel_error_BT_64bit);
%MAX_rel_error_SEM_64bit = max(Abs_error_SEM_64bit);
%MAX_abs_error_SEM_64bit = max(Rel_error_SEM_64bit);










