def __init__( self, model, fs, outputName, globalLoudnessFeature=None, loudnessLevelFunction=None, loudnessLevel='abs' ): self.iterator = DynamicLoudnessIterator( model, fs, outputName, globalLoudnessFeature, loudnessLevelFunction ) self.targetLoudnessLevel = loudnessLevel self.tol = 0.02 self.nIters = 20 self.predictions = None self.converged = False self.fs = fs self.duration = 1 self.alpha = 0.5 if ((type(loudnessLevel) is str) or (loudnessLevel == 2.4)): self.freqs = freqsISO389 self.sPLs = thresholdsISO389 self.targetLoudnessLevel = 2.4 else: contour = ISO2262003LoudnessContours() self.freqs, self.sPLs = contour.phonToSPL(None, loudnessLevel) self.predictions = np.zeros(self.sPLs.size)
def __init__(self, model, fs, outputName, globalLoudnessFeature = None, loudnessLevelFunction = None, loudnessLevel = 'abs'): self.iterator = DynamicLoudnessIterator(model, fs, outputName, globalLoudnessFeature, loudnessLevelFunction) self.targetLoudnessLevel = loudnessLevel self.tol = 0.02 self.nIters = 20 self.predictions = None self.converged = False self.fs = fs self.duration = 1 self.alpha = 0.5 if ((type(loudnessLevel) is str) or (loudnessLevel == 2.4)): self.freqs = freqsISO389 self.sPLs = thresholdsISO389 self.targetLoudnessLevel = 2.4 else: contour = ISO2262003LoudnessContours() self.freqs, self.sPLs = contour.phonToSPL(None, loudnessLevel) self.predictions = np.zeros(self.sPLs.size)
class DynamicLoudnessContourPredictor(): def __init__(self, model, fs, outputName, globalLoudnessFeature = None, loudnessLevelFunction = None, loudnessLevel = 'abs'): self.iterator = DynamicLoudnessIterator(model, fs, outputName, globalLoudnessFeature, loudnessLevelFunction) self.targetLoudnessLevel = loudnessLevel self.tol = 0.02 self.nIters = 20 self.predictions = None self.converged = False self.fs = fs self.duration = 1 self.alpha = 0.5 if ((type(loudnessLevel) is str) or (loudnessLevel == 2.4)): self.freqs = freqsISO389 self.sPLs = thresholdsISO389 self.targetLoudnessLevel = 2.4 else: contour = ISO2262003LoudnessContours() self.freqs, self.sPLs = contour.phonToSPL(None, loudnessLevel) self.predictions = np.zeros(self.sPLs.size) def process(self): self.predictions = np.zeros(self.freqs.size) self.converged = np.zeros(self.freqs.size, dtype = bool) for i, freq in enumerate(self.freqs): print 'Freq: %0.2f, initial guess: %0.2f' % (freq, self.sPLs[i]) s = Sound.tone([freq], dur = self.duration, fs = self.fs) s.applyRamp(0.1) s.useDBSPL() s.normalise(self.sPLs[i], 'RMS') self.predictions[i] = self.sPLs[i] self.predictions[i] += self.iterator.process(s.data, self.targetLoudnessLevel, self.tol, self.nIters, self.alpha) self.converged[i] = self.iterator.converged def plotPredictions(self): plt.semilogx(self.freqs, self.sPLs, label = 'ISO') plt.semilogx(self.freqs, self.predictions, color = 'r', linestyle = '--', label = 'Predicted') plt.legend() plt.show() def computeErrors(self): ''' Returns a tuple of error vector (target - prediction), RMSE and maximum absolute error. ''' error = self.sPLs - self.predictions rMSE = np.sqrt(np.mean(error * error)) maxE = np.max(np.abs(error)) return (error, rMSE, maxE) def getResults(self): outputDic = {} outputDic['Errors'], outputDic['RMSE'], outputDic['MXAE'] = self.computeErrors() outputDic['Frequencies'] = self.freqs outputDic['Targets'] = self.sPLs outputDic['Predictions'] = self.predictions outputDic['Converged'] = self.converged return outputDic
class DynamicLoudnessContourPredictor(): def __init__( self, model, fs, outputName, globalLoudnessFeature=None, loudnessLevelFunction=None, loudnessLevel='abs' ): self.iterator = DynamicLoudnessIterator( model, fs, outputName, globalLoudnessFeature, loudnessLevelFunction ) self.targetLoudnessLevel = loudnessLevel self.tol = 0.02 self.nIters = 20 self.predictions = None self.converged = False self.fs = fs self.duration = 1 self.alpha = 0.5 if ((type(loudnessLevel) is str) or (loudnessLevel == 2.4)): self.freqs = freqsISO389 self.sPLs = thresholdsISO389 self.targetLoudnessLevel = 2.4 else: contour = ISO2262003LoudnessContours() self.freqs, self.sPLs = contour.phonToSPL(None, loudnessLevel) self.predictions = np.zeros(self.sPLs.size) def process(self): self.predictions = np.zeros(self.freqs.size) self.converged = np.zeros( self.freqs.size, dtype=bool ) for i, freq in enumerate(self.freqs): print 'Freq: %0.2f, initial guess: %0.2f' % (freq, self.sPLs[i]) s = Sound.tone([freq], dur=self.duration, fs=self.fs) s.applyRamp(0.1) s.useDBSPL() s.normalise(self.sPLs[i], 'RMS') self.predictions[i] = self.sPLs[i] self.predictions[i] += self.iterator.process( s.data, self.targetLoudnessLevel, self.tol, self.nIters, self.alpha ) self.converged[i] = self.iterator.converged def plotPredictions(self): plt.semilogx( self.freqs, self.sPLs, label='ISO' ) plt.semilogx( self.freqs, self.predictions, color='r', linestyle='--', label='Predicted' ) plt.legend() plt.show() def computeErrors(self): ''' Returns a tuple of error vector (target - prediction), RMSE and maximum absolute error. ''' error = self.sPLs - self.predictions rMSE = np.sqrt(np.mean(error * error)) maxE = np.max(np.abs(error)) return (error, rMSE, maxE) def getResults(self): outputDic = {} outputDic['Errors'], outputDic['RMSE'], outputDic['MXAE'] = \ self.computeErrors() outputDic['Frequencies'] = self.freqs outputDic['Targets'] = self.sPLs outputDic['Predictions'] = self.predictions outputDic['Converged'] = self.converged return outputDic