%% Process description
% Transfer function to state-space
Kp = 0.703; Tp = 1.62;
ysP = 7.5; usP = 1.5;  % The standard point
G1 = tf([Kp], [Tp 1],'InputDelay', 10.0); G1d = c2d(G1,1,'zoh');
[Ap,Bp,Cp,Dp] = ssdata(G1d);  % Convert tf to state-space

% Noise magnitude (Uniform)
ar = -0.30*0; br = 0.30*0;
% Noise magnitude (Gaussian)
noise_mean = 0; noise_var = 0.1^2;

% Input range
inp_range = 1:0.01:4;
%% Generate steady-state images
% Set-up
NoI_SS = 1; % Total number of images to be generated
time_ub = 50; % Maximum time step to be represented in each image
time_lb = 15; % Minimum time step to be represented in each image
time_range = time_lb:1:time_ub;

% Image properties
img_size = 300;

% Initialization
track = 1;
figure('Units','pixels','Position',[10 10 img_size img_size]);

% Generate
while track <= NoI_SS
    % Select time step randomly
    time_range1 = time_range; time_range1 = time_range1(randperm(length(time_range1)));
    time_lim = time_range1(1);
    
    % Select steady-state input
    inp_range1 = inp_range; inp_range1 = inp_range1(randperm(length(inp_range1)));
    inp_sim = inp_range1(1);
    
    % Initialize input-output
    inp_plot = []; out_plot = []; time_plot = 1:1:time_lim;
    time_sim = 1; inp = inp_sim;
    state = inv(1-Ap)*Bp*(inp-usP); out = ysP + Cp*state + (br-ar)*rand + ar;
    
    % Simulation
    while time_sim <= time_lim
        state = Ap*state + Bp*(inp-usP); out = ysP + Cp*state + (br-ar)*rand + ar;
        inp_plot = [inp_plot,inp]; out_plot = [out_plot,out];
        time_sim = time_sim + 1;
    end
    
    % Gaussian noise
    out_plot = out_plot + sqrt(noise_var)*randn(size(out_plot)) + noise_mean;
  
    % Plot
    clf
    fig_name = sprintf('SSfig %d',track);
    plot(time_plot,out_plot)%,20,'filled') % Default dot size 36
    %set(gca,'XColor', 'none','YColor','none')
    xlim([1 time_ub]); ylim([5 10]);
    
    % Save
    path = 'C:\Users\DELL\Desktop\CNN\Code\Exper1\SS';
    %saveas(gcf,fullfile(path,fig_name),'jpeg')
    %clf
    
    % Next image
    track = track + 1;
end