-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.m
66 lines (48 loc) · 1.56 KB
/
cluster.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
function [idx] = cluster(spikes,optional_cluster_override,pca_yes)
% Scott Grimes - Max Planck Cybernetics - 2011
% Clustering
L=length(spikes(1,:)); %length of the spike
spike_num=length(spikes(:,1)); %number of spikes
if pca_yes ==0 %if not using pca...
%wavelet decomp
spikes_wave=0;
for i=1:spike_num
[c,l] = wavedec(spikes(i,:),5,'db5');
if i==1
spikes_wave=c;
else
spikes_wave=[spikes_wave;c];
end
end
fprintf('Wavelet Decomp Complete...\n')
clear c
%ends wavelet decomp
idx = zeros(spike_num,1);
%normalizing spikes
for i = 1:spike_num
spikes(i,1:L) = spikes(i,:)/norm(spikes(i,:));
end
if optional_cluster_override==0 %auto detect cluster number
idx = kmeans([spikes_wave],15,'EmptyAction','drop');
k = cluster_consol([spikes_wave],idx);
idx = kmeans([spikes_wave],k,'EmptyAction','drop');
fprintf('Using %i-Clusters\n',k);
else %manual cluster number
idx = kmeans([spikes_wave],optional_cluster_override,'EmptyAction','drop');
end
else %using PCA
[coef,score,latent,tsquare]=princomp(spikes);
fprintf('PC loadings calculated...\n');
comp = cumsum(latent)./sum(latent);
k = find(comp>=.98,1);
if optional_cluster_override==0
idx = kmeans(score(:,1:k),15,'EmptyAction','drop');
k = cluster_consol(score(:,1:k),idx);
idx = kmeans(score(:,1:k),k,'EmptyAction','drop');
fprintf('Using %i-Clusters\n',k);
clear coef
else
idx = kmeans(score(:,1:k),optional_cluster_override,'EmptyAction','drop'); %manual cluster number
end
end
fprintf('Sorting Completed - %i Clusters\n',max(idx))