Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading .bin files #59

Open
ransona opened this issue Dec 10, 2024 · 1 comment
Open

reading .bin files #59

ransona opened this issue Dec 10, 2024 · 1 comment

Comments

@ransona
Copy link
Collaborator

ransona commented Dec 10, 2024

this can be done with this file:
https://github.com/neurogears/ranson-rig/blob/master/src/matlab/harpread.m

@ransona
Copy link
Collaborator Author

ransona commented Dec 10, 2024

this may be an updated version:
function data = harpread(filePath)
fid = fopen(filePath,'r');
data = fread(fid,'*uint8'); % read data as raw binary
stride = data(2) + 2; % size of each message
count = length(data) / int32(stride); % number of messages in file
payloadsize = stride - 12; % size of each message payload
payloadtype = bitand(data(5), bitcmp(uint8(0x10))); % the type of payload data
elementsize = bitand(payloadtype, uint8(0x3)); % the size in bytes of each element
payloadshape = [count, payloadsize / elementsize]; % the dimensions of the data matrix
messages = reshape(data, stride, count);
% structure all message data
seconds = typecast(reshape(messages(6:9, :),[],1), 'uint32'); % the seconds part of the timestamp
ticks = typecast(reshape(messages(10:11, :),[],1), 'uint16'); % the 32-microsecond ticks part of each timestamp
seconds = double(ticks) * 32e-6 + double(seconds); % the message timestamp

payload = messages(12:12 + payloadsize - 1, :);     % extract the payload data
switch payloadtype                                  % get the payload data type
case 1
    dtype = 'uint8';
case 2
    dtype = 'uint16';
case 4
    dtype = 'uint32';
case 8
    dtype = 'uint64';
case 129
    dtype = 'int8';
case 130
    dtype = 'int16';
case 132
    dtype = 'int32';
case 136
    dtype = 'int64';
case 68
    dtype = 'float32';
end

payload = typecast(payload, dtype);                 % convert payload data type
payload = reshape(payload, elementsize, count)';    % reshape into final data matrix
data = [seconds double(payload)];                   % convert data to double and return

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant