Good day. I need to calculate the SNR of the audio file. I found the information that in there is a finished function in the package: scipy.stats.signaltonoise. But did not find examples of use. How does this function actually work (calculate)?

I also found an example:

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) 

maybe someone already wrote something like that?

  • signaltonoise - was removed from scipy.stats - in modern versions of scipy it is not. Have you already looked here? Do you have a formula for which is SNR? - MaxU
  • @MaxU, I think, the standard formula to occur. Through the ratio of the amplitudes. - asai94
  • @ Maxu, or will not go? - asai94

1 answer 1

This feature has been removed from the SciPy.stats API, and since version 0.16 it is missing from SciPy.stats .

Here is the definition of this function (from the old version of scipy) :

 def signaltonoise(a, axis=0, ddof=0): """ The signal-to-noise ratio of the input data. Returns the signal-to-noise ratio of `a`, here defined as the mean divided by the standard deviation. Parameters ---------- a : array_like An array_like object containing the sample data. axis : int or None, optional If axis is equal to None, the array is first ravel'd. If axis is an integer, this is the axis over which to operate. Default is 0. ddof : int, optional Degrees of freedom correction for standard deviation. Default is 0. Returns ------- s2n : ndarray The mean to standard deviation ratio(s) along `axis`, or 0 where the standard deviation is 0. """ a = np.asanyarray(a) m = a.mean(axis) sd = a.std(axis=axis, ddof=ddof) return np.where(sd == 0, 0, m/sd) 
  • thank. Do you know if there is a replacement for this function? - asai94
  • @ asai94, you can take this as is (from the answer) and try ... - MaxU