A story packed with absolute values in numpy.ndarray

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()

ダウンロード (2).png

Workaround

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

A story packed with absolute values in numpy.ndarray
A story about competing with a friend in Othello AI Preparation
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
[Small story] How to save matplotlib graphs in a batch with Jupyter
Fill in missing values with Scikit-learn impute
A memo packed with RADEX environment construction
Function to extract the maximum and minimum values ​​in a slice with Go
A confusing story with two ways to implement XGBoost in Python + overall notes
[Python] Get the files in a folder with Python
Draw a graph with Japanese labels in Jupyter
[Python3] A story stuck with time zone conversion
Delete data in a pattern with Redis Cluster
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Delete rows with arbitrary values in pandas DataFrame
Build a Django environment with Vagrant in 5 minutes
A story stuck with handling Python binary data
Clone with a specific branch / tag in GitPython
A story about implementing a login screen with django
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
Sort dict in dict (dictionary in dictionary) with a specific key
Handle integer types with missing values in Pandas
Configure a module with multiple files in Django
The story that fits in with pip installation
How to convert / restore a string with [] in python
A story about making 3D space recognition with Python
A story about using Resona's software token with 1Password
A story about predicting exchange rates with Deep Learning
I want to transition with a button in flask
Convert a text file with hexadecimal values to a binary file
Playing with a user-local artificial intelligence API in Python
Make a simple Slackbot with interactive button in python
Try embedding Python in a C ++ program with pybind11
Drawing a tree structure with D3.js in Jupyter Notebook
A story about making Hanon-like sheet music with Python
A story about trying a (Golang +) Python monorepo with Bazel
I want to work with a robot in python.
A addictive point in "Bayesian inference experience with Python"
A story about reflecting Discord activity in Slack Status
Output a character string with line breaks in PyYAML
A story about how theano was moved with TSUBAME 2.0
A story linked with Google Cloud Storage with a little ingenuity
A story about a Linux beginner passing LPIC101 in a week
Display a histogram of image brightness values in python
I can't manipulate iframes in a page with Selenium
Run a Python file with relative import in PyCharm
Create a fake Minecraft server in Python with Quarry
Stop an instance with a specific tag in Boto3
A story that went missing when I specified a path starting with a tilde (~) in python open
[Note] A story about trying to override a class method with two underscores in Python 3 series.
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
[Django] A story about getting stuck in a swamp trying to validate a zip with form [TDD]