Compute the Signal To Noise ratio in audio files in Python

Computing the “signal to noise” ratio of an audio file is pretty simple if it’s already a wav file – if not, I suggest you convert it to one first.

If you’re doing a lot of these, this can take up a lot of disk space – I’m doing audio lectures, which are on average 30mb mp3s. I’ve found it helpful to think about trying to write scripts that you can ctrl-c and re-run.

One minor note here is that audio files are typically one or two channels (left-right), so you can potentially have two values for signal-to-noise. You should be able to add these together without issue, if you wish to get one value.

This does [1] on the wavfile data, as [0] has the sample rate.

import as wavfile
import numpy
import os.path

def snr(file):
  if (os.path.isfile(file)):
    data =[1]
    singleChannel = data
      singleChannel = numpy.sum(data, axis=1)
      # was mono after all
    norm = singleChannel / (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))
    return stats.signaltonoise(norm)