-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReAlignSpikes.m
69 lines (64 loc) · 2.63 KB
/
ReAlignSpikes.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
function Spikes=ReAlignSpikes(CurrentWaveforms,RawTraces,SpikeTimes,UnitIDs)
for ElNum=1:size(RawTraces,1)
% if find(size(UnitIDs)==max(size(UnitIDs)))==1
% UnitIDs=UnitIDs';
% end
%flip waveforms
flipWF=0;
if find(size(CurrentWaveforms)==max(size(CurrentWaveforms)))==2
flipWF=1;
CurrentWaveforms=CurrentWaveforms';
end
uniqIDs=unique(UnitIDs);
if isempty(CurrentWaveforms)
try
% Spikes.Offline_Threshold.Units{ElNum,1}=zeros(1,numel(find(Spikes.Offline_Threshold.data{Channels(ElNum)})));
% Spikes.Offline_Threshold.SpikeTimes{ElNum,1}=find(Spikes.Offline_Threshold.data{Channels(ElNum)});
Spikes.Waveforms{ElNum,1}=ExtractChunks(RawTraces(ElNum,:),...
SpikeTimes,40,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% 0.25 bit per uV, so divide by 4 - adjust according to
% recording system
Spikes.Waveforms{ElNum,1}=Spikes.Waveforms{ElNum,1}./4;
catch
end
else
Spikes.Waveforms{ElNum,1}=CurrentWaveforms;
end
try
% figure; hold on
wfPeak=nan(length(uniqIDs),1);
for unit=1:length(uniqIDs)
meanWF=mean(Spikes.Waveforms{ElNum,1}(UnitIDs==uniqIDs(unit),:));
% plot(meanWF);
thWF=meanWF<min([-std(meanWF) min(meanWF)/2]); %crappy wf have low std
minimas = find(diff(thWF) >= 1,1)+1;
deriveWF=diff(meanWF); % plot(deriveWF)
try
wfPeak(unit)=minimas-find(deriveWF(minimas:-1:1)>0,1)+2;
catch %no peak before trough: align to first value by default
wfPeak(unit)=1;
end
end
for unit=1:length(uniqIDs)
if wfPeak(unit)>min(wfPeak)
SpikeTimes(UnitIDs==uniqIDs(unit))=SpikeTimes(UnitIDs==uniqIDs(unit))+(wfPeak(unit)-min(wfPeak));
end
end
% Spikes.Offline_Threshold.Units{ElNum,1}=zeros(1,numel(find(Spikes.Offline_Threshold.data{Channels(ElNum)})));
% Spikes.Offline_Threshold.SpikeTimes{ElNum,1}=find(Spikes.Offline_Threshold.data{Channels(ElNum)});
Spikes.Waveforms{ElNum,1}=ExtractChunks(RawTraces(ElNum,:),...
SpikeTimes,40,'tshifted'); %'tzero' 'tmiddle' 'tshifted'
% 0.25 bit per uV, so divide by 4 - adjust according to
% recording system
Spikes.Waveforms{ElNum,1}=Spikes.Waveforms{ElNum,1}./4;
if flipWF==1
Spikes.Waveforms{ElNum,1}={Spikes.Waveforms{ElNum,1}'};
end
Spikes.SpikeTimes={SpikeTimes};
catch
end
% figure; hold on
% for unit=1:length(unique(UnitIDs))
% plot(mean(Spikes.Waveforms{ElNum,1}(UnitIDs==uniqIDs(unit),:)))
% end
end