%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%Octave:
%Frequency response analysis
%
%Finn Aakre Haugen
%University of South-Eastern Norway
%finn.haugen@usn.no
%24 Oct 2018
%
%----------------------------------------------

clear all
close all
format compact

%----------------------------------------------
%Creating the transfer function:

s = tf('s')

K = 1%Gain
T = 1%Time constant
H1 = K/(T*s+1)

%Note: 
%In Octave, s-transfer functions can not include time-delays (but they can in Matlab).
%However, time-delays can be included in z-transfer functions
%(which are discrete-time transfer functions).

%----------------------------------------------
%Plotting the Bode plot:

figure(1)
bode(H1)
grid minor on

%----------------------------------------------
%Simulating and plotting with sinusoidal input:

dt = 0.001;
t_start = 0;
t_stop = 20;
t = [t_start:dt:t_stop]';
w = 10 %Signal frequency [rad/s]
A = 1;
u = A*sin(w*t);%Input signal
y = lsim(H1,u,t);%Response in output signal

%Plotting:
figure(2)
plot(t,u,'b',t,y,'r')
xmin = t_start; xmax = t_stop;
ymin = -1.2; ymax = 1.2;
axis([xmin xmax ymin ymax]);
title('Sine input (u, blue) and response (y, red) of H1(s)')
xlabel('t [s]')
grid minor on
