本文整理汇总了Python中librosa.power_to_db方法的典型用法代码示例。如果您正苦于以下问题:Python librosa.power_to_db方法的具体用法?Python librosa.power_to_db怎么用?Python librosa.power_to_db使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类librosa
的用法示例。
在下文中一共展示了librosa.power_to_db方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extract_logmel
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def extract_logmel(y, sr, size=3):
"""
extract log mel spectrogram feature
:param y: the input signal (audio time series)
:param sr: sample rate of 'y'
:param size: the length (seconds) of random crop from original audio, default as 3 seconds
:return: log-mel spectrogram feature
"""
# normalization
y = y.astype(np.float32)
normalization_factor = 1 / np.max(np.abs(y))
y = y * normalization_factor
# random crop
start = random.randint(0, len(y) - size * sr)
y = y[start: start + size * sr]
# extract log mel spectrogram #####
melspectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024, n_mels=60)
logmelspec = librosa.power_to_db(melspectrogram)
return logmelspec
示例2: extract_mfcc
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def extract_mfcc(y, sr, size=3):
"""
extract MFCC feature
:param y: np.ndarray [shape=(n,)], real-valued the input signal (audio time series)
:param sr: sample rate of 'y'
:param size: the length (seconds) of random crop from original audio, default as 3 seconds
:return: MFCC feature
"""
# normalization
y = y.astype(np.float32)
normalization_factor = 1 / np.max(np.abs(y))
y = y * normalization_factor
# random crop
start = random.randint(0, len(y) - size * sr)
y = y[start: start + size * sr]
# extract log mel spectrogram #####
melspectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024)
mfcc = librosa.feature.mfcc(S=librosa.power_to_db(melspectrogram), n_mfcc=20)
mfcc_delta = librosa.feature.delta(mfcc)
mfcc_delta_delta = librosa.feature.delta(mfcc_delta)
mfcc_comb = np.concatenate([mfcc, mfcc_delta, mfcc_delta_delta], axis=0)
return mfcc_comb
示例3: prepare_testset
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def prepare_testset(dataset_name):
spec_folder=common.SPECTRO_PATH+SPECTRO_FOLDER+"/"
test_folder=common.DATA_DIR+'/spectro_%s_testset/' % dataset_name
if not os.path.exists(test_folder):
os.makedirs(test_folder)
items = open(common.DATASETS_DIR+'/items_index_test_%s.tsv' % dataset_name).read().splitlines()
testset = []
testset_index = []
for t,track_id in enumerate(items):
if MSD:
msd_folder = track_id[2]+"/"+track_id[3]+"/"+track_id[4]+"/"
else:
msd_folder = ""
file = spec_folder+msd_folder+track_id+".pk"
try:
spec = pickle.load(open(file))
spec = librosa.power_to_db(np.abs(spec) ** 2,ref=np.max).T
pickle.dump(spec, open(test_folder+track_id+".pk","wb"))
testset.append(track_id)
testset_index.append(t)
if t%1000==0:
print t
except:
print "no exist", file
示例4: compute_spectrograms
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def compute_spectrograms(filename):
out_rate = 12000
N_FFT = 512
HOP_LEN = 256
frames, rate = librosa.load(filename, sr=out_rate, mono=True)
if len(frames) < out_rate*3:
# if less then 3 second - can't process
raise Exception("Audio duration is too short")
logam = librosa.power_to_db
melgram = librosa.feature.melspectrogram
x = logam(melgram(y=frames, sr=out_rate, hop_length=HOP_LEN,
n_fft=N_FFT, n_mels=N_MEL_BANDS) ** 2,
ref=1.0)
# now going through spectrogram with the stride of the segment duration
for start_idx in range(0, x.shape[1] - SEGMENT_DUR + 1, SEGMENT_DUR):
yield x[:, start_idx:start_idx + SEGMENT_DUR]
示例5: compute
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def compute(self, chunk, sampling_rate, corpus=None, utterance=None):
# Compute mel-spetrogram
power_spec = np.abs(spectral.stft_from_frames(chunk.data.T)) ** 2
mel = np.abs(librosa.feature.melspectrogram(S=power_spec, n_mels=self.n_mels, sr=sampling_rate))
mel_power = librosa.power_to_db(mel)
# Compute onset strengths
oenv = librosa.onset.onset_strength(S=mel_power, center=False)
# Switch dimensions and add dimension to have frames
oenv = oenv.T.reshape(oenv.shape[0], -1)
# Remove context
oenv = oenv[chunk.left_context:oenv.shape[0] - chunk.right_context]
return oenv
示例6: test_compute_online
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def test_compute_online(self):
# Data: 41523 samples, 16 kHz
# yields 40 frames with frame-size 2048 and hop-size 1024
test_file_path = resources.sample_wav_file('wav_1.wav')
y, sr = librosa.load(test_file_path, sr=None)
# EXPECTED
y_pad = np.pad(y, (0, 1024), mode='constant', constant_values=0)
S = np.abs(librosa.stft(y_pad, center=False, n_fft=2048, hop_length=1024)) ** 2
S = librosa.feature.melspectrogram(S=S, n_mels=128, sr=sr)
S = librosa.power_to_db(S)
onsets = librosa.onset.onset_strength(S=S, center=False)
exp_tgram = librosa.feature.tempogram(onset_envelope=onsets, sr=sr, win_length=4, center=True).T
# ACTUAL
test_file = tracks.FileTrack('idx', test_file_path)
tgram_step = pipeline.Tempogram(win_length=4)
tgram_gen = tgram_step.process_track_online(test_file, 2048, 1024, chunk_size=5)
chunks = list(tgram_gen)
tgrams = np.vstack(chunks)
assert np.allclose(tgrams, exp_tgram)
示例7: test_compute_cleanup_after_one_utterance
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def test_compute_cleanup_after_one_utterance(self):
test_file_path = resources.sample_wav_file('wav_1.wav')
y, sr = librosa.load(test_file_path, sr=None)
frames = librosa.util.frame(y, frame_length=2048, hop_length=1024).T
# EXPECTED
S = np.abs(librosa.stft(y, center=False, n_fft=2048, hop_length=1024)) ** 2
S = librosa.feature.melspectrogram(S=S, n_mels=128, sr=sr)
S = librosa.power_to_db(S)
onsets = librosa.onset.onset_strength(S=S, center=False)
exp_tgram = librosa.feature.tempogram(onset_envelope=onsets, sr=sr, win_length=11, center=True).T
# ACTUAL
tgram_step = pipeline.Tempogram(win_length=11)
# FIRST RUN
tgrams = tgram_step.process_frames(frames, sr, last=True)
assert np.allclose(tgrams, exp_tgram)
# SECOND RUN
tgrams = tgram_step.process_frames(frames, sr, last=True)
assert np.allclose(tgrams, exp_tgram)
示例8: test_compute
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def test_compute(self):
test_file_path = resources.sample_wav_file('wav_1.wav')
y, sr = librosa.load(test_file_path, sr=None)
frames = librosa.util.frame(y, frame_length=2048, hop_length=1024).T
# EXPECTED
S = np.abs(librosa.stft(y, center=False, n_fft=2048, hop_length=1024)) ** 2
S = librosa.feature.melspectrogram(S=S, n_mels=128, sr=sr)
S = librosa.power_to_db(S)
exp_onsets = librosa.onset.onset_strength(S=S, center=False).T
exp_onsets = exp_onsets.reshape(exp_onsets.shape[0], 1)
# ACTUAL
onset = pipeline.OnsetStrength()
onsets = onset.process_frames(frames, sr, last=True)
assert np.allclose(onsets, exp_onsets)
示例9: test_compute_online
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def test_compute_online(self):
test_file_path = resources.sample_wav_file('wav_1.wav')
y, sr = librosa.load(test_file_path, sr=None)
# EXPECTED
y_pad = np.pad(y, (0, 1024), mode='constant', constant_values=0)
S = np.abs(librosa.stft(y_pad, center=False, n_fft=2048, hop_length=1024)) ** 2
S = librosa.feature.melspectrogram(S=S, n_mels=128, sr=sr)
S = librosa.power_to_db(S)
exp_onsets = librosa.onset.onset_strength(S=S, center=False).T
exp_onsets = exp_onsets.reshape(exp_onsets.shape[0], 1)
# ACTUAL
test_file = tracks.FileTrack('idx', test_file_path)
onset = pipeline.OnsetStrength()
onset_gen = onset.process_track_online(test_file, 2048, 1024, chunk_size=5)
chunks = list(onset_gen)
onsets = np.vstack(chunks)
print(onsets.shape, exp_onsets.shape)
assert np.allclose(onsets, exp_onsets)
示例10: calc_power_spectrogram
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def calc_power_spectrogram(audio_data, samplerate, n_mels=128, n_fft=512, hop_length=160):
"""
Calculate power spectrogram from the given raw audio data
Args:
audio_data: numpyarray of raw audio wave
samplerate: the sample rate of the `audio_data`
n_mels: the number of mels to generate
n_fft: the window size of the fft
hop_length: the hop length for the window
Returns: the spectrogram in the form [time, n_mels]
"""
spectrogram = librosa.feature.melspectrogram(audio_data, sr=samplerate, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length)
# convert to log scale (dB)
log_spectrogram = librosa.power_to_db(spectrogram, ref=np.max)
# normalize
normalized_spectrogram = normalize(log_spectrogram)
return normalized_spectrogram.T
示例11: wav_to_spec
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def wav_to_spec(wav_audio, hparams):
"""Transforms the contents of a wav file into a series of spectrograms."""
if hparams.spec_type == 'raw':
spec = _wav_to_framed_samples(wav_audio, hparams)
else:
if hparams.spec_type == 'cqt':
spec = _wav_to_cqt(wav_audio, hparams)
elif hparams.spec_type == 'mel':
spec = _wav_to_mel(wav_audio, hparams)
else:
raise ValueError('Invalid spec_type: {}'.format(hparams.spec_type))
if hparams.spec_log_amplitude:
spec = librosa.power_to_db(spec)
return spec
示例12: visualization_tensor_spectrogram
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def visualization_tensor_spectrogram(mel_spectrogram, title):
"""visualizing first one result of SpecAugment
# Arguments:
mel_spectrogram(ndarray): mel_spectrogram to visualize.
title(String): plot figure's title
"""
# session for plotting
sess = tf.InteractiveSession()
mel_spectrogram = mel_spectrogram.eval()
# Show mel-spectrogram using librosa's specshow.
plt.figure(figsize=(10, 4))
librosa.display.specshow(librosa.power_to_db(mel_spectrogram[0, :, :, 0], ref=np.max), y_axis='mel', fmax=8000,
x_axis='time')
# plt.colorbar(format='%+2.0f dB')
plt.title(title)
plt.tight_layout()
plt.show()
示例13: plot_mel_spectrogram
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def plot_mel_spectrogram(audio_data,
sr,
nfft=None,
melbands=64,
delta=None,
**kwargs):
spectrogram = y_limited_spectrogram(audio_data,
sr=sr,
nfft=nfft,
ylim=kwargs['ylim'])
kwargs['scale'] = 'mel'
if delta:
spectrogram = librosa.feature.delta(spectrogram, order=delta)
spectrogram = librosa.feature.melspectrogram(S=np.abs(spectrogram)**2,
sr=sr,
n_mels=melbands)
spectrogram = librosa.power_to_db(spectrogram, ref=np.max, top_db=None)
return _create_plot(spectrogram, sr, nfft, **kwargs)
示例14: audio_to_melspectrogram
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def audio_to_melspectrogram(audio):
spectrogram = librosa.feature.melspectrogram(audio,
sr=config.sampling_rate,
n_mels=config.n_mels,
hop_length=config.hop_length,
n_fft=config.n_fft,
fmin=config.fmin,
fmax=config.fmax)
spectrogram = librosa.power_to_db(spectrogram)
spectrogram = spectrogram.astype(np.float32)
return spectrogram
示例15: compute
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import power_to_db [as 别名]
def compute(self, chunk, sampling_rate, corpus=None, utterance=None):
power_spec = np.abs(stft_from_frames(chunk.data.T)) ** 2
mel = librosa.feature.melspectrogram(S=power_spec, n_mels=self.n_mels, sr=sampling_rate)
mel_power = librosa.power_to_db(mel)
mfcc = librosa.feature.mfcc(S=mel_power, n_mfcc=self.n_mfcc)
return mfcc.T