matlab版本R2018a。
1、新建function文件
文件名averaging_filter.m
内容如下:
% y = averaging_filter(x)
% Take an input vector signal 'x' and produce an output vector signal 'y' with
% same type and shape as 'x' but filtered.
function y = averaging_filter(x) %#codegen
% Use a persistent variable 'buffer' that represents a sliding window of
% 16 samples at a time.
persistent buffer;
if isempty(buffer)
buffer = zeros(16,1);
end
y = zeros(size(x), class(x));
for i = 1:numel(x)
% Scroll the buffer
buffer(2:end) = buffer(1:end-1);
% Add a new sample value to the buffer
buffer(1) = x(i);
% Compute the current average value of the window and
%