-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplaceecodes.m
110 lines (83 loc) · 3.37 KB
/
replaceecodes.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
function [newecodes, newetimes,clus_names] = replaceecodes(ecodes,etimes,figs)
if nargin <3
figs=0;
end
global triggertimes spike2times clustercodes
whentrigs = round(triggertimes.*1e3);
whenspikes = round(spike2times(clustercodes ~= 0).*1e3);
whatcodes = double(clustercodes(clustercodes ~= 0));
clus_names = unique(whatcodes);
%% recast in terms of REX times
starttrigs = etimes(ecodes == 1001);
keep_min_rex = min(starttrigs);
keep_min_spk2 = min(whentrigs);
starttrigs = starttrigs - keep_min_rex + 1;
whentrigs = whentrigs - keep_min_spk2 + 1;
rast_starttrigs = 1:max(starttrigs);
rast_whentrigs = 1:max(whentrigs);
rast_starttrigs = double(ismember(rast_starttrigs, starttrigs));
rast_whentrigs = double(ismember(rast_whentrigs, whentrigs));
if length(whentrigs)/sum(ecodes == 1001) == 2 %expected ratio of triggers to trials (2 triggers per trial
whentrigs=whentrigs(1:2:end); %keep only start trigger times and remove end triggers. Makes for better correlation
where_max = 0;
else %either spurious codes in token task, or wrong recording sequence (e.g. Spike2 recording started after REX recording)
disp('Warning! Inconsistent number of triggers. Will attempt to align via cross correlation.');
[corr_vec,lag_range] = xcorr(rast_starttrigs,rast_whentrigs);
where_max = lag_range(corr_vec == max(corr_vec));
%where_max = 0;
%pause;
end
offset = keep_min_rex - keep_min_spk2 + where_max(1);
if figs
fprintf('There are %d triggers\n',length(whentrigs));
fprintf('There are %d start times\n',sum(ecodes == 1001));
figure(101);
plot(lag_range,corr_vec,'ko');
title('Cross correlation of trigger times and trial start times');
figure(99);clf;
whentrigs = whentrigs + where_max(1) + keep_min_rex - 1;
plot(whentrigs,whentrigs.^0,'rd','MarkerSize',20); % red diamonds: spike2 triggers
hold on;
plot(etimes(ecodes == 1001),(etimes(ecodes==1001)).^0,'ko','MarkerSize',20); % black circles rex start codes
ticks = unique([etimes(ecodes == 1001); whentrigs]);
set(gca,'XTick',ticks)
set(gca,'XTickLabel',sprintf('%3.0f|',ticks))
legend('Spk2 trigs','REX start codes');
starttrigs = etimes(ecodes == 1001);
align_error = zeros(length(starttrigs),1);
for a = 1:length(starttrigs)
curr_trig = starttrigs(a);
errors = abs(whentrigs-curr_trig);
align_error(a) = min(errors);
end
error_bar = mean(align_error);
fprintf('The alignment is %5.2f miliseconds off on average',error_bar);
end
whenspikes = whenspikes + offset;
%% Remove old spikes
newecodes = ecodes;
newetimes = etimes;
newetimes(newecodes == 610) = [];
newecodes(newecodes == 610) = [];
%% Remove old references to analog
atimes = newetimes(newecodes == -112);
newetimes(newecodes == -112) = [];
newecodes(newecodes == -112) = [];
%% Label new clusters
howmanyclus = length(clus_names);
for a = 1:howmanyclus
clus_label = 600+clus_names(a);
thesespikes = whenspikes(whatcodes == clus_names(a)); % Isolate cluster
addecodes = clus_label.*ones(length(thesespikes),1);
newecodes = [ newecodes; addecodes];
newetimes = [ newetimes; thesespikes]; % Add new spikes
end
%% sort
[newetimes,ind] = sort(newetimes);
newecodes = newecodes(ind);
%% add analog references
addacodes = -112.*ones(length(atimes),1);
newetimes = [newetimes; atimes];
newecodes = [newecodes; addacodes];
end
%end