Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_sharex_waveplot_ms(y, sr, S_abs):
# Correct time range ~= 4.6 s or 4600ms
# Due to shared x_axis, both plots are plotted in 'ms'.
plt.figure(figsize=(8, 8))
ax = plt.subplot(2, 1, 1)
librosa.display.waveplot(y, sr)
plt.xlabel('') # hide the x label here, which is not propagated automatically
plt.subplot(2, 1, 2, sharex=ax)
librosa.display.specshow(librosa.amplitude_to_db(S_abs, ref=np.max), x_axis='ms')
plt.xlabel('') # hide the x label here, which is not propagated automatically
return plt.gcf()
def test_amplitude_to_db():
srand()
NOISE_FLOOR = 1e-6
# Make some noise
x = np.abs(np.random.randn(1000)) + NOISE_FLOOR
db1 = librosa.amplitude_to_db(x, top_db=None)
db2 = librosa.power_to_db(x**2, top_db=None)
assert np.allclose(db1, db2)
def amp2db(amp):
return librosa.amplitude_to_db(amp)
####################################################################
# We can plot the two components along with the original spectrogram
# Pre-compute a global reference power from the input spectrum
rp = np.max(np.abs(D))
plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
librosa.display.specshow(librosa.amplitude_to_db(D, ref=rp), 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')
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')
plt.colorbar()
plt.title('Percussive spectrogram')
plt.tight_layout()
#################################################################################
# The default HPSS above assigns energy to each time-frequency bin according to
# whether a horizontal (harmonic) or vertical (percussive) filter responds higher
# at that position.
#
# This assumes that all energy belongs to either a harmonic or percussive source,
def plot_all(true_spectrogram, ds_spectrogram, reco_spectrogram,
true_waveform, ds_waveform, reco_waveform,
true_sr, ds_sr, reco_sr, ofile, n_fft):
# max_frame = 200
max_frame = 100
plt.figure(figsize=(8, 6))
if not (true_sr == ds_sr == reco_sr):
print('Warning: time axis on waveform plots will be meaningless')
# compute dB-scale magnitudes
true_dB = librosa.amplitude_to_db(true_spectrogram, ref=np.max)
ds_dB = librosa.amplitude_to_db(ds_spectrogram, ref=np.max)
reco_dB = librosa.amplitude_to_db(reco_spectrogram, ref=np.max)
# compute LSD
true_X = np.log10(np.abs(true_spectrogram)**2)
ds_X = np.log10(np.abs(ds_spectrogram)**2)
reco_X = np.log10(np.abs(reco_spectrogram)**2)
ds_X_diff_squared = (true_X - ds_X)**2
reco_X_diff_squared = (true_X - reco_X)**2
ds_lsd = np.mean(np.sqrt(np.mean(ds_X_diff_squared, axis=0)))
reco_lsd = np.mean(np.sqrt(np.mean(reco_X_diff_squared, axis=0)))
# spectrogram plots
# cmap = 'nipy_spectral'
# cmap = 'rainbow_r'
# cmap = 'gist_rainbow'
'''
# Pre-emphasis
y_preem = preemphasis(wav, coeff=preemphasis_coeff)
# Get spectrogram
D = librosa.stft(y=y_preem, n_fft=n_fft,
hop_length=hop_length, win_length=win_length)
mag = np.abs(D)
# Get mel-spectrogram
mel_basis = librosa.filters.mel(
hp.Default.sr, hp.Default.n_fft, hp.Default.n_mels) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, mag) # (n_mels, t) # mel spectrogram
# Get mfccs
db = librosa.amplitude_to_db(mel)
mfccs = np.dot(librosa.filters.dct(hp.Default.n_mfcc, db.shape[0]), db)
# Log
mag = np.log(mag + sys.float_info.epsilon)
mel = np.log(mel + sys.float_info.epsilon)
# Normalization
# self.y_log_spec = (y_log_spec - hp.mean_log_spec) / hp.std_log_spec
# self.y_log_spec = (y_log_spec - hp.min_log_spec) / (hp.max_log_spec - hp.min_log_spec)
return mfccs.T, mag.T, mel.T # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)
def _compute_spectrograms(self):
self.audio_signal.stft_params.window_length = self.metadata['n_fft']
self.audio_signal.stft_params.n_fft_bins = self.metadata['n_fft']
self.audio_signal.stft_params.hop_length = self.metadata['hop_length']
self.stft = self.audio_signal.stft(
overwrite=True,
remove_reflection=True,
use_librosa=self.use_librosa_stft
)
self.log_spectrogram = librosa.amplitude_to_db(
np.abs(self.stft),
ref=np.max
)
'''
# Pre-emphasis
y_preem = preemphasis(wav, coeff=preemphasis_coeff)
# Get spectrogram
D = librosa.stft(y=y_preem, n_fft=n_fft,
hop_length=hop_length, win_length=win_length)
mag = np.abs(D)
# Get mel-spectrogram
mel_basis = librosa.filters.mel(
hp.Default.sr, hp.Default.n_fft, hp.Default.n_mels) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, mag) # (n_mels, t) # mel spectrogram
# Get mfccs
db = librosa.amplitude_to_db(mel)
mfccs = np.dot(librosa.filters.dct(hp.Default.n_mfcc, db.shape[0]), db)
# Log
mag = np.log(mag + sys.float_info.epsilon)
mel = np.log(mel + sys.float_info.epsilon)
# Normalization
# self.y_log_spec = (y_log_spec - hp.mean_log_spec) / hp.std_log_spec
# self.y_log_spec = (y_log_spec - hp.min_log_spec) / (hp.max_log_spec - hp.min_log_spec)
return mfccs.T, mag.T, mel.T # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)
def wav2melspec_db(wav, sr, n_fft, win_length, hop_length, n_mels, normalize=False, max_db=None, min_db=None,
time_first=True, **kwargs):
# Mel-spectrogram
mel_spec = wav2melspec(wav, sr, n_fft, win_length, hop_length, n_mels, time_first=False, **kwargs)
# Decibel
mel_db = librosa.amplitude_to_db(mel_spec)
# Normalization
mel_db = normalize_db(mel_db, max_db, min_db) if normalize else mel_db
# Time-axis first
if time_first:
mel_db = mel_db.T # (t, n_mels)
return mel_db