How to read 12bit raw data faster?
Show older comments
I made this program to read 12-bits raw data (see attachment), it worked well, but very slowly, can anyone help me find a new method to speed it up? Thanks very much. the raw data file's too big having 4 columns & 2,000,000 points in each column. Anyone have interest in it, please sent me a email, shaoj@mst.edu, I will sent the data to you. Thanks again.
function [data,lengthofData]=rawDataRead(filename,N,transFilename)
format long;
fid=fopen(filename,'r');
d1=fread(fid,[N,inf],'uint16');
len1=length(d1(1,:))
data_binary=dec2bin(d1);
data=zeros(len1,N);
for j=1:N
count_num=0;
for i=1:len1
%b_1=data_binary((i-1)*N+j,1:4); %the first bits of each data, no use.
b_2=data_binary((i-1)*N+j,5:5); %the fifth bit of each data.
if (b_2=='1')
b_2Dec=0;
else
b_2Dec=2.500611;
end
b_3=data_binary((i-1)*N+j,6:16); %the sixth to sixteenth bits of each data.
data(i,j)=bin2dec(b_3)*0.001221+b_2Dec;
if count_num==30000
count_num=0;
fprintf('Loading....,%g',((j-1)*len1+i)/len1/N*100);
fprintf('%%\n');
else
count_num=count_num+1;
end
end
end
fidout=fopen(transFilename,'w');
for i=1:len1
if N==1
fprintf(fidout, '%g\n',data(i,1));
elseif N==2
fprintf(fidout,'%g,%g\n',data(i,1),data(i,2));
elseif N==3
fprintf(fidout, '%g,%g,%g\n',data(i,1),data(i,2),data(i,3));
elseif N==4
fprintf(fidout, '%g,%g,%g,%g\n',data(i,1),data(i,2),data(i,3),data(i,4));
else
fprintf('Error, N must be one of 1,2,3,4.');
end
end
fclose(fidout);
lengthofData=length(data);
end
Answers (2)
Walter Roberson
on 29 Jan 2016
You can vectorize. It looks to me as if your code should be equivalent to the below:
fmt = [repmat('%g,', 1, N-1), '%g\n']
fid = fopen(filename,'r');
d1 = fread(fid,[N,inf],'uint16');
fclose(fid);
%The 2.500611 is if bit 12 is 0, not if it is 1
data = bitand(d1, 2^11-1) * 0.001221 + (1-bitget(d1, 12)) * 2.500611;
fidout = fopen(transFilename,'w');
fprintf(fidout, fmt, data);
fclose(fidout);
Guillaume
on 30 Jan 2016
Certainly, using dec2bin is going to be slow since it involves conversion to and from strings. You can manipulate bits directly as per Walter's answer which is going to be much faster.
I'm not clear on what your code is doing, it doesn't look like it's just reading 12-bit raw data but if reading 12-bit raw data is your goal, then it can be done directly at the fread level:
fread(fid, [N Inf], 'ubit12')
1 Comment
Walter Roberson
on 30 Jan 2016
This would be for 12 bit data that is packed together in consecutive bits, like
11111111 11112222 22222222
|12345678|9ABC1234|56789ABC|
where the upper row indicates which "word" it belongs to, and the lower row shows the byte boundaries and gives the data bit numbers with ABC representing 10, 11, 12. This would be two 12-bit words packed in every 3 bytes.
The code as posted by the user is for 12 bit data that is stored as the lower (least significant) 12 bits of 16 bits:
11111111 11111111 11112222 22222222
|xxxx1234|56789ABC|xxxx1234|56789ABC|
with the x indicating "don't care". This is two bytes for each "word", no byte being used for different words.
Categories
Find more on Data Import and Export in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!