【Python百日进阶-Web开发-音频】Day708 - 时域处理 librosa.lpc

一、时域处理

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。

>Raises
	ParameterError 参数错误
		如果y不是有效的音频librosa.util.valid_audio
		if or not 整数order < 1

	FloatingPointError 浮点错误
		如果y是病态的

1.1.2 示例

1.1.2.1 在整个系列上以 16 阶计算 y 的 LP 系数
import librosa
import matplotlib.pyplot as plt

y, 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+04
  4.3119559e+04 -8.9374883e+04  1.4575309e+05 -1.9689898e+05
  2.2603498e+05 -2.2253350e+05  1.8789294e+05 -1.3422914e+05
  7.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 plt

y, 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-04
  1.37889292e-04  1.45363854e-04  1.64773126e-04  1.44854566e-04
  8.11248246e-05  1.45325233e-04  1.61147531e-04  8.44563183e-05
  1.06217769e-04  2.14754604e-04  2.60770466e-04  2.09023739e-04
  2.17636290e-04  2.16922228e-04  2.51500285e-04  2.52697617e-04
  3.40511993e-04  4.06945765e-04  4.40902222e-04  4.00981953e-04
  4.33149398e-04  4.93944972e-04  5.28876786e-04  5.71412791e-04
  5.83402929e-04  5.51324105e-04  5.36435342e-04  6.53159048e-04
  6.43022766e-04  5.53502119e-04  5.82284993e-04  7.19504664e-04
  6.47331763e-04  5.02119714e-04  4.79943701e-04  5.23787690e-04
  5.37002459e-04  5.05408272e-04  4.76755260e-04  4.27798484e-04
  3.82401457e-04  3.15428391e-04  2.95730948e-04  2.82254070e-04
  2.79527187e-04  2.88271054e-04  2.59202585e-04  2.16831162e-04
  2.21402937e-04  2.74864666e-04  2.80846347e-04  3.13285709e-04
  3.13992146e-04  3.66888940e-04  3.59804282e-04  2.63114925e-04
  2.18466361e-04  3.15178477e-04  3.74660885e-04  3.19922634e-04
  3.72263778e-04  3.58520454e-04  3.70525551e-04  4.36630624e-04
  4.45862679e-04  3.50899820e-04  4.06820531e-04  4.73972526e-04
  4.92125750e-04  4.54730354e-04  3.86143394e-04  4.12736088e-04
  4.97016183e-04  4.14129638e-04  2.80787761e-04  3.62383173e-04
  3.51943891e-04  2.69913697e-04  3.07713373e-04  4.19746240e-04
  4.02662641e-04  3.85927357e-04  3.40315106e-04  3.06073605e-04
  2.47361400e-04  2.57594307e-04  2.92757293e-04  2.40874404e-04
  2.70390359e-04  3.90405417e-04  4.12237801e-04  2.25744094e-04
  1.58009032e-04  1.84657925e-04  1.77676717e-04  1.95829329e-04
  3.26295529e-04  3.87715758e-04  3.96582589e-04  3.04580957e-04
  2.05543140e-04  1.48147185e-04  1.87027108e-04  1.77614376e-04
  2.45546893e-04  2.62790767e-04  2.25741926e-04  1.86788588e-04
  1.85506942e-04  1.65438891e-04  9.87605308e-05  1.42741861e-04
  1.63947960e-04  1.98309033e-04  9.96296876e-05  5.45161311e-05
  1.34513830e-05  2.36715205e-05 -4.80770323e-05 -1.73432127e-05
 -2.03911623e-06  7.66380617e-05  9.54798088e-05  1.21854471e-04
  1.44483696e-04  1.17219446e-04  1.03793267e-04  1.30333108e-04
  9.93434514e-05  9.50878093e-05  2.11334380e-04  3.26510868e-04
  2.84571754e-04  1.90839986e-04  2.04732365e-04  2.43039802e-04
  2.27008015e-04  2.41896443e-04  2.51963647e-04  2.75728758e-04
  2.67904485e-04  2.23343857e-04  1.76891815e-04  1.44003119e-04
  1.63609482e-04  1.38935939e-04  1.18333533e-04  1.12699825e-04
  9.91054476e-05  5.42084890e-05 -1.91804284e-05 -3.10450559e-06
  8.15462408e-05  1.42159755e-04  1.62784636e-05 -1.33475784e-04
 -1.65147576e-04 -9.31437826e-05 -8.30128556e-06 -7.31406908e-06
  3.85753810e-06 -1.33903595e-05  4.54643450e-05 -1.10429028e-05
 -2.46078125e-05  7.49093742e-05  1.32239307e-04  1.46154140e-04
  1.57430055e-04  1.68582861e-04  2.36734370e-04  2.11797364e-04
  1.88606064e-04  2.17038963e-04  2.40809080e-04  3.23942222e-04
  4.16389492e-04  3.34435928e-04  2.34914216e-04  2.95801787e-04
  2.94819183e-04  2.12682644e-04  2.60467234e-04  3.37957725e-04
  3.00610176e-04  2.82293127e-04  2.74660299e-04  1.94515145e-04
  2.20349655e-04  2.40593421e-04  2.92778102e-04  1.88134829e-04
  2.37444183e-04  3.40740953e-04  2.82818626e-04  3.09329393e-04
  3.41376319e-04  4.44998004e-04  4.65953199e-04  5.30930120e-04
  5.29128243e-04  5.58425789e-04  5.54715283e-04  5.21164096e-04
  5.70738746e-04  6.71095797e-04  6.29816088e-04  5.65187016e-04
  6.13344950e-04  6.85927516e-04  6.88585744e-04  6.33260002e-04
  6.50124392e-04  6.08350732e-04  5.54182741e-04  5.33508719e-04
  6.07319991e-04  6.62888575e-04  6.13867014e-04  5.27661585e-04
  4.57838032e-04  4.01673140e-04  3.71595583e-04  3.91147973e-04
  4.63141012e-04  5.70156903e-04  6.13787328e-04  6.27317873e-04
  6.58174627e-04  6.05559675e-04  6.48640795e-04  6.15973142e-04
  7.16390728e-04  7.14116672e-04  7.89883605e-04  8.09062389e-04
  7.99874775e-04  7.71570543e-04  7.32545566e-04  7.68040714e-04
  7.52743625e-04  7.67628953e-04  8.31097306e-04  8.78411636e-04
  9.32153431e-04  9.40081431e-04  8.91754113e-04  8.47310643e-04
  8.39371525e-04  8.89794610e-04  9.00006620e-04  8.13433027e-04
  7.66678189e-04  7.87897559e-04  7.48607505e-04  6.98024232e-04
  6.39678619e-04  6.48814777e-04  6.77619071e-04  5.54253522e-04
  4.83181997e-04  5.62100555e-04  5.91860502e-04  5.56626415e-04
  4.78705566e-04  4.61247284e-04  4.04335384e-04  4.63164033e-04
  5.29729994e-04  6.81568112e-04  6.86341373e-04  5.84251713e-04
  5.28333127e-04  4.79577924e-04  3.54282733e-04  4.56227805e-04
  6.12638541e-04  6.24920765e-04  6.45749969e-04  6.71656511e-04
  6.50883652e-04  5.91458171e-04  5.26275428e-04  4.88127349e-04
  4.82213683e-04  5.26737131e-04  5.46338793e-04  5.76737337e-04
  5.55883977e-04  4.51062398e-04  3.67945235e-04  4.13711881e-04
  3.61570826e-04  3.65244923e-04  3.61629354e-04  3.69159912e-04
  3.35314107e-04  3.09350929e-04  3.38723767e-04  2.78174004e-04
  1.37493378e-04  1.13665199e-04  1.86327947e-04  1.73253968e-04
  1.00370715e-04  2.04288212e-04  2.32314575e-04  1.63529505e-04
  2.29475772e-04  2.52356724e-04  2.28857709e-04  1.18640397e-04
  2.14267013e-04  1.70984800e-04  1.20211480e-04 -1.68519546e-05
  5.56984669e-05  1.19950942e-04  1.06331303e-04  1.65614940e-04
  1.16886556e-04  6.77938515e-05 -4.55389272e-05 -2.77533436e-05
  2.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-05
  3.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-04
  1.41164608e-04  2.15456559e-04  2.18956673e-04  1.13575326e-04
  6.41610604e-05 -1.08732629e-05  1.64197172e-05  5.50460572e-05
  7.07402796e-05 -2.53228955e-05 -3.14787139e-05  4.46344129e-05
  9.72716298e-05  1.41922370e-04  1.16331852e-04  6.42958839e-05
  4.18882337e-05  1.36276998e-04  9.83575883e-05  8.44348760e-05
  6.28674388e-05 -3.39997860e-06 -7.80903065e-05 -4.16404037e-05
  3.31525371e-05  4.09631793e-05 -4.33711903e-05  7.06169203e-06
  5.20047470e-05  4.35000693e-05 -1.02567210e-05 -1.99978949e-05
 -4.44903817e-05 -4.49519284e-05 -5.27158772e-05  4.02046935e-05
  9.80040713e-05  8.30742720e-05  7.70356928e-05  1.66900412e-04
  1.77449605e-04  7.24201600e-05  1.05944717e-04  1.28038169e-04
  1.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-04
  1.89461782e-04  1.24827951e-04  1.44406232e-04  1.65583157e-04
  1.39157136e-04  6.88219500e-05  1.54199779e-04  1.61392073e-04
  6.98553597e-05  1.08343063e-04  2.30237285e-04  2.64703952e-04
  1.96815336e-04  2.15755701e-04  2.13436243e-04  2.53595086e-04
  2.48988403e-04  3.50453915e-04  4.12152356e-04  4.39953619e-04
  3.87847988e-04  4.32010895e-04  4.96825906e-04  5.26733652e-04
  5.69928300e-04  5.76438468e-04  5.37218942e-04  5.25540285e-04
  6.63265527e-04  6.31299325e-04  5.29407537e-04  5.78248330e-04
  7.32135111e-04  6.24789515e-04  4.69169261e-04  4.68661240e-04
  5.23268069e-04  5.30969440e-04  4.92098786e-04  4.64399643e-04
  4.12682689e-04  3.68606266e-04  2.98931762e-04  2.87733305e-04
  2.75543446e-04  2.74721988e-04  2.85318408e-04  2.50146959e-04
  2.06126648e-04  2.18763630e-04  2.79870193e-04  2.77530047e-04
  3.14052183e-04  3.09247806e-04  3.70370207e-04  3.52999537e-04
  2.42278791e-04  2.07341834e-04  3.27055192e-04  3.79163091e-04
  3.05476850e-04  3.75565432e-04  3.50581546e-04  3.66863214e-04
  4.41320140e-04  4.40552024e-04  3.29002280e-04  4.10206968e-04
  4.78264705e-04  4.87644225e-04  4.41200910e-04  3.68270989e-04
  4.10947784e-04  5.03919787e-04  3.93345089e-04  2.53325188e-04
  3.70908244e-04  3.44679564e-04  2.51512870e-04  3.09495251e-04
  4.32657540e-04  3.93460616e-04  3.77045091e-04  3.27134939e-04
  2.95394918e-04  2.33351538e-04  2.55375217e-04  2.94314002e-04
  2.28148726e-04  2.71315020e-04  4.05154942e-04  4.09632176e-04
  1.89922550e-04  1.43820285e-04  1.86414500e-04  1.73712820e-04
  1.95940198e-04  3.43850099e-04  3.92351474e-04  3.91972460e-04
  2.83914577e-04  1.85192321e-04  1.35903239e-04  1.90866810e-04
  1.73230021e-04  2.53514874e-04  2.61706255e-04  2.15821793e-04
  1.77142137e-04  1.82409512e-04  1.59396500e-04  8.56732619e-05
  1.48152132e-04  1.65082202e-04  2.01190674e-04  8.09826231e-05
  4.58521893e-05  6.12566026e-06  2.50759459e-05 -5.97671178e-05
 -1.17476963e-05  6.44953848e-07  8.90863109e-05  9.72655026e-05
  1.24536951e-04  1.46166281e-04  1.10677216e-04  9.98575156e-05
  1.32912800e-04  9.24326165e-05  9.28764022e-05  2.28206304e-04
  3.41412130e-04  2.72892209e-04  1.71636692e-04  2.03966868e-04
  2.45912103e-04  2.20710877e-04  2.40727547e-04  2.49803112e-04
  2.75573955e-04  2.62395969e-04  2.12158972e-04  1.66099107e-04
  1.36070937e-04  1.64471709e-04  1.32506124e-04  1.12928649e-04
  1.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-04
  1.46300478e-04  1.56944250e-04  1.67902855e-04  2.44876897e-04
  2.04192558e-04  1.81663277e-04  2.18602842e-04  2.41196389e-04
  3.33329631e-04  4.25958297e-04  3.15048309e-04  2.14024288e-04
  3.01769822e-04  2.90079278e-04  1.95150449e-04  2.64711970e-04
  3.46149929e-04  2.89477838e-04  2.74743013e-04  2.69080250e-04
  1.77609682e-04  2.21411873e-04  2.40372894e-04  2.97284642e-04
  1.67082311e-04  2.42310037e-04  3.53362693e-04  2.68396087e-04
  3.09129660e-04  3.41639381e-04  4.56060106e-04  4.62362970e-04
  5.33962495e-04  5.20614628e-04  5.54848138e-04  5.45474278e-04
  5.07271219e-04  5.70484621e-04  6.78087637e-04  6.12899652e-04
  5.45225462e-04  6.12184901e-04  6.87875672e-04  6.78373601e-04
  6.13855767e-04  6.42970561e-04  5.91681393e-04  5.36204836e-04
  5.21656338e-04  6.10699494e-04  6.62245009e-04  5.95855987e-04
  5.04542108e-04  4.38640060e-04  3.85712988e-04  3.60623009e-04
  3.88474071e-04  4.68440112e-04  5.79867356e-04  6.11835727e-04
  6.19939724e-04  6.53321139e-04  5.87054612e-04  6.46053772e-04
  6.00763886e-04  7.22691004e-04  7.02653957e-04  7.90772380e-04
  7.99846194e-04  7.85884599e-04  7.54705833e-04  7.14427641e-04
  7.62288251e-04  7.38425088e-04  7.58310834e-04  8.29215725e-04
  8.72996927e-04  9.27019732e-04  9.26884527e-04  8.69556249e-04
  8.26474776e-04  8.24985548e-04  8.84742269e-04  8.87826722e-04
  7.85820375e-04  7.46691500e-04  7.79363082e-04  7.30194681e-04
  6.78438124e-04  6.19651501e-04  6.40341809e-04  6.72108474e-04
  5.24281387e-04  4.63374906e-04  5.67066125e-04  5.87844766e-04
  5.41892207e-04  4.57780752e-04  4.51072276e-04  3.88204498e-04
  4.66181160e-04  5.33056379e-04  6.97320193e-04  6.76530589e-04
  5.57502095e-04  5.10452474e-04  4.63694500e-04  3.27075641e-04
  4.66825262e-04  6.30251523e-04  6.17363414e-04  6.39351112e-04
  6.65736101e-04  6.37194868e-04  5.71991295e-04  5.06821030e-04
  4.73949809e-04  4.73714585e-04  5.26289556e-04  5.41268023e-04
  5.73066685e-04  5.43653704e-04  4.25903684e-04  3.47836560e-04
  4.15231630e-04  3.46929648e-04  3.60220528e-04  3.55397574e-04
  3.64743227e-04  3.24250783e-04  3.00056221e-04  3.38564477e-04
  2.63368075e-04  1.10979891e-04  1.07773589e-04  1.96033633e-04
  1.68302640e-04  8.61830733e-05  2.19132508e-04  2.33571226e-04
  1.49073208e-04  2.37348599e-04  2.52410939e-04  2.21237702e-04
  9.76989436e-05  2.27519688e-04  1.60833035e-04  1.09548346e-04
 -4.03462168e-05  6.74094004e-05  1.29227806e-04  1.02322683e-04
  1.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-06
  4.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-05
  1.43374644e-04  1.40869210e-04  2.24993130e-04  2.16169545e-04
  9.35505363e-05  5.46022381e-05 -2.37094759e-05  2.08955620e-05
  6.08874732e-05  7.23639078e-05 -4.15798072e-05 -3.20577136e-05
  5.71343141e-05  1.04886890e-04  1.47461363e-04  1.10093466e-04
  5.42805856e-05  3.73553463e-05  1.50523952e-04  9.02609879e-05
  8.07131157e-05  5.81550039e-05 -1.48325688e-05 -8.98250564e-05
 -3.46775925e-05  4.56015926e-05  4.16819824e-05 -5.73155545e-05
  1.56931282e-05  5.89880982e-05  4.13518245e-05 -1.94147259e-05
 -2.13762477e-05 -4.80457662e-05 -4.43351819e-05 -5.32444245e-05
  5.56862745e-05  1.06502671e-04  7.91990550e-05  7.47950740e-05
  1.79888618e-04  1.76527550e-04  5.30942525e-05  1.10112993e-04
  1.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()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岳涛@心馨电脑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值