-
Notifications
You must be signed in to change notification settings - Fork 6
/
load_em_events.m
100 lines (89 loc) · 3.14 KB
/
load_em_events.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
function em_data=load_em_events(filename, snapfile, flipX, flipY)
% em_data = load_em_events(filename, snapfile = '', flipX=0, flipY=0)
% em_data = load_em_events(filename)
%
% Loads data from files generated by the StreamLogger consumer for an EM data
% stream. This function read the events from the file and looks for pair of
% conversion events. Its output consists of the inverses of the integration time
% for each successful gray level acquisition (which are directly representatives
% of gray levels).
% timestamps are in uS
% em_data is a structure containing the fields ts, x, y and gray
%
% flipX, flipY allow to flip the image around the X and Y axes. If these values
% are non zero, the corresponding dimension will be flipped considering its size
% to be the value contained in the 'flip' variable (i.e. X = flipX - X)
% (They defaults to 0 if non-specified)
if ~exist('flipX','var')
flipX = 0;
end
if ~exist('flipY','var')
flipY = 0;
end
height = 0;
width = 0;
output_index = 1;
cd_data = load_cd_events(filename, flipX, flipY);
if exist('snapfile', 'var') && ~isempty(snapfile)
fileID = fopen(snapfile, 'r');
if fileID < 0
disp 'Provided snapshot file does not exist, ignoring it'
end
else
fileID = -1;
end
if fileID >= 0
fscanf(fileID, '%f',1);
v = fscanf(fileID, '%f',1);
height = fscanf(fileID, '%f',1);
width = fscanf(fileID, '%f',1);
conv_start = -1*ones(width+1,height+1);
em_data.ts = zeros(1,width*height+sum(cd_data.p==1));
em_data.x = zeros(size(em_data.ts));
em_data.y = zeros(size(em_data.ts));
em_data.gray = zeros(size(em_data.ts));
for j = 1:height
for i = 1:width
emvalid = fscanf(fileID, '%f',1);
c = fscanf(fileID, '%f',1);
lmesurement = fscanf(fileID, '%f',1);
conv_start(i,j) = fscanf(fileID, '%f',1);
ltd = fscanf(fileID, '%f',1);
svalid = fscanf(fileID, '%f',1);
if lmesurement > 0
em_data.ts(output_index) = 0;
em_data.x(output_index) = i-1;
em_data.y(output_index) = j-1;
em_data.gray(output_index) = lmesurement;
output_index=output_index+1;
end
end
fscanf(fileID, '%f',1);
end
else
% no snap file
conv_start = -1*ones(max(cd_data.x)+1,max(cd_data.y)+1);
em_data.ts = zeros(1,sum(cd_data.p==1));
em_data.x = zeros(size(em_data.ts));
em_data.y = zeros(size(em_data.ts));
em_data.gray = zeros(size(em_data.ts));
end
for i=1:length(cd_data.ts)
if cd_data.p(i)==1
if conv_start(cd_data.x(i)+1,cd_data.y(i)+1) > 0
em_data.ts(output_index) = cd_data.ts(i);
em_data.x(output_index) = cd_data.x(i);
em_data.y(output_index) = cd_data.y(i);
em_data.gray(output_index) = 1/(cd_data.ts(i)-conv_start(cd_data.x(i)+1,cd_data.y(i)+1));
output_index=output_index+1;
conv_start(cd_data.x(i)+1,cd_data.y(i)+1) = -1;
end
else
conv_start(cd_data.x(i)+1,cd_data.y(i)+1) = cd_data.ts(i);
end
end
em_data.ts(output_index:end) = [];
em_data.x(output_index:end) = [];
em_data.y(output_index:end) = [];
em_data.gray(output_index:end) = [];
end