本文整理汇总了Python中librosa.stft方法的典型用法代码示例。如果您正苦于以下问题:Python librosa.stft方法的具体用法?Python librosa.stft怎么用?Python librosa.stft使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类librosa
的用法示例。
在下文中一共展示了librosa.stft方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_compute
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [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)
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)
tgrams = tgram_step.process_frames(frames, sr, last=True)
assert np.allclose(tgrams, exp_tgram)
示例2: test_compute_online
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [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)
示例3: test_compute_cleanup_after_one_utterance
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [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)
示例4: test_compute
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [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)
示例5: test_compute_online
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [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)
示例6: wav_to_spec_batch
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def wav_to_spec_batch(wavs, n_fft, hop_length = None):
# Short-time Fourier transform (STFT) for wav matrix in batch
# n_fft : int > 0 [scalar] FFT window size.
# hop_length : int > 0 [scalar] number audio of frames between STFT columns. If unspecified, defaults win_length / 4.
assert (wavs.ndim == 2), 'Single wav uses librosa.stft() directly'
stft_matrices = list()
for wav in wavs:
stft_matrix = librosa.stft(wav, n_fft = n_fft, hop_length = hop_length)
stft_matrices.append(stft_matrix)
stft_matrices = np.array(stft_matrices)
return stft_matrices
示例7: get_spectrogram
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def get_spectrogram(y,
n_fft=1024,
win_length_samples=0.04,
hop_length_samples=0.02,
window=scipy.signal.hamming(1024, sym=False),
center=True,
spectrogram_type='magnitude',
params_extract=None):
if spectrogram_type == 'power':
return np.abs(librosa.stft(y + params_extract.get('eps'),
n_fft=n_fft,
win_length=win_length_samples,
hop_length=hop_length_samples,
center=center,
window=window)) ** 2
示例8: get_stft
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def get_stft(y,sr,window_size=None, window_shift=None):
if window_size is None:
n_fft = int(0.025*sr)
else:
n_fft = int(window_size*0.001*sr)
if window_shift is None:
hop_length = int(0.010*sr)
else:
hop_length = int(window_shift*0.001*sr)
stft = np.abs(librosa.stft(y,n_fft=n_fft,hop_length=hop_length)) #comes in complex numbers.. have to take absolute value
stft = np.transpose(stft)
stft -= (np.mean(stft, axis=0) + 1e-8)
return stft
#super experimental. I wanted fundamental frequency but this was easier
开发者ID:a-n-rose,项目名称:Build-CNN-or-LSTM-or-CNNLSTM-with-speech-features,代码行数:18,代码来源:feature_extraction_functions.py
示例9: apply_noise
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def apply_noise(y,sr,wavefile):
#at random apply varying amounts of environment noise
rand_scale = random.choice([0.0,0.25,0.5,0.75])
#rand_scale = 0.75
if rand_scale > 0.0:
total_length = len(y)/sr
y_noise,sr = librosa.load(wavefile,sr=16000)
envnoise_normalized = prep_data_vad_noise.normalize(y_noise)
envnoise_scaled = prep_data_vad_noise.scale_noise(envnoise_normalized,rand_scale)
envnoise_matched = prep_data_vad_noise.match_length(envnoise_scaled,sr,total_length)
if len(envnoise_matched) != len(y):
diff = int(len(y) - len(envnoise_matched))
if diff < 0:
envnoise_matched = envnoise_matched[:diff]
else:
envnoise_matched = np.append(envnoise_matched,np.zeros(diff,))
y += envnoise_matched
return y
#collects the actual features, according to the settings assigned
#such as with noise, voice activity detection/beginning silence removal, etc.
#mfcc, fbank, stft, delta, dom_freq
开发者ID:a-n-rose,项目名称:Build-CNN-or-LSTM-or-CNNLSTM-with-speech-features,代码行数:25,代码来源:feature_extraction_functions.py
示例10: griffin_lim
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def griffin_lim(spectrogram):
'''Applies Griffin-Lim's raw.'''
X_best = copy.deepcopy(spectrogram)
for i in range(hp.n_iter):
X_t = invert_spectrogram(X_best)
est = librosa.stft(X_t, hp.n_fft, hp.hop_length, win_length=hp.win_length)
phase = est / np.maximum(1e-8, np.abs(est))
X_best = spectrogram * phase
X_t = invert_spectrogram(X_best)
y = np.real(X_t)
return y
示例11: _stft
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def _stft(y):
return librosa.stft(y=y, n_fft=hparams.fft_size, hop_length=get_hop_size())
示例12: plot_spectrum
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def plot_spectrum(sound_files, sound_names):
"""plot log power spectrum"""
i = 1
fig = plt.figure(figsize=(20, 64))
for f, n in zip(sound_files, sound_names):
y, sr = librosa.load(os.path.join('./data/esc10/audio/', f))
plt.subplot(10, 1, i)
D = librosa.logamplitude(np.abs(librosa.stft(y)) ** 2, ref_power=np.max)
librosa.display.specshow(D, sr=sr, y_axis='log')
plt.title(n + ' - ' + 'Spectrum')
i += 1
plt.tight_layout(pad=10)
plt.show()
示例13: griffin_lim
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def griffin_lim(spectrogram):
'''Applies Griffin-Lim's raw.
'''
X_best = copy.deepcopy(spectrogram)
for i in range(hp.n_iter):
X_t = invert_spectrogram(X_best)
est = librosa.stft(X_t, hp.n_fft, hp.hop_length, win_length=hp.win_length)
phase = est / np.maximum(1e-8, np.abs(est))
X_best = spectrogram * phase
X_t = invert_spectrogram(X_best)
y = np.real(X_t)
return y
示例14: wavs_to_specs
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def wavs_to_specs(wavs, n_fft = 1024, hop_length = None):
stfts = list()
for wav in wavs:
stft = librosa.stft(wav, n_fft = n_fft, hop_length = hop_length)
stfts.append(stft)
return stfts
示例15: compute_spec
# 需要导入模块: import librosa [as 别名]
# 或者: from librosa import stft [as 别名]
def compute_spec(audio_file,spectro_file):
# Get actual audio
audio, sr = librosa.load(audio_file, sr=config['resample_sr'])
# Compute spectrogram
if config['spectrogram_type']=='cqt':
spec = librosa.cqt(audio, sr=sr, hop_length=config['hop'], n_bins=config['cqt_bins'])
elif config['spectrogram_type']=='mel':
spec = librosa.feature.melspectrogram(y=audio, sr=sr, hop_length=config['hop'],n_fft=config['n_fft'],n_mels=config['n_mels'])
elif config['spectrogram_type']=='stft':
spec = librosa.stft(y=audio,n_fft=config['n_fft'])
# Write results:
with open(spectro_file, "w") as f:
pickle.dump(spec, f, protocol=-1) # spec shape: MxN.