本文主要是介绍【Python百日进阶-Web开发-音频】Day708 - 时域处理 librosa.lpc,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、时域处理
- 1.1 librosa.lpc
- 1.1.1 语法与参数
- 1.1.2 示例
- 1.1.2.1 在整个系列上以 16 阶计算 y 的 LP 系数
- 1.1.2.2 计算 LP 系数,并绘制原始序列的 LP 估计值
一、时域处理
1.1 librosa.lpc
https://librosa.org/doc/latest/generated/librosa.lpc.html
1.1.1 语法与参数
librosa.lpc(y, *, order, axis=- 1)[source]
Burg 方法的线性预测系数
该函数应用 Burg 的方法来估计线性滤波器y的系数order。Burg 方法是 Yule-Walker 方法的扩展,这两种方法有时都称为通过自相关进行的 LPC 参数估计。
它遵循 Marple 在介绍中描述的描述和实现方法。NB 本文描述了一种不同的方法,此处未实施,但之所以选择该方法,是因为它在介绍中清楚地解释了 Burg 的技术。
拉里·马普尔。一种新的自回归谱分析算法。IEEE Transactions on Acoustics, Speech, and Signal Processing vol 28, no. 4,1980 年。
>参数y:np.ndarray [shape=(…, n)]适合的时间序列。支持多通道..order:int > 0线性滤波器的阶axis:int计算系数的轴>返回值anp.ndarray [shape=(…, order + 1)]LP预测误差系数,即滤波器分母多项式。请注意,沿指定的长度axis将是order+1。>RaisesParameterError 参数错误如果y不是有效的音频librosa.util.valid_audioif or not 整数order < 1FloatingPointError 浮点错误如果y是病态的
1.1.2 示例
1.1.2.1 在整个系列上以 16 阶计算 y 的 LP 系数
import librosa
import matplotlib.pyplot as plty, sr = librosa.load(librosa.ex('libri1'))
# Downloading file '5703-47212-0000.ogg' from 'https://librosa.org/data/audio/5703-47212-0000.ogg' to 'C:\Users\Administrator\AppData\Local\librosa\librosa\Cache'.
print(sr) # 22050
print(y)
"""
[0.00037424 0.00036333 0.00024905 ... 0.00101743 0.0023917 0.00200722]
"""
print(y.shape)
"""(327222,)"""
print(librosa.lpc(y, order=16))
"""
[ 1.0000000e+00 2.1623010e+02 2.4168977e+03 -1.4709037e+044.3119559e+04 -8.9374883e+04 1.4575309e+05 -1.9689898e+052.2603498e+05 -2.2253350e+05 1.8789294e+05 -1.3422914e+057.8647133e+04 -3.5680410e+04 1.0686700e+04 -1.3110452e+03-9.8074751e+00]
"""
print(librosa.lpc(y, order=16).shape)
"""(17,)"""
1.1.2.2 计算 LP 系数,并绘制原始序列的 LP 估计值
import librosa
import scipy
import numpy as np
import matplotlib.pyplot as plty, sr = librosa.load(librosa.ex('libri1'), duration=0.020)
# Downloading file '5703-47212-0000.ogg' from 'https://librosa.org/data/audio/5703-47212-0000.ogg' to 'C:\Users\Administrator\AppData\Local\librosa\librosa\Cache'.
print(sr) # 22050
print(y)
"""
[ 3.74241004e-04 3.63326573e-04 2.49054952e-04 2.00918788e-041.37889292e-04 1.45363854e-04 1.64773126e-04 1.44854566e-048.11248246e-05 1.45325233e-04 1.61147531e-04 8.44563183e-051.06217769e-04 2.14754604e-04 2.60770466e-04 2.09023739e-042.17636290e-04 2.16922228e-04 2.51500285e-04 2.52697617e-043.40511993e-04 4.06945765e-04 4.40902222e-04 4.00981953e-044.33149398e-04 4.93944972e-04 5.28876786e-04 5.71412791e-045.83402929e-04 5.51324105e-04 5.36435342e-04 6.53159048e-046.43022766e-04 5.53502119e-04 5.82284993e-04 7.19504664e-046.47331763e-04 5.02119714e-04 4.79943701e-04 5.23787690e-045.37002459e-04 5.05408272e-04 4.76755260e-04 4.27798484e-043.82401457e-04 3.15428391e-04 2.95730948e-04 2.82254070e-042.79527187e-04 2.88271054e-04 2.59202585e-04 2.16831162e-042.21402937e-04 2.74864666e-04 2.80846347e-04 3.13285709e-043.13992146e-04 3.66888940e-04 3.59804282e-04 2.63114925e-042.18466361e-04 3.15178477e-04 3.74660885e-04 3.19922634e-043.72263778e-04 3.58520454e-04 3.70525551e-04 4.36630624e-044.45862679e-04 3.50899820e-04 4.06820531e-04 4.73972526e-044.92125750e-04 4.54730354e-04 3.86143394e-04 4.12736088e-044.97016183e-04 4.14129638e-04 2.80787761e-04 3.62383173e-043.51943891e-04 2.69913697e-04 3.07713373e-04 4.19746240e-044.02662641e-04 3.85927357e-04 3.40315106e-04 3.06073605e-042.47361400e-04 2.57594307e-04 2.92757293e-04 2.40874404e-042.70390359e-04 3.90405417e-04 4.12237801e-04 2.25744094e-041.58009032e-04 1.84657925e-04 1.77676717e-04 1.95829329e-043.26295529e-04 3.87715758e-04 3.96582589e-04 3.04580957e-042.05543140e-04 1.48147185e-04 1.87027108e-04 1.77614376e-042.45546893e-04 2.62790767e-04 2.25741926e-04 1.86788588e-041.85506942e-04 1.65438891e-04 9.87605308e-05 1.42741861e-041.63947960e-04 1.98309033e-04 9.96296876e-05 5.45161311e-051.34513830e-05 2.36715205e-05 -4.80770323e-05 -1.73432127e-05-2.03911623e-06 7.66380617e-05 9.54798088e-05 1.21854471e-041.44483696e-04 1.17219446e-04 1.03793267e-04 1.30333108e-049.93434514e-05 9.50878093e-05 2.11334380e-04 3.26510868e-042.84571754e-04 1.90839986e-04 2.04732365e-04 2.43039802e-042.27008015e-04 2.41896443e-04 2.51963647e-04 2.75728758e-042.67904485e-04 2.23343857e-04 1.76891815e-04 1.44003119e-041.63609482e-04 1.38935939e-04 1.18333533e-04 1.12699825e-049.91054476e-05 5.42084890e-05 -1.91804284e-05 -3.10450559e-068.15462408e-05 1.42159755e-04 1.62784636e-05 -1.33475784e-04-1.65147576e-04 -9.31437826e-05 -8.30128556e-06 -7.31406908e-063.85753810e-06 -1.33903595e-05 4.54643450e-05 -1.10429028e-05-2.46078125e-05 7.49093742e-05 1.32239307e-04 1.46154140e-041.57430055e-04 1.68582861e-04 2.36734370e-04 2.11797364e-041.88606064e-04 2.17038963e-04 2.40809080e-04 3.23942222e-044.16389492e-04 3.34435928e-04 2.34914216e-04 2.95801787e-042.94819183e-04 2.12682644e-04 2.60467234e-04 3.37957725e-043.00610176e-04 2.82293127e-04 2.74660299e-04 1.94515145e-042.20349655e-04 2.40593421e-04 2.92778102e-04 1.88134829e-042.37444183e-04 3.40740953e-04 2.82818626e-04 3.09329393e-043.41376319e-04 4.44998004e-04 4.65953199e-04 5.30930120e-045.29128243e-04 5.58425789e-04 5.54715283e-04 5.21164096e-045.70738746e-04 6.71095797e-04 6.29816088e-04 5.65187016e-046.13344950e-04 6.85927516e-04 6.88585744e-04 6.33260002e-046.50124392e-04 6.08350732e-04 5.54182741e-04 5.33508719e-046.07319991e-04 6.62888575e-04 6.13867014e-04 5.27661585e-044.57838032e-04 4.01673140e-04 3.71595583e-04 3.91147973e-044.63141012e-04 5.70156903e-04 6.13787328e-04 6.27317873e-046.58174627e-04 6.05559675e-04 6.48640795e-04 6.15973142e-047.16390728e-04 7.14116672e-04 7.89883605e-04 8.09062389e-047.99874775e-04 7.71570543e-04 7.32545566e-04 7.68040714e-047.52743625e-04 7.67628953e-04 8.31097306e-04 8.78411636e-049.32153431e-04 9.40081431e-04 8.91754113e-04 8.47310643e-048.39371525e-04 8.89794610e-04 9.00006620e-04 8.13433027e-047.66678189e-04 7.87897559e-04 7.48607505e-04 6.98024232e-046.39678619e-04 6.48814777e-04 6.77619071e-04 5.54253522e-044.83181997e-04 5.62100555e-04 5.91860502e-04 5.56626415e-044.78705566e-04 4.61247284e-04 4.04335384e-04 4.63164033e-045.29729994e-04 6.81568112e-04 6.86341373e-04 5.84251713e-045.28333127e-04 4.79577924e-04 3.54282733e-04 4.56227805e-046.12638541e-04 6.24920765e-04 6.45749969e-04 6.71656511e-046.50883652e-04 5.91458171e-04 5.26275428e-04 4.88127349e-044.82213683e-04 5.26737131e-04 5.46338793e-04 5.76737337e-045.55883977e-04 4.51062398e-04 3.67945235e-04 4.13711881e-043.61570826e-04 3.65244923e-04 3.61629354e-04 3.69159912e-043.35314107e-04 3.09350929e-04 3.38723767e-04 2.78174004e-041.37493378e-04 1.13665199e-04 1.86327947e-04 1.73253968e-041.00370715e-04 2.04288212e-04 2.32314575e-04 1.63529505e-042.29475772e-04 2.52356724e-04 2.28857709e-04 1.18640397e-042.14267013e-04 1.70984800e-04 1.20211480e-04 -1.68519546e-055.56984669e-05 1.19950942e-04 1.06331303e-04 1.65614940e-041.16886556e-04 6.77938515e-05 -4.55389272e-05 -2.77533436e-052.30154328e-05 5.70320080e-05 -2.04365551e-05 -7.23802659e-05-6.58360805e-05 -1.16969466e-04 -2.66023271e-04 -2.22122108e-04-1.17165320e-04 -1.50838634e-04 -1.32887420e-04 -9.54821444e-05-1.93932938e-04 -3.11665761e-04 -2.80224194e-04 -2.11845429e-04-8.19636989e-05 -2.00514678e-05 -2.33755454e-05 -2.07523757e-04-2.96963844e-04 -2.75999686e-04 -2.94202560e-04 -2.19997542e-04-2.26255172e-04 -1.34575559e-04 -1.92868552e-04 -1.55517861e-04-2.17837718e-04 -2.86579190e-04 -1.60139243e-04 -1.48202831e-04-1.34904549e-04 -9.81480262e-05 -9.85823135e-06 3.64539010e-053.81818390e-05 -6.58452918e-05 -1.39175507e-04 -1.68178842e-04-8.68184579e-05 -2.24133764e-05 -1.82860240e-05 -1.02453661e-04-7.32126791e-05 -1.20640419e-04 -1.74206216e-04 -1.69955718e-04-1.76475645e-04 -1.99713235e-04 -1.92188556e-04 -1.37191295e-04-8.64780450e-05 -1.11011432e-05 -8.28397460e-05 -1.09937733e-04-9.83901846e-05 -9.36103534e-05 -3.46199813e-05 -6.95557974e-05-5.28098499e-05 -6.72796305e-05 3.59878977e-05 -1.40018074e-05-9.10120143e-05 -5.76564162e-05 4.28403218e-05 1.30244705e-041.41164608e-04 2.15456559e-04 2.18956673e-04 1.13575326e-046.41610604e-05 -1.08732629e-05 1.64197172e-05 5.50460572e-057.07402796e-05 -2.53228955e-05 -3.14787139e-05 4.46344129e-059.72716298e-05 1.41922370e-04 1.16331852e-04 6.42958839e-054.18882337e-05 1.36276998e-04 9.83575883e-05 8.44348760e-056.28674388e-05 -3.39997860e-06 -7.80903065e-05 -4.16404037e-053.31525371e-05 4.09631793e-05 -4.33711903e-05 7.06169203e-065.20047470e-05 4.35000693e-05 -1.02567210e-05 -1.99978949e-05-4.44903817e-05 -4.49519284e-05 -5.27158772e-05 4.02046935e-059.80040713e-05 8.30742720e-05 7.70356928e-05 1.66900412e-041.77449605e-04 7.24201600e-05 1.05944717e-04 1.28038169e-041.03827952e-04]
"""
print(y.shape)
"""(441,)"""a = librosa.lpc(y, order=2)
print(a)
"""
[ 1. -1.1578176 0.17331728]
"""
print(a.shape)
"""(3,)"""b = np.hstack([[0], -1*a[1:]])
print(b)
"""
[ 0. 1.1578176 -0.17331728]
"""
print(b.shape)
"""(3,)"""y_hat = scipy.signal.lfilter(b, [1], y)
print(y_hat)
"""
[ 0.00000000e+00 4.33302822e-04 3.55803468e-04 2.25389433e-041.89461782e-04 1.24827951e-04 1.44406232e-04 1.65583157e-041.39157136e-04 6.88219500e-05 1.54199779e-04 1.61392073e-046.98553597e-05 1.08343063e-04 2.30237285e-04 2.64703952e-041.96815336e-04 2.15755701e-04 2.13436243e-04 2.53595086e-042.48988403e-04 3.50453915e-04 4.12152356e-04 4.39953619e-043.87847988e-04 4.32010895e-04 4.96825906e-04 5.26733652e-045.69928300e-04 5.76438468e-04 5.37218942e-04 5.25540285e-046.63265527e-04 6.31299325e-04 5.29407537e-04 5.78248330e-047.32135111e-04 6.24789515e-04 4.69169261e-04 4.68661240e-045.23268069e-04 5.30969440e-04 4.92098786e-04 4.64399643e-044.12682689e-04 3.68606266e-04 2.98931762e-04 2.87733305e-042.75543446e-04 2.74721988e-04 2.85318408e-04 2.50146959e-042.06126648e-04 2.18763630e-04 2.79870193e-04 2.77530047e-043.14052183e-04 3.09247806e-04 3.70370207e-04 3.52999537e-042.42278791e-04 2.07341834e-04 3.27055192e-04 3.79163091e-043.05476850e-04 3.75565432e-04 3.50581546e-04 3.66863214e-044.41320140e-04 4.40552024e-04 3.29002280e-04 4.10206968e-044.78264705e-04 4.87644225e-04 4.41200910e-04 3.68270989e-044.10947784e-04 5.03919787e-04 3.93345089e-04 2.53325188e-043.70908244e-04 3.44679564e-04 2.51512870e-04 3.09495251e-044.32657540e-04 3.93460616e-04 3.77045091e-04 3.27134939e-042.95394918e-04 2.33351538e-04 2.55375217e-04 2.94314002e-042.28148726e-04 2.71315020e-04 4.05154942e-04 4.09632176e-041.89922550e-04 1.43820285e-04 1.86414500e-04 1.73712820e-041.95940198e-04 3.43850099e-04 3.92351474e-04 3.91972460e-042.83914577e-04 1.85192321e-04 1.35903239e-04 1.90866810e-041.73230021e-04 2.53514874e-04 2.61706255e-04 2.15821793e-041.77142137e-04 1.82409512e-04 1.59396500e-04 8.56732619e-051.48152132e-04 1.65082202e-04 2.01190674e-04 8.09826231e-054.58521893e-05 6.12566026e-06 2.50759459e-05 -5.97671178e-05-1.17476963e-05 6.44953848e-07 8.90863109e-05 9.72655026e-051.24536951e-04 1.46166281e-04 1.10677216e-04 9.98575156e-051.32912800e-04 9.24326165e-05 9.28764022e-05 2.28206304e-043.41412130e-04 2.72892209e-04 1.71636692e-04 2.03966868e-042.45912103e-04 2.20710877e-04 2.40727547e-04 2.49803112e-042.75573955e-04 2.62395969e-04 2.12158972e-04 1.66099107e-041.36070937e-04 1.64471709e-04 1.32506124e-04 1.12928649e-041.09976595e-04 9.52132042e-05 4.55868558e-05 -3.16027056e-05-2.70151487e-07 9.49537375e-05 1.50461694e-04 -5.79125092e-06-1.57361951e-04 -1.68077110e-04 -7.92205818e-05 6.53205282e-06-7.02960166e-06 5.73398010e-06 -1.61721720e-05 5.49601997e-05-2.06654240e-05 -2.65774326e-05 9.09963513e-05 1.40125908e-041.46300478e-04 1.56944250e-04 1.67902855e-04 2.44876897e-042.04192558e-04 1.81663277e-04 2.18602842e-04 2.41196389e-043.33329631e-04 4.25958297e-04 3.15048309e-04 2.14024288e-043.01769822e-04 2.90079278e-04 1.95150449e-04 2.64711970e-043.46149929e-04 2.89477838e-04 2.74743013e-04 2.69080250e-041.77609682e-04 2.21411873e-04 2.40372894e-04 2.97284642e-041.67082311e-04 2.42310037e-04 3.53362693e-04 2.68396087e-043.09129660e-04 3.41639381e-04 4.56060106e-04 4.62362970e-045.33962495e-04 5.20614628e-04 5.54848138e-04 5.45474278e-045.07271219e-04 5.70484621e-04 6.78087637e-04 6.12899652e-045.45225462e-04 6.12184901e-04 6.87875672e-04 6.78373601e-046.13855767e-04 6.42970561e-04 5.91681393e-04 5.36204836e-045.21656338e-04 6.10699494e-04 6.62245009e-04 5.95855987e-045.04542108e-04 4.38640060e-04 3.85712988e-04 3.60623009e-043.88474071e-04 4.68440112e-04 5.79867356e-04 6.11835727e-046.19939724e-04 6.53321139e-04 5.87054612e-04 6.46053772e-046.00763886e-04 7.22691004e-04 7.02653957e-04 7.90772380e-047.99846194e-04 7.85884599e-04 7.54705833e-04 7.14427641e-047.62288251e-04 7.38425088e-04 7.58310834e-04 8.29215725e-048.72996927e-04 9.27019732e-04 9.26884527e-04 8.69556249e-048.26474776e-04 8.24985548e-04 8.84742269e-04 8.87826722e-047.85820375e-04 7.46691500e-04 7.79363082e-04 7.30194681e-046.78438124e-04 6.19651501e-04 6.40341809e-04 6.72108474e-045.24281387e-04 4.63374906e-04 5.67066125e-04 5.87844766e-045.41892207e-04 4.57780752e-04 4.51072276e-04 3.88204498e-044.66181160e-04 5.33056379e-04 6.97320193e-04 6.76530589e-045.57502095e-04 5.10452474e-04 4.63694500e-04 3.27075641e-044.66825262e-04 6.30251523e-04 6.17363414e-04 6.39351112e-046.65736101e-04 6.37194868e-04 5.71991295e-04 5.06821030e-044.73949809e-04 4.73714585e-04 5.26289556e-04 5.41268023e-045.73066685e-04 5.43653704e-04 4.25903684e-04 3.47836560e-044.15231630e-04 3.46929648e-04 3.60220528e-04 3.55397574e-043.64743227e-04 3.24250783e-04 3.00056221e-04 3.38564477e-042.63368075e-04 1.10979891e-04 1.07773589e-04 1.96033633e-041.68302640e-04 8.61830733e-05 2.19132508e-04 2.33571226e-041.49073208e-04 2.37348599e-04 2.52410939e-04 2.21237702e-049.76989436e-05 2.27519688e-04 1.60833035e-04 1.09548346e-04-4.03462168e-05 6.74094004e-05 1.29227806e-04 1.02322683e-041.73322840e-04 1.06629381e-04 5.82344541e-05 -6.44756177e-05-2.42406266e-05 3.14578074e-05 6.20436905e-05 -3.35464360e-05-8.02611377e-05 -6.36814218e-05 -1.24018776e-04 -2.87733596e-04-2.11070455e-04 -9.71584699e-05 -1.54336851e-04 -1.27716452e-04-8.75192208e-05 -2.07990263e-04 -3.27240174e-04 -2.70431442e-04-1.96710670e-04 -5.81825392e-05 -9.01021674e-06 -2.35893520e-05-2.36223273e-04 -3.07862512e-04 -2.68088328e-04 -2.92797387e-04-2.03726639e-04 -2.23832844e-04 -1.16600019e-04 -1.99982335e-04-1.46633863e-04 -2.25262411e-04 -2.94051389e-04 -1.35742908e-04-1.43836948e-04 -1.30508749e-04 -9.02562224e-05 5.59671548e-064.39155701e-05 3.78895142e-05 -8.28544105e-05 -1.49727724e-04-1.70598902e-04 -7.13716388e-05 -1.09034625e-05 -1.72872549e-05-1.15453369e-04 -6.70099383e-05 -1.26990579e-04 -1.80789953e-04-1.66584774e-04 -1.74870345e-04 -2.00645219e-04 -1.87905538e-04-1.25532898e-04 -7.63481801e-05 2.13504086e-06 -9.39892961e-05-1.12930283e-04 -9.48637783e-05 -9.13309954e-05 -2.38593316e-05-7.45326854e-05 -4.90889519e-05 -6.87446807e-05 5.33281442e-05-2.24488637e-05 -1.02948557e-04 -5.09816584e-05 5.95941321e-051.43374644e-04 1.40869210e-04 2.24993130e-04 2.16169545e-049.35505363e-05 5.46022381e-05 -2.37094759e-05 2.08955620e-056.08874732e-05 7.23639078e-05 -4.15798072e-05 -3.20577136e-055.71343141e-05 1.04886890e-04 1.47461363e-04 1.10093466e-045.42805856e-05 3.73553463e-05 1.50523952e-04 9.02609879e-058.07131157e-05 5.81550039e-05 -1.48325688e-05 -8.98250564e-05-3.46775925e-05 4.56015926e-05 4.16819824e-05 -5.73155545e-051.56931282e-05 5.89880982e-05 4.13518245e-05 -1.94147259e-05-2.13762477e-05 -4.80457662e-05 -4.43351819e-05 -5.32444245e-055.56862745e-05 1.06502671e-04 7.91990550e-05 7.47950740e-051.79888618e-04 1.76527550e-04 5.30942525e-05 1.10112993e-041.29882796e-04]
"""
print(y_hat.shape)
"""(441,)"""fig, ax = plt.subplots()
ax.plot(y)
ax.plot(y_hat, linestyle='--')
ax.legend(['y', 'y_hat'])
ax.set_title('LP Model Forward Prediction')
plt.show()
这篇关于【Python百日进阶-Web开发-音频】Day708 - 时域处理 librosa.lpc的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!