Moving Average block returns wrong values for column vector input - matlab

I am using Simulink for real-time classification using a trained Fine-KNN model. The input data for the model is a 50-point moving average vector [6x1]. I am using the DSP moving average block for this purpose with sliding window technique (window size = 50 and simulating using code generator). When I compare the input and the output of this block for real-time values, I get the following plot:
It is clear from the plot that there is something wrong with the output as there is quite a discrepancy between the input and the output. What could possibly be the problem or am I doing something wrong?
Edit (after Cris's comment):
Here are some screenshots to showcase some modeling parameters within Simulink:
Screenshot showing probes for measuring actual input and moving average output along with the Moving Average block parameters
Probes
Other block parameters that might be affecting the performance of the model:
a. OPC Config real-time block parameters
b. OPC Read block parameters
PS: One issue that I can think of is that the actual input is fed to the Moving Average in real-time at 10ms time-step and I am not sure if the moving average block has a buffer to store up to the "Window Length" data as it keeps coming in. What I mean by this is, the moving average block might not have access to 50 values of the input signals for quite some time and I am not sure how it deals with that kind of a situation.

I can reproduce this with the following minimal example:
So a constant input of [1; 2; 3] gives a moving average of roughly 2 (the average of the input elements) in all elements, when you would expect an output of [1; 2; 3] since each element is constant.
In your example, the inputs average approximately 0.62, which you are seeing in the output from the moving average.
Using a demux to split your vector up gives the desired output
The docs say that the moving average block should be able to handle this though
The Moving Average block computes the moving average of the input signal along each channel independently over time.
It turns out that a channel in this case is a column of your vector. Since you have a column vector, the columns in each iteration are getting stacked and averaged. Unfortunately the underlying code is sealed so we can't check this theory other than by trying it out.
Reshape your input to be a row array using the reshape block.
Then you get the expected output

Related

Input signal optimization

I have a system, described by a black-box, that takes as input a signal in time (let's say something similar to a sine wave) and returns a single scalar as output.
My goal is to find the optimum signal that maximizes/minimizes the output. As constraints, the time average of the signal must be kept constant and the minimum and maximum value of the signal must be within a specific range.
The parameters to be optimized are all the points describing my signal in time.
I want to use the scipy.optimize library to minimize the output of my black-box system however, everytime the parameters are modified by the optimizer, I need to smooth the input signal in order to avoid discontinuity.
My question is: is it possible to access the input variables after they are modified by the algorithm, smooth them, and substitute them at every iteration of the algorithm?

Matlab: Comparing two signals with different time values and placed impulses

We are analysing some signals that contains an impuls in the form of a dip in the standard signal in matlab.
Signals
As you can see on the picture, we need to find the difference between the "Zlotty" and the "Krone". The two graphs besides each other, are the graphs that needs to be analyzed.
As you can see the time of the impulse is different in when it occures and in how long the impuls is. We can not use the Time as a value of measurements because that can vary randomly.
Each graph is made by vectors containing 2.5mio datapoints.
How would you use matlab to find a difference?
You could split the problem into two parts. Ensuring the same time scale for both signals and finding a possible time shift in the alignment of the resulting signals. The first part could be achieved by using the resample function of Matlab; and the second task by using cross-correlation. Using two nested for loops, you could perform a search for the "best" stretch factor and time shift that result in the maximum correlation coefficient.

matlab block averaging time series data

Q:MATLAB RELATED: Can someone help me with a MATLAB code for block averaging of time series dataset? Also how do I determine the optimal number of blocks
Background:I have a large time series dataset (position versus time) which I break into 20 smaller blocks. I need to find the variance of position for each block. Since there is a possibility that there is autocorrelation of data, normal averaging doesn't work for me, and I would need to perform block averaging.
Check out the reshape command followed by a mean and/or var (for average or variance)
At the MATLAB terminal, type:
help reshape

How to model "for loop" & "memory things" in Matlab/Simulink

In my undergrad thesis I am creating a neural network to control automated shifting algorithm for a vehicle.
I have created the nn from scratch starting from .m script which works correctly. I tested it to recognize some shapes.
A brief background information;
NN rewires neurons which are mathematical blocks located in a layer. There are multiple layers. output of a layer is input of preceding layer. Actual output is subtracted from known output and error is obtained by this manner. By using back propagation algorithm which are some algebraic equation the coefficient of neurons are updated.
What I want to do is;
in code there are 6 input matrices, don't have to be matrix just anything and corresponding outputs. lets call them as x(i) matrices and y(i) vectors. In for loop I go through each matrix and vector to teach the network. Finally by using last known updated coeffs networks give some responses according to unknown input.
I couldn't find the way that, how to simulate that for loop in simulink to go through each different input and output pairs. When the network is done with one pair it should change the input and compare with corresponding output then update the coefficient matrices.
I model the layers as given and just fed with one input but I need multiple.
When it comes to automatic transmission control issue it should do all this real time. It should continuously read the output and updates the coeffs and gives the decision.
Check out the "For each Subsystem". Exists since 2011b
To create the input signals you use the "Concatenate" Block which would have six inputs in your case, and a three dimensional output x.dim = [1x20x6] then you could iterate over the third dimension...
A very useful pattern to create smaller models that run faster and to keep your code DRY (Dont repeat yourself)

NARX Neural network prediction?

I am trying to solve a time series problem using the NARX Neural Network solution that Matlab provides. I am trying to understand how to predict actual values, but the results I get are almost perfect! The errors are so small that I am not sure if I am actually predicting. I just want to make sure I am doing everything right!
Basically I train the network with some samples using the GUI solution. Then I use the following script to test the neural network with new samples:
X = num2cell(open2(1:end))'; % input
T = num2cell(close2(1:end))'; % this is the output I should get
net = removedelay(net);
[Xs,Xi,Ai,Ts] = preparets(net,X,{},T);
Y = net(Xs,Xi,Ai);
plotresponse(Ts,Y)
view(net)
Y = cell2mat(Y);
T = cell2mat(T);
sizey = length(Y);
sizet = length(T);
T = T(1:sizey);
figure
plot(1:sizey,T,1:sizey,Y)
The graph I am getting is almost identical to the original target time series function. The errors are really small and the only difference is that the graph (Y) is shifted 2 samples to the left. But, am I really predicting?
Here's part of the graph:
Thanks in advance!
Update: The actual prediction graph is shifted to the right and not to the left. The targets provided by the preparets function (blue) occurs before! So it doesn't show it's actually predicting.
Right Shift
Your graph shows a timeshift of 1 (not 2!) timestep(s). This is not ideal, but can happen when the delays are badly chosen which leads to this kind of delay pattern. (For further explanation have a look at this question on MATLAB CENTRAL. In fact, Greg Heath posted a lot of material on ANNs, very worth the read even though it's sometimes a bit short to be understood immediately, especially for beginners.) So, to avoid this you have to look into the correlation patterns of your data.
Removedelay()
Now, I'm assuming that you wanted to correct for this behaviour by removing the delay of the network instead. Unfortunately, this is not what removedelay() is meant for:
This example uses a timedelaynet, but can be adopted for NAR and NARX networks as well, and I found the description very helpful. In combination with a quote from removedelay's documentation
The result is a network which behaves identically, except that outputs are produced n timesteps later.
it becomes clear that you're not changing the network, instead you only change the time dependence of your y-values, so your network will try to predict one time step ahead. You can see this behaviour at the very end of your T and Y vectors where Y will have an additional value while T fills this space with NaN (because you obviously cannot generate more targets out of the blue).
removedelay() is supposed to be used in combination with a closed loop design, so that you can obtain predicted values early to use them as direct input for the next step. In this case, it also makes sense to increase the output delay by more than just one which is why you can pass an additional argument n:
net = removedelay(net,n);
To prove that the additional time step is not used you can simulate the desired data set with your trained net and then simulate the same set with removedelay(). They are going to be identical except for the last value of the Y curve (see Figure 1).
Fig. 1: Both plots are based on the same net trained with the first 3500 data points of MATLAB's heat exchanger example. Shown are the simulation results for the last 500 values in the set that have not been used in the training process. The results are identical except for an additional value for the one on the left using removedelay().
Errors
Your errors have to be very small if you're using a representative training set. Therefore, the prediction for similar, new data will be good because your net is not overfitted.
Conclusions
So, are you predicting? No, you are simulating. Simulating your network's behaviour is based on inputs of your previously unknown data set, not the targets (they only have to be passed to allow for performance evaluation). Therefore, passing new data to your net with or without removedelay() is simulation in both cases because it is based on provided inputs. Removing the delay doesn't make a difference for these results.
Prediction, on the other hand, requires no input data because it really just continues the pattern the network has learned so far without taking new input into account.
Suggestions
If all you want is to have an unknown data set with valid input values passed to your net for simulation, you could just as well pass it as part of the testing set by using the divideblock or divideint options.
If you want to make use of early prediction by removedelay() or need prediction in general because your inputs have holes or are unreliable for other reasons, you should consider simulating your unknown set with a closed loop. Should its performance be all too awful you can also train a closed loop network from the very beginning.