-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsmooth.m
30 lines (28 loc) · 1.02 KB
/
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
function sdata = smooth(data, winwidth, type)
if winwidth < 1
sdata = data;
return;
end
if nargin < 3
type = 'gauss';
end
data = data(:)'; %ensure data as a row;
winwidth = ceil(winwidth/2)*2; % force even winsize for odd window
switch type,
case 'hann'
window = 0.5*(1 - cos(2*pi*(0:1/winwidth:1))); % hanning window
case 'mean'
window = ones(1,winwidth+1); %moving average
case 'gauss'
window = normpdf(1:(winwidth+1), winwidth/2+1, winwidth/8);
case 'expl'
window = [zeros(1, winwidth/2), exp(-3*(0:2/winwidth:1))];
% case 'bateman'
% window = bateman(1:winwidth,0,0,5,50);
otherwise
error('Unknown type')
end
window = window / sum(window); % normalize window
data_ext = [ones(1,winwidth/2)*data(1), data, ones(1,winwidth/2)*data(end)]; %extend data to reduce convolution error at beginning and end
sdata_ext = conv(data_ext, window); % convolute with window
sdata = sdata_ext(1+winwidth : end-winwidth); %cut to data length