浮点到16位二进制补码二进制,Python [英] Floating point to 16 bit Twos Complement Binary, Python
问题描述
我正在处理包含-1和1之间浮点的CSV文件。所有这些浮点都必须转换为16位2s补码, 0B。从那里开始,我将把这个数字转换成二进制补码的字符串表示形式,所有来自CSV的数据将被写入一个.dat文件,而且两者之间没有空格。因此,例如,如果我读取CSV文件并且它有两个条目[0.006534,-.1232],我将把每个条目转换为它们各自的2s补码并将它们一个接一个地写入一个.dat文件。
问题是我在代码中卡住了如何将浮点数转换为16位二进制补码。我一直在寻找其他职位,如这,我被告知使用.float()函数,但我没有运气。
有人可以帮我写一个脚本来取一个浮点数,并返回它的16位二进制补码字符串吗?它必须是16位,因为我正在处理MIT 16标准。
我正在使用python 3.4 btw解决方案
为了回答这个问题在标题中:将Python float
转换为 IEEE 754半精度二进制浮点格式,您可以使用 binary16
:
>>> from binary16 import binary16
>>> binary16(0.006534)
b'\xb0\x1e'
>>> binary16( - 。1232)
b'\xe2\xaf'
numpy
会产生类似的结果:
>>> import numpy as np
>>> np.array([0.006534,-.1232],np.float16).tostring()
b'\xb1\x1e\xe3\xaf'
>>> np.array([0.006534,-.1232],'> f2')。tostring()#big-endian
b'\x1e\xb1\xaf\xe3'
我的目标是将振幅保存为ecg mit信号格式16
..snip ..
输入是一个包含fp的.CSV文件.WAV文件(这是心电图的记录)的振幅值。
您可以直接读取wav文件,写入相应的16位二进制补码幅度,以小端字节顺序从最高有效位('< h'
结构格式):
$ b
#!/ usr / bin / env python3
导入波
将wave.open('ecg.wav')作为wavfile打开('ecg.mit16','wb')作为output_file:
断言wavfile.getnchannels()== 1#mono
assert wavfile.getsampwidth()== 2#16bit
output_file.writelines(iter(lambda:wavfile.readframes(4096),b''))
Python 3中有一个错误 .readframes()
有时会返回 str
,而不是 bytes
。要解决这个问题,可以使用,如果不是数据
,那么对于空 str
和字节
:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $#$ / $ / $ / $ /
with wave.open('ecg.wav')as wavfile,open('ecg.mit16','wb')as output_file:
assert wavfile.getnchannels()== 1#mono
assert wavfile.getsampwidth()== 2#16bit
True:
data = wavfile.readframes(4096)
如果不是数据:
break
output_file .write(data)
so I think questions like this have been asked before but I'm having quite a bit of trouble getting this implemented.
I'm dealing with CSV files that contain floating points between -1 and 1. All of these floating points have to be converted to 16 bit 2s complement without the leading '0b'. From there, I will convert that number to a string representation of the 2s complement, and all of those from the CSV will be written will be written to a .dat file with no space in between. So for example, if I read in the CSV file and it has two entries [0.006534, -.1232], I will convert each entry to their respective 2s complement and write them one after another onto a .dat file.
The problem is I'm getting stuck in my code on how to convert the floating point to a 16 bit 2s complement. I've been looking at other posts like this and I've been told to use the .float() function but I've had no luck.
Can someone help me write a script that will take in a floating point number, and return the 16 bit 2s complement string of it? It has to be exactly 16 bits because I'm dealing with the MIT 16 standard.
I am using python 3.4 btw
To answer the question in the title: to convert a Python float
to IEEE 754 half-precision binary floating-point format, you could use binary16
:
>>> from binary16 import binary16
>>> binary16(0.006534)
b'\xb0\x1e'
>>> binary16(-.1232)
b'\xe2\xaf'
numpy
produces similar results:
>>> import numpy as np
>>> np.array([0.006534, -.1232], np.float16).tostring()
b'\xb1\x1e\xe3\xaf'
>>> np.array([0.006534, -.1232], '>f2').tostring() # big-endian
b'\x1e\xb1\xaf\xe3'
My goal was to save the amplitudes as the ecg mit signal format 16
..snip..
the input is a .CSV file containing the f.p. values of the amplitude from a .WAV file (which is the recording of an ECG).
You could read the wav file directly and write the corresponding 16-bit two's complement amplitudes in little-endian byte order where any unused high-order bits are sign-extended from the most significant bit ('<h'
struct format):
#!/usr/bin/env python3
import wave
with wave.open('ecg.wav') as wavfile, open('ecg.mit16', 'wb') as output_file:
assert wavfile.getnchannels() == 1 # mono
assert wavfile.getsampwidth() == 2 # 16bit
output_file.writelines(iter(lambda: wavfile.readframes(4096), b''))
There is a bug in Python 3 that .readframes()
returns str
instead of bytes
sometimes. To workaround it, use if not data
test that works on both empty str
and bytes
:
#!/usr/bin/env python3
import wave
with wave.open('ecg.wav') as wavfile, open('ecg.mit16', 'wb') as output_file:
assert wavfile.getnchannels() == 1 # mono
assert wavfile.getsampwidth() == 2 # 16bit
while True:
data = wavfile.readframes(4096)
if not data:
break
output_file.write(data)
这篇关于浮点到16位二进制补码二进制,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!