I found that reading data, which are packed in 24 bit integer format, using Matlab 'fread' with 'int24' option takes a lot of time. I found that, if I read data in 'int32' or 'int16' or 'int8', the reading time is extremely faster as compared to 'int24'. Is there a better way to reduce the reduce the time for reading 24 bit integer data?
To get a feel of the problem, a sample code is given below.
clear all; close all; clc;
% generate some data and write it as a binary file
n=10000000;
x=randn(n,1);
fp=fopen('file1.bin', 'w');
fwrite(fp, x);
fclose(fp);
% read data in 24-bit format and measure the time
% please note that the data we get here will be different from 'x'.
% The sole purpose of the code is to demonstrate the delay in reading
% 'int24'
tic;
fp=fopen('file1.bin', 'r');
y1=fread(fp, n, 'int24');
fclose(fp);
toc;
% read data in 32-bit format and measure the time
% please note that the data we get here will be different from 'x'.
% The sole purpose of the code is to demonstrate the delay in reading
% 'int24'
tic;
fp=fopen('file1.bin', 'r');
y2=fread(fp, n, 'int32');
fclose(fp);
toc;
The output reads: Elapsed time is 1.066489 seconds. Elapsed time is 0.047944 seconds.
Though the 'int32' version reads more data (32*n bits), it is 25 times faster than 'int24' reading.