forked from open-ephys/simpleclust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_mua2features_2.m
179 lines (116 loc) · 3.27 KB
/
sc_mua2features_2.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
function features = mua2features_2(mua)
%writes a spike parm file for tetrode data
%
% ONLY DOES ONE CHANNEL RIGHT NOW
%
%D=(squeeze(reshape(tt.waveforms,1,32*size(tt.waveforms,1),size(tt.waveforms,3) )));
D= (squeeze(reshape(mua.waveforms,1,64,size(mua.waveforms,3))));
D=[D(1:2:end,:);D(2:2:end,:)];
%% run PCA
% D=[ D(1:2:end,:) , D(2:2:end,:)];
if size(D,2)<size(D,1) % pad to square if there are fewer spikes than points per waveform
D(:,size(D,1))=0;
end;
for i=1:size(D,1)
% possibly get rid of outliers for the pca
%{
level=0.001;
q=quantile(D(i,:),[level,1-level]);
D(i,D(i,:)>q(2))=q(2);
D(i,D(i,:)<q(1))=q(1);
%}
% center/rescale
D(i,:)=D(i,:)-mean(D(i,:));
s=std(D(i,:));
if s>0
D(i,:)=D(i,:)./s;
else
D(i,:)=D(i,:).*0;
end;
end;
disp('computing pca');
c=princomp(D,'econ');
coeffs=c(:,1:8);
%}
%% get Wavelet coeffs
%{
disp(' computing Wavelet features...')
coeffs=wave_features_wc_mod_8(D)./10;
%}
%plot(coeffs(:,1),coeffs(:,2),'.','MarkerSize',.5)
%% write data
disp(' making features ..')
features=[];
for n = 1:length(mua.ts)
spike = squeeze(mua.waveforms(:,:,n))./20;
% divide by 20 to get it into the default range for xclust
% this shouldn't matter for any further processing
% interpolate so we get better peaks etc?
%calculate Nonlinear energy (Teager energy operator)
for d=1:2 %size(spike,1)
x=spike(d,:);
neo(d) = mean(x(2:end-1).^2 - ( x(1:end-2) .* x(3:end) ))*10;
end;
[closest_time] = 0;
ind =0 ;
% nearest_pos_x = pos.xval(ind);
% nearest_pos_y = pos.yval(ind);
[peaks,peak_inds] = max(spike');
[troughs,trough_inds] = min(spike');
max_height = max(peaks+abs(troughs));
widths = trough_inds - peak_inds;
f = find(widths > 0);
if numel(f) > 0
max_width = max(widths(f));
else
max_width = 0;
end
%disp(n-1)
if size(coeffs,2)>2
pc = coeffs(n,1:8).*100;
% tt.Pcomps(i,3:4) = 0;
else
pc = [0 0 0 0 0 0 0 0 0];
end;
% pc = [0 0 0 0];
for d=1:2 %size(spike,1)
x=spike(d,:);
mid(d)=round(numel(x)/2)+1;
dd=diff(x);
y=min(find (( sign(dd(2:end))~=sign(dd(1:end-1)) ) .* x(2:end-1)<0 )+1);
if numel(y)>0
mins(d) = y;
else
mind(d)=0;
end;
pretime=0;
%{
clf;
plot(spike); hold on;
plot(dd,'g');
plot(dd.*0,'g');
plot(mid,spike(mid),'ro');
plot(mins,spike(mins),'rx');
%}
% min_before(d)=max(mins(mins<=mid));
% min_after(d)=min(mins(mins>=mid));
end;
features.ts(n)=mua.ts(n);
features.id(n)=n;
features.data(1,n)=peaks(1);
features.data(2,n)=peaks(2);
features.data(3,n)=neo(1);
features.data(4,n)=neo(2);
for i=1:8
features.data(4+i,n)=pc(i);
end;
features.data(12,n)=mua.ts(n);
end
features.name{1}='peak a';
features.name{2}='peak b';
features.name{3}='neo a';
features.name{4}='neo b';
for i=1:8
features.name{4+i}=['PCA ',num2str(i)];
end;
features.name{12}='time';