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 scipy.io.wavfile as wavfile
import numpy
import os.path
def snr(file):
if (os.path.isfile(file)):
data = wavfile.read(fileWav)[1]
singleChannel = data
try:
singleChannel = numpy.sum(data, axis=1)
except:
# was mono after all
pass
norm = singleChannel / (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))
return stats.signaltonoise(norm)
Hi Gary, Can you tell me what is the purpose of using the below code,
norm = singleChannel / (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))
Hi Gary,
Thanks for the code !
fileWav wasn’t defined prior to the 7th line. I think you ment : data = wavfile.read(file)[1]
Also, stats wasn’t imported (import scipy.stats as stats)
—
Waad
Hi Gary,
1) What is the difference between this stats.signaltonoise(data) vs the above code (i.e., the lines where you sum and divide)?
2) How to interpret the stats.signaltonoise(data) output?
Thx!
the tats.signaltonoise(data) can be used by importing scipy as stats. This function actually calculates the snr.
import scipy.io.wavfile as wavfile
import numpy as np
import os.path
def signaltonoise(a, axis=0, ddof=0):
a = np.asanyarray(a)
m = a.mean(axis)
sd = a.std(axis=axis, ddof=ddof)
return np.where(sd == 0, 0, m/sd)
def snr(file):
if (os.path.isfile(file)):
data = wavfile.read(file)[1]
singleChannel = data
try:
singleChannel = np.sum(data, axis=1)
except:
pass
norm = singleChannel / (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))
return signaltonoise(norm)