Narx network in real-time task
Good afternoon,
I understand that the topic of NARX networks has been discussed here for over 10 years, but I would like to revisit it.
I attempted to implement a solution for processing data from a sensor in real-time, but unfortunately, I could not find any examples demonstrating how to do this correctly. Most publications on this topic either conclude with a performance evaluation of the network or focus on discussions of the code generated by the application.
Let me share some code:
% targer timeseries
T = num2cell(lfarr(1:8000));
% input timeseries from sensor
X = num2cell(source(1:8000));
The signals are extremely simple – data from the sensor and its smoothed version.
Next, I followed the standard steps outlined in the documentation and various discussions on this forum:
%create narxnet
sh = 7
d1 = [1:sh];
d2 = [1:sh];
narx_net = narxnet(d1,d2,20);
narx_netnet.divideFcn = ‘divideblock’;
(If Greg suddenly comes, tell him that I remember about autocorrelation and am already carefully studying his code from user group :))
% standard data preparation
[Xs,Xi,Ai,Ts] = preparets(narx_net,X,{},T);
% training
rng( ‘default’ )
[narx_net, tr, Ys, Es, Xf, Af ] = train(narx_net,Xs,Ts,Xi,Ai);
% performance evaluation
[Y,Xf,Af] = narx_net(Xs,Xi,Ai);
perf = perform(narx_net,Ts,Y) % perf = 3.4381e-08. I think it’s not bad.
The regression diagram speaks for itself
Next I close the network and simulate signal processing on the remainder of the data that the network has not seen.
Here, I tried to base my approach on the last two paragraphs of the documentation – Following Closed-Loop Simulation with Open-Loop Simulation.
[narx_netc,Xic,Aic] = closeloop(narx_net, Xf, Af);
% number of prediction steps
k = 5;
% A loop that simulates a real-time data flow
for i = 8001:length(lf_arr)
% getting a new value from the sensor
newSensorValue = num2cell(source(i));
% local variable for indexing the prediction array
ii = 1;
% array of predictions
res = [];
% predictions for k steps
for j=i:i+k
[Yc, Xic, Aic] = narx_netc(newSensorValue, Xic, Aic);
% disp([‘Predicted meaning: ‘, num2str(cell2mat(Yc))]);
res(ii) = cell2mat(Yc);
ii = ii+1;
end
% saving results
res1(i-8000,:) = res’;
% form new input and target arrays with new input value and the predicted result
u = [X(2:end),newSensorValue];
y = [T(2:end), {res(1)}];
% repeating initialization cycle for next simulation step
[X1,Xi,Ai,T1] = preparets(narx_net,u,{},y);
[Y,Xio,Aio] = narx_net(X1,Xi,Ai);
[narx_netc,Xic,Aic] = closeloop(narx_net, Xio, Aio);
end
The network starts working, but after some steps its normal operation stops.
I would be immensely grateful for any insights into where my reasoning or actions might have gone astray.
ThanksGood afternoon,
I understand that the topic of NARX networks has been discussed here for over 10 years, but I would like to revisit it.
I attempted to implement a solution for processing data from a sensor in real-time, but unfortunately, I could not find any examples demonstrating how to do this correctly. Most publications on this topic either conclude with a performance evaluation of the network or focus on discussions of the code generated by the application.
Let me share some code:
% targer timeseries
T = num2cell(lfarr(1:8000));
% input timeseries from sensor
X = num2cell(source(1:8000));
The signals are extremely simple – data from the sensor and its smoothed version.
Next, I followed the standard steps outlined in the documentation and various discussions on this forum:
%create narxnet
sh = 7
d1 = [1:sh];
d2 = [1:sh];
narx_net = narxnet(d1,d2,20);
narx_netnet.divideFcn = ‘divideblock’;
(If Greg suddenly comes, tell him that I remember about autocorrelation and am already carefully studying his code from user group :))
% standard data preparation
[Xs,Xi,Ai,Ts] = preparets(narx_net,X,{},T);
% training
rng( ‘default’ )
[narx_net, tr, Ys, Es, Xf, Af ] = train(narx_net,Xs,Ts,Xi,Ai);
% performance evaluation
[Y,Xf,Af] = narx_net(Xs,Xi,Ai);
perf = perform(narx_net,Ts,Y) % perf = 3.4381e-08. I think it’s not bad.
The regression diagram speaks for itself
Next I close the network and simulate signal processing on the remainder of the data that the network has not seen.
Here, I tried to base my approach on the last two paragraphs of the documentation – Following Closed-Loop Simulation with Open-Loop Simulation.
[narx_netc,Xic,Aic] = closeloop(narx_net, Xf, Af);
% number of prediction steps
k = 5;
% A loop that simulates a real-time data flow
for i = 8001:length(lf_arr)
% getting a new value from the sensor
newSensorValue = num2cell(source(i));
% local variable for indexing the prediction array
ii = 1;
% array of predictions
res = [];
% predictions for k steps
for j=i:i+k
[Yc, Xic, Aic] = narx_netc(newSensorValue, Xic, Aic);
% disp([‘Predicted meaning: ‘, num2str(cell2mat(Yc))]);
res(ii) = cell2mat(Yc);
ii = ii+1;
end
% saving results
res1(i-8000,:) = res’;
% form new input and target arrays with new input value and the predicted result
u = [X(2:end),newSensorValue];
y = [T(2:end), {res(1)}];
% repeating initialization cycle for next simulation step
[X1,Xi,Ai,T1] = preparets(narx_net,u,{},y);
[Y,Xio,Aio] = narx_net(X1,Xi,Ai);
[narx_netc,Xic,Aic] = closeloop(narx_net, Xio, Aio);
end
The network starts working, but after some steps its normal operation stops.
I would be immensely grateful for any insights into where my reasoning or actions might have gone astray.
Thanks Good afternoon,
I understand that the topic of NARX networks has been discussed here for over 10 years, but I would like to revisit it.
I attempted to implement a solution for processing data from a sensor in real-time, but unfortunately, I could not find any examples demonstrating how to do this correctly. Most publications on this topic either conclude with a performance evaluation of the network or focus on discussions of the code generated by the application.
Let me share some code:
% targer timeseries
T = num2cell(lfarr(1:8000));
% input timeseries from sensor
X = num2cell(source(1:8000));
The signals are extremely simple – data from the sensor and its smoothed version.
Next, I followed the standard steps outlined in the documentation and various discussions on this forum:
%create narxnet
sh = 7
d1 = [1:sh];
d2 = [1:sh];
narx_net = narxnet(d1,d2,20);
narx_netnet.divideFcn = ‘divideblock’;
(If Greg suddenly comes, tell him that I remember about autocorrelation and am already carefully studying his code from user group :))
% standard data preparation
[Xs,Xi,Ai,Ts] = preparets(narx_net,X,{},T);
% training
rng( ‘default’ )
[narx_net, tr, Ys, Es, Xf, Af ] = train(narx_net,Xs,Ts,Xi,Ai);
% performance evaluation
[Y,Xf,Af] = narx_net(Xs,Xi,Ai);
perf = perform(narx_net,Ts,Y) % perf = 3.4381e-08. I think it’s not bad.
The regression diagram speaks for itself
Next I close the network and simulate signal processing on the remainder of the data that the network has not seen.
Here, I tried to base my approach on the last two paragraphs of the documentation – Following Closed-Loop Simulation with Open-Loop Simulation.
[narx_netc,Xic,Aic] = closeloop(narx_net, Xf, Af);
% number of prediction steps
k = 5;
% A loop that simulates a real-time data flow
for i = 8001:length(lf_arr)
% getting a new value from the sensor
newSensorValue = num2cell(source(i));
% local variable for indexing the prediction array
ii = 1;
% array of predictions
res = [];
% predictions for k steps
for j=i:i+k
[Yc, Xic, Aic] = narx_netc(newSensorValue, Xic, Aic);
% disp([‘Predicted meaning: ‘, num2str(cell2mat(Yc))]);
res(ii) = cell2mat(Yc);
ii = ii+1;
end
% saving results
res1(i-8000,:) = res’;
% form new input and target arrays with new input value and the predicted result
u = [X(2:end),newSensorValue];
y = [T(2:end), {res(1)}];
% repeating initialization cycle for next simulation step
[X1,Xi,Ai,T1] = preparets(narx_net,u,{},y);
[Y,Xio,Aio] = narx_net(X1,Xi,Ai);
[narx_netc,Xic,Aic] = closeloop(narx_net, Xio, Aio);
end
The network starts working, but after some steps its normal operation stops.
I would be immensely grateful for any insights into where my reasoning or actions might have gone astray.
Thanks narx network MATLAB Answers — New Questions