Skip to content

Commit

Permalink
perceptual_weighting moved into core
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Jun 22, 2014
1 parent 35140f6 commit 20f088c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
31 changes: 31 additions & 0 deletions librosa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,37 @@ def logamplitude(S, ref_power=1.0, amin=1e-10, top_db=80.0):

return log_spec

def perceptual_weighting(S, frequencies, **kwargs):
'''Perceptual weighting of a power spectrogram:
``S_p[f] = A_weighting(f) + 10*log(S[f] / ref_power)``
:usage:
>>> # Re-weight a CQT representation, using peak power as reference
>>> CQT = librosa.cqt(y, sr, fmin=55, fmax=440)
>>> freqs = librosa.cqt_frequencies(CQT.shape[0], fmin=55)
>>> percept_CQT = librosa.perceptual_weighting(CQT, freqs,
ref_power=np.max)
:parameters:
- S : np.ndarray, shape=(d,t)
Power spectrogram
- frequencies : np.ndarray, shape=(d,)
Center frequency for each row of ``S``
- *kwargs*
Additional keyword arguments to pass to ``librosa.logamplitude``.
:returns:
- S_p : np.ndarray, shape=(d,t)
perceptually weighted version of ``S``
'''

offset = A_weighting(frequencies).reshape((-1, 1))

return offset + logamplitude(S, **kwargs)

def magphase(D):
"""Separate a complex-valued spectrogram D into its magnitude (S)
and phase (P) components, so that ``D = S * P``.
Expand Down
14 changes: 9 additions & 5 deletions librosa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def piptrack(y=None, sr=22050, S=None, n_fft=4096, fmin=150.0, fmax=4000.0, thre
return pitches, mags

#-- Mel spectrogram and MFCCs --#
def mfcc(S=None, y=None, sr=22050, n_mfcc=20):
def mfcc(y=None, sr=22050, S=None, n_mfcc=20, **kwargs):
"""Mel-frequency cepstral coefficients
:usage:
Expand All @@ -553,18 +553,22 @@ def mfcc(S=None, y=None, sr=22050, n_mfcc=20):
>>> mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
:parameters:
- S : np.ndarray or None
log-power Mel spectrogram
- y : np.ndarray or None
audio time series
- sr : int > 0
sampling rate of ``y``
- S : np.ndarray or None
log-power Mel spectrogram
- n_mfcc: int > 0
number of MFCCs to return
- *kwargs*
Additional keyword arguments for ``librosa.feature.melspectrogram``, if
operating on time series data
.. note::
One of ``S`` or ``y, sr`` must be provided.
Expand All @@ -577,7 +581,7 @@ def mfcc(S=None, y=None, sr=22050, n_mfcc=20):
"""

if S is None:
S = librosa.logamplitude(melspectrogram(y=y, sr=sr))
S = librosa.logamplitude(melspectrogram(y=y, sr=sr, **kwargs))

return np.dot(librosa.filters.dct(n_mfcc, S.shape[0]), S)

Expand Down

0 comments on commit 20f088c

Please sign in to comment.