[英]How to read an ArrayBuffer with binary data in “4 byte single”/floating point/IEEE 754 encoded data?
I need to loop over a binary file via an arrayBuffer
and retrieve sets of 1024 floating points. 我需要通过arrayBuffer
二进制文件并检索1024个浮点集。 I'm doing this: 我正在这样做:
// chunk_size = 1024
// chunk_len = 48
// response_buffer = ArrayBuffer
// response_buffer.byteLength = 49152
for (i = chunk_len; i > 0; i -= 1) {
switch (i) {
case (chunk_len):
// create view from last chunk of 1024 out of 49152 total
float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size));
// add_data(net_len, float_buffer);
break;
case 0:
break;
default:
float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size)), chunk_size);
//add_data(net_len, float_buffer);
break;
}
}
My problem is if I call this on the first run for the end of my buffer: 我的问题是,如果我在缓冲区的结尾第一次运行时调用了此方法:
// i - 1 = 47 * chunk_size
new Float32Array(response_buffer, ((i - 1) * chunk_size));
the same statement fails on the next run where I call: 同一条语句在我调用的下一次运行中失败:
new Float32Array(response_buffer, ((i - 1) * chunk_size), 1024);
Although I can read here , that I can do this: 尽管我可以在这里阅读,但是我可以做到这一点:
Float32Array Float32Array(
ArrayBuffer buffer,
optional unsigned long byteOffset,
optional unsigned long length
);
Question : 问题 :
Why is my loop failing after declaring the first Float32Array
view on my response_offer
ArrayBuffer? 在response_offer
ArrayBuffer上声明第一个Float32Array
视图后,为什么循环失败?
I think you have an extra ")" in the first line of your "default" case. 我认为您在“默认”案例的第一行中有一个额外的“)”。
float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size)), chunk_size);
Should be: 应该:
float_buffer = new Float32Array(response_buffer, ((i - 1) * chunk_size), chunk_size);
So. 所以。 Finally understand... maybe this helps the next person wondering: 终于明白了……也许这有助于下一个人想知道:
First off - I was all wrong in trying to read my data, which is 4 byte single format . 首先-在尝试读取我的数据( 4字节单一格式)时 ,我全错了。
arrayBuffer
with byteLength = 49182
this means there are that many entries in my array. 如果我有一个arrayBuffer
byteLength = 49182
这意味着我的数组中有很多条目。 4 byte single format
, I found out with some SO-help and searching that this is readable with getFloat32
AND that 4 entries comprise one "real" value 因为我的数组是4 byte single format
,我发现了一些SO-帮助和搜索 ,这是可读与getFloat32
和 4项包括一个“真实”的价值 So if I have 12000 data-points and 49182/4 = 12288 datapoints, I will have 288 empty data points at the end of my data structure. 因此,如果我有12000个数据点和49182/4 = 12288个数据点,则在数据结构的末尾将有288个空数据点。
So my binary data should be stored like this: 所以我的二进制数据应该这样存储:
0 - 1024 a 1025 - 2048 a 2049 - 3072 a 3072 - 4000 [a 4001 - 4096 b] 4097 - 5120 b 5121 - 6144 b 6145 - 7168 b 7169 - 8000 [b 8001 - 8192 c] 8193 - 9216 c 9217 - 10240 c 10240 - 11264 c 11264 - 12000 [c 12000 - 12288 0]
My final snippet will contain 288 empty results, because 3x4000 datapoints in 1024 chunks will return some empty results 我的最后一个代码段将包含288个空结果,因为1024个块中的3x4000数据点将返回一些空结果
To read, I found a nice snippet here (dynamic high range rendering) , which helped me to this: 阅读时,我在这里找到了一个不错的代码段(动态高范围渲染) ,这有助于我做到这一点:
// ... raw_data = ... data = new DataView(raw_data); ... tmp_data = new Float32Array(byte_len / Float32Array.BYTES_PER_ELEMENT); len = tmp_data.length; // Incoming data is raw floating point values with little-endian byte ordering. for (i = 0; i < len; i += 1) { tmp_data[i] = data.getFloat32(i * Float32Array.BYTES_PER_ELEMENT, true); }
Now I have a single array with which I can work and build my processing structure. 现在,我有了一个数组,可以使用它来构建我的处理结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.