forked from petercorke/toolbox-common-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilt1d.m
73 lines (60 loc) · 2 KB
/
filt1d.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%FILT1D 1-dimensional rank filter
%
% Y = FILT1D(X, OPTIONS) is the minimum, maximum or median value (1xN) of the
% vector X (1xN) compute over an odd length sliding window.
%
% Options::
% 'max' Compute maximum value over the window (default)
% 'min' Compute minimum value over the window
% 'median' Compute minimum value over the window
% 'width',W Width of the window (default 5)
%
% Notes::
% - If the window width is even, it is incremented by one.
% - The first and last elements of X are replicated so the output vector is the
% same length as the input vector.
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
% Copyright (c) Peter Corke 6/93
% vectorized version 8/95 pic
function m = filt1d(s, varargin)
opt.width = 5;
opt.op = {'max', 'min', 'median'};
opt = tb_optparse(opt, varargin);
% enforce a column vector
s = s(:)';
% enforce odd window length
w2 = floor(opt.width/2);
w = 2*w2 + 1;
n = length(s);
m = zeros(w,n+w-1);
s0 = s(1); sl = s(n);
% replicate first and last elements
for i=0:(w-1),
m(i+1,:) = [s0*ones(1,i) s sl*ones(1,w-i-1)];
end
switch (opt.op)
case 'max'
m = max(m);
case 'min'
m = min(m);
case 'median'
m = median(m);
end
m = m(w2+1:w2+n);