-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathload_ext_trigger_data.m
70 lines (58 loc) · 2 KB
/
load_ext_trigger_data.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
function ext_trigger=load_ext_trigger_data(filename)
% ext_trigger_data = load_ext_trigger_data(filename)
%
% Loads data from files generated by the StreamLogger consumer for EventExtTrigger
% events [ts,x,y,p,id].
% timestamps are in uS
% ext_trigger is a structure containing the fields ts, x, y, p and id
f=fopen(filename);
% Parse header if any
header = [];
endOfHeader = 0;
numCommentLine = 0;
while (endOfHeader==0)
bod = ftell(f);
tline = fgets(f,256);
if(tline(1)~='%')
endOfHeader = 1;
else
words = strsplit(tline);
if (length(words) > 2 )
if (strcmp(words{2} , 'Date'))
if (length(words) > 3)
header = [header; {words{2}, horzcat(words{3}, ' ', words{4})}];
end
else
header = [header; {words{2}, words{3}}];
end
end
numCommentLine = numCommentLine+1;
end
end
fseek(f,bod,'bof');
evType = 0;
evSize = 8;
if (numCommentLine>0) % Ensure compatibility with previous files.
% Read event type
evType = fread(f,1,'char');
% Read event size
evSize = fread(f,1,'char');
end
bof=ftell(f);
fseek(f,0,'eof');
numEvents=floor((ftell(f)-bof)/evSize);
% read data
fseek(f,bof,'bof'); % start just after header
allTs=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % ts are 4 bytes (uint32) skipping 4 bytes after each
fseek(f,bof+4,'bof'); % timestamps start 4 after bof
allAddr=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % addr are each 4 bytes (uint32) separated by 4 byte timestamps
fclose(f);
ext_trigger.ts = double(allTs);
polmask = hex2dec('000000F');
idmask = hex2dec('FC00000');
polshift=0; % bits to shift p to right
idshift=26;
addr=abs(allAddr); % make sure non-negative or an error will result from bitand (glitches can somehow result in negative addresses...)
ext_trigger.p=-1+2*double(bitshift(bitand(addr,polmask),-polshift)); % 1 for ON, -1 for OFF
ext_trigger.id=double(bitshift(bitand(addr,idmask),-idshift)); % id addresses
end