forked from najjajm/myosort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smooth1D.m
202 lines (175 loc) · 5.57 KB
/
smooth1D.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
%% SMOOTH1D 1-dimensional smoothing
% Filters a signal via the fast fourier transform. Automatically detects
% and filters along the non-singleton dimension if only one exists;
% otherwise, smooths along any specified input dimension.
%
% SYNTAX
% Y = smooth1D(X, Fs, method, [norm], varargin)
%
% REQUIRED INPUTS
% X (numeric): time series array of any size
% Fs (scalar): sample frequency in Hz
% method (string): smoothing method. Options:
% 'beta' (Beta filter)
% 'gau' (Gaussian filter)
% 'box' (boxcar filter)
%
% OPTIONAL INPUTS
% norm (logical): if true, normalizes output by amplitude of the filter's
% impulse response. (default: false)
%
% PARAMETER INPUTS
% 'dim', <integer>: dimension to apply filter. (default: 1, if X has more
% than one non-singleton dimension)
%
% 'betaAlpha', <scalar>: beta shape parameter (alpha). (default: 3)
%
% 'betaBeta', <scalar>: beta shape parameter (beta). (default: 5)
%
% 'betaDur', <scalar>: beta duration (s). (default: 0.275)
%
% 'boxDur', <scalar>: boxcar duration (s). (default: 0.1)
%
% 'gauSd', <scalar>: standard deviation of Gaussian (s). (default: 0.025)
%
% 'gauWid', <scalar>: width of Gaussian filter (multiples of SD). (default: 4)
%
% OUTPUTS
% Y (numeric): smoothed input
%
% EXAMPLES
%
% % demonstrate that it works by filtering a delta function
% Fs = 1e3;
% t = -1:1/Fs:1;
% x = zeros(size(t));
% x(t==0) = 1;
%
% % filter with a truncated distribution (for neural data) and a full distribution with alpha=2, beta=5
% figure
% plot(t,x,'k')
% hold on
% plot(t,smooth1D(x,Fs,'beta',true),'b')
% plot(t,smooth1D(x,Fs,'beta',true,'betaAlpha',2,'betaBeta',5,'betaDur',1),'r')
%
% % filter with 10, 50, and 100 ms Gaussians
% figure
% plot(t,x,'k')
% hold on
% for sd = [10,50,100]*1e-3
% plot(t,smooth1D(x,Fs,'gau',true,'gauSd',sd))
% end
% set(gca,'ylim',[0 1])
%
% % filter with 250 and 500 ms boxcar
% figure
% plot(t,x,'k')
% hold on
% for dur = [250,500]*1e-3
% plot(t,smooth1D(x,Fs,'box',true,'boxDur',dur))
% end
% set(gca,'xtick',-1:0.125:1)
% set(gca,'ylim',[0 1])
%
% % filter multiple inputs at once
% N = 100;
% x = zeros(length(t),N);
% for ii = 1:N
% x(t==round(rand-0.5,log10(Fs)),ii) = 1;
% end
% figure
% plot(smooth1D(x,Fs,'gau'),'k')
%
% % filter along an arbitrary dimension (will be the same as above)
% x = permute(x,[3 2 1]);
% y = smooth1D(x,Fs,'gau','dim',3);
% figure
% plot(permute(y,[3 2 1]),'k')
%
% IMPLEMENTATION
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% SEE ALSO:
% Authors: Najja Marshall
% Emails: [email protected]
% Dated: July 2017
function Y = smooth1D(X, Fs, method, varargin)
%% Parse inputs
% initialize input parser
P = inputParser;
P.FunctionName = 'SMOOTH1D';
% validation functions
isscalarnum = @(x,lb,ub) isscalar(x) && isnumeric(x) && x>lb && x<ub;
% add required, optional, and parameter-value pair arguments
addRequired(P, 'X', @isnumeric)
addRequired(P, 'Fs', @(x) isscalarnum(x,0,Inf))
addRequired(P, 'method', @(s) ischar(s) && ismember(s,{'beta','box','gau'}))
addParameter(P, 'norm', false, @islogical)
addParameter(P, 'dim', 1, @(x) isscalarnum(x,0,ndims(X)+1) && x==round(x))
addParameter(P, 'betaAlpha', 3, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'betaBeta', 5, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'betaDur', 0.275, @(x) isscalarnum(x,0-eps,1+eps))
addParameter(P, 'boxDur', 0.1, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'gauSd', 0.025, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'gauWid', 4, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'sd', [], @(x) isscalarnum(x,0,Inf));
addParameter(P, 'wid', [], @(x) isscalarnum(x,0,Inf));
% clear workspace (parser object retains the data while staying small)
parse(P, X, Fs, method, varargin{:});
clear ans varargin
%% Make impulse response
switch method
case 'beta' % beta
% convert parameters to samples
xx = 0:1/Fs:P.Results.betaDur;
% impulse response
a = P.Results.betaAlpha;
b = P.Results.betaBeta;
B = (gamma(a)*gamma(b))/gamma(a+b);
fx = (xx.^(a-1).*(1-xx).^(b-1))/B;
case 'box' % Boxcar
% convert parameters to samples
wid = round(Fs * P.Results.boxDur);
wid = wid + mod(wid,2);
% sample points
xx = -wid/2:wid/2;
% impulse response
fx = ones(size(xx));
fx = [zeros(1,wid/2), fx, zeros(1,wid/2)];
case 'gau' % Gaussian
% convert parameters to samples
sd = Fs * P.Results.gauSd;
wid = round(sd * P.Results.gauWid);
% Gaussian samples
xx = -wid:wid;
% impulse response
fx = 1/(sd*sqrt(2*pi)) * exp(-xx.^2/(2*sd^2));
end
%% Filter using FFT
% set filter dimension
szX = size(X);
if nnz(szX > 1) == 1
filtDim = find(szX > 1);
else
filtDim = P.Results.dim;
end
% pad x to the length of the impulse response
padLen = min(floor(length(fx)/2), szX(filtDim));
permOrd = [filtDim,setdiff(1:ndims(X),filtDim)];
Xpad = permute(double(X),permOrd);
Xpad = cat(1,mean(Xpad(1:padLen,:,:),1).*ones(padLen,size(Xpad,2),size(Xpad,3)), Xpad,...
mean(Xpad(end-padLen+1:end,:,:),1).*ones(padLen,size(Xpad,2),size(Xpad,3)));
% FFT
nPad = 2^nextpow2(size(Xpad,1));
Y = ifft(fft(Xpad,nPad).*fft(fx(:),nPad));
% remove padding
Y = Y(2*padLen + (1:szX(filtDim)),:,:);
% reshape to input dimensions
[~,unpermOrd] = sort(permOrd);
Y = permute(Y,unpermOrd);
% normalize by magnitude of impulse response
if P.Results.norm
Y = Y/max(fx);
end