Skip to content

Commit

Permalink
Merge 61eb016 into abc5ce7
Browse files Browse the repository at this point in the history
  • Loading branch information
lostanlen committed Jun 28, 2018
2 parents abc5ce7 + 61eb016 commit 12decdb
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 78 deletions.
38 changes: 27 additions & 11 deletions docs/examples/plot_hprss.py
Expand Up @@ -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)


####################################################################
Expand All @@ -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()
Expand All @@ -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)


#############################################################################
Expand All @@ -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)
Expand Down
16 changes: 6 additions & 10 deletions docs/examples/plot_presets.py
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
19 changes: 10 additions & 9 deletions docs/examples/plot_segmentation.py
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 7 additions & 3 deletions docs/examples/plot_superflux.py
Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 2 additions & 1 deletion examples/LibROSA audio effects and playback.ipynb
Expand Up @@ -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",
Expand Down
13 changes: 8 additions & 5 deletions examples/LibROSA demo.ipynb
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion librosa/core/audio.py
Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion librosa/core/constantq.py
Expand Up @@ -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')
Expand Down

0 comments on commit 12decdb

Please sign in to comment.