├── LICENSE ├── README.md ├── generate_em_snapshot.m ├── load_cd_events.m ├── load_em_events.m ├── load_ext_trigger_data.m └── play_cd_recording.m /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 Prophesee 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MATLAB scripts 2 | 3 | In this repo you will find some basic scripts to show how to work with data from Prophesee sensors in [MATLAB](https://www.mathworks.com/products/matlab.html). 4 | These scripts require a [DAT file](https://docs.prophesee.ai/stable/data/file_formats/dat.html) as an input with data recorded with [one of our cameras](https://docs.prophesee.ai/stable/hw/evk/index.html). 5 | 6 | If you only have a RAW file, you need to convert it to DAT format first. To this end, use our application [metavision_file_to_dat](https://docs.prophesee.ai/stable/samples/modules/driver/file_to_dat.html) 7 | which is part of [Metavision SDK](https://docs.prophesee.ai). 8 | 9 | To try out those scripts, you can also download and use one of the [RAW and DAT files we provide](https://docs.prophesee.ai/stable/datasets.html). 10 | 11 | ## Main features 12 | 13 | The MATLAB scripts allow to: 14 | * [`load_cd_events.m`](load_cd_events.m): uploads CD events to MATLAB 15 | * [`load_em_events.m`](load_em_events.m): uploads EM events to MATLAB *(deprecated for the current sensor generation)* 16 | * [`load_ext_trigger_data.m`](load_ext_trigger_data.m): uploads data from external triggers to MATLAB 17 | * [`play_cd_recording.m`](play_cd_recording.m): generates a video from CD events 18 | * [`generate_em_snapshot.m`](generate_em_snapshot.m): generates gray-scale images from EM events *(deprecated for the current sensor generation)* 19 | 20 | Note that by default the provided scripts will attempt to upload the entire DAT files into memory. 21 | Therefore, it will be useful to split your DAT file into several smaller files at first, or modify the Matlab scripts to limit the memory requirements. 22 | 23 | 24 | ## Uploading CD data to MATLAB 25 | 26 | 27 | The script [`load_cd_events.m`](load_cd_events.m) provides an example of uploading CD events to MATLAB. 28 | 29 | The function `load_cd_events` requires the following input arguments: 30 | * path to DAT file with recorded CD events 31 | * flipX flag - for flipping data over the X axis (0 by default) 32 | * flipY flag - for flipping data over the Y axis (0 by default) 33 | 34 | The script returns a data structure containing the following fields for every CD event: 35 | 36 | * `x` - x-position of the event in the sensor matrix 37 | * `y` - y-position of the event in the sensor matrix 38 | * `ts` - timestamp of the event 39 | * `p` - polarity of the event 40 | 41 | Example of script execution: 42 | ``` 43 | cd_data = load_cd_events("/path/to/my_record.dat") 44 | ``` 45 | 46 | ## Generating a video and frames from CD events 47 | 48 | The script [`play_cd_recording.m`](play_cd_recording.m) provides an example of generating a video from CD events and replaying this video. 49 | 50 | The function `play_cd_recording` requires the following input arguments: 51 | * `filename` - path to a DAT file with recorded CD events 52 | * `width` - width of the sensor 53 | * `height` - height of the sensor 54 | * `framerate` - frame rate of the output file 55 | * `acc_time` - accumulation time (in microseconds). This is the duration during which each event will be shown 56 | 57 | The script returns a structure containing a sequence of frames generated by iteratively accumulating 58 | the information from the CD events. Each frame shows the CD events generated by the sensor over the given 59 | accumulation time duration preceding the timestamp associated with the frame. For instance, if the given frame rate 60 | is 1000 FPS (frames per seconds) and the accumulation time is 10000 µs (microseconds), each CD event will be displayed 61 | during 10 frames. See [Generating frames from CD events](https://docs.prophesee.ai/stable/concepts.html#generating-frames-from-cd-events) SDK doc page 62 | to know more on this topic. 63 | 64 | For example, for a [Gen3 sensor](https://docs.prophesee.ai/stable/hw/sensors/gen31.html), the following command will generate a 25 FPS video with the accumulation time of 10000 µs: 65 | ``` 66 | play_cd_recording('/path/to/my_record.dat',640,480,25,10000) 67 | ``` 68 | 69 | ## Contact 70 | This code is open to contributions, thus do not hesitate to propose pull requests or create/fix bug reports. 71 | 72 | For any other information, [contact us](https://www.prophesee.ai/contact-us/) 73 | 74 | -------------------------------------------------------------------------------- /generate_em_snapshot.m: -------------------------------------------------------------------------------- 1 | function img=generate_em_snapshot(em_data, start_time, duration, sx, sy) 2 | % img = generate_em_snapshot(em_data, from, win, sx=304, sy=240) 3 | % 4 | % Makes a snapshot of the gray levels contained in an em_data structure. 5 | % This snapshot consists of the last gray level value contained in em_data 6 | % for each pixel from 'start_time' and for 'duration' (all given in uS) 7 | % 8 | % 'sx' and 'sy' represent the size of the generated image and defaults to 9 | % the size of the GEN1 sensor when non-specified 10 | 11 | if ~exist('sx','var') 12 | sx = 304; 13 | end 14 | if ~exist('sy','var') 15 | sy = 240; 16 | end 17 | 18 | img = zeros(sy,sx); 19 | start = find(em_data.ts>=start_time, 1, 'first'); 20 | end_time = start_time+duration; 21 | 22 | for i=start:length(em_data.ts) 23 | if (em_data.ts(i) > end_time) 24 | break; 25 | end 26 | img(em_data.y(i)+1,em_data.x(i)+1) = em_data.gray(i); 27 | end 28 | -------------------------------------------------------------------------------- /load_cd_events.m: -------------------------------------------------------------------------------- 1 | function cd_data = load_cd_events(filename, flipX, flipY) 2 | % cd_data = load_cd_events(filename, flipX=0, flipY=0) 3 | % 4 | % Loads data from files generated by the StreamLogger consumer for any type 5 | % of event. This function only read (t,x,y,p) and discard other fields (if 6 | % any) of events. 7 | % timestamps are in uS 8 | % cd_data is a structure containing the fields ts, x, y and p 9 | % 10 | % flipX, flipY allow to flip the image around the X and Y axes. If these values 11 | % are non zero, the corresponding dimension will be flipped considering its size 12 | % to be the value contained in the 'flip' variable (i.e. X = flipX - X) 13 | % (They defaults to 0 if non-specified) 14 | 15 | if ~exist('flipX','var') 16 | flipX = 0; 17 | end 18 | if ~exist('flipY','var') 19 | flipY = 0; 20 | end 21 | 22 | f=fopen(filename); 23 | 24 | % Parse header if any 25 | header = []; 26 | endOfHeader = 0; 27 | numCommentLine = 0; 28 | while (endOfHeader==0) 29 | bod = ftell(f); 30 | tline = fgets(f,256); 31 | if(tline(1)~='%') 32 | endOfHeader = 1; 33 | else 34 | words = strsplit(tline); 35 | if (length(words) > 2 ) 36 | if (strcmp(words{2} , 'Date')) 37 | if (length(words) > 3) 38 | header = [header; {words{2}, horzcat(words{3}, ' ', words{4})}]; 39 | end 40 | else 41 | header = [header; {words{2}, words{3}}]; 42 | end 43 | end 44 | numCommentLine = numCommentLine+1; 45 | end 46 | end 47 | fseek(f,bod,'bof'); 48 | 49 | evType = 0; 50 | evSize = 8; 51 | if (numCommentLine>0) % Ensure compatibility with previous files. 52 | % Read event type 53 | evType = fread(f,1,'char'); 54 | % Read event size 55 | evSize = fread(f,1,'char'); 56 | end 57 | 58 | 59 | bof=ftell(f); 60 | 61 | fseek(f,0,'eof'); 62 | numEvents=floor((ftell(f)-bof)/evSize); 63 | 64 | % read data 65 | fseek(f,bof,'bof'); % start just after header 66 | allTs=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % ts are 4 bytes (uint32) skipping 4 bytes after each 67 | fseek(f,bof+4,'bof'); % timestamps start 4 after bof 68 | allAddr=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % addr are each 4 bytes (uint32) separated by 4 byte timestamps 69 | 70 | fclose(f); 71 | 72 | cd_data.ts = double(allTs); 73 | 74 | version = 0; 75 | index = find(strcmp(header(:,1), 'Version')); 76 | if (~isempty(index)) 77 | version = header{index, 2}; 78 | end 79 | 80 | if (version < 2) 81 | xmask = hex2dec('000001FF'); 82 | ymask = hex2dec('0001FE00'); 83 | polmask = hex2dec('00020000'); 84 | xshift=0; % bits to shift x to right 85 | yshift=9; % bits to shift y to right 86 | polshift=17; % bits to shift p to right 87 | else 88 | xmask = hex2dec('00003FFF'); 89 | ymask = hex2dec('0FFFC000'); 90 | polmask = hex2dec('10000000'); 91 | xshift=0; % bits to shift x to right 92 | yshift=14; % bits to shift y to right 93 | polshift=28; % bits to shift p to right 94 | end 95 | 96 | addr=abs(allAddr); % make sure non-negative or an error will result from bitand (glitches can somehow result in negative addressses...) 97 | cd_data.x=double(bitshift(bitand(addr,xmask),-xshift)); % x addresses 98 | cd_data.y=double(bitshift(bitand(addr,ymask),-yshift)); % y addresses 99 | cd_data.p=-1+2*double(bitshift(bitand(addr,polmask),-polshift)); % 1 for ON, -1 for OFF 100 | 101 | if (flipX > 0) 102 | cd_data.x = flipX - cd_data.x; 103 | end 104 | 105 | if (flipY > 0) 106 | cd_data.y = flipY - cd_data.y; 107 | end 108 | 109 | end 110 | -------------------------------------------------------------------------------- /load_em_events.m: -------------------------------------------------------------------------------- 1 | function em_data=load_em_events(filename, snapfile, flipX, flipY) 2 | % em_data = load_em_events(filename, snapfile = '', flipX=0, flipY=0) 3 | % em_data = load_em_events(filename) 4 | % 5 | % Loads data from files generated by the StreamLogger consumer for an EM data 6 | % stream. This function read the events from the file and looks for pair of 7 | % conversion events. Its output consists of the inverses of the integration time 8 | % for each successful gray level acquisition (which are directly representatives 9 | % of gray levels). 10 | 11 | % timestamps are in uS 12 | % em_data is a structure containing the fields ts, x, y and gray 13 | % 14 | % flipX, flipY allow to flip the image around the X and Y axes. If these values 15 | % are non zero, the corresponding dimension will be flipped considering its size 16 | % to be the value contained in the 'flip' variable (i.e. X = flipX - X) 17 | % (They defaults to 0 if non-specified) 18 | 19 | if ~exist('flipX','var') 20 | flipX = 0; 21 | end 22 | if ~exist('flipY','var') 23 | flipY = 0; 24 | end 25 | 26 | height = 0; 27 | width = 0; 28 | 29 | output_index = 1; 30 | 31 | 32 | cd_data = load_cd_events(filename, flipX, flipY); 33 | 34 | if exist('snapfile', 'var') && ~isempty(snapfile) 35 | fileID = fopen(snapfile, 'r'); 36 | if fileID < 0 37 | disp 'Provided snapshot file does not exist, ignoring it' 38 | end 39 | else 40 | fileID = -1; 41 | end 42 | 43 | if fileID >= 0 44 | fscanf(fileID, '%f',1); 45 | v = fscanf(fileID, '%f',1); 46 | height = fscanf(fileID, '%f',1); 47 | width = fscanf(fileID, '%f',1); 48 | conv_start = -1*ones(width+1,height+1); 49 | em_data.ts = zeros(1,width*height+sum(cd_data.p==1)); 50 | em_data.x = zeros(size(em_data.ts)); 51 | em_data.y = zeros(size(em_data.ts)); 52 | em_data.gray = zeros(size(em_data.ts)); 53 | 54 | for j = 1:height 55 | for i = 1:width 56 | emvalid = fscanf(fileID, '%f',1); 57 | c = fscanf(fileID, '%f',1); 58 | lmesurement = fscanf(fileID, '%f',1); 59 | conv_start(i,j) = fscanf(fileID, '%f',1); 60 | ltd = fscanf(fileID, '%f',1); 61 | svalid = fscanf(fileID, '%f',1); 62 | 63 | if lmesurement > 0 64 | em_data.ts(output_index) = 0; 65 | em_data.x(output_index) = i-1; 66 | em_data.y(output_index) = j-1; 67 | em_data.gray(output_index) = lmesurement; 68 | output_index=output_index+1; 69 | end 70 | end 71 | fscanf(fileID, '%f',1); 72 | end 73 | else 74 | % no snap file 75 | conv_start = -1*ones(max(cd_data.x)+1,max(cd_data.y)+1); 76 | em_data.ts = zeros(1,sum(cd_data.p==1)); 77 | em_data.x = zeros(size(em_data.ts)); 78 | em_data.y = zeros(size(em_data.ts)); 79 | em_data.gray = zeros(size(em_data.ts)); 80 | end 81 | for i=1:length(cd_data.ts) 82 | if cd_data.p(i)==1 83 | if conv_start(cd_data.x(i)+1,cd_data.y(i)+1) > 0 84 | em_data.ts(output_index) = cd_data.ts(i); 85 | em_data.x(output_index) = cd_data.x(i); 86 | em_data.y(output_index) = cd_data.y(i); 87 | em_data.gray(output_index) = 1/(cd_data.ts(i)-conv_start(cd_data.x(i)+1,cd_data.y(i)+1)); 88 | output_index=output_index+1; 89 | conv_start(cd_data.x(i)+1,cd_data.y(i)+1) = -1; 90 | end 91 | else 92 | conv_start(cd_data.x(i)+1,cd_data.y(i)+1) = cd_data.ts(i); 93 | end 94 | end 95 | em_data.ts(output_index:end) = []; 96 | em_data.x(output_index:end) = []; 97 | em_data.y(output_index:end) = []; 98 | em_data.gray(output_index:end) = []; 99 | end 100 | 101 | -------------------------------------------------------------------------------- /load_ext_trigger_data.m: -------------------------------------------------------------------------------- 1 | function ext_trigger=load_ext_trigger_data(filename) 2 | % ext_trigger_data = load_ext_trigger_data(filename) 3 | % 4 | % Loads data from files generated by the StreamLogger consumer for EventExtTrigger 5 | % events [ts,x,y,p,id]. 6 | % timestamps are in uS 7 | % ext_trigger is a structure containing the fields ts, x, y, p and id 8 | 9 | f=fopen(filename); 10 | 11 | % Parse header if any 12 | header = []; 13 | endOfHeader = 0; 14 | numCommentLine = 0; 15 | while (endOfHeader==0) 16 | bod = ftell(f); 17 | tline = fgets(f,256); 18 | if(tline(1)~='%') 19 | endOfHeader = 1; 20 | else 21 | words = strsplit(tline); 22 | if (length(words) > 2 ) 23 | if (strcmp(words{2} , 'Date')) 24 | if (length(words) > 3) 25 | header = [header; {words{2}, horzcat(words{3}, ' ', words{4})}]; 26 | end 27 | else 28 | header = [header; {words{2}, words{3}}]; 29 | end 30 | end 31 | numCommentLine = numCommentLine+1; 32 | end 33 | end 34 | fseek(f,bod,'bof'); 35 | 36 | evType = 0; 37 | evSize = 8; 38 | if (numCommentLine>0) % Ensure compatibility with previous files. 39 | % Read event type 40 | evType = fread(f,1,'char'); 41 | % Read event size 42 | evSize = fread(f,1,'char'); 43 | end 44 | 45 | 46 | bof=ftell(f); 47 | 48 | fseek(f,0,'eof'); 49 | numEvents=floor((ftell(f)-bof)/evSize); 50 | 51 | % read data 52 | fseek(f,bof,'bof'); % start just after header 53 | allTs=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % ts are 4 bytes (uint32) skipping 4 bytes after each 54 | fseek(f,bof+4,'bof'); % timestamps start 4 after bof 55 | allAddr=uint32(fread(f,numEvents,'uint32',evSize-4,'l')); % addr are each 4 bytes (uint32) separated by 4 byte timestamps 56 | 57 | fclose(f); 58 | 59 | ext_trigger.ts = double(allTs); 60 | 61 | polmask = hex2dec('000000F'); 62 | idmask = hex2dec('FC00000'); 63 | 64 | polshift=0; % bits to shift p to right 65 | idshift=26; 66 | 67 | addr=abs(allAddr); % make sure non-negative or an error will result from bitand (glitches can somehow result in negative addresses...) 68 | ext_trigger.p=-1+2*double(bitshift(bitand(addr,polmask),-polshift)); % 1 for ON, -1 for OFF 69 | ext_trigger.id=double(bitshift(bitand(addr,idmask),-idshift)); % id addresses 70 | end 71 | -------------------------------------------------------------------------------- /play_cd_recording.m: -------------------------------------------------------------------------------- 1 | function mov = play_cd_recording(filename, width, height, framerate, acc_time) 2 | % Function to replay of file of CD events 3 | % 4 | % mov = play_cd_recording(filename, width, height, framerate, acc_time) 5 | % filename: path and name of the file to replay 6 | % width, height: resolution of the sensor, defaults to 304 and 240 7 | % framerate: framerate of the generated images in frames per second (defaults to 25) 8 | % acc_time: time to leave the events in the frames in microseconds (defaults to 10000 us) 9 | % 10 | % returns a movie which can be used by the 'movie' command 11 | 12 | if ~exist('width', 'var') 13 | width = 304; 14 | end 15 | if ~exist('height', 'var') 16 | height = 240; 17 | end 18 | if ~exist('framerate', 'var') 19 | framerate = 25; 20 | end 21 | if ~exist('acc_time', 'var') 22 | acc_time = 10000; 23 | end 24 | 25 | 26 | % Load data from the give file 27 | cd_data = load_cd_events(filename); 28 | % Make the pixel index start at 1 for compatibility for Matlab matrices 29 | % indexes (in the files pixels coordinates start at 0) 30 | cd_data.x = cd_data.x + 1; 31 | cd_data.y = cd_data.y + 1; 32 | 33 | % Compute internal parameters 34 | frame_time = floor(1/framerate * 1e6); % in microseconds 35 | 36 | % Initialize state storage 37 | cd_img = 0.5*ones(height, width); 38 | ts_img = zeros(height, width); 39 | last_frame_ts = 0; 40 | frame_idx = 1; 41 | 42 | % Build a figure for movie display and store the first empty frame 43 | figure(); 44 | image(cd_img); 45 | colormap gray; 46 | colormap_size = size(colormap, 1); 47 | mov(frame_idx) = getframe(gcf); 48 | frame_idx = frame_idx+1; 49 | 50 | % Go through all events in file 51 | for i=1:length(cd_data.ts) 52 | % Get timestamp of current event 53 | cur_ts = cd_data.ts(i); 54 | % Check if images should be generated 55 | while (cur_ts > last_frame_ts + frame_time) 56 | % Update frame by removing old events 57 | last_frame_ts = last_frame_ts + frame_time; 58 | cd_img(ts_img < last_frame_ts - acc_time) = 0.5; 59 | % scale and display image 60 | image(colormap_size*cd_img); 61 | mov(frame_idx) = getframe(); 62 | frame_idx = frame_idx+1; 63 | end 64 | % Add event to state 65 | if cd_data.p(i) == 1 66 | % Put a white dot for ON events 67 | cd_img(cd_data.y(i), cd_data.x(i)) = 1; 68 | else 69 | % Put a black dot for OFF events 70 | cd_img(cd_data.y(i), cd_data.x(i)) = 0; 71 | end 72 | ts_img(cd_data.y(i), cd_data.x(i)) = cur_ts; 73 | end --------------------------------------------------------------------------------