Conversion of float to int16
34 views (last 30 days)
Show older comments
Hi everyone
I have ECG data in float, and I would like to convert it into int16 to be used by the manually created toolbox. To convert float into int16, I multiplied data with 2^15 and cast it. However, the output of the toolbox is not the desired output. So I tried to analyze the previous ECG data and how it was converted to int16 format(2 bytes), yet I did not understand how data_float is converted into data_int16(both mat files are attached). Any suggestions would be highly appreciated.
Thank you.
2 Comments
Jan
on 12 Mar 2023
What is the corresponding code in "the manually created toolbox"? Profer to post the code instead of paraphrasing its intention.
Answers (2)
Walter Roberson
on 12 Mar 2023
s1 = load('data_float.mat');
data_float = s1.data_float;
s2 = load('data_int16.mat');
data_int16 = s2.data_int16;
A = [data_float(:), ones(numel(data_float),1)];
b = double(data_int16(:));
coeffs = A\b
projected = coeffs(1) .* data_float(:) + coeffs(2);
difference = projected - b;
subplot(4,1,1); plot(data_float); title('data\_float');
subplot(4,1,2); plot(data_int16); title('data\_int16');
subplot(4,1,3); plot(data_int16); hold on; plot(projected); hold off; legend({'int16', 'projected'});
subplot(4,1,4); plot(difference); title('difference projected minus actual')
If there were a linear relationship between the float and int16 then it would be captured perfectly (to within round-off) by the projected data. But you can see that the difference between projected and actual starts low and ends high. This tells us that some kind of trend was removed.
Jan
on 12 Mar 2023
The signals are not related by a linear transformation:
The signals look similar, but the int16 version has a downwards trend at the end and the peak heights are not equivalent. Maybe a filter was applies also?
Instead of guessing the applied transformation, it would be reliable to look in the original code or to ask the author.
See Also
Categories
Find more on Single-Rate Filters in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!