Matlab, fread, speed up reading file with multiple data types and multiple sample rates

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

Using Matlab R2015a. I'm using fread to read sensor data from a binary file. The data contains multiple precisions. One signal is sampled at 5 times the rate of the other signals. The file is structured as a sequence of batches, where each batch has e.g. 1 sample of signal_A, but 5 samples of signal_B.

I have googled for examples on how to load and format the data fast, but the solutions I have seen only represent cases where there is a single sampling rate, making the solution simpler, as far as I can tell.

What I would like to avoid is the use of for-loops, which is quite slow. See below for an illustration of how the data is arranged in the file, and a simple code example of what I have now.

Any suggestions on how to speed this up?

clear; fid = fopen('binaryFile.bin','r'); signal_B = [];  numBatches = Inf; % read all batchSize = 17; % each batch takes up 17 bytes  % PART 1 (everything that is not signal_B) batchSerialNumber = fread(fid, numBatches, '*uint32', batchSize-4); fseek(fid, 4, 'bof'); % rewind signal_A = fread(fid, numBatches, '*uint16', batchSize-2); fseek(fid, 6, 'bof'); % rewind misc_info = fread(fid, numBatches, '*uint8', batchSize-1);  % PART 2 (signal_B) for i = 1:length(batchSerialNumber)     fseek(fid, ((i-1)*batchSize) + 7, 'bof'); % set position in file, according to batch number (i)     signal_B = [signal_B; fread(fid, 5, '*int16')]; % read the 5 samples of signal_B in this batch end 

回答1:

More googling and a nice solution eventually appeared... approximately 100 times faster than using for-loop.

clear; fid = fopen('binaryFile.bin','r');  numBatches = Inf; % read all batchSize = 17; % each batch takes up 17 bytes  % PART 1 (everything that is not signal_B) batchSerialNumber = fread(fid, numBatches, '*uint32', batchSize-4); fseek(fid, 4, 'bof'); % rewind signal_A = fread(fid, numBatches, '*uint16', batchSize-2); fseek(fid, 6, 'bof'); % rewind misc_info = fread(fid, numBatches, '*uint8', batchSize-1); fseek(fid, 7, 'bof'); % rewind  % PART 2 (signal_B) signal_B_line_1 = fread(fid, numBatches, '*int16', batchSize-2); fseek(fid, 9, 'bof'); signal_B_line_2 = fread(fid, numBatches, '*int16', batchSize-2); fseek(fid, 11, 'bof'); signal_B_line_3 = fread(fid, numBatches, '*int16', batchSize-2); fseek(fid, 13, 'bof'); signal_B_line_4 = fread(fid, numBatches, '*int16', batchSize-2); fseek(fid, 15, 'bof'); signal_B_line_5 = fread(fid, numBatches, '*int16', batchSize-2);  signal_B(length(batchSerialNumber)*5,1) = int16(0);  signal_B(1:5:end,1) = signal_B_line_1; signal_B(2:5:end,1) = signal_B_line_2; signal_B(3:5:end,1) = signal_B_line_3; signal_B(4:5:end,1) = signal_B_line_4; signal_B(5:5:end,1) = signal_B_line_5; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!