-
Notifications
You must be signed in to change notification settings - Fork 9
/
nt_smooth.m
52 lines (43 loc) · 1.19 KB
/
nt_smooth.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
function x=nt_smooth(x,T,nIterations,nodelayflag)
%y=nt_smooth(x,T,nIterations,nodelayflag) - smooth by convolution with square window
%
% y: smoothed data
%
% x: data to smooth
% T: samples, size of window (can be fractionary)
% nIterations: number of iterations of smoothing operation (large --> gaussian kernel)
% nodelayflag: if true, compensate for delay [default:false]
%
nt_greetings;
if nargin<4||isempty(nodelayflag); nodelayflag=0; end
if nargin<3||isempty(nIterations); nIterations=1; end
if nargin<2; help nt_smooth ; error('!'); end
if ndims(x)>4; error('!'); end
integ=floor(T);
frac=T-integ;
if integ>=size(x,1);
x=repmat(mean(x),[size(x,1),1,1,1]);
return;
end
% remove onset step
mn=mean(x(1:(integ+1),:,:),1);
x=bsxfun(@minus,x,mn);
if nIterations==1 && frac==0;
% faster
x=cumsum(x);
x(T+1:end,:)=x(T+1:end,:)-x(1:end-T,:);
x=x/T;
else
% filter kernel
B=[ones(integ,1);frac]/T;
for k=1:nIterations-1
B=conv(B,[ones(integ,1);frac]/T);
end
x=filter(B,1,x);
end
if nodelayflag
shift=round(T/2*nIterations); %[shift n*T]
x=[x(shift+1:end,:,:,:); zeros(shift,size(x,2),size(x,3),size(x,4))];
end
% restore DC
x=bsxfun(@plus,x,mn);