文章目录
- 一、时域处理
- 1.1 librosa.autocorrelate
- 1.1.1 语法与参数
- 1.1.2 例子
- 1.1.2.1 计算完全自相关y
- 1.1.2.2 计算长达 4 秒的起始强度自相关
一、时域处理
1.1 librosa.autocorrelate
https://librosa.org/doc/latest/generated/librosa.autocorrelate.html
1.1.1 语法与参数
librosa.autocorrelate(y, *, max_size=None, axis=- 1)[source]
有界滞后自相关
>参数
y:np.ndarray
数组自相关
max_size:int > 0 or None
最大相关滞后。如果未指定,则默认为y.shape[axis](无界)
axis:int
自相关所沿的轴。默认情况下,采用最后一个轴 (-1)。
>返回值
z:np.ndarray
y*y沿指定轴截断的自相关。如果max_size指定,则z.shape[axis]有界到max_size。
Notes
此函数缓存在 20 级。
1.1.2 例子
1.1.2.1 计算完全自相关y
# 计算完全自相关y
import librosa
y, sr = librosa.load(librosa.ex('trumpet'))
print(librosa.autocorrelate(y))
"""
[ 6.89931420e+02 6.23597636e+02 4.48787565e+02 ... -2.87956876e-09
3.70971744e-08 -1.79581785e-08]
"""
1.1.2.2 计算长达 4 秒的起始强度自相关
# 计算完全自相关y
import librosa
import matplotlib.pyplot as plt
y, sr = librosa.load(librosa.ex('trumpet'))
print(sr) # 22050
print(y)
"""
[-1.4068224e-03 -4.4607223e-04 -4.1098078e-04 ... 7.9623060e-06
-3.0417003e-05 1.2765067e-05]
"""
odf = librosa.onset.onset_strength(y=y, sr=sr, hop_length=512)
print(odf)
"""
[0.00000000e+00 0.00000000e+00 0.00000000e+00 4.17845964e+00
1.13509560e+00 1.82122588e-01 1.72356606e-01 1.18283510e-01
9.44824517e-01 3.77103591e+00 6.58177662e+00 8.25611687e+00
3.08506632e+00 3.13406616e-01 3.21937829e-01 6.42343104e-01
9.93093491e-01 4.78707838e+00 3.84358883e+00 1.58594739e+00
8.31244528e-01 6.17250919e-01 6.10312700e-01 2.01938367e+00
3.42684698e+00 1.95094514e+00 7.68183517e+00 6.41882753e+00
1.50753009e+00 4.19695750e-02 2.26590529e-01 2.75454521e+00
4.37553072e+00 2.39662790e+00 8.77303123e-01 5.22520900e-01
4.79927957e-01 9.28298354e-01 2.72709799e+00 3.76103878e+00
7.26637745e+00 5.59427738e+00 1.51596546e+00 7.97890782e-01
2.35249877e+00 5.24696827e+00 1.06628597e+00 5.39485574e-01
7.38652992e+00 1.74603093e+00 1.20418048e+00 1.48233950e+00
7.93847799e-01 5.63979506e-01 1.04662836e+00 1.12031078e+00
1.52801478e+00 2.27576208e+00 1.67852926e+00 5.42153120e-01
4.30115104e-01 1.14976578e+01 7.16263056e+00 1.58617485e+00
8.34993541e-01 1.45350015e+00 1.20756626e+00 2.30439115e+00
2.23954248e+00 1.16052115e+00 4.63239580e-01 1.82088804e+00
4.34435797e+00 1.93747187e+00 6.00777268e-01 1.12067342e+00
1.01238251e+00 9.11972642e-01 5.94635606e-01 4.24022168e-01
6.86975598e-01 2.40581989e+00 1.97992146e+00 2.59550065e-01
1.47426531e-01 2.51426369e-01 5.56935489e-01 9.42625761e-01
1.73099213e+01 9.25381374e+00 1.64803064e+00 3.17822486e-01
8.00392628e-02 1.61836475e-01 8.90664399e-01 4.78239107e+00
3.45063853e+00 1.85295865e-01 0.00000000e+00 2.01156676e-01
3.59036386e-01 1.59203696e+00 1.06260805e+01 6.80992413e+00
2.17226887e+00 1.31850600e+00 1.51034641e+00 1.46862054e+00
5.56796491e-01 2.81662226e+00 7.62025452e+00 4.23897266e+00
2.00937366e+00 7.33176589e-01 1.18238747e-01 3.85864466e-01
5.32984078e-01 6.84280992e-01 1.07605183e+00 5.22916436e-01
4.39226091e-01 4.45047468e-01 6.01421237e-01 6.27728999e-01
1.11530638e+00 1.18479598e+00 6.13896489e-01 4.82841969e-01
3.52134496e-01 1.86522460e+00 3.70014620e+00 2.87164974e+00
5.38166285e-01 6.92774355e-02 7.51249552e-01 7.63877273e-01
3.22526246e-01 3.95352423e-01 3.15897495e-01 4.12356138e-01
6.48618221e-01 3.21835697e-01 5.21747947e-01 4.26418334e-01
3.89231294e-01 3.27717274e-01 2.83269316e-01 2.52752095e-01
3.10896307e-01 2.42993012e-01 3.30240250e-01 1.92404851e-01
2.78108686e-01 3.10559481e-01 1.96977884e-01 2.10453570e-01
2.03978002e-01 7.99052417e-02 1.41900450e-01 2.24013209e-01
8.00894499e-02 5.86625934e-02 1.05982393e-01 1.56429648e-01
3.60565186e-02 3.05968225e-02 1.74235672e-01 1.86321586e-01
7.59349167e-02 2.53845155e-02 5.09086251e-03 1.57953560e-01
8.69446397e-02 5.50147891e-03 2.30761170e-02 1.36366785e-02
1.11565441e-01 9.87712741e-02 6.89095557e-02 5.31762838e-02
4.49128151e-02 8.09687376e-03 5.56744933e-02 1.94908381e-02
4.04570401e-02 3.24057639e-02 5.90607524e-03 1.78426504e-03
3.76984477e-03 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00
0.00000000e+00 0.00000000e+00]
"""
ac = librosa.autocorrelate(odf, max_size=4*sr//512)
print(ac)
"""
[1.53906712e+03 9.72211277e+02 4.92505001e+02 4.03518881e+02
4.01009760e+02 4.55849992e+02 6.30879802e+02 8.08636093e+02
7.22594166e+02 5.15360410e+02 4.17751475e+02 3.72633792e+02
4.01515542e+02 6.41693803e+02 8.10333052e+02 7.04471193e+02
6.59103241e+02 5.47369439e+02 4.10996238e+02 3.97787237e+02
5.36795931e+02 7.27394773e+02 7.21893275e+02 5.89398316e+02
4.69588717e+02 3.46915001e+02 4.22965487e+02 6.33560203e+02
5.72802961e+02 5.29171844e+02 5.35185634e+02 4.66096728e+02
3.67880019e+02 3.91257789e+02 5.09466514e+02 5.06506079e+02
4.17350796e+02 4.41280018e+02 4.14528568e+02 3.79579254e+02
5.31012676e+02 5.72173825e+02 4.65350621e+02 4.35922076e+02
4.17520243e+02 3.90662502e+02 3.46334063e+02 4.09263730e+02
5.00527804e+02 4.92848446e+02 4.67597709e+02 4.00200707e+02
3.07747809e+02 2.75222936e+02 3.58832558e+02 3.74153283e+02
3.67172822e+02 3.77770927e+02 3.38165891e+02 2.38069584e+02
2.31962478e+02 4.12046273e+02 5.62207889e+02 4.43822348e+02
3.73691208e+02 3.01545109e+02 2.12601367e+02 1.93659925e+02
2.61277135e+02 3.66016253e+02 4.15180653e+02 3.85427956e+02
2.70161026e+02 1.82499103e+02 1.58810504e+02 2.05499836e+02
3.04012456e+02 4.10349950e+02 4.47543010e+02 3.54504355e+02
2.11730394e+02 1.47646496e+02 1.59501251e+02 2.26138506e+02
3.11921796e+02 3.78457948e+02 2.75469899e+02 1.61481591e+02
1.16316245e+02 1.30806374e+02 1.77704839e+02 2.56155174e+02
2.94401117e+02 2.45425704e+02 1.65182036e+02 1.20023037e+02
1.02225389e+02 1.10797814e+02 1.61196699e+02 2.27559806e+02
1.95140666e+02 1.42654793e+02 1.12380182e+02 1.07640898e+02
1.06058834e+02 9.19490257e+01 9.56289088e+01 1.12639943e+02
8.85596608e+01 6.53390897e+01 5.73982420e+01 6.01536160e+01
7.10954285e+01 8.00590962e+01 7.28558421e+01 5.90015632e+01
4.90849105e+01 4.82818836e+01 6.28295448e+01 8.09906405e+01
8.24768210e+01 6.68630668e+01 4.81898681e+01 3.86925437e+01
3.48336984e+01 3.36460767e+01 4.08995839e+01 4.44533933e+01
3.68989018e+01 2.63342097e+01 2.36941134e+01 2.67743027e+01
2.52934559e+01 2.13689544e+01 1.96097660e+01 1.88746848e+01
1.86427164e+01 1.75929579e+01 1.51959437e+01 1.65010795e+01
1.54526910e+01 1.42109166e+01 1.28271039e+01 1.13763007e+01
1.14906392e+01 1.12357692e+01 8.63172385e+00 7.85063634e+00
7.74348190e+00 8.06154820e+00 7.65753230e+00 6.33001453e+00
6.18590446e+00 5.56364664e+00 4.72584960e+00 5.08954996e+00
5.58797583e+00 4.37632184e+00 3.64204534e+00 3.51674417e+00
3.85626082e+00 3.15784639e+00 2.47818328e+00 2.28884723e+00
2.08978321e+00 2.31281267e+00 2.39195938e+00 2.43994969e+00
2.53125267e+00 1.61125242e+00 9.51273640e-01 9.96806807e-01]
进程已结束,退出代码0
"""
fig, ax = plt.subplots()
ax.plot(ac)
ax.set(title='Auto-correlation', xlabel='Lag(frames)')
plt.show()