Using FP16 data in MATLAB

조회 수: 46 (최근 30일)
Tony
Tony 2020년 8월 6일
편집: James Tursa 2020년 8월 7일
I have an embedded application that generates half precision (fp16) output that I would like to import and use in MATLAB. Would someone please advise me the easiest way to do that?
  댓글 수: 2
Tony
Tony 2020년 8월 6일
I should clarify, I'm storing the out put in binary file format, and reading them into MATLAB using fread (which doesn't support "half" as a precision. I can't seem to cast the binary data into a floating point number.
James Tursa
James Tursa 2020년 8월 7일
What version of MATLAB are you using?

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2020년 8월 7일
편집: James Tursa 2020년 8월 7일
If you have R2018b or later, you can fread as uint16 and then typecast into half type. E.g.,
% Generate some sample data
>> d = [-inf -pi 0 pi inf nan]
d =
-Inf -3.1416 0 3.1416 Inf NaN
>> h = half(d)
h =
1×6 half row vector
-Inf -3.1406 0 3.1406 Inf NaN
>> u = storedInteger(h)
u =
1×6 uint16 row vector
64512 49736 0 16968 31744 65024
% Convert the uint16 values to half precision
>> H = half.typecast(u)
H =
1×6 half row vector
-Inf -3.1406 0 3.1406 Inf NaN
If you don't have R2018b or later, then the half type is not availalbe and you will be stuck with converting the values to single or double precision if you want to work with them in MATLAB. In that case, use fread as uint16 and then use the following FEX submission to turn the values into single or double:
E.g.,
>> S = halfprecision(u,'single')
S =
1×6 single row vector
-Inf -3.1406 0 3.1406 Inf NaN
  댓글 수: 1
Tony
Tony 2020년 8월 7일
That works well, thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by