Converting double values file to 2 byte per sample.
12 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a .txt file with 128 x 11900 vlaues. These values are doubles ranging from -0.00023462 to +1.0719. I need to convert them to 2 bytes per sample and store in a .bin file. I am not getting how to do it. I tried using the 'fread' function of matlab but all I am able to do is convert each sample to 8 bytes(double precision). Is there a way I can convert each sample into 2 bytes each?
0 件のコメント
採用された回答
Walter Roberson
2020 年 7 月 16 日
two_bytes = uint16(floor(YourMatrix/1.636E-5)+15);
and fwrite() as uint16 .
reconstructed = (double(two_bytes) - 15) * 1.636E-5
the reconstruction error can be up to 1.636E-5 . You can reduce the average reconstruction error by using round() instead of floor(), but then you have to work on the boundary condition (the +15 or -15) (though there happens to be just enough slack room in the representation so that it is not a problem.)
その他の回答 (1 件)
James Tursa
2020 年 7 月 16 日
If you want a two byte floating point representation you can use half precision. E.g.,
This is one of the standard floating point formats, but will result in less precision than Walter's method because some of the bits are used for sign and exponent.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!