forked from OHBA-analysis/osl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osl_movavg.m
65 lines (52 loc) · 1.77 KB
/
osl_movavg.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
function [data_avg,t_avg] = osl_movavg(data,t,winsize,overlap,resamp,robust)
% Moving window averaging
% [data_avg,t_avg] = OSL_MOVAVG(data,t,winsize,overlap,resamp,robust)
% -----------------------------------------------------------------
% data - data to average
% t - vector of sample times
% winsize - window to average over (samples)
% overlap - amount of segment overlapping (e.g. 0.75 = 75% overlap)
% resamp - resample to original data length
% robust - use robust averaging (% of data to exlude)
% -----------------------------------------------------------------
% AB 2011
if nargin < 4
overlap = 0.75;
end
if nargin < 5
resamp = 0;
end
if nargin < 6
robust = 0;
end
if isempty(t)
t = 1:length(data);
end
data = data(:)'; t = t(:)';
data_pad = [nan(1,winsize),data,nan(1,winsize)];
t_pad = [nan(1,winsize),t,nan(1,winsize)];
bf = buffer(data_pad,winsize,round(winsize*overlap),'nodelay');
tbf = buffer(t_pad,winsize,round(winsize*overlap),'nodelay');
% reject data sections where more than 25% of data is NaN
nans = isnan(bf);
bf(nans) = 0;
tbf(nans) = 0;
sum_nans = sum(nans);
bf( :,sum_nans > 0.25*winsize) = nan; % - do this by replacing all with nans
tbf(:,sum_nans > 0.25*winsize) = nan; % - do this by replacing all with nans
if robust ~= 0
data_avg = trimmean(bf,robust);
else
data_avg = sum(bf)./(size(bf,1) - sum_nans);
end
t_avg = sum(tbf)./(size(tbf,1) - sum_nans);
if resamp
t_start = t_avg(find(~isnan(t_avg),1,'first'));
t_end = t_avg(find(~isnan(t_avg),1,'last'));
data_i = nan(size(t));
data_i(t>=t_start & t<t_end) = interp1(t_avg(t_avg>=t_start & t_avg<t_end),data_avg(t_avg>=t_start & t_avg<t_end),t(t>=t_start & t<t_end),'linear',nan);
data_avg = data_i;
data_avg(isnan(data)) = nan;
t_avg = t;
end
end