-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevTriggerAvg.m
49 lines (41 loc) · 1.65 KB
/
evTriggerAvg.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
function RF = evTriggerAvg(stim, stim_binedges, num_avgbin, event_stamps)
%
% RF (or avg) = evTriggerAvg(stim, stim_binedges, num_avgbin, event_stamps)
%
% stim - stimulus data (N by M by # frames)
% stim_binedges - Real time edge stamps of the stimulus frame or bin
% num_avgbin - how long do you want to see the stimulus ahead of the
% response?
% event_stamps - Real time stamps of the events (ms)
% initialize avg matrix as an unsigned integer
%nbox = size(stim,1);
avg = zeros(size(stim,1), size(stim,2), num_avgbin,'uint32');
% how long do you want to see the stimulus that elicit the event?
% n_avgbin = 20; % frameduration 30 ms * 20 = 600 ms
% where are those events in the stim bin ?
% Convert event real-time stamps into stim bin timestamps (index)
% and # of events
% Length(Nevent) = Length(# frame) + 1
Nevents = histc(event_stamps, stim_binedges);
% last element is adding to the former element.
Nevents(end-1) = Nevents(end-1) + Nevents(end);
Nevents = Nevents(1:(end-1));
% gather the stim snippet
% index number for the timing when the event happened.
idx = find(Nevents>0);
% ignore the first few frames shorter than nframe, and last element.
idx = idx(idx>num_avgbin);
% Now, idx has same length as stim frame rate
if isempty(idx)
disp('FUNCTION: evTriggerAvg: no elements in idx');
return;
end
% What are the stimulus ahead of those events?
for i = 1:length(idx)
num = Nevents(idx(i));
snippet = stim(:, :, (idx(i)-num_avgbin+1):idx(i) );
avg = avg + num * cast(snippet, 'uint32');
end
RF = double(avg)/sum(Nevents(idx));
% RF = cast( double(avg)/sum(Nevents(idx)), 'uint8' );
end