how to store the data in an array in simulink and compute the sum of squares of each element?

22 vues (au cours des 30 derniers jours)
I want to store real time data in an array and compute the sum of square of each elements and get the output. How can I achieve this without using a MATLAB function in simulink?
To make things clear, I am attaching my MATLAB function below. Basically, this code stores every sample in an array. Once the array is full, it shifts the elements to the right by one position and stores the next element at the start of the array. Again, I want to get the same output as below, but using only the Simulink blocks available.
function y = fcn(u,rst)
persistent str i;
if isempty(str)
str = zeros(1,3000);
end
if isempty(i)
i = 0;
end
i = i+1;
if (i<=3000)
str(i) = u;
else
for k=0:length(str)-2
str(end-k) = str(end-k-1);
end
str(1) = u;
end
y = sum(str.^2);
I tried to use the Queue block available, which is a FIFO register. I can store the elements and pop it when needed, but I don't know how to compute the sum of square of the elements stored in the queue.

Réponses (3)

di liu
di liu le 9 Avr 2020
Hi Saurav,
Do you solve this problem finally? I have met the same issue as you, could you tell me how to do. Thank you.

Walter Roberson
Walter Roberson le 29 Juil 2017
Suppose you use an arithmetic block to square the signals. Then tap the output. One branch goes to a https://www.mathworks.com/help/simulink/slref/discretetimeintegrator.html discrete integrator. The branch goes to a fifo of length 3000 (or whatever) and then to a discrete integrator. Now take the unbuffered integrator value minus the buffered integrator value: the result should be the sum of the values that are in the buffer. (Or, looking again, perhaps use a Cumulative Sum block https://www.mathworks.com/help/dsp/ref/cumulativesum.html instead of discrete time integrator)
Caution: Unless you happen to be using relatively low-value integer signals, the cumulative round-off error could be significant.
There is an alternate formulation. If you use a buffer block with an overlap equal to (length of buffer minus 1) then at each step you would get a frame of all of the values in the buffer, which you could add, perhaps using a Matrix Sum block.
  2 commentaires
Saurav Kumar
Saurav Kumar le 31 Juil 2017
Thanks, Walter. I am using a buffer, but I'm still not getting the result. The MATLAB function in the code is the same as posted. I was having issues with using a buffer, since my current model is continuous and the buffer is discrete. To convert the continuous time to discrete time, I'm using a zero order hold block.
I have attached the file for your reference. I would like to have the same output with using the buffer block, but I see a bump at t=30 seconds.
Walter Roberson
Walter Roberson le 31 Juil 2017
You cannot really talk about a certain number of samples ago for a continuous model, as the values are logically defined at each intermediate point and the number of intermediate points that will be used will depend on the needs of the adaptive solver. Not all of the solvers are guaranteed to only calculate forward in time, by the way. You can sample periodically, though.

Connectez-vous pour commenter.


Rintu Bhaskar
Rintu Bhaskar le 18 Sep 2021
Hello Saurav,
I am having similar problem. I need to store data (real number) and the have to fetch it for another calculation.
Can you share your solution, please?
Thank you.

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by