Ejemplo n.º 1
0
class DynamicLoudnessIterator():
    
    def __init__(self, 
            model, 
            fs,
            output = None,
            globalLoudnessFeature = None,
            loudnessLevelFunction = None,
            nInputEars = 1):

        self.extractor = DynamicLoudnessExtractor(model, fs, nInputEars, output)
        self.outputName = model.getOutputModulesToAggregate()[0]
        self.converged = False

        if globalLoudnessFeature is None:
            self.globalLoudnessFeature = np.mean
        else:
            self.globalLoudnessFeature = globalLoudnessFeature
        
        if loudnessLevelFunction is None:
            self.loudnessLevelFunction = asIs
        else:
            self.loudnessLevelFunction = loudnessLevelFunction

    def extractLoudness(self, signal, gainInDecibels = 0):
        signalIn = signal.copy() * 10 ** (gainInDecibels / 20.0)
        self.extractor.process(signalIn)
        timeSeries = self.extractor.outputDict[self.outputName]
        loudness = self.globalLoudnessFeature(timeSeries)
        loudnessLevel = self.loudnessLevelFunction(loudness)
        return loudnessLevel

    def process(self, inputSignal, targetLoudness, tol = 0.1, nIters = 5, alpha = 1.0):

        # First check if the target loudness is a signal
        if type(targetLoudness) is np.ndarray:
            targetLoudness = self.extractLoudness(targetLoudness)

        storedGain = 0

        self.converged = False
        for i in range(nIters):

            loudnessLevel = self.extractLoudness(inputSignal, storedGain)

            error = targetLoudness - loudnessLevel

            print (('Gain: %0.3f, Loudness Level: %0.3f, ' +
                    'Error: %0.3f') % (storedGain, loudnessLevel, error))

            if np.abs(error) < tol:
                self.converged = True
                break
            else:
                storedGain += error * alpha
                if (i == (nIters-1)):
                    print "Reached iteration limit, not solved within desired error tolerance."

        return storedGain
Ejemplo n.º 2
0
    def __init__(
        self,
        model,
        fs,
        output=None,
        globalLoudnessFeature=None,
        loudnessLevelFunction=None,
        nInputEars=1
    ):

        self.extractor = DynamicLoudnessExtractor(
            model, fs, nInputEars, output
        )
        self.outputName = model.getOutputModulesToAggregate()[0]
        self.converged = False

        if globalLoudnessFeature is None:
            self.globalLoudnessFeature = np.mean
        else:
            self.globalLoudnessFeature = globalLoudnessFeature

        if loudnessLevelFunction is None:
            self.loudnessLevelFunction = asIs
        else:
            self.loudnessLevelFunction = loudnessLevelFunction
Ejemplo n.º 3
0
    def __init__(self,
                 model,
                 fs,
                 output=None,
                 globalLoudnessFeature=None,
                 loudnessLevelFunction=None,
                 nInputEars=1):

        self.extractor = DynamicLoudnessExtractor(model, fs, nInputEars,
                                                  output)
        self.outputName = model.getOutputModulesToAggregate()[0]
        self.converged = False

        if globalLoudnessFeature is None:
            self.globalLoudnessFeature = np.mean
        else:
            self.globalLoudnessFeature = globalLoudnessFeature

        if loudnessLevelFunction is None:
            self.loudnessLevelFunction = asIs
        else:
            self.loudnessLevelFunction = loudnessLevelFunction
Ejemplo n.º 4
0
class DynamicLoudnessIterator():
    def __init__(self,
                 model,
                 fs,
                 output=None,
                 globalLoudnessFeature=None,
                 loudnessLevelFunction=None,
                 nInputEars=1):

        self.extractor = DynamicLoudnessExtractor(model, fs, nInputEars,
                                                  output)
        self.outputName = model.getOutputModulesToAggregate()[0]
        self.converged = False

        if globalLoudnessFeature is None:
            self.globalLoudnessFeature = np.mean
        else:
            self.globalLoudnessFeature = globalLoudnessFeature

        if loudnessLevelFunction is None:
            self.loudnessLevelFunction = asIs
        else:
            self.loudnessLevelFunction = loudnessLevelFunction

    def extractLoudness(self, signal, gainInDecibels=0):
        signalIn = signal.copy() * 10**(gainInDecibels / 20.0)
        self.extractor.process(signalIn)
        timeSeries = self.extractor.outputDict[self.outputName]
        loudness = self.globalLoudnessFeature(timeSeries)
        loudnessLevel = self.loudnessLevelFunction(loudness)
        return loudnessLevel

    def process(self,
                inputSignal,
                targetLoudness,
                tol=0.1,
                nIters=5,
                alpha=1.0):

        # First check if the target loudness is a signal
        if type(targetLoudness) is np.ndarray:
            targetLoudness = self.extractLoudness(targetLoudness)

        storedGain = 0

        self.converged = False
        for i in range(nIters):

            loudnessLevel = self.extractLoudness(inputSignal, storedGain)

            error = targetLoudness - loudnessLevel

            print(('Gain: %0.3f, Loudness Level: %0.3f, ' + 'Error: %0.3f') %
                  (storedGain, loudnessLevel, error))

            if np.abs(error) < tol:
                self.converged = True
                break
            else:
                storedGain += error * alpha
                if (i == (nIters - 1)):
                    print "Reached iteration limit, not solved" \
                          "within desired error tolerance."

        return storedGain