How do I find amplitude of wav file in python?

3.5k Views Asked by At

I am working with wav files analysis using the librosa library in python. I used librosa.load() to load the audio file. Apparently this function loads the wav file into a numpy array with normalised amplitude values in the range -1 to 1. But I need to get the actual amplitude values for processing. How can I find that?

Thanks in advance!

2

There are 2 best solutions below

2
On BEST ANSWER

You can't. As Hendrik mentioned, the signal is digital and the amplitude in the WAV file won't tell you anything about the actual sound wave amplitude / sound power. That's completely lost the moment it was digitalised to WAV.

That being said, you can compute e.g. loudness, a relative perception of the sound power. If you are dealing with human auditory system, one of the recommended approaches is to:

  1. Use to the Bark scale (Bark scale better reflects how we hear).
  2. Compute energy in each bin.
  3. (Optional) Normalise by the overall sum.

If you don't want to compute it yourself, check out e.g. YAAFE.

2
On

You observed correctly that librosa always normalizes the samples to mono [-1:1] (and also 22050 Hz). That said, it's digital audio, so could multiply with whatever you want to get a different scale. If you insist, that your samples are on a scale of -2^15 to 2^15, simply multiply with 2^15. It pretty much means the same.

You won't gain anything, except dragging a peculiarity of the encoding audio format into your data.

That said, if that's what you want, you could use PySoundFile like this:

import soundfile as sf

y, sr = sf.read('existing_file.wav', dtype='int16')

The parameter dtype='int16' tells the library to assume a signed 16bit format per sample.