Caution

You're reading the documentation for a development version. For the latest released version, please have a look at 0.10.2.

librosa.effects.preemphasis

librosa.effects.preemphasis(y, *, coef=0.97, zi=None, return_zf=False)[source]

Pre-emphasize an audio signal with a first-order differencing filter:

y[n] -> y[n] - coef * y[n-1]

Parameters:
ynp.ndarray [shape=(…, n)]

Audio signal. Multi-channel is supported.

coefpositive number

Pre-emphasis coefficient. Typical values of coef are between 0 and 1.

At the limit coef=0, the signal is unchanged.

At coef=1, the result is the first-order difference of the signal.

The default (0.97) matches the pre-emphasis filter used in the HTK implementation of MFCCs [1].

zinumber

Initial filter state. When making successive calls to non-overlapping frames, this can be set to the zf returned from the previous call. (See example below.)

By default zi is initialized as 2*y[0] - y[1].

return_zfboolean

If True, return the final filter state. If False, only return the pre-emphasized signal.

Returns:
y_outnp.ndarray

pre-emphasized signal

zfnumber

if return_zf=True, the final filter state is also returned

See also

deemphasis

Examples

Apply a standard pre-emphasis filter

>>> import matplotlib.pyplot as plt
>>> y, sr = librosa.load(librosa.ex('trumpet'))
>>> y_filt = librosa.effects.preemphasis(y)
>>> # and plot the results for comparison
>>> S_orig = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max, top_db=None)
>>> S_preemph = librosa.amplitude_to_db(np.abs(librosa.stft(y_filt)), ref=np.max, top_db=None)
>>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
>>> librosa.display.specshow(S_orig, y_axis='log', x_axis='time', ax=ax[0])
>>> ax[0].set(title='Original signal')
>>> ax[0].label_outer()
>>> img = librosa.display.specshow(S_preemph, y_axis='log', x_axis='time', ax=ax[1])
>>> ax[1].set(title='Pre-emphasized signal')
>>> fig.colorbar(img, ax=ax, format="%+2.f dB")

Apply pre-emphasis in pieces for block streaming. Note that the second block initializes zi with the final state zf returned by the first call.

>>> y_filt_1, zf = librosa.effects.preemphasis(y[:1000], return_zf=True)
>>> y_filt_2, zf = librosa.effects.preemphasis(y[1000:], zi=zf, return_zf=True)
>>> np.allclose(y_filt, np.concatenate([y_filt_1, y_filt_2]))
True
../_images/librosa-effects-preemphasis-1.png