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

S
Santiago · Sep 23, 2020 · 2K views
Question
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 PhD Expert
Answered Nov 20, 2025
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);
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!