% easyplot.m: Modification of readplot.m

clear all, close all, clc

% Change the path in the argument of dlmread to suit yourself.
x = dlmread('1.txt');

% Determine the number of rows and columns of x.
% If all went well, the number of rows will be equal to 1.
[rows,cols] = size(x);

% Truncate x so that x has an even number of columns.
if cols/2 > floor(cols/2)
  x = x(:,1:cols-1);
  cols = cols-1;
end

T = 0.002; % sampling time
t = 0:T:T*(cols/2-1);
w = x(1,1:2:cols-1); % This is the vector of speeds

% Filter the speed with a LP filter of cutoff frequency Fcf
% to obtain filtered version of wf:   
Fcf = 2;  % Hz.
wf(1) = w(1);
for k = 1:cols/2-1
 wf(k+1) = (1-2*pi*Fcf*T)*wf(k) + 2*pi*Fcf*T*w(k);
 
 % Another filter, which is similar to the current filter in 
 % main-prog-exp6.c, is
 %
 % Fcf = 50;
 % wf(k+1) = (Fcf*T/(2+Fcf*T))*( w(k+1)+w(k) )+((2-Fcf*T)/(2+Fcf*T))*wf(k);
end

% Without feedback of disturbance observer

% 1: Plot w and T_Lhat with DOB
% TL = x(1,2:2:cols)/100000; 
% subplot(2,1,1); 
% plot(t,w,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] with hat i_L fed back');
% subplot(2,1,2); 
% plot(t,TL,"linewidth",1.5); grid(gca,'minor');
% title('hat T_L [Nm] with hat i_L fed back');
% print -dpdf 1.pdf
% figure
% Filtered version of the data plotted above
% subplot(2,1,1); 
% plot(t,wf,"linewidth",1.5); grid(gca,'minor');
% title('Filtered w [rad/s] with hat i_L fed back');
% subplot(2,1,2); 
% plot(t,TL*62/9.8/1.25e-2,"linewidth",1.5); grid(gca,'minor');
% title('hat m [kg] with hat i_L fed back');
% print -dpdf 1f.pdf

% 2: Plot w and u with DOB
% u = x(1,2:2:cols)/10; % This is the vector of voltages.
% subplot(2,1,1); 
% plot(t,w,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] with hat i_L fed back');
% subplot(2,1,2); 
% plot(t,u,"linewidth",1.5); grid(gca,'minor');
% title('u [V] with hat i_L fed back');
% print -dpdf 2.pdf
% figure
% Filtered version of the data plotted above
% subplot(2,1,1); 
% plot(t,wf,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] with hat i_L fed back');
% subplot(2,1,2); 
% plot(t,u,"linewidth",1.5); grid(gca,'minor');
% title('u [V] with hat i_L fed back');
% print -dpdf 2f.pdf

% 3: Plot w and T_Lhat without DOB
% TL = x(1,2:2:cols)/100000; % This is the vector of voltages.
% subplot(2,1,1); 
% plot(t,w,"linewidth",1.5); grid(gca,'minor');
% title('w (rad/s) without hat i_L fed back');
% subplot(2,1,2); 
% plot(t,TL,"linewidth",1.5); grid(gca,'minor');
% title('hat T_L (Nm) without hat i_L fed back');
% print -dpdf 3.pdf
% figure
% Filtered version of the data plotted above
% subplot(2,1,1); 
% plot(t,wf,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] without hat i_L fed back');
% subplot(2,1,2); 
% plot(t,TL*62/9.8/1.25e-2,"linewidth",1.5); grid(gca,'minor');
% title('hat m [kg] without hat i_L fed back');
% print -dpdf 3f.pdf

% 4: Plot w and u without DOB
% u = x(1,2:2:cols)/10; % This is the vector of voltages.
% subplot(2,1,1); 
% plot(t,w,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] without hat i_L fed back');
% subplot(2,1,2); 
% plot(t,u,"linewidth",1.5); grid(gca,'minor');
% title('u [V] without hat i_L fed back');
% print -dpdf 4.pdf
% figure
% Filtered version of the data plotted above
% subplot(2,1,1); 
% plot(t,wf,"linewidth",1.5); grid(gca,'minor');
% title('w [rad/s] without hat i_L fed back');
% subplot(2,1,2); 
% plot(t,u,"linewidth",1.5); grid(gca,'minor');
% title('u [V] without hat i_L fed back');
% print -dpdf 4f.pdf

