-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoving_window.m
More file actions
49 lines (42 loc) · 1.57 KB
/
moving_window.m
File metadata and controls
49 lines (42 loc) · 1.57 KB
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
function data_out = moving_window(data_in,window_size,function_handle,position_flag)
%this function applies the function "function_handle" in a rolling window
%across the first dimension of the data, with window size determined by
%window_size
%position_flag controls whether the window is centered on the point (0) or it
%precedes it (1)
%get the size of the data
data_num = size(data_in,1);
%allocate memory for the output
data_out = zeros(size(data_in));
%for the moving window range
for window = 1:data_num
switch position_flag
case 0
%define the behavior depending on the position
if window < ceil(window_size/2)
%get the actual window
window_idx = 1:window_size;
elseif window > data_num - floor(window_size/2)
%get the actual window
window_idx = data_num-window_size:data_num;
else
%get the actual window
window_idx = window-round(window_size/2)+1:window+round(window_size/2)-1;
end
case 1
%define the behavior depending on the position
if window < window_size
%get the actual window
window_idx = 1:window_size;
else
%get the actual window
window_idx = window-window_size+1:window;
end
end
%calculate the value
data_out(window,:) = function_handle(data_in(window_idx,:));
end
% figure
% plot(data_in(:,1))
% hold on
% plot(data_out(:,1))