def __init__(self, model, fs=32000, outputs=None, nInputSources=1, nInputEars=1, numSecondsToPadStartBy=0, numSecondsToPadEndBy=0.2, frameTimeOffset=0, gainInDecibels=0.0): ''' Model The input loudness model - must be dynamic. fs The sampling frequency nInputEars Number of ears used by the input array to be analysed. ''' if not model.isDynamic(): raise ValueError("Model must be dynamic.") self.model = model self.fs = int(fs) self.nInputEars = int(nInputEars) self.nInputSources = int(nInputSources) self.rate = model.getRate() # desired rate in Hz self.hopSize = int(np.round(fs / self.rate)) self.timeStep = float(self.hopSize) / fs self.inputBuf = ln.SignalBank() self.inputBuf.initialize(self.nInputSources, self.nInputEars, 1, self.hopSize, self.fs) self.outputs = outputs if self.outputs is not None: if isinstance(self.outputs, str): self.outputs = [self.outputs] else: raise ValueError("Must specify outputs") ''' if aggregate: self.model.setOutputsToAggregate(self.outputs) ''' if not self.model.initialize(self.inputBuf): raise ValueError("Problem initialising the model!") self.outputDict = {} self.nSamplesToPadStart = int( np.round(numSecondsToPadStartBy * self.fs)) self.nSamplesToPadEnd = int( np.round(numSecondsToPadEndBy * self.fs)) self.frameTimeOffset = frameTimeOffset self.x = None self.processed = False self.loudness = None self.globalLoudness = None
def __init__(self, model, outputs): if model.isDynamic(): raise ValueError("Model cannot be dynamic") self.model = model if outputs is None: raise ValueError("Must specify outputs") if type(outputs) is not list: self.outputs = [outputs] else: self.outputs = outputs self.outputDict = {} self.outputDict['Frequencies'] = np.array([]) self.nEars = 0 self.bank = ln.SignalBank()
def __init__( self, model, fs=32000, nInputEars=1, outputs=None ): ''' Model The input loudness model - must be dynamic. fs The sampling frequency nInputEars Number of ears used by the input array to be analysed. ''' if not model.isDynamic(): raise ValueError("Model must be dynamic.") self.model = model self.fs = int(fs) self.nInputEars = nInputEars self.rate = model.getRate() # desired rate in Hz self.hopSize = int(round(fs / self.rate)) self.timeStep = float(self.hopSize) / fs self.inputBuf = ln.SignalBank() self.inputBuf.initialize(self.nInputEars, 1, self.hopSize, self.fs) self.outputs = outputs if self.outputs is not None: if type(self.outputs) is not list: self.outputs = [self.outputs] self.model.setOutputsToAggregate(self.outputs) else: raise ValueError("Must specify outputs") if not self.model.initialize(self.inputBuf): raise ValueError("Problem initialising the model!") self.outputDict = {} self.nSamplesToPadStart = 0 self.nSamplesToPadEnd = int(0.2*self.fs) # see below self.frameTimeOffset = 0 self.x = None self.processed = False self.loudness = None self.globalLoudness = None
import loudness as ln # Input setup fs = 32000 frameSizes = np.array([2048, 1024, 512]) nSources = 1 # hard-coded for one source nEars = 2 # hard-coded for two ears nBands = frameSizes.size x = np.random.randn(nSources, nEars, nBands, frameSizes[0]) for frame in range(1, nBands): x[0, 0, frame, frameSizes[frame]:] = 0 x[0, 1, frame, frameSizes[frame]:] = 0 x /= np.max(np.abs(x), -1).reshape((nSources, nEars, frameSizes.size, 1)) # Initialisation inputBuf = ln.SignalBank() inputBuf.initialize(nSources, nEars, nBands, frameSizes[0], fs) inputBuf.setSignals(x) # Power spectrum setup bandFreqs = np.array([10, 500, 5000, 15001]) uniform = True spectrumModule = ln.PowerSpectrum(bandFreqs, frameSizes, uniform, ln.PowerSpectrum.AVERAGE_POWER, 1.0) spectrumModule.initialize(inputBuf) spectrumBank = spectrumModule.getOutput() spectrumLoudness = spectrumBank.getSignals()
psd = 10 ** ((20 * np.random.randn(halfPoints, 1) + 70) / 10.0) psd /= halfPoints ''' #use for pure tone psd = np.zeros((inputFreqs.size, 1)) k1000 = int(np.round(1000 * (2048 / 32e3))) psd[k1000] = 10**(40/10.0) ''' roexbankPy = DoubleRoexBank() roexbankPy.initialize(inputFreqs) roexbankPy.process(psd) excitationPy = roexbankPy.excitation # loudness side psdLN = ln.SignalBank() psdLN.initialize(1, halfPoints, 1, 32000) psdLN.setCentreFreqs(inputFreqs) psdLN.setSignals(psd.reshape((1, psd.size, 1))) bankLN = ln.DoubleRoexBank(1.5, 40.2, 0.1) bankLN.initialize(psdLN) bankLN.process(psdLN) bankLNout = bankLN.getOutput() excitationLN = bankLNout.getSignals().flatten() print bankLNout.getCentreFreqs() plt.semilogx(roexbankPy.fc, 10 * np.log10(excitationPy + 1e-10), 'k') plt.semilogx(bankLNout.getCentreFreqs(), 10 * np.log10(excitationLN + 1e-10), 'r--', marker='o') plt.show() print "Equality test: ", np.allclose(excitationLN, excitationPy[:, 0])
bins = np.zeros((nBands, 2), dtype='int') nBins = np.zeros(nBands, dtype='int') hannWindows = [] normFactors = np.zeros(nBands) for i in range(nBands): bins[i][0] = int(np.ceil(bands[i] * windowSizes[i] / float(fs))) bins[i][1] = int(np.ceil(bands[i + 1] * windowSizes[i] / float(fs))) nBins[i] = bins[i][1] - bins[i][0] windowStart[i] = windowSizes[0] / 2 - windowSizes[i] / 2 normFactors[i] = 2.0 / (2e-5 * 2e-5 * windowSizes[i]**2) if applyHannWindow: hannWindows.append(periodicHann(windowSizes[i])) normFactors[i] *= np.mean(hannWindows[i]**2) nTotalBins = np.sum(nBins) bankIn = ln.SignalBank() bankIn.initialize(1, 1, blockSize, fs) HGDFT = ln.HoppingGoertzelDFT(bands, windowSizes, hopSize, applyHannWindow, True) HGDFT.initialize(bankIn) bankOut = HGDFT.getOutput() x = np.random.randn(windowSizes[0] * 48) nFrames = int((x.size - windowSizes[0] / 2) / float(hopSize)) + 1 sFFT = np.zeros((nFrames, nTotalBins)) sHGDFT = np.zeros((nFrames, nTotalBins)) nBlocks = int(x.size / float(blockSize)) frame = 0 for block in range(nBlocks):
N = np.floor(N * 1000 + 0.5) / 1000.0 else: '''total loudness is rounded to 2 decimals''' N = np.floor(N * 100 + 0.5) / 100.0 N_specif = NS N_tot = N BarkAxis = np.arange(0.1, Ntotalbarkbands + BarkStep, BarkStep) '''LN = gene_sone2phon_ISO532B(N_tot)''' return NM, N_specif, BarkAxis, N_tot # Input signal minLevel = 40 maxLevel = 80 numPoints = 100 bank = ln.SignalBank() bank.initialize( 1, numPoints, 1, 1, ) bank.setCentreFreqs(np.logspace(1, 4, numPoints)) levels = np.random.rand(1, numPoints, 1) * (maxLevel - minLevel) + minLevel bank.setSignals(10.0**(levels / 10.0)) # Loudness setup octBank = ln.OctaveBank(3, 2, True, True) mainL = ln.MainLoudnessDIN456311991(False) il = ln.InstantaneousLoudnessDIN456311991() octBank.addTargetModule(mainL)
# Apply gains to original specific loudness self.inhibitedSpecificLoudness = specificLoudness.copy() self.inhibitedSpecificLoudness[:, 0] /= inhibLeft self.inhibitedSpecificLoudness[:, 1] /= inhibRight # python side cams = np.arange(1.8, 39, 0.1) model = BinauralInhibitionMG2007(cams) # input specific loudness patterns (one per ear) pattern = np.random.rand(cams.size, 2) model.process(pattern) # loudness side specificLoudnessBank = ln.SignalBank() specificLoudnessBank.initialize(1, 2, cams.size, 1, 1) specificLoudnessBank.setChannelSpacingInCams(0.1) specificLoudnessBank.setSignals(pattern.T.reshape((1, 2, pattern.shape[0], 1))) binauralModel = ln.BinauralInhibitionMG2007() binauralModel.initialize(specificLoudnessBank) binauralModel.process(specificLoudnessBank) inhibitedSpecificLoudnessBank = binauralModel.getOutput() inhibitedSpecificLoudness =\ inhibitedSpecificLoudnessBank.getSignals().reshape((2, cams.size)).T print ("Python vs loudness equality test applied to " + "inhibited specific loudness patterns: %r " % np.allclose(inhibitedSpecificLoudness,
import numpy as np import loudness as ln fs = 32000 N = 10000 x = np.arange(0, N) # Input SignalBank bufSize = 32 nEars = 2 nChannels = 1 inputBank = ln.SignalBank() inputBank.initialize(nEars, nChannels, bufSize, int(fs)) # Frame generator frameSize = 2048 hopSize = 32 startAtWindowCentre = True frameGen = ln.FrameGenerator(frameSize, hopSize, startAtWindowCentre) frameGen.initialize(inputBank) outputBank = frameGen.getOutput() nBlocks = int(x.size / bufSize) if startAtWindowCentre: nProcessedBlocks = int(nBlocks - 0.5 * frameSize / hopSize + 1) else: nProcessedBlocks = int(nBlocks - frameSize / hopSize + 1) frames = np.zeros((nEars, nProcessedBlocks, frameSize)) frameIndex = 0 for block in range(nBlocks):
import numpy as np import loudness as ln windowSize = 20 xLen = 40 bufSize = 10 numFrames = xLen / bufSize # Input and expected output (moving average) x = np.random.randn(xLen) y = np.convolve(x, np.ones(windowSize) / float(windowSize))[0:xLen] # Configure an input bank with 2 ears & 2 channels sig = ln.SignalBank() sig.initialize(2, 2, bufSize, 1000) # Simple moving average only sma = ln.SMA(windowSize, True, False) sma.initialize(sig) out = sma.getOutput() for i in range(numFrames): print "Frame", i start = i * bufSize end = start + bufSize sig.setSignals( np.tile(x[start:end], (sig.getNEars(), sig.getNChannels(), 1))) sma.process(sig) for ear in range(sig.getNEars()): for chn in range(sig.getNChannels()): print(
a[0] = 1.0 a[1] = 2 * (omegaSqrd - 1) / denom a[2] = (omegaSqrd - (omega / Q) + 1) / denom # design a high pass flter at 48kHz b_48k, a_48k = butter(2, 100.0 / 48e3, 'highpass') b_32k = b_48k.copy() a_32k = a_48k.copy() # solve for coeffcients at new fs = 32kHz updateFilterCoefficients(b_32k, a_32k, 48e3, 32e3) # Loudness side - input fs = 32kHz sigIn = ln.SignalBank() sigIn.initialize(1, 1, 2048, 32000) x = np.random.randn(2048) sigIn.setSignal(0, 0, x) # input coefficients designed for 48kHz filt = ln.Biquad(b_48k, a_48k) filt.setCoefficientFs(48e3) filt.initialize(sigIn) sigOut = filt.getOutput() # process using scipy's lfilter yScipy = lfilter(b_32k, a_32k, x) # process loudness side filt.process(sigIn)