pcm2float

 I would like to share different implementations of Pulse-code modulation (PCM) to float conversion. Usually we receive PCM in int16 type, but some models expect normalized float64. The logic for conversion is not comlex and even so I have managed to find so interesting options.

The basic one logic looks like this:


 def pcm2float(sig, dtype=np.float64):s
    sig = np.asarray(sig)  # make sure it's a NumPy array
    assert sig.dtype.kind == 'i', "'sig' must be an array of signed integers!"
    dtype = np.dtype(dtype)  # allow string input (e.g. 'f')

    # Note that 'min' has a greater (by 1) absolute value than 'max'!
    # Therefore, we use 'min' here to avoid clipping.
    return sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)

The code is taken from StackOverflow answer on a question SciPy wavfile: music in, garbage out?

What it does?

I think the most important is in the line. Let's split it on the logical parts:

- Convert NumPy array to desired dtype:
sig.astype(dtype)  # Converts numpy array to desired dtype
- Get machine limits for integer type
np.iinfo(sig.dtype)  # Returns object which looks like iinfo(min=-32768, max=32767, dtype=int16)
- Get largest value from limits object. Usually largest by module is minimal value:
-np.iinfo(sig.dtype).min  # -(-32768)
And that's pretty it. Because we used type to type conversion in line #4 we can convert minimal limit to required conversion type.

There is also more interesting variation of the normalization. I have found it in the python-audio repository Code example below:

    i = np.iinfo(dtype)
    abs_max = 2 ** (i.bits - 1)	# i.bits returns actual dtype number, e.g. np.int16 --> 16
    offset = i.min + abs_max
    return (sig * abs_max + offset).clip(i.min, i.max).astype(dtype)
Similar variation I've found in audio-format.py repository:

    i = np.iinfo(sig.dtype)
    abs_max = 2 ** (i.bits - 1)  # i.bits returns actual dtype number, e.g. np.int16 --> 16
    offset = i.min + abs_max
    return (sig.astype(dtype) - offset) / abs_max
Good examples can be found also in the next Jupyter notebook Reading and Writing Audio Files with wave.

Comments

Popular posts from this blog

Install Kubeflow locally

RabbitMQ and OpenShift