diff --git a/docs/examples/plot_hprss.py b/docs/examples/plot_hprss.py index 13ede1896b..faf2aad869 100644 --- a/docs/examples/plot_hprss.py +++ b/docs/examples/plot_hprss.py @@ -30,12 +30,17 @@ ############################################### # Compute the short-time Fourier transform of y D = librosa.stft(y) +D_power = librosa.magphase(D, power=2)[0] +reference_power = np.max(D_power) +D_dB = librosa.power_to_db(D_power, ref=rp) ##################################################### # Decompose D into harmonic and percussive components # # :math:`D = D_\text{harmonic} + D_\text{percussive}` D_harmonic, D_percussive = librosa.decompose.hpss(D) +D_harmonic_dB = librosa.power_to_db(librosa.magphase(D_harmonic, power=2)[0], ref=rp) +D_percussive_dB = librosa.power_to_db(librosa.magphase(D_percussive, power=2)[0], ref=rp) #################################################################### @@ -47,17 +52,17 @@ plt.figure(figsize=(12, 8)) plt.subplot(3, 1, 1) -librosa.display.specshow(librosa.amplitude_to_db(D, ref=rp), y_axis='log') +librosa.display.specshow(D_dB, y_axis='log') plt.colorbar() plt.title('Full spectrogram') plt.subplot(3, 1, 2) -librosa.display.specshow(librosa.amplitude_to_db(D_harmonic, ref=rp), y_axis='log') +librosa.display.specshow(D_harmonic_dB, y_axis='log') plt.colorbar() plt.title('Harmonic spectrogram') plt.subplot(3, 1, 3) -librosa.display.specshow(librosa.amplitude_to_db(D_percussive, ref=rp), y_axis='log', x_axis='time') +librosa.display.specshow(D_percussive_dB, y_axis='log', x_axis='time') plt.colorbar() plt.title('Percussive spectrogram') plt.tight_layout() @@ -80,9 +85,20 @@ # Let's compute separations for a few different margins and compare the results below D_harmonic2, D_percussive2 = librosa.decompose.hpss(D, margin=2) +D_harmonic2_dB = librosa.power_to_db(librosa.magphase(D_harmonic2, power=2)[0], ref=rp) +D_percussive2_dB = librosa.power_to_db(librosa.magphase(D_percussive2, power=2)[0], ref=rp) + D_harmonic4, D_percussive4 = librosa.decompose.hpss(D, margin=4) +D_harmonic4_dB = librosa.power_to_db(librosa.magphase(D_harmonic4, power=2)[0], ref=rp) +D_percussive4_dB = librosa.power_to_db(librosa.magphase(D_percussive4, power=2)[0], ref=rp) + D_harmonic8, D_percussive8 = librosa.decompose.hpss(D, margin=8) +D_harmonic8_dB = librosa.power_to_db(librosa.magphase(D_harmonic8, power=2)[0], ref=rp) +D_percussive8_dB = librosa.power_to_db(librosa.magphase(D_percussive8, power=2)[0], ref=rp) + D_harmonic16, D_percussive16 = librosa.decompose.hpss(D, margin=16) +D_harmonic16_dB = librosa.power_to_db(librosa.magphase(D_harmonic16, power=2)[0], ref=rp) +D_percussive16_dB = librosa.power_to_db(librosa.magphase(D_percussive16, power=2)[0], ref=rp) ############################################################################# @@ -91,41 +107,41 @@ plt.figure(figsize=(10, 10)) plt.subplot(5, 2, 1) -librosa.display.specshow(librosa.amplitude_to_db(D_harmonic, ref=rp), y_axis='log') +librosa.display.specshow(D_harmonic_dB, y_axis='log') plt.title('Harmonic') plt.yticks([]) plt.ylabel('margin=1') plt.subplot(5, 2, 2) -librosa.display.specshow(librosa.amplitude_to_db(D_percussive, ref=rp), y_axis='log') +librosa.display.specshow(D_percussive_dB, y_axis='log') plt.title('Percussive') plt.yticks([]), plt.ylabel('') plt.subplot(5, 2, 3) -librosa.display.specshow(librosa.amplitude_to_db(D_harmonic2, ref=rp), y_axis='log') +librosa.display.specshow(D_harmonic2_dB, y_axis='log') plt.yticks([]) plt.ylabel('margin=2') plt.subplot(5, 2, 4) -librosa.display.specshow(librosa.amplitude_to_db(D_percussive2, ref=rp), y_axis='log') +librosa.display.specshow(D_percussive2_dB, y_axis='log') plt.yticks([]) ,plt.ylabel('') plt.subplot(5, 2, 5) -librosa.display.specshow(librosa.amplitude_to_db(D_harmonic4, ref=rp), y_axis='log') +librosa.display.specshow(D_harmonic4_dB, y_axis='log') plt.yticks([]) plt.ylabel('margin=4') plt.subplot(5, 2, 6) -librosa.display.specshow(librosa.amplitude_to_db(D_percussive4, ref=rp), y_axis='log') +librosa.display.specshow(D_percussive4_dB, y_axis='log') plt.yticks([]), plt.ylabel('') plt.subplot(5, 2, 7) -librosa.display.specshow(librosa.amplitude_to_db(D_harmonic8, ref=rp), y_axis='log') +librosa.display.specshow(D_harmonic8_dB, y_axis='log') plt.yticks([]) plt.ylabel('margin=8') plt.subplot(5, 2, 8) -librosa.display.specshow(librosa.amplitude_to_db(D_percussive8, ref=rp), y_axis='log') +librosa.display.specshow(D_percussive8_dB, y_axis='log') plt.yticks([]), plt.ylabel('') plt.subplot(5, 2, 9) diff --git a/docs/examples/plot_presets.py b/docs/examples/plot_presets.py index 488d4d0ffb..e1c9583da2 100644 --- a/docs/examples/plot_presets.py +++ b/docs/examples/plot_presets.py @@ -59,29 +59,25 @@ ###################################################################### # Now we can load in a file and do some analysis with the new defaults filename = 'audio/Karissa_Hobbs_-_09_-_Lets_Go_Fishin.mp3' - y, sr = librosa.load(filename, duration=5, offset=35) -# Generate a Mel spectrogram: - +# Generate a mel spectrogram: M = librosa.feature.melspectrogram(y=y) # Of course, you can still override the new default manually, e.g.: - -M_highres = librosa.feature.melspectrogram(y=y, hop_length=512) - +M_hop512 = librosa.feature.melspectrogram(y=y, hop_length=512) # And plot the results plt.figure(figsize=(6, 6)) ax = plt.subplot(3, 1, 1) -librosa.display.specshow(librosa.power_to_db(M, ref=np.max), +librosa.display.specshow(librosa.amplitude_to_db(M, ref=np.max), y_axis='mel', x_axis='time') plt.title('44100/1024/4096') plt.subplot(3, 1, 2, sharex=ax, sharey=ax) -librosa.display.specshow(librosa.power_to_db(M_highres, ref=np.max), +librosa.display.specshow(librosa.amplitude_to_db(M_hop512, ref=np.max), hop_length=512, y_axis='mel', x_axis='time') plt.title('44100/512/4096') @@ -91,10 +87,10 @@ librosa['sr'] = 11025 y2, sr2 = librosa.load(filename, duration=5, offset=35) -M2 = librosa.feature.melspectrogram(y=y2, sr=sr2) +M_11k = librosa.feature.melspectrogram(y=y2, sr=sr2) plt.subplot(3, 1, 3, sharex=ax, sharey=ax) -librosa.display.specshow(librosa.power_to_db(M2, ref=np.max), +librosa.display.specshow(librosa.amplitude_to_db(M_11k, ref=np.max), y_axis='mel', x_axis='time') plt.title('11025/1024/4096') diff --git a/docs/examples/plot_segmentation.py b/docs/examples/plot_segmentation.py index 9d938eb5f6..198d84fa66 100644 --- a/docs/examples/plot_segmentation.py +++ b/docs/examples/plot_segmentation.py @@ -43,13 +43,14 @@ # Next, we'll compute and plot a log-power CQT BINS_PER_OCTAVE = 12 * 3 N_OCTAVES = 7 -C = librosa.amplitude_to_db(librosa.cqt(y=y, sr=sr, - bins_per_octave=BINS_PER_OCTAVE, - n_bins=N_OCTAVES * BINS_PER_OCTAVE), - ref=np.max) +C = librosa.cqt(y=y, sr=sr, + bins_per_octave=BINS_PER_OCTAVE, + n_bins=N_OCTAVES * BINS_PER_OCTAVE), + ref=np.max) +C_dB = librosa.power_to_db(librosa.magphase(C, power=2)[0]) plt.figure(figsize=(12, 4)) -librosa.display.specshow(C, y_axis='cqt_hz', sr=sr, +librosa.display.specshow(C_dB, y_axis='cqt_hz', sr=sr, bins_per_octave=BINS_PER_OCTAVE, x_axis='time') plt.tight_layout() @@ -58,7 +59,7 @@ ########################################################## # To reduce dimensionality, we'll beat-synchronous the CQT tempo, beats = librosa.beat.beat_track(y=y, sr=sr, trim=False) -Csync = librosa.util.sync(C, beats, aggregate=np.median) +Csync_dB = librosa.util.sync(C_dB, beats, aggregate=np.median) # For plotting purposes, we'll need the timing of the beats # we fix_frames to include non-beat frames 0 and C.shape[1] (final frame) @@ -68,7 +69,7 @@ sr=sr) plt.figure(figsize=(12, 4)) -librosa.display.specshow(Csync, bins_per_octave=12*3, +librosa.display.specshow(Csync_dB, bins_per_octave=12*3, y_axis='cqt_hz', x_axis='time', x_coords=beat_times) plt.tight_layout() @@ -79,7 +80,7 @@ # (Equation 1) # width=3 prevents links within the same bar # mode='affinity' here implements S_rep (after Eq. 8) -R = librosa.segment.recurrence_matrix(Csync, width=3, mode='affinity', +R = librosa.segment.recurrence_matrix(Csync_dB, width=3, mode='affinity', sym=True) # Enhance diagonals with a median filter (Equation 2) @@ -230,7 +231,7 @@ fmin=librosa.note_to_hz('C1'), bins_per_octave=BINS_PER_OCTAVE) -librosa.display.specshow(C, y_axis='cqt_hz', sr=sr, +librosa.display.specshow(C_dB, y_axis='cqt_hz', sr=sr, bins_per_octave=BINS_PER_OCTAVE, x_axis='time') ax = plt.gca() diff --git a/docs/examples/plot_superflux.py b/docs/examples/plot_superflux.py index 2a1b0292bd..bd2634cd52 100644 --- a/docs/examples/plot_superflux.py +++ b/docs/examples/plot_superflux.py @@ -55,8 +55,12 @@ n_mels=n_mels) +# Extract magnitude and convert to dB +S_dB = librosa.amplitude_to_db(librosa.magphase(S)[0], ref=np.max) + + plt.figure(figsize=(6, 4)) -librosa.display.specshow(librosa.power_to_db(S, ref=np.max), +librosa.display.specshow(S_dB, y_axis='mel', x_axis='time', sr=sr, hop_length=hop_length, fmin=fmin, fmax=fmax) plt.tight_layout() @@ -72,7 +76,7 @@ ######################################### # And similarly with the superflux method -odf_sf = librosa.onset.onset_strength(S=librosa.power_to_db(S, ref=np.max), +odf_sf = librosa.onset.onset_strength(S=S_dB, sr=sr, hop_length=hop_length, lag=lag, max_size=max_size) @@ -100,7 +104,7 @@ hop_length=hop_length) ax = plt.subplot(2, 1, 2) -librosa.display.specshow(librosa.power_to_db(S, ref=np.max), +librosa.display.specshow(S_dB, y_axis='mel', x_axis='time', sr=sr, hop_length=hop_length, fmin=fmin, fmax=fmax) plt.xlim([0, 5.0]) diff --git a/examples/LibROSA audio effects and playback.ipynb b/examples/LibROSA audio effects and playback.ipynb index 848fb71467..d8303ca8c3 100644 --- a/examples/LibROSA audio effects and playback.ipynb +++ b/examples/LibROSA audio effects and playback.ipynb @@ -256,7 +256,8 @@ "plt.figure(figsize=(12,4))\n", "\n", "plt.subplot(1,2,1)\n", - "librosa.display.specshow(librosa.amplitude_to_db(components, ref=np.max), y_axis='log')\n", + "components_db = librosa.amplitude_to_db(np.abs(components), ref=np.max)\n", + "librosa.display.specshow(components_db, y_axis='log')\n", "plt.xlabel('Component')\n", "plt.ylabel('Frequency')\n", "plt.title('Components')\n", diff --git a/examples/LibROSA demo.ipynb b/examples/LibROSA demo.ipynb index 730d232dd3..d9836850c5 100644 --- a/examples/LibROSA demo.ipynb +++ b/examples/LibROSA demo.ipynb @@ -108,11 +108,12 @@ } ], "source": [ - "# Let's make and display a mel-scaled power (energy-squared) spectrogram\n", + "# Let's make and display a mel-scaled power (squared energy) spectrogram\n", "S = librosa.feature.melspectrogram(y, sr=sr, n_mels=128)\n", + "S_power = librosa.magphase(S, power=2)[0]\n", "\n", "# Convert to log scale (dB). We'll use the peak power (max) as reference.\n", - "log_S = librosa.power_to_db(S, ref=np.max)\n", + "log_S = librosa.amplitude_to_db(S_power, ref=np.max)\n", "\n", "# Make a new figure\n", "plt.figure(figsize=(12,4))\n", @@ -167,13 +168,15 @@ ], "source": [ "# What do the spectrograms look like?\n", - "# Let's make and display a mel-scaled power (energy-squared) spectrogram\n", + "# Let's make and display a mel-scaled power (squared energy) spectrogram\n", "S_harmonic = librosa.feature.melspectrogram(y_harmonic, sr=sr)\n", "S_percussive = librosa.feature.melspectrogram(y_percussive, sr=sr)\n", + "Sh_power = librosa.magphase(S_harmonic, power=2)[0]\n", + "Sp_power = librosa.magphase(S_percussive, power=2)[0]\n", "\n", "# Convert to log scale (dB). We'll use the peak power as reference.\n", - "log_Sh = librosa.power_to_db(S_harmonic, ref=np.max)\n", - "log_Sp = librosa.power_to_db(S_percussive, ref=np.max)\n", + "log_Sh = librosa.power_to_db(Sh_power, ref=np.max)\n", + "log_Sp = librosa.power_to_db(Sp_power, ref=np.max)\n", "\n", "# Make a new figure\n", "plt.figure(figsize=(12,6))\n", diff --git a/librosa/core/audio.py b/librosa/core/audio.py index 2aaa29c058..52adf4b286 100644 --- a/librosa/core/audio.py +++ b/librosa/core/audio.py @@ -647,8 +647,9 @@ def clicks(times=None, frames=None, sr=22050, hop_length=512, >>> import matplotlib.pyplot as plt >>> plt.figure() >>> S = librosa.feature.melspectrogram(y=y, sr=sr) + >>> S_power = librosa.magphase(S, power=2)[0] >>> ax = plt.subplot(2,1,2) - >>> librosa.display.specshow(librosa.power_to_db(S, ref=np.max), + >>> librosa.display.specshow(librosa.power_to_db(S_power, ref=np.max), ... x_axis='time', y_axis='mel') >>> plt.subplot(2,1,1, sharex=ax) >>> librosa.display.waveplot(y_beat_times, sr=sr, label='Beat clicks') diff --git a/librosa/core/constantq.py b/librosa/core/constantq.py index bd11b4180b..c598ff0f28 100644 --- a/librosa/core/constantq.py +++ b/librosa/core/constantq.py @@ -119,7 +119,8 @@ def cqt(y, sr=22050, hop_length=512, fmin=None, n_bins=84, >>> import matplotlib.pyplot as plt >>> y, sr = librosa.load(librosa.util.example_audio_file()) >>> C = librosa.cqt(y, sr=sr) - >>> librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max), + >>> C_dB = librosa.power_to_db(librosa.magphase(C, power=2)[0], ref=np.max) + >>> librosa.display.specshow(C_dB, ... sr=sr, x_axis='time', y_axis='cqt_note') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Constant-Q power spectrum') diff --git a/librosa/core/spectrum.py b/librosa/core/spectrum.py index 31dac103c3..a85c9ff6f5 100644 --- a/librosa/core/spectrum.py +++ b/librosa/core/spectrum.py @@ -131,9 +131,8 @@ def stft(y, n_fft=2048, hop_length=None, win_length=None, window='hann', >>> import matplotlib.pyplot as plt - >>> librosa.display.specshow(librosa.amplitude_to_db(D, - ... ref=np.max), - ... y_axis='log', x_axis='time') + >>> D_dB = librosa.power_to_db(librosa.magphase(D, power=2)[0], ref=np.max) + >>> librosa.display.specshow(D_dB, y_axis='log', x_axis='time') >>> plt.title('Power spectrogram') >>> plt.colorbar(format='%+2.0f dB') >>> plt.tight_layout() @@ -810,8 +809,8 @@ def power_to_db(S, ref=1.0, amin=1e-10, top_db=80.0): Get a power spectrogram from a waveform ``y`` >>> y, sr = librosa.load(librosa.util.example_audio_file()) - >>> S = np.abs(librosa.stft(y)) - >>> librosa.power_to_db(S**2) + >>> S_power = librosa.magphase(librosa.stft(y), power=2)[0] + >>> librosa.power_to_db(S_power) array([[-33.293, -27.32 , ..., -33.293, -33.293], [-33.293, -25.723, ..., -33.293, -33.293], ..., @@ -820,7 +819,7 @@ def power_to_db(S, ref=1.0, amin=1e-10, top_db=80.0): Compute dB relative to peak power - >>> librosa.power_to_db(S**2, ref=np.max) + >>> librosa.power_to_db(S_power, ref=np.max) array([[-80. , -74.027, ..., -80. , -80. ], [-80. , -72.431, ..., -80. , -80. ], ..., @@ -830,7 +829,7 @@ def power_to_db(S, ref=1.0, amin=1e-10, top_db=80.0): Or compare to median power - >>> librosa.power_to_db(S**2, ref=np.median) + >>> librosa.power_to_db(S_power, ref=np.median) array([[-0.189, 5.784, ..., -0.189, -0.189], [-0.189, 7.381, ..., -0.189, -0.189], ..., @@ -843,11 +842,11 @@ def power_to_db(S, ref=1.0, amin=1e-10, top_db=80.0): >>> import matplotlib.pyplot as plt >>> plt.figure() >>> plt.subplot(2, 1, 1) - >>> librosa.display.specshow(S**2, sr=sr, y_axis='log') + >>> librosa.display.specshow(S_power, sr=sr, y_axis='log') >>> plt.colorbar() >>> plt.title('Power spectrogram') >>> plt.subplot(2, 1, 2) - >>> librosa.display.specshow(librosa.power_to_db(S**2, ref=np.max), + >>> librosa.display.specshow(librosa.power_to_db(S_power, ref=np.max), ... sr=sr, y_axis='log', x_axis='time') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Log-Power spectrogram') @@ -861,9 +860,9 @@ def power_to_db(S, ref=1.0, amin=1e-10, top_db=80.0): raise ParameterError('amin must be strictly positive') if np.issubdtype(S.dtype, np.complexfloating): - warnings.warn('power_to_db was called on complex input so phase ' + warnings.warn('power_to_db was called on complex input. Therefore, phase ' 'information will be discarded. To suppress this warning, ' - 'call power_to_db(magphase(D, power=2)[0]) instead.') + 'call power_to_db(magphase(S, power=2)[0]) instead.') magnitude = np.abs(S) else: magnitude = S @@ -956,9 +955,9 @@ def amplitude_to_db(S, ref=1.0, amin=1e-5, top_db=80.0): S = np.asarray(S) if np.issubdtype(S.dtype, np.complexfloating): - warnings.warn('amplitude_to_db was called on complex input so phase ' + warnings.warn('amplitude_to_db was called on complex input. Therefore, phase ' 'information will be discarded. To suppress this warning, ' - 'call amplitude_to_db(magphase(D)[0]) instead.') + 'call amplitude_to_db(np.abs(S)) instead.') magnitude = np.abs(S) @@ -1039,9 +1038,10 @@ def perceptual_weighting(S, frequencies, **kwargs): >>> y, sr = librosa.load(librosa.util.example_audio_file()) >>> CQT = librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('A1')) - >>> freqs = librosa.cqt_frequencies(CQT.shape[0], + >>> CQT_power = librosa.magphase(CQT, power=2)[0] + >>> freqs = librosa.cqt_frequencies(CQT_power.shape[0], ... fmin=librosa.note_to_hz('A1')) - >>> perceptual_CQT = librosa.perceptual_weighting(CQT**2, + >>> perceptual_CQT = librosa.perceptual_weighting(CQT_power, ... freqs, ... ref=np.max) >>> perceptual_CQT @@ -1054,7 +1054,7 @@ def perceptual_weighting(S, frequencies, **kwargs): >>> import matplotlib.pyplot as plt >>> plt.figure() >>> plt.subplot(2, 1, 1) - >>> librosa.display.specshow(librosa.amplitude_to_db(CQT, + >>> librosa.display.specshow(librosa.power_to_db(CQT_power, ... ref=np.max), ... fmin=librosa.note_to_hz('A1'), ... y_axis='cqt_hz') diff --git a/librosa/display.py b/librosa/display.py index a95fccf0ff..5d4fa7e872 100644 --- a/librosa/display.py +++ b/librosa/display.py @@ -580,9 +580,10 @@ def specshow(data, x_coords=None, y_coords=None, >>> y, sr = librosa.load(librosa.util.example_audio_file()) >>> plt.figure(figsize=(12, 8)) - >>> D = librosa.amplitude_to_db(librosa.stft(y), ref=np.max) + >>> D = librosa.stft(y) + >>> D_dB = librosa.power_to_db(librosa.magphase(D, power=2)[0], ref=np.max) >>> plt.subplot(4, 2, 1) - >>> librosa.display.specshow(D, y_axis='linear') + >>> librosa.display.specshow(D_dB, y_axis='linear') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Linear-frequency power spectrogram') @@ -590,21 +591,22 @@ def specshow(data, x_coords=None, y_coords=None, Or on a logarithmic scale >>> plt.subplot(4, 2, 2) - >>> librosa.display.specshow(D, y_axis='log') + >>> librosa.display.specshow(D_dB, y_axis='log') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Log-frequency power spectrogram') Or use a CQT scale - >>> CQT = librosa.amplitude_to_db(librosa.cqt(y, sr=sr), ref=np.max) + >>> CQT = librosa.cqt(y, sr=sr) + >>> CQT_dB = librosa.power_to_db(librosa.magphase(CQT, power=2)[0], ref=np.max) >>> plt.subplot(4, 2, 3) >>> librosa.display.specshow(CQT, y_axis='cqt_note') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Constant-Q power spectrogram (note)') >>> plt.subplot(4, 2, 4) - >>> librosa.display.specshow(CQT, y_axis='cqt_hz') + >>> librosa.display.specshow(CQT_dB, y_axis='cqt_hz') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Constant-Q power spectrogram (Hz)') @@ -621,7 +623,7 @@ def specshow(data, x_coords=None, y_coords=None, Force a grayscale colormap (white -> black) >>> plt.subplot(4, 2, 6) - >>> librosa.display.specshow(D, cmap='gray_r', y_axis='linear') + >>> librosa.display.specshow(D_dB, cmap='gray_r', y_axis='linear') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Linear power spectrogram (grayscale)') @@ -629,7 +631,7 @@ def specshow(data, x_coords=None, y_coords=None, Draw time markers automatically >>> plt.subplot(4, 2, 7) - >>> librosa.display.specshow(D, x_axis='time', y_axis='log') + >>> librosa.display.specshow(D_dB, x_axis='time', y_axis='log') >>> plt.colorbar(format='%+2.0f dB') >>> plt.title('Log power spectrogram') diff --git a/librosa/feature/spectral.py b/librosa/feature/spectral.py index f631200c70..30181f7e1a 100644 --- a/librosa/feature/spectral.py +++ b/librosa/feature/spectral.py @@ -1467,13 +1467,12 @@ def melspectrogram(y=None, sr=22050, S=None, n_fft=2048, hop_length=512, Using a pre-computed power spectrogram - >>> D = np.abs(librosa.stft(y))**2 + >>> D = librosa.magphase(librosa.stft(y), power=2)[0] >>> S = librosa.feature.melspectrogram(S=D) >>> # Passing through arguments to the Mel filters >>> S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, ... fmax=8000) - >>> import matplotlib.pyplot as plt >>> plt.figure(figsize=(10, 4)) >>> librosa.display.specshow(librosa.power_to_db(S, diff --git a/librosa/onset.py b/librosa/onset.py index 0c17ee7df6..b59e2912fc 100644 --- a/librosa/onset.py +++ b/librosa/onset.py @@ -120,10 +120,10 @@ def onset_detect(y=None, sr=22050, onset_envelope=None, hop_length=512, >>> import matplotlib.pyplot as plt >>> D = librosa.stft(y) + >>> D_dB = librosa.power_to_db(librosa.magphase(D, power=2)[0], ref=np.max) >>> plt.figure() >>> ax1 = plt.subplot(2, 1, 1) - >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), - ... x_axis='time', y_axis='log') + >>> librosa.display.specshow(D_dB, x_axis='time', y_axis='log') >>> plt.title('Power spectrogram') >>> plt.subplot(2, 1, 2, sharex=ax1) >>> plt.plot(times, o_env, label='Onset strength') @@ -270,11 +270,11 @@ def onset_strength(y=None, sr=22050, S=None, lag=1, max_size=1, >>> y, sr = librosa.load(librosa.util.example_audio_file(), ... duration=10.0) >>> D = librosa.stft(y) + >>> D_dB = librosa.power_to_db(librosa.magphase(D, power=2)[0], ref=np.max) >>> times = librosa.frames_to_time(np.arange(D.shape[1])) >>> plt.figure() >>> ax1 = plt.subplot(2, 1, 1) - >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), - ... y_axis='log', x_axis='time') + >>> librosa.display.specshow(D_dB, y_axis='log', x_axis='time') >>> plt.title('Power spectrogram') Construct a standard onset function @@ -474,10 +474,10 @@ def onset_strength_multi(y=None, sr=22050, S=None, lag=1, max_size=1, >>> y, sr = librosa.load(librosa.util.example_audio_file(), ... duration=10.0) >>> D = librosa.stft(y) + >>> D_dB = librosa.power_to_db(librosa.magphase(D)[0], ref=np.max) >>> plt.figure() >>> plt.subplot(2, 1, 1) - >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), - ... y_axis='log') + >>> librosa.display.specshow(D_dB, y_axis='log') >>> plt.title('Power spectrogram') Construct a standard onset function over four sub-bands diff --git a/librosa/util/utils.py b/librosa/util/utils.py index 3fe9434bde..539f4c5be3 100644 --- a/librosa/util/utils.py +++ b/librosa/util/utils.py @@ -898,8 +898,8 @@ def peak_pick(x, pre_max, post_max, pre_avg, post_avg, delta, wait): >>> plt.figure() >>> ax = plt.subplot(2, 1, 2) >>> D = librosa.stft(y) - >>> librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), - ... y_axis='log', x_axis='time') + >>> D_dB = librosa.power_to_db(librosa.magphase(D, power=2)[0], ref=np.max) + >>> librosa.display.specshow(D_dB, y_axis='log', x_axis='time') >>> plt.subplot(2, 1, 1, sharex=ax) >>> plt.plot(times, onset_env, alpha=0.8, label='Onset strength') >>> plt.vlines(times[peaks], 0, @@ -1305,7 +1305,7 @@ def sync(data, idx, aggregate=None, pad=True, axis=-1): >>> y, sr = librosa.load(librosa.util.example_audio_file()) >>> tempo, beats = librosa.beat.beat_track(y=y, sr=sr, trim=False) - >>> cqt = librosa.cqt(y=y, sr=sr) + >>> cqt = librosa.magphase(librosa.cqt(y=y, sr=sr), power=2)[0] >>> beats = librosa.util.fix_frames(beats, x_max=cqt.shape[1]) By default, use mean aggregation @@ -1331,18 +1331,18 @@ def sync(data, idx, aggregate=None, pad=True, axis=-1): >>> subbeat_t = librosa.frames_to_time(sub_beats, sr=sr) >>> plt.figure() >>> plt.subplot(3, 1, 1) - >>> librosa.display.specshow(librosa.amplitude_to_db(cqt, + >>> librosa.display.specshow(librosa.power_to_db(cqt, ... ref=np.max), ... x_axis='time') >>> plt.title('CQT power, shape={}'.format(cqt.shape)) >>> plt.subplot(3, 1, 2) - >>> librosa.display.specshow(librosa.amplitude_to_db(cqt_med, + >>> librosa.display.specshow(librosa.power_to_db(cqt_med, ... ref=np.max), ... x_coords=beat_t, x_axis='time') >>> plt.title('Beat synchronous CQT power, ' ... 'shape={}'.format(cqt_med.shape)) >>> plt.subplot(3, 1, 3) - >>> librosa.display.specshow(librosa.amplitude_to_db(cqt_med_sub, + >>> librosa.display.specshow(librosa.power_to_db(cqt_med_sub, ... ref=np.max), ... x_coords=subbeat_t, x_axis='time') >>> plt.title('Sub-beat synchronous CQT power, '