-
Notifications
You must be signed in to change notification settings - Fork 1
/
mavg.m
41 lines (36 loc) · 969 Bytes
/
mavg.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function y = mavg(x, w)
% MAVG Applies a moving average (low-pass) filter to vector x using a
% window of size w.
%
% y = MAVG(x, w)
%
% Parameters:
% x - Input vector.
% w - Size of moving average window.
%
% Returns:
% y - Output vector, after moving average filter.
%
% Details:
% The format of the data in each file is the following: columns
% correspond to outputs, while rows correspond to iterations.
%
% Copyright (c) 2015 Nuno Fachada
% Distributed under the MIT License (See accompanying file LICENSE or copy
% at http://opensource.org/licenses/MIT)
%
% If window size is 0...
if w == 0
% ... return vector as is.
y = x;
else
% ... otherwise find moving average.
y = zeros(numel(x) - w, 1);
for i = 1:numel(y)
if i <= w
y(i) = sum(x((i - (i - 1)):(i + (i - 1)))) / (2 * i - 1);
else
y(i) = sum(x((i - w):(i + w))) / (2 * w + 1);
end;
end;
end;