forked from najjajm/myosort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavetemplate.m
executable file
·116 lines (98 loc) · 3.06 KB
/
wavetemplate.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
%% WAVETEMPLATE waveform template
% Constructs the multi-channel waveform template from a set of waveforms
%
% SYNTAX
% [u, srtIdx] = wavetemplate(w, varargin)
%
% REQUIRED INPUTS
% w (cell): array of waveforms of dimensions [units x channels]
%
% OPTIONAL INPUTS: none
%
% PARAMETER INPUTS
% 'sort', <logical>: if true, sorts set of templates by their average
% energy across channels (default: false)
%
% 'plot', <logical>: if true, plots templates (default: false)
%
% 'method', <string>: if 'mean' (default), estimates template as the mean
% over all waveform shapes. If 'pmax', estimates template as the
% smoothed maximum of the probability density at each sample point
%
% 'Fs', <scalar>: sample rate in Hz (default: 3e4)
%
% 'frame', <scalar>: fraction of waveform duration used to compute
% templates (default: 1)
%
% OUTPUTS
% u (numeric): 3D multi-channel template array of dimensions
% [wave length x channels x units]
%
% srtIdx (numeric): vector of indices corresponding to the sort order (if
% applied)
%
% EXAMPLE(S)
%
%
% IMPLEMENTATION
% Other m-files required: PLOTWAVETEMPLATE
% Subfunctions: PLOTWAVETEMPLATE
% MAT-files required: none
%
% SEE ALSO: PLOTWAVETEMPLATE
% Authors: Najja Marshall
% Emails: [email protected]
% Dated: February 2019
function [u, srtIdx] = wavetemplate(w, varargin)
%% Parse inputs
% initialize input parser
P = inputParser;
P.FunctionName = 'WAVETEMPLATE';
% validation functions
isscalarnum = @(x,lb,ub) isscalar(x) && isnumeric(x) && x>lb && x<ub;
% add required, optional, and parameter-value pair arguments
addRequired(P, 'w', @iscell)
addParameter(P, 'sort', false, @islogical)
addParameter(P, 'plot', false, @islogical)
addParameter(P, 'method', 'mean', @(x) ischar(x) && ismember(x,{'mean','pmax'}))
addParameter(P, 'Fs', 3e4, @(x) isscalarnum(x,0,Inf))
addParameter(P, 'frame', 1, @(x) isscalarnum(x,0-eps,1+eps))
% clear workspace (parser object retains the data while staying small)
parse(P, w, varargin{:});
clear ans varargin
%%
[nUnits,nChannels] = size(w);
waveLen = size(w{1},2);
% set frame
len = round(waveLen*P.Results.frame);
win = waveLen/2 + (1-len/2:len/2);
% average and reshape to length x channel x unit
u = cellfun(@(x) mean(x(:,win),1)',w,'uni',false);
if strcmp(P.Results.method, 'mean')
u = cell2mat(reshape(u',1,nChannels,nUnits));
else % re-estimate template based on maximum probability density
uMax = cellfun(@max,u);
uMin = cellfun(@min,u);
u = cell2mat(reshape(u',1,nChannels,nUnits));
for ii = 1:nUnits
for jj = 1:nChannels
yl = [min(-500,2*uMin(ii,jj)), max(500,2*uMax(ii,jj))];
[~,b,lc] = histfun(w{ii,jj},'lim',yl);
[~,maxIdx] = max(lc,[],1);
u(:,jj,ii) = smooth1D(b(maxIdx)',P.Results.Fs,'gau','sd',5e-5);
end
end
end
% sort by energy
if P.Results.sort
ener = squeeze(sum(u.^2,1));
ener = mean(ener,1);
[~,srtIdx] = sort(ener);
u = u(:,:,srtIdx);
else
srtIdx = 1:nUnits;
end
% plot
if P.Results.plot
plotwavetemplate(u);
end