Recently, I've been doing a little operation on audio files.
With reference to this page, I read it as a 16-bit integer `` `numpy.ndarray``` type.
Display sound waveform in Python (Wav file)
import numpy as np
import wave
def read_wavefile(filename):
"""wav format file numpy.Read as ndarray"""
wf = wave.open(filename , 'r')
buf = wf.readframes(wf.getnframes())
#Convert binary data to 16-bit integer
return np.frombuffer(buf, dtype='int16')
I wanted to take the absolute value of this voice, so I will convert the array with `` `numpy.absolute```.
arr = read_wavefile('test.wav')
arr_abs = np.absolute(arr)
However, the value `-32768``` in this is not converted to a positive value and remains. If you take the histogram, you can see that the other negative values are positive, but only
`-32768``` remains (although the values are fine).
import matplotlib.pyplot as plt
print(type(arr_abs))
# => <class 'numpy.ndarray'>
print(arr_abs.min())
# => -32768
plt.hist(np.absolute(arr), bins=100)
plt.show()
This was resolved by reading as a 32-bit integer on the first read.
def read_wavefile(filename):
"""wav format file numpy.Read as ndarray"""
wf = wave.open(filename , 'r')
buf = wf.readframes(wf.getnframes())
#Convert binary data to 32-bit integer
arr = np.frombuffer(buf, dtype='int16')
return arr.astype(np.int32)
arr = read_wavefile('test.wav')
arr_abs = np.absolute(arr)
print(arr_abs.min())
# => 0
This is probably because 16bit integers can only represent from -32768 to 32767, so `numpy.absolue
I think that the value whose sign was converted with `cannot be converted and the value as it is is output.
(I couldn't find any documentation detailing these specifications)
When using a program for numerical calculation such as Python's numpy or R language, it is often recognized that the data type of C language is used as it is for speeding up, so it is necessary to be more careful than usual. Did.
By the way, the version of `` `numpy``` used this time is 1.13.0.
$ pip freeze | grep numpy
numpy==1.13.0
Recommended Posts