-
Notifications
You must be signed in to change notification settings - Fork 0
/
NiMultiTest.asv
166 lines (140 loc) · 4.26 KB
/
NiMultiTest.asv
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function xPrediction = NiMultiTest(sourceFile, netName)
close all
%{
Creates live plot of network predicted data
Plots both the true data and delta (the difference between adjacent data
points)
%}
%path to access source file and network
sourceFile = fullfile(pwd, 'Network Chaos Data', sourceFile);
netName = fullfile(pwd, 'Saved Series Nets', netName);
%load file to analyze. Filter and normalize data
newMap = load(sourceFile);
xfiltData = sgolayfilt(newMap,2,21);
xMean = mean(xfiltData);
xStd = std(xfiltData);
xStdData = (xfiltData - xMean)/xStd;
%load and extract network from struct
net = load(netName);
fn = fieldnames(net);
net = net.(fn{1});
numIn = net.inputs{1}.size;
%variables to consider: tau, startTest, lengthTest
tau = 27;
initLength = 200;
random = rand / 2;
%startTest = floor(length(xStdData)*random) + 200 + (tau * numIn);
startTest = 2826;
errorThreshold = .15;
rowTest = 1;
%determine length of test
[~, locs] = findpeaks(xStdData, 'MinPeakDistance', 70);
numCycles = 10;
testLength = numCycles * (locs(2) - locs(1));
%create delta map as target output
x2Data = xfiltData (2:end);
x1Data = xfiltData(1:end-1);
dStdData = x2Data - x1Data;
dStdData = sgolayfilt(dStdData,2,21);
%data and plotting parameters
startRowTest = startTest + (tau * (rowTest - 1));
startTrue = startRowTest - initLength + 1;
% initialize matrices
xPrediction = zeros(numIn,testLength - 1);
dPrediction = zeros(numIn, 1);
%create initial values of xPrediction
i = 1;
j = startTest;
while i <= numIn
xPrediction(i,1) = xStdData(j);
i = i + 1;
j = j + tau;
end
%isolate true data to plot against
xInitTrue = xfiltData(startTrue:startRowTest);
xPresTrue = xfiltData(startRowTest:(startRowTest + testLength - 1));
xInitTrue = xInitTrue';
xPresTrue = xPresTrue';
%xreference plots
xInit = (1:initLength);
xPres = (initLength:initLength + testLength - 1);
%Used to render a live visualization, commented to speed up prediction
hold on
subplot(2,1,1)
plot(xPres,xPresTrue)
subplot(2,1,2)
plot(xPres,dStdData(startRowTest:(startRowTest + testLength - 1)))
%}
%series prediction
i = 2;
while i <= testLength
dPrediction(:,1) = net(xPrediction(:,(i-1)));
%rescale data
xPrediction(:,i-1) = xPrediction(:,i-1) * xStd + xMean;
%add delta to real data
xPrediction(:,i) = xPrediction(:,i-1) + dPrediction;
%{
The following block can be used to render a live visualization of the
network-predicted data. It is commented our for now to speed up the
final rendering.
-------------------------------
subplot(2,1,1)
hold on
plot(xPres(i),xPrediction(rowTest,i),'.','MarkerSize',5)
subplot(2,1,2)
hold on
plot(xPres(i),dPrediction(rowTest),'.','MarkerSize',5)
pause(.003)
%}
%standardize prediction data again
xPrediction(:,i) = (xPrediction(:,i) - xMean)/xStd;
i = i + 1;
end
%rescale prediction data one more time
xPrediction(:,i-1) = xPrediction(:,i-1) * xStd + xMean;
xPrediction = xPrediction(rowTest,:); %isolate column of data
%calculate prediction limit of trained ANN
predictionLimit = 0;
xError = xPrediction - xPresTrue;
for i = 1:testLength
if abs(xError(i)) >= errorThreshold
predictionLimit = i;
error = xError(i);
disp('Prediction Limit ='), disp(predictionLimit)
disp('Error'), disp(error)
break
end
end
%plot true and predict logistic map on the same graph
subplot(2,1,1)
hold on
title('True and Predicted Current')
initCurve = plot(xInit,xInitTrue,'b');
set(initCurve,'handleVisibility','off')
plot(xPres,xPresTrue,'b')
plot(xPres,xPrediction, 'r')
ylabel('Current (A)')
xlabel('Time (s)')
title('True and Predicted Current')
xline(predictionLimit + initLength,':k','LineWidth',2);
xline(initLength,'k','LineWidth',2);
legend('True map','Predicted map','Prediction Limit','Start of Prediction')
xlim([0 initLength + testLength])
xticks([])
xticks(initLength:200:initLength + testLength)
xticklabels(0:1:(testLength/200))
hold on
%plot the error between true and predicted
subplot(2,1,2)
hold on
plot(xError)
ylabel('Error (Predicted - Target) in Current (A)')
xlabel('Time (s)')
title('Prediction Error')
xline(predictionLimit,':k','LineWidth',2);
legend('Error','Prediction Limit')
xlim([0 testLength])
xticks([])
xticks(0:200:testLength)
xticklabels(0:1:(testLength/200))
hold off