Burhan
Burhan

Reputation: 35

Moving Average block returns wrong values for column vector input

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:

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:

  1. Screenshot showing probes for measuring actual input and moving average output along with the Moving Average block parameters Probes

  2. 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.

Upvotes: 3

Views: 676

Answers (1)

Wolfie
Wolfie

Reputation: 30047

I can reproduce this with the following minimal example:

blocks

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

demux example

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

enter image description here

Upvotes: 3

Related Questions