How to create a Numpy dtype that includes 24 bit integers?

I have a binary that has a recording structure of 400 24-bit signed large endian numbers, followed by a 16-bit large end, signed short. I want to do the following:

from numpy import dtype , fromfile
record_dtype = dtype([('samples','>i3',(400,)),('marker','>i2')])
data = fromfile('binary_file.dat',dtype=record_dtype)

Unfortunately, I get:

TypeError: data type not understood

In response to '> i3'. How to define dtype to read in 24-bit binary numbers?

+3
source share
1 answer

I had about a terabyte file that was in a 24-bit four-channel PCM.

Of course, I did not want to touch on any other part than what I wanted, and so I did this:

import numpy as np
from numpy.lib.stride_tricks import as_strided

rawdatamap = np.memmap('4ch24bit800GBdatafile.pcm', dtype=np.dtype('u1'),mode='r')

# in case of a truncated frame at the end
usablebytes = rawdatamap.shape[0]-rawdatamap.shape[0]%12

frames = int(usablebytes/12)
rawbytes = rawdatamap[:usablebytes]

realdata = as_strided(rawbytes.view(np.int32), strides=(12,3,), shape=(frames,4))

someusefulpart = realdata[hugeoffset:hugeoffset+smallerthanram]&0x00ffffff

This made a copy from the file smallerthanrambytes of memory.

bytemask! , 32- - , .

, :

scaled_ch2_datum_at_framenum = scalefactor*(realdata[framenum,1]&0x00ffffff)-shiftoffset

, , .

, , 64- .

NB. . endian, dtype ...&0x00ffffff ...&ffffff00>>8

+2

All Articles