def do_lpc(spec, order, axis=0, error_normal=False): coeff, error, k = lpc(spec, order, axis=axis) if error_normal: error = np.reshape(error, (1, len(error))) error = np.repeat(error, order + 1, axis=axis) return coeff / error else: return coeff[1:, :]
def lpcres(signal, order, usefft=True): """Compute the LPC residual of a signal. The LPC residual is the 'error' signal from LPC analysis, and is defined as: res[n] = x[n] - xe[n] = 1 + a[1] x[n-1] + ... + a[p] x[n-p] Where x is the input signal and xe the linear prediction of x. Parameters ---------- signal : array-like input signal. If rank of signal is 2, each row is assumed to be an independant signal on which the LPC is computed. Rank > 2 is not supported. order : int LPC order Returns ------- res : array-like LPC residual Note ---- The LPC residual can also be seen as the input of the LPC analysis filter. As the LPC filter is a whitening filter, it is a whitened version of the signal. In AR modelling, the residual is simply the estimated excitation of the AR filter. """ if signal.ndim == 1: return lfilter(lpc(signal, order)[0], 1., signal) elif signal.ndim == 2: if usefft: cf = lpc(signal, order, axis=-1)[0] else: c = acorr(signal, maxlag=order, onesided=True)/signal.shape[-1] cf = levinson(c, order, axis=-1)[0] return slfilter(cf, np.ones((cf.shape[0], 1)), signal) else: raise ValueError("Input of rank > 2 not supported yet")
def lpcres(signal, order, usefft=True): """Compute the LPC residual of a signal. The LPC residual is the 'error' signal from LPC analysis, and is defined as: res[n] = x[n] - xe[n] = 1 + a[1] x[n-1] + ... + a[p] x[n-p] Where x is the input signal and xe the linear prediction of x. Parameters ---------- signal : array-like input signal. If rank of signal is 2, each row is assumed to be an independant signal on which the LPC is computed. Rank > 2 is not supported. order : int LPC order Returns ------- res : array-like LPC residual Note ---- The LPC residual can also be seen as the input of the LPC analysis filter. As the LPC filter is a whitening filter, it is a whitened version of the signal. In AR modelling, the residual is simply the estimated excitation of the AR filter. """ if signal.ndim == 1: return lfilter(lpc(signal, order)[0], 1., signal) elif signal.ndim == 2: if usefft: cf = lpc(signal, order, axis=-1)[0] else: c = acorr(signal, maxlag=order, onesided=True) / signal.shape[-1] cf = levinson(c, order, axis=-1)[0] return slfilter(cf, np.ones((cf.shape[0], 1)), signal) else: raise ValueError("Input of rank > 2 not supported yet")
def lpcc(frames, n_lpc, n_lpcc): """ n_lpc:lpc阶数 n_lpcc:lpcc阶数 """ lpcs = [] lpccs = [] for frame in frames: lpc = levinson_lpc.lpc(frame, n_lpc)[0] lpcc = lpc_to_cc(lpc, n_lpc, n_lpcc) lpcs.append(lpc) lpccs.append(lpcc) return lpcs, lpccs
def test_r2(self): """Testing LPC residual of a set of windows.""" order = 12 x = np.random.randn(10, 24) res = lpcres(x, order) r_res = np.empty(x.shape, x.dtype) for i in range(10): r_res[i] = lfilter(lpc(x[i], order)[0], 1., x[i]) assert_array_almost_equal(res, r_res) res = lpcres(x, order, usefft=False) assert_array_almost_equal(res, r_res)
def test_r2(self): """Testing LPC residual of a set of windows.""" order = 12 x = np.random.randn(10, 24) res = lpcres(x, order) r_res = np.empty(x.shape, x.dtype) for i in range(10): r_res[i] = lfilter(lpc(x[i], order)[0], 1., x[i]) assert_array_almost_equal(res, r_res) res = lpcres(x, order, usefft=False) assert_array_almost_equal(res, r_res)
def get_lpcc(filename): """ gets lpccs for each frame in a wav file filename: name of wav file with .wav returns the lpcc features in each frame as a list of lists """ print "Getting LPCC" (rate, sig) = wav.read(filename) frames = sigproc.framesig(sig, 0.025 * rate, 0.01 * rate) lpccs = [[]] * len(frames) for x in xrange(0, len(frames)): lpcc_feat = lpc(frames[x], 12) for feature in lpcc_feat[0]: feature = float(feature) lpccs[x] = lpcc_feat[0] return numpy.asarray(lpccs)
from scikits.talkbox.linpred.levinson_lpc import lpc from scipy.io import wavfile import matplotlib.pyplot as plt import numpy as np p = 15 start = 5500 rate, data = wavfile.read("xe.wav") data_1 = data[5000:5256] a, e, k = lpc(data_1, p, -1) # print a a_1 = np.zeros(256, ) # for i in range(len(a)): # a_1[i] = a[i] a_1[:len(a)] = a # print a_1 a_1_fft = np.fft.fft(a_1) a_1_abs = np.abs(a_1_fft) a_1_final = -np.log10(a_1_abs) plt.plot(a_1_final[:len(a_1_final) // 2]) plt.grid() plt.savefig("xe.png") for i in range(1, len(a_1_final) / 2 - 1): if (a_1_final[i] > a_1_final[i - 1] and a_1_final[i] > a_1_final[i + 1]): f_k = i * 1 * rate / 256
a = np.array(U) + lam * np.array(V) # eを更新 e[k + 1] = e[k] * (1.0 - lam * lam) return a, e[-1] if __name__ == "__main__": #original = np.zeros(128) #for i in range(len(original)): #original[i] = np.sin(i * 0.01) + 0.75 * np.sin(i * 0.03) + 0.5 * np.sin(i * 0.05) + 0.25 * np.sin(i * 0.11) fs, original = wavfile.read("a.wav") print levinson_lpc.lpc(original, 16) lpcOrder = 16 # LPC係数の次数 # 自己相関関数を計算 # r[0]からr[lpcOrder]までlpcOrder+1個必要 r = autocorr(original, lpcOrder + 1) for i in range(lpcOrder + 1): print "r[%d]: %f" % (i, r[i]) # Levinson-Durbinアルゴリズムを用いてLPC係数と最小誤差を計算 a, e = LevinsonDurbin(r, lpcOrder) print "*** result ***" print "a:", a print "e:", e
def mouse_click(self, event): if not event.inaxes: return x, y = event.xdata, event.ydata self.ly1.set_xdata(x) self.ly2.set_xdata(x + self.window_len) self.x = x self.y = y ###############draw ax2########################## self.ax2.clear() self.ax2.set_title('tu tuong quan') y_windows = self.y_or[int(x * self.sr):int((x + self.window_len) * self.sr)] R_list = compute_arcf_list(y_windows, start=0, stop=301) ##################################################################### p = 14 A = [1.0] B = [1.0, 0.95] signal_emph = signal.lfilter(B, A, y_windows, axis=-1, zi=None) a, e, k = lpc(signal_emph, p, -1) # a = np.append(a, np.zeros(self.nfft - p)) a = np.concatenate((a, np.zeros(512 - len(a)))) A_fft = np.fft.fft(a) afft_log = -10 * np.log(np.abs(A_fft[:len(A_fft) / 2])) self.ax_lpc_fft.clear() self.ax_lpc_fft.plot(range(len(afft_log)), afft_log) # print("lpca: ", a) # print("lpce: ", e) # print("lpck: ", k) ########################################################################### # Find the first low point d = diff(R_list) start = find(d > 0)[0] # Find the next peak after the low point (other than 0 lag). This bit is # not reliable for long signals, due to the desired peak occurring between # samples, and other peaks appearing higher. # Should use a weighting function to de-emphasize the peaks at longer lags. # Also could zero-pad before doing circular autocorrelation. max_arcf_loc = argmax(R_list[start:]) + start self.ax2.plot(range(len(R_list)), R_list) R_second_list = R_list[(max_arcf_loc + 1):] d = diff(R_second_list) start = find(d > 0)[0] second_arcf_loc = argmax(R_second_list[start:]) + start print("f0_auto_corelation: ", float(self.sr) / (second_arcf_loc + 1)) self.ax2.axvline(max_arcf_loc, color='y') self.ax2.axvline(max_arcf_loc + second_arcf_loc + 1, color='r') ###############draw ax4 tinh tu tuong quan bang amdf############# self.ax4.clear() dk_list = compute_dk_list(y_or=y_windows) d = diff(dk_list) start = find(d > 0)[0] min_index1 = np.argmin(dk_list[start:]) + start dk_second_list = dk_list[(min_index1 + 1):] d = diff(dk_second_list) start = find(d > 0)[0] min_index2 = np.argmin(dk_second_list[start:]) + start self.ax4.axvline(min_index1, color='y') self.ax4.axvline(min_index2 + min_index1 + 1, color='r') self.ax4.plot(range(len(dk_list)), dk_list) print("start:", start, "minindex1:", dk_list[min_index2]) plt.draw()
V.append(1) a = np.array(U) + lam * np.array(V) # eを更新 e[k + 1] = e[k] * (1.0 - lam * lam) return a, e[-1] if __name__ == "__main__": #original = np.zeros(128) #for i in range(len(original)): #original[i] = np.sin(i * 0.01) + 0.75 * np.sin(i * 0.03) + 0.5 * np.sin(i * 0.05) + 0.25 * np.sin(i * 0.11) fs, original = wavfile.read("a.wav") print levinson_lpc.lpc(original, 16) lpcOrder = 16 # LPC係数の次数 # 自己相関関数を計算 # r[0]からr[lpcOrder]までlpcOrder+1個必要 r = autocorr(original, lpcOrder + 1) for i in range(lpcOrder + 1): print "r[%d]: %f" % (i, r[i]) # Levinson-Durbinアルゴリズムを用いてLPC係数と最小誤差を計算 a, e = LevinsonDurbin(r, lpcOrder) print "*** result ***" print "a:", a print "e:", e
def lpcc(self, signal): lpc = levinson_lpc.lpc(signal, self.n_lpc)[0] return lpc[1:]
data1[i] = 0; else: data1[i] = dat[i] for i in range(0, K, 1): x = 0 for j in range(0, N - i, 1): x = x + data1[j] * data1[j + i] kq.append(x) return kq win = data[start:start + N] ax[0].plot(win) a, e, k = lpc(win, 4, -1) win = AzAll(data, start, N, a) ax[1].plot(win) Rwin = R(win) ax[2].plot(Rwin) F0 = [] xplot4 = [] for i in range(0, len(data) - N, N ): win1 = data[i:i+N] a, e, k = lpc(win1, 4, -1) win2 = AzAll(data, i, N, a) Rwin = R(win2)
def LPC(self, frameh): # error self.a, self.e, self.k = lpc(frameh, self.p, -1) self.a = np.append(self.a, np.zeros(self.nfft - self.p))
def configFilter(data): out = [] for i in range(1, len(data), 1): out.append(data[i] - 0.98 * data[i-1]) return np.array(out) rate, data = read('khoosoothunhus.wav') fig, (ax1, ax2, ax3, ax4) = pl.subplots(4 , 1 , sharex=False) ax4.plot(data) win = data[32500:32756] ax1.plot(win) win = configFilter(win) ax2.plot(win) a, e, k = lpc(win, 14, -1) print(k) data1 = np.zeros(256) data1[:len(a)] = a data1 = fft(data1) data1 = -np.log(np.abs(data1)) for i in range(1,len(data1)/2): if data1[i] > data1[i-1] and data1[i] > data1[i+1] : print(i*1.0 /256 * rate) ax3.plot(data1[0:len(data1)/2])
def lpcc(signal): #lpc = tb.lpc(signal, n_lpc)[0] lpc = levinson_lpc.lpc(signal, n_lpc)[0] print lpc lpcc = lpc_to_cc(lpc) return lpcc
import numpy as np import matplotlib.pyplot as plot import scipy.signal as sg import wave from scipy import signal # from master.scikits.talkbox.linpred.levinson_lpc import levinson,lpc from scikits.talkbox.linpred.levinson_lpc import lpc from scipy.io.wavfile import read rate, signal = read("../Xe.wav") signalA = [] for i in range(6000, 6512): signalA.append(signal[i]) signalA = np.array(signalA) p = 14 a, e, k = lpc(signalA, p, -1) a = np.append(a, np.zeros(512-14)) data_freq = np.fft.fft(a, 512) data_freq = sg.resample(data_freq,512) magSpectrum = np.abs(data_freq) # magDb = 1/magSpectrum magDb = -np.log(magSpectrum) signalArray = [] signalDK = [] for k in range(6000, 6900): signalArray.append(signal[k]) signalArray = sg.resample(signalArray, 900) for k in range(0, 149): # day la K 0..150 sig = 0 for m in range(0, 299): sig = sig + np.abs(signalArray[m] - signalArray[ m - k]) # day la D(k)
def lpcc(self, signal): lpc = levinson_lpc.lpc(signal, self.n_lpc)[0] return lpc[1:]
out = [] for i in range(1, len(data), 1): out.append(data[i] - 0.98 * data[i - 1]) return np.array(out) rate, data = read('e.wav') fig, (ax1, ax2, ax3, ax4) = pl.subplots(4, 1, sharex=False) size = 512 win = data[5000:5000 + size] ax1.plot(win) win = configFilter(win) ax2.plot(win) a, e, k = lpc(win, 20, -1) print(k) data1 = np.zeros(size) data1[:len(a)] = a data1 = fft(data1) data1 = -np.log(np.abs(data1)) for i in range(1, len(data1) / 2): if data1[i] > data1[i - 1] and data1[i] > data1[i + 1]: print(i * 1.0 / size * rate) ax3.plot(data1[0:len(data1) / 2])