-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPEM_prederror.m
75 lines (55 loc) · 1.57 KB
/
PEM_prederror.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
close all
clearvars
clc
%% Data
% Read from file
data = load('data/SNLS80mV.mat');
% Sampling frequency
fs = 610.35;
% Training indices
split = 40100;
ix_trn = split+1:131072;
ix_val = 1:split;
% Time
time_trn = [0:(length(ix_trn)-1)]/fs;
time_val = [0:(length(ix_val)-1)]/fs;
% Split into training and validation
output_trn = data.V2(ix_trn);
input_trn = data.V1(ix_trn);
output_val = data.V2(ix_val);
input_val = data.V1(ix_val);
%% Model
% Nonlinear ARX model of 2 previous observations and no delayed input
load('models/narx_sigmoidnet4.mat')
%% Predict
% Map training data to inital states
xf = data2state(narx_sigmoidnet4, iddata(output_val(1:2)', input_val(1:2)', 1/fs));
% Set starting values
opt = simOptions('InitialCondition', xf);
% Collect validation data
val_data = iddata(output_val(3:end)', input_val(3:end)', 1/fs);
% 1-step ahead predictions
predictions = predict(narx_sigmoidnet4, val_data, 1);
pred_states = [0; 0; predictions.OutputData];
% Prediction error
pred_error = (output_val' - pred_states).^2;
% Save predictions
save("results/results_narx_sigmoidnet4_ksteppred.mat", "pred_states", "pred_error");
%% Visualize
% Subsample for visualisation
ss = 40;
ix_viz = 1:ss:length(pred_states);
% Plot validation data and predictions
figure; hold on;
plot(output_val(ix_viz)', 'red')
plot(pred_states(ix_viz), 'blue')
xlabel("time (t)");
legend('true', 'predictions')
set(gcf, 'Color', 'w');
% Plot prediction error
figure; hold on;
scatter(ix_viz, pred_error(ix_viz), '.', 'k')
xlabel("time (t)");
ylabel("error");
set(gca, 'YScale', 'log')
set(gcf, 'Color', 'w');