Skip to content

Commit

Permalink
Merge d267c0a into fe7f444
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Nov 2, 2016
2 parents fe7f444 + d267c0a commit d511e4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
24 changes: 19 additions & 5 deletions librosa/feature/spectral.py
Expand Up @@ -9,6 +9,7 @@
from .. import util
from .. import filters
from ..util.exceptions import ParameterError
from ..util.deprecation import Deprecated, rename_kw

from ..core.time_frequency import fft_frequencies
from ..core.audio import zero_crossings, to_mono
Expand Down Expand Up @@ -495,7 +496,8 @@ def spectral_rolloff(y=None, sr=22050, S=None, n_fft=2048, hop_length=512,
return np.nanmin(ind * freq, axis=0, keepdims=True)


def rmse(y=None, S=None, n_fft=2048, hop_length=512):
def rmse(y=None, S=None, frame_length=2048, hop_length=512,
n_fft=Deprecated()):
'''Compute root-mean-square (RMS) energy for each frame, either from the
audio samples `y` or from a spectrogram `S`.
Expand All @@ -513,12 +515,16 @@ def rmse(y=None, S=None, n_fft=2048, hop_length=512):
S : np.ndarray [shape=(d, t)] or None
(optional) spectrogram magnitude. Required if `y` is not input.
n_fft : int > 0 [scalar]
FFT window size
frame_length : int > 0 [scalar]
length of analysis frame (in samples) for energy calculation
hop_length : int > 0 [scalar]
hop length for STFT. See `librosa.core.stft` for details.
n_fft : [DEPRECATED]
.. warning:: This parameter name was deprecated in librosa 0.5.0
Use the `frame_length` parameter instead.
The `n_fft` parameter will be removed in librosa 0.6.0.
Returns
-------
Expand Down Expand Up @@ -557,12 +563,20 @@ def rmse(y=None, S=None, n_fft=2048, hop_length=512):
>>> librosa.feature.rmse(S=S)
'''
frame_length = rename_kw('n_fft', n_fft,
'frame_length', frame_length,
'0.5', '0.6')

if y is not None and S is not None:
raise ValueError('Either `y` or `S` should be input.')
if y is not None:
x = util.frame(to_mono(y), frame_length=n_fft, hop_length=hop_length)
x = util.frame(to_mono(y),
frame_length=frame_length,
hop_length=hop_length)
elif S is not None:
x, _ = _spectrogram(y=y, S=S, n_fft=n_fft, hop_length=hop_length)
x, _ = _spectrogram(y=y, S=S,
n_fft=frame_length,
hop_length=hop_length)
else:
raise ValueError('Either `y` or `S` must be input.')
return np.sqrt(np.mean(np.abs(x)**2, axis=0, keepdims=True))
Expand Down
26 changes: 21 additions & 5 deletions tests/test_features.py
Expand Up @@ -2,10 +2,13 @@
# -*- encoding: utf-8 -*-

from __future__ import print_function
import librosa
import warnings
import numpy as np

from nose.tools import raises, eq_

import librosa

from test_core import load

# Disable cache
Expand Down Expand Up @@ -333,8 +336,10 @@ def __test_consistency(frame_length, hop_length):
center=False))[0]

# Try both RMS methods.
rms1 = librosa.feature.rmse(S=S, n_fft=frame_length, hop_length=hop_length)
rms2 = librosa.feature.rmse(y=y, n_fft=frame_length, hop_length=hop_length)
rms1 = librosa.feature.rmse(S=S, frame_length=frame_length,
hop_length=hop_length)
rms2 = librosa.feature.rmse(y=y, frame_length=frame_length,
hop_length=hop_length)

# Normalize envelopes.
rms1 /= rms1.max()
Expand All @@ -343,14 +348,25 @@ def __test_consistency(frame_length, hop_length):
# Ensure results are similar.
np.testing.assert_allclose(rms1, rms2, rtol=1e-2)

for n_fft in [2048, 4096]:
for frame_length in [2048, 4096]:
for hop_length in [128, 512, 1024]:
yield __test_consistency, n_fft, hop_length
yield __test_consistency, frame_length, hop_length

for n in range(10, 100, 10):
yield __test, n


def test_rmse_nfft():

warnings.resetwarnings()
warnings.simplefilter('always')
with warnings.catch_warnings(record=True) as out:
librosa.feature.rmse(y=np.zeros(8192), n_fft=1024)
assert len(out) > 0
assert out[0].category is DeprecationWarning
assert 'renamed' in str(out[0].message).lower()


def test_zcr_synthetic():

def __test_zcr(rate, y, frame_length, hop_length, center):
Expand Down

0 comments on commit d511e4a

Please sign in to comment.