clc;
close all;
clearvars;


%load('Merane_data\Merane4\data__26_10_2021__K5--p__prbs3.mat')
%load('Merane_data\Sinus\9_11_2021__K5--p__amp0.2_freq1_2_3rad__N1.mat')

%load('Merane_data\Merane4\data__26_10_2021__K10__prbs2.mat');
%load('Merane_data\Merane10\9_11_2021__K1--p^2__prbs10.mat')

%load('Merane_data\NaZpracovanie\K--p\K5--p__prbs3_8.mat');
%load('Merane_data\NaZpracovanie\K--p^2\K1--p^2__prbs10_6.mat');
%load('Merane_data\NaZpracovanie\K--p+1\K10--p+1__prbs3_5.mat');
%load('Merane_data\NaZpracovanie\K\K10__prbs2_8.mat');
%load('Merane_data\NaZpracovanie\sinus\K5--p__amp0.2_freq1_2_3rad__N5.mat');

%load("Merane_data\BP\Tuhost\Sinus\SIN_T_1-p^2_N7.mat");
load("Merane_data\BP\BezTuhosti\Sinus\SIN_BT_1-p^2_N5.mat");


%load("Merane_data/NaZpracovanie/K--p^2/K1--p^2__prbs10_5.mat");
%load(['Merane_data\BP/Tuhost/PRBS/PRBS_T_1-p^2_N3.mat']);
%load("Merane_data\BP\BezTuhosti\PRBS\PRBS_BT_1-p^2_N7.mat");

NP = 1;
NZ = 1;
IODelay = 0.35;
TS = 0.01;

data = iddata(out.action.data,out.error.data, TS);
sys = tfest(data, NP, NZ, IODelay)

figure(1);
subplot(3,1,1);
plot(out.input, 'linewidth', 1);
hold on;
plot(out.output, 'linewidth', 1);
legend('w(t)','y(t)');
xlabel('t [s]');
ylabel('w(t), y(t)');
title('Budiaci signál a výstup systému');
%title('Forcing function and system output')
grid on;
axis([0 60 -0.5 0.5]);

%figure(2);
subplot(3,1,2);
plot(out.error, 'linewidth', 1);
xlabel('t [s]');
ylabel('e(t)');
title('Priebeh regulačnej odchýlky');
%title('Regulation error')
grid on;
axis([0 60 -0.2 0.2]);
yticks(-0.2:0.1:0.2);

subplot(3,1,3);
um = lsim(sys, out.error.data, out.error.time);
plot(out.error.time, um, 'linewidth', 1);
hold on;
plot(out.action, 'linewidth', 1);
legend('Simulovaný zásah', 'Meraný zásah');
%legend('Simulated','Measured')
xlabel('t [s]');
ylabel('x(t), x''(t)');
title('Priebeh akčného zásahu');
%title('Akční zásah operátora')
grid on;
yticks(-0.4:0.2:0.4);
axis([0 60 -0.4 0.4]);

figure(4)
bode(tf(sys.numerator, sys.denominator))
grid on;


set(figure(1), 'position', [450 100 500 500]);
%set(figure(2), 'position', [450 400 500 350]);
%set(figure(4), 'position', [450 400 500 350]);