I'm trying to do simple averaging in Simulink and need some help with it.

Illustration
Santiago - 2020-09-23T09:34:26+00:00
Question: I'm trying to do simple averaging in Simulink and need some help with it.

Assume AI'm acquiring a noisy signal at 1 mS interval and I want to get 1 point every 50 mS. I would like to collect the data for the 50 mS and spit out an average for logging. I'm not sure how to do it.

Expert Answer

Profile picture of John Michell John Michell answered . 2025-11-20

function y = my_average(u) %# codegen
persistent i buf bufSum;
if isempty(buf)
    buf = zeros(1,50);
    bufSum = 0;
    i = 1;
end
bufSum = bufSum - buf(i);
buf(i) = u;
bufSum = bufSum + u;
i = i + 1;
if i > numel(buf)
    i = 1;
end
y = bufSum / numel(buf);


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!