0

我有几个 .wav 声音文件,它们的敲击信号非常相似,持续时间约为 60 毫秒。我可以很好地使用 libROSA 的发病检测来确定它们的发病时间。我现在想使用起始时间从文件中提取约 60 毫秒的相关音频片段。这是我到目前为止所做的:

import librosa
import matplotlib.pyplot as plt
import numpy as np

x, sr = librosa.load("C:/data/test.wav")
onset_frames = librosa.onset.onset_detect(x, sr=sr, wait=1, pre_avg=1, post_avg=1, pre_max=1, 
post_max=1)
print(onset_frames) # frame numbers of estimated onsets

onset_times = librosa.frames_to_time(onset_frames)

o_env = librosa.onset.onset_strength(x, sr=sr)
times = librosa.frames_to_time(np.arange(len(o_env)), sr=sr)
onset_frames = librosa.util.peak_pick(o_env, 10, 10, 10, 10, 2, 60)

D = np.abs(librosa.stft(x))
plt.figure(figsize=(15,10))
ax1 = plt.subplot(2, 1, 1)
librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
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')
plt.vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9, linestyle='--', 
label='Onsets')
plt.axis('tight')
plt.legend(frameon=True, framealpha=0.75)
plt.show()
print(onset_times)

输出功率谱图和起始图

如果我使用以下代码在开始后提取 100ms 段(带回溯),我没有得到正确的段:

 onsets = librosa.onset.onset_detect(x, backtrack = True, units = 'time')
 onsetsnb = librosa.onset.onset_detect(x, units = 'time')
 
 i = 1
 for onset in onsetsnb:
   current_sample, sr = librosa.load("C:/data/test.wav", offset = onset, duration = .100)
   sf.write(f'./clicks/clicks{i}.wav', current_sample, sr)
   i+=1

 print("complete") 

我想知道如何使用“onset_frames”或“onset_times”,可以通过编辑函数“librosa.util.peak_pick”中的峰值拾取参数来提取28个段。有人可以在这里给我一个提示吗?

4

0 回答 0