2010/spike10: loadaerdat.m

File loadaerdat.m, 3.2 KB (added by shih, 8 years ago)

file for loading jaer files in Matlab

Line 
1function [allAddr,allTs]=loadaerdat(file);
2%function [allAddr,allTs]=loadaerdat(file);
3% loads events from a .dat file.
4% allAddr are int16 raw addresses.
5% allTs are int32 timestamps (1 us tick).
6% noarg invocations open file browser dialog (in the case of no input argument) directly create vars allAddr, allTs in
7% base workspace (in the case of no output argument).
8%
9% Header lines starting with '#' are ignored and printed
10%
11% Note: it is possible that the header parser can be fooled if the first
12% data byte is the comment character '#'; in this case the header must be
13% manually removed before parsing. Each header line starts with '#' and
14% ends with the hex characters 0x0D 0x0A (CRLF, windows line ending).
15
16maxEvents=30e6;
17
18if nargin==0,
19    [filename,path,filterindex]=uigetfile('*.dat','Select recorded retina data file');
20    if filename==0, return; end
21end
22if nargin==1,
23    path='';
24    filename=file;
25end
26
27
28f=fopen([path,filename],'r');
29% skip header lines
30bof=ftell(f);
31line=native2unicode(fgets(f));
32tok='#!AER-DAT';
33version=0;
34
35while line(1)=='#',
36    if strncmp(line,tok, length(tok))==1,
37        version=sscanf(line(length(tok)+1:end),'%f');
38    end
39    fprintf('%s\n',line(1:end-2)); % print line using \n for newline, discarding CRLF written by java under windows
40    bof=ftell(f);
41    line=native2unicode(fgets(f)); % gets the line including line ending chars
42end
43
44switch version,
45    case 0
46        fprintf('No #!AER-DAT version header found, assuming 16 bit addresses\n');
47        version=1;
48    case 1
49        fprintf('Addresses are 16 bit\n');
50    case 2
51        fprintf('Addresses are 32 bit\n');
52    otherwise
53        fprintf('Unknown file version %g',version);
54end
55
56numBytesPerEvent=6;
57switch(version)
58    case 1
59        numBytesPerEvent=6;
60    case 2
61        numBytesPerEvent=8;
62end
63
64       
65fseek(f,0,'eof');
66numEvents=floor((ftell(f)-bof)/numBytesPerEvent); % 6 bytes/event
67if numEvents>maxEvents,
68    fprintf('clipping to %d events although there are %d events in file\n',maxEvents,numEvents);
69    numEvents=maxEvents;
70end
71
72% read data
73fseek(f,bof,'bof'); % start just after header
74switch version,
75    case 1
76        allAddr=uint16(fread(f,numEvents,'uint16',4,'b')); % addr are each 2 bytes (uint16) separated by 4 byte timestamps
77        fseek(f,bof+2,'bof'); % timestamps start 2 after bof
78        allTs=uint32(fread(f,numEvents,'uint32',2,'b')); % ts are 4 bytes (uint32) skipping 2 bytes after each
79    case 2
80        allAddr=uint32(fread(f,numEvents,'uint32',4,'b')); % addr are each 4 bytes (uint32) separated by 4 byte timestamps
81        fseek(f,bof+4,'bof'); % timestamps start 4 after bof
82        allTs=uint32(fread(f,numEvents,'uint32',4,'b')); % ts are 4 bytes (uint32) skipping 4 bytes after each
83end
84
85fclose(f);
86
87if nargout==0,
88   assignin('base','allAddr',allAddr);
89   assignin('base','allTs',allTs);
90   fprintf('%d events assigned in base workspace as allAddr,allTs\n', length(allAddr));
91   dt=allTs(end)-allTs(1);
92   fprintf('min addr=%d, max addr=%d, Ts0=%d, deltaT=%d=%.2f s assuming 1 us timestamps\n',...
93       min(allAddr), max(allAddr), allTs(1), dt,double(dt)/1e6);
94end