Skip to content

Commit

Permalink
updated docs and added full scipy.signal.get_window backwards compat …
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
bmcfee committed Oct 3, 2016
1 parent e31a842 commit 1220647
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
21 changes: 15 additions & 6 deletions librosa/core/spectrum.py
Expand Up @@ -53,11 +53,14 @@ def stft(y, n_fft=2048, hop_length=None, win_length=None, window='hann',
If unspecified, defaults to ``win_length = n_fft``.
window : string, tuple, function, or np.ndarray [shape=(n_fft,)]
- a window specification (string or tuple); see `scipy.signal.get_window`
window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
- a window specification (string, tuple, or number);
see `scipy.signal.get_window`
- a window function, such as `scipy.signal.hanning`
- a vector or array of length `n_fft`
.. see also:: `filters.get_window`
center : boolean
- If `True`, the signal `y` is padded so that frame
`D[:, t]` is centered at `y[t * hop_length]`.
Expand Down Expand Up @@ -205,11 +208,14 @@ def istft(stft_matrix, hop_length=None, win_length=None, window='hann',
If unspecified, defaults to `n_fft`.
window : string, tuple, function, np.ndarray [shape=(n_fft,)]
- a window specification (string or tuple); see `scipy.signal.get_window`
window : string, tuple, number, function, np.ndarray [shape=(n_fft,)]
- a window specification (string, tuple, or number);
see `scipy.signal.get_window`
- a window function, such as `scipy.signal.hanning`
- a user-specified window vector of length `n_fft`
.. see also:: `filters.get_window`
center : boolean
- If `True`, `D` is assumed to have centered frames.
- If `False`, `D` is assumed to have left-aligned frames.
Expand Down Expand Up @@ -324,12 +330,15 @@ def ifgram(y, sr=22050, n_fft=2048, hop_length=None, win_length=None,
Window length. Defaults to `n_fft`.
See `stft` for details.
window : string, tuple, function, or np.ndarray [shape=(n_fft,)]
- a window specification (string or tuple); see `scipy.signal.get_window`
window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]
- a window specification (string, tuple, number);
see `scipy.signal.get_window`
- a window function, such as `scipy.signal.hanning`
- a user-specified window vector of length `n_fft`
See `stft` for details.
.. see also:: `filters.get_window`
norm : bool
Normalize the STFT.
Expand Down
4 changes: 2 additions & 2 deletions librosa/feature/rhythm.py
Expand Up @@ -48,8 +48,8 @@ def tempogram(y=None, sr=22050, onset_envelope=None, hop_length=512,
If `True`, onset autocorrelation windows are centered.
If `False`, windows are left-aligned.
window : string, function, tuple, or np.ndarray [shape=(win_length,)]
A window specification as in `core.stft`
window : string, function, number, tuple, or np.ndarray [shape=(win_length,)]
A window specification as in `core.stft`.
norm : {np.inf, -np.inf, 0, float > 0, None}
Normalization mode. Set to `None` to disable normalization.
Expand Down
11 changes: 8 additions & 3 deletions librosa/filters.py
Expand Up @@ -765,12 +765,14 @@ def get_window(window, Nx, fftbins=True):
Parameters
----------
window : string, tuple, callable, or list-like
window : string, tuple, number, callable, or list-like
The window specification:
- If string, it's the name of the window function (e.g., `'hann'`)
- If tuple, it's the name of the window function and any parameters
(e.g., `('kaiser', 4.0)`)
- If numeric, it is treated as the beta parameter of the `'kaiser'`
window.
- If callable, it's a function that accepts one integer argument
(the window length)
- If list-like, it's a pre-computed window of the correct length `Nx`
Expand All @@ -780,7 +782,7 @@ def get_window(window, Nx, fftbins=True):
fftbins : bool, optional
If True (default), create a periodic window for use with FFT
If False, create a symmetric window for use with filter design
If False, create a symmetric window for filter design applications.
Returns
-------
Expand All @@ -800,13 +802,16 @@ def get_window(window, Nx, fftbins=True):
if six.callable(window):
return window(Nx)

elif isinstance(window, (str, tuple)):
elif (isinstance(window, (six.string_types, tuple)) or
np.isscalar(window)):
# TODO: if we add custom window functions in librosa, call them here

return scipy.signal.get_window(window, Nx, fftbins=fftbins)

elif isinstance(window, (np.ndarray, list)):
if len(window) == Nx:
return np.asarray(window)

raise ParameterError('Window size mismatch: '
'{:d} != {:d}'.format(len(window), Nx))
else:
Expand Down
19 changes: 12 additions & 7 deletions tests/test_filters.py
Expand Up @@ -317,15 +317,20 @@ def __test(n_bins, bins_per_octave, n_chroma, fmin, base_c, window):
n_chroma, fmin, base_c, window)


def test_get_window_tuple():
@raises(librosa.ParameterError)
def test_get_window_fail():

x1 = scipy.signal.get_window(('kaiser', 4.0), 32)
x2 = librosa.filters.get_window(('kaiser', 4.0), 32)
librosa.filters.get_window(None, 32)

assert np.allclose(x1, x2)

def test_get_window():

@raises(librosa.ParameterError)
def test_get_window_fail():
def __test(window):

librosa.filters.get_window(None, 32)
w1 = librosa.filters.get_window(window, 32)
w2 = scipy.signal.get_window(window, 32)

assert np.allclose(w1, w2)

for window in ['hann', u'hann', 4.0, ('kaiser', 4.0)]:
yield __test, window

0 comments on commit 1220647

Please sign in to comment.