librosa.onset.onset_detect 函数介绍

1 音符起始点检测介绍

音符起始点检测(onset detection)是音乐信号处理中非常重要的一个算法。节拍和速度(tempo)的检测都会基于音符起始点的检测。

音符起始点就是其字面意思,表示琴键按下的那个时刻。

2 函数介绍

librosa.onset.onset_detect 函数

librosa.onset.onset_detect(*, 
y: Optional[ndarray] = None, 
sr: float = 22050, 
onset_envelope: Optional[ndarray] = None, 
hop_length: int = 512, 
backtrack: bool = False, 
energy: Optional[ndarray] = None, 
units: str = 'frames', 
normalize: bool = True,
**kwargs: Any)→ ndarray

参数介绍:
y:一维音频样本
sr:音频采样率
hop_length:样本分割间隔
units:{‘frames’, ‘samples’, ‘time’} 默认为 ‘frames’
backtrack:是否回溯,backtrack默认为 False,这样会造成,找出起始位置并不是能量开始增加的位置,也即不是真正的起始位置。backtrack为向前追溯找到能量开始增加的点,即真正的起始点

返回:
起始位置序列,默认返回 frame 索引

例子:

import librosa

# 加载内置音频
y, sr = librosa.load(librosa.ex('trumpet'))
# 返回起始点时间点
librosa.onset.onset_detect(y=y, sr=sr, units='time')

Out>>>array([0.06965986, 0.23219955, 0.39473923, 0.60371882, 0.74303855,
       0.92879819, 1.04489796, 1.11455782, 1.41641723, 1.67183673,
       1.88081633, 2.04335601, 2.20589569, 2.36843537, 2.55419501,
       3.0185941 ])
o_env = librosa.onset.onset_strength(y=y, sr=sr)
times = librosa.times_like(o_env, sr=sr)
onset_frames = librosa.onset.onset_detect(onset_envelope=o_env, sr=sr)
import matplotlib.pyplot as plt
import numpy as np
# 短时傅里叶变换
D = np.abs(librosa.stft(y))
fig, ax = plt.subplots(nrows=2, sharex=True)
librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max),
                         x_axis='time', y_axis='log', ax=ax[0])
ax[0].set(title='Power spectrogram')
ax[0].label_outer()
ax[1].plot(times, o_env, label='Onset strength')
# 起始点位置作为竖线画出
ax[1].vlines(times[onset_frames], 0, o_env.max(), color='r', alpha=0.9,
           linestyle='--', label='Onsets')
ax[1].legend()

librosa.onset.onset_detect 函数介绍_第1张图片

参考:

结合Python代码介绍音符起始点检测 (onset detection)
librosa.onset.onset_detect

你可能感兴趣的:(Python,python,开发语言,librosa,onset_detect)