%matplotlib inline
import seaborn
import numpy, scipy, matplotlib.pyplot as plt, IPython.display as ipd
import librosa, librosa.display
plt.rcParams['figure.figsize'] = (15, 5)
Often, the raw amplitude of a signal in the time- or frequency-domain is not as perceptually relevant to humans as the amplitude converted into other units, e.g. using a logarithmic scale.
For example, let's consider a pure tone whose amplitude grows louder linearly. Define the time variable:
T = 4.0 # duration in seconds
sr = 22050 # sampling rate in Hertz
t = numpy.linspace(0, T, int(T*sr), endpoint=False)
Create a signal whose amplitude grows linearly:
amplitude = numpy.linspace(0, 1, int(T*sr), endpoint=False) # time-varying amplitude
x = amplitude*numpy.sin(2*numpy.pi*440*t)
Listen:
ipd.Audio(x, rate=sr)
Plot the signal:
librosa.display.waveplot(x, sr=sr)
Now consider a signal whose amplitude grows exponentially, i.e. the logarithm of the amplitude is linear:
amplitude = numpy.logspace(-2, 0, int(T*sr), endpoint=False, base=10.0)
x = amplitude*numpy.sin(2*numpy.pi*440*t)
ipd.Audio(x, rate=sr)
librosa.display.waveplot(x, sr=sr)
Even though the amplitude grows exponentially, to us, the increase in loudness seems more gradual. This phenomenon is an example of the Weber-Fechner law (Wikipedia) which states that the relationship between a stimulus and human perception is logarithmic.
x, sr = librosa.load('audio/latin_groove.mp3', duration=8)
ipd.Audio(x, rate=sr)
X = librosa.stft(x)
X.shape
Raw amplitude:
Xmag = abs(X)
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()
Xmag = librosa.logamplitude(X)
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()
One common variant is the $\log (1 + \lambda x)$ function, sometimes known as logarithmic compression (FMP, p. 125). This function operates like $y = \lambda x$ when $\lambda x$ is small, but it operates like $y = \log \lambda x$ when $\lambda x$ is large.
Xmag = numpy.log10(1+10*abs(X))
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()
Decibel (Wikipedia)
Xmag = librosa.amplitude_to_db(abs(X))
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()
freqs = librosa.core.fft_frequencies(sr=sr)
Xmag = librosa.perceptual_weighting(abs(X)**2, freqs)
librosa.display.specshow(Xmag, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()