Conversion of float to int16

35 visualizaciones (últimos 30 días)
Geerthy Thambiraj
Geerthy Thambiraj el 12 de Mzo. de 2023
Comentada: Geerthy Thambiraj el 12 de Mzo. de 2023
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 comentarios
Jan
Jan el 12 de Mzo. de 2023
What is the corresponding code in "the manually created toolbox"? Profer to post the code instead of paraphrasing its intention.
Geerthy Thambiraj
Geerthy Thambiraj el 12 de Mzo. de 2023
Thank you. I apologize.
Input to the code is in .bin format and this is how the .bin file is created once its been converted to the int16 format.
Once the rawdata.bin file is created, it is read by the part of the toolbox (the second part of the code).
% Data_float into int16 and .bin file created
ecg_int=data_float*32767; % multiplying by 2^16-1 but i didnt get the desire output
val=cast(ecg_int,'int16');
fid_write = fopen('rawdata.bin','w');
fwrite(fid_write,val','int16');
fclose(fid_write);
% Read by the part of the code in the toolbox
samplestepsize = 600000;
overlapstepsize = 2500;
numfullreads = 0;
fid = fopen(rawdata.bin,'r');
fileoffset = 0;
fseek(fid,fileoffset,'bof');
if readnum == 0
data = fread(fid, [1,samplestepsize], 'int16'); % Read data
else
data = fread(fid, [1,samplestepsize+overlapstepsize], 'int16');
end;

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 12 de Mzo. de 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
coeffs = 2×1
896.9373 645.8154
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.
  1 comentario
Geerthy Thambiraj
Geerthy Thambiraj el 12 de Mzo. de 2023
Thank you very much for the detailed analysis. This confirms the direct casting to int16 from float will not yield the desired results. Let me recheck the process. Thank you.

Iniciar sesión para comentar.


Jan
Jan el 12 de Mzo. de 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.
  1 comentario
Geerthy Thambiraj
Geerthy Thambiraj el 12 de Mzo. de 2023
Thank you for your valuable suggestions. The filter is applied once the data is read by the toolbox for further processing.

Iniciar sesión para comentar.

Categorías

Más información sobre Single-Rate Filters en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by