Best way to buffer vector data in Simulink for processing over a time window

I'm working with a lidar-equipped robot, and every 0.2 seconds it provides an array of 360 values corresponding to the return intensity received at each angle. For my application, I need to do some statistical analysis on the data over a user-defined time-window. For offline processing in MATLAB, I've just been appending each new array of scan values to a matrix, so that I have a 360-column "Intensities" matrix which grows over time, and I can select the time-window for processing by selecting the appropriate rows from the matrix.
intensitiesMatrix(i,:) = new_scandata_array;
Now I'm trying to transition my code to Simulink (particularly because I want to use Stateflow for certain aspects of my application) and I want to do the same kind of data buffering so I can perform windowed processing on the data in real-time. For some reason I'm having a hard time figuring out the best way to collect my successive scan vectors into a form that I can then process, as I did in my regular MATLAB approach. I've looked a bit at Data Stores; is that the right approach? Or is there a good way to do this with matrices as I was doing in MATLAB?

 Accepted Answer

Using Data Stores would indeed be a solution.
However, the dimensions of your matrix increases over time, making it a variable size variable. This has many implications, and certainly is not good code to deploy on a physical hardware target. At some point it would just run out of memory. Typically you will need to set a maximum matrix size that it may grow into to make sure you don't get a memory overrun.

4 Comments

Thanks for your response! I agree that a continually growing matrix would not be a good idea. My thought was to have a buffer so that the matrix would always have the most recent sensor data corresponding to the desired window length (e.g. if the desired window was 40, then the matrix would have 40 rows with the most recent 40 sensor arrays provided by the Lidar system). However, I wasn't sure how to implement this kind of buffer system with Data Stores, i.e. how to make the most recent array bump out the oldest one. Any thoughts on that?
You can use a Delay block, a Selector block and a Vector Concatenate to create a buffer with the 40 latest data vectors of 360 points.
See attached for a model in R2020b.
The only downside is that with each new data step, it needs to shift (copy paste) the 39 first columns one position over, and then store the new data column in the front. So it needs to copy paste a lot.
Alternatively, you could use an Assignment block to store the new data vector in the data buffer using an index. This way, the buffer will become a circular buffer with a moving window. It will overwrite the oldest data with the new data. It will not need to copy paste the data each time, but you will need to maintain an index and wrap the index back to 1 when it reaches 40. Also, for whatever postprocessing you want to do with it, it could become a little bit more complex with a circular buffer.
I have same scenarios. What about queue? How do we use queue for same operation?

Sign in to comment.

More Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 28 Jul 2021

Edited:

on 28 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!