From fd96b58a8763ae9963b4d0a92977f1617756a807 Mon Sep 17 00:00:00 2001 From: Keunwoo Choi Date: Fri, 29 Dec 2017 21:46:56 +0000 Subject: [PATCH] add explanations and reference, fix correctness test, add power=2.0 --- librosa/feature/spectral.py | 31 ++++++++++++++++++++--- tests/data/feature-spectral-flatness.npy | Bin 872 -> 0 bytes tests/test_features.py | 8 +++--- 3 files changed, 31 insertions(+), 8 deletions(-) delete mode 100644 tests/data/feature-spectral-flatness.npy diff --git a/librosa/feature/spectral.py b/librosa/feature/spectral.py index ad756e7e9b..ae895dbe8d 100644 --- a/librosa/feature/spectral.py +++ b/librosa/feature/spectral.py @@ -500,16 +500,27 @@ 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 spectral_flatness(y=None, S=None, n_fft=2048, hop_length=512, amin=1e-10): +def spectral_flatness(y=None, S=None, n_fft=2048, hop_length=512, + amin=1e-10, power=2.0): '''Compute spectral flatness + Spectral flatness (or tonality coefficient) is a measure to + quantify how much noise-like a sound is, as opposed to being + tone-like [1]_. A high spectral flatness (closer to 1.0) + indicates the spectrum is similar to white noise. + It is often converted to decibel. + + .. [1] Dubnov, Shlomo "Generalization of spectral flatness + measure for non-gaussian linear processes" + IEEE Signal Processing Letters, 2004, Vol. 11. + Parameters ---------- y : np.ndarray [shape=(n,)] or None audio time series S : np.ndarray [shape=(d, t)] or None - (optional) spectrogram magnitude + (optional) pre-computed spectrogram magnitude n_fft : int > 0 [scalar] FFT window size @@ -520,6 +531,10 @@ def spectral_flatness(y=None, S=None, n_fft=2048, hop_length=512, amin=1e-10): amin : float > 0 [scalar] minimum threshold for `S` (=added noise floor for numerical stability) + power : float > 0 [scalar] + Exponent for the magnitude spectrogram. + e.g., 1 for energy, 2 for power, etc. + Power spectrogram is usually used for computing spectral flatness. Returns ------- @@ -545,6 +560,14 @@ def spectral_flatness(y=None, S=None, n_fft=2048, hop_length=512, amin=1e-10): array([[ 1.00000e+00, 5.82299e-03, 5.64624e-04, ..., 9.99063e-01, 1.00000e+00, 1.00000e+00]], dtype=float32) + From power spectrogram input + + >>> S, phase = librosa.magphase(librosa.stft(y)) + >>> S_power = S ** 2 + >>> librosa.feature.spectral_flatness(S=S_power, power=1.0) + array([[ 1.00000e+00, 5.82299e-03, 5.64624e-04, ..., 9.99063e-01, + 1.00000e+00, 1.00000e+00]], dtype=float32) + ''' if amin <= 0: raise ParameterError('amin must be strictly positive') @@ -559,9 +582,9 @@ def spectral_flatness(y=None, S=None, n_fft=2048, hop_length=512, amin=1e-10): raise ParameterError('Spectral flatness is only defined ' 'with non-negative energies') - gmean = np.exp(np.mean(np.log(np.maximum(amin, S ** 2)), + gmean = np.exp(np.mean(np.log(np.maximum(amin, S ** power)), axis=0, keepdims=True)) - amean = np.mean(np.maximum(amin, S ** 2), axis=0, keepdims=True) + amean = np.mean(np.maximum(amin, S ** power), axis=0, keepdims=True) return gmean / amean diff --git a/tests/data/feature-spectral-flatness.npy b/tests/data/feature-spectral-flatness.npy deleted file mode 100644 index 8601a2f53161498ac405e5d94a87f18717640d2d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 872 zcmXZQe^8Tk9Ki7hrv^bfkjP=;lw@O$aBk1@`Fy|6z(PTXQksf!%*J9PPl0hmgCNL; zY~79kFEnj=gGUW%lBf~J6&Ys37RHYxccu_W7AfVCr*PdcyX*bm`}N)n+cGv~1uoyH>bqnE3cQWe2?rL;o& ze~SG1@W27J$=%XAUVgrO^@Y$PW~hlS#K z%&nSO53iXRT;X)#H^Cdo>x*q9^Pz}p`V;AHLkevZze%FTI`N&zQ!w&MI6R*}%4KXC zsKAI#KZ>5qV#R2pV)Z=$2dhm*`8s9!S44RRnknY+Gnq{frbLb)I>vsH7 zQG-2|4tT@P!P9V;+dTvjabpVB%Efrca38N!awIxyFA809>=L`tI==(u-5+3jQz&U! z*og_Ma4a3~ghl!z;BKN&GCKy&XC8-`o-|UftRlYBQQXLE#WlZO2W8(Gc!~KKUR+Ry zby^i>8)~5Cdlmd332+l}hau7$hfB}AkK@cWOyJwG|DQILU8*1{(h@4zt7&h6o$8#; zG(PZh>u;0q5#jas1K(mHTn zT@GP7J=lM&!unPv9v;>~t(=ESu@j8ZA`G6`fcLgMM{ecRk%v$Iihm6oF!z^daC@T| zXN^UpC**#n847PGBx>ZdL`Z&JPYeI1@LOeVo0d@otvJ2le^*V