def librosa_lpc( X, order ): try: return lpc( X, order ) except: res = np.zeros( ( order + 1, ) ) res[ 0 ] = 1. return res
def estimate_formants_lpc(waveform, sample_rate, num_formants=5): hamming_win = np.hamming(len(waveform)) # Apply window and high pass filter. x_win = waveform * hamming_win x_filt = lfilter([1], [1.0, 0.63], x_win) # Get LPC. From mathworks link above, the general rule is that the # order is two times the expected number of formants plus 2. We use # 5 as a base because we discard the formant f0 and want f1...f4. lpc_rep = lpc(x_filt, 2 + int(sample_rate / 1000)) # Calculate the frequencies. roots = [r for r in np.roots(lpc_rep) if np.imag(r) >= 0] angles = np.arctan2(np.imag(roots), np.real(roots)) return sorted(angles * (sample_rate / (2 * math.pi)))
def extract_with_lpc(audio: np.array, samplerate: int): """ Extracts audio characteristics using LPC :param audio: (np.array) audio to be extracted :param samplerate: (int) corresponding to the samplerate of audio :return: characteristics of the signal """ windows = split_in_windows(audio, samplerate) # for each window, get the coefficients of the LPC features = np.array([libcore.lpc(window, 12) for window in windows]) return preprocessing.scale(features)
def calcFormantes(directorio, asignacion): #plt.figure() #plt.title(directorio) for filename in os.listdir(directorio): global X global Y #plt.figure() #plt.title(filename) fs, x = wv.read(directorio + '/' + filename) n = len(x) ham = scipy.signal.hamming(n) mult = ham * x[:, 1] y = scipy.signal.lfilter([1], [1, 0.63], mult) a = lc.lpc(y, 12) w, h = scipy.signal.freqz([0] + -1 * a[1:], [1], n) tupla = findForm(w, h) if tupla[0] != 0 and tupla[1] != 0: X.append(tupla) Y.append(asignacion)
def main(): rate, data = wave.read(AUDIO_FILE) # Convert data to float so that it is compatible with # librosa lpc algorithm data = np.array(data, dtype=np.float) d_lpc = lib_core.lpc(data, 16) # Get the time domain values to plot on the x-axis time = np.linspace(0, len(data) / rate, num=len(data)) # Rebuild signal using the LPC coefficients d_hat = sig.lfilter([0] + -1 * d_lpc[1:], [1], data) # Graph the lpc signal on top of the original signal plt.figure(1) plt.title("Original Signal") plt.plot(time, data, color="r", label="Data") plt.plot(time, d_hat, color="b", label="LPC", linestyle="--") plt.show() # Need to change d_hat to be integers so that the wav file may be read d_hat = np.array(d_hat, dtype=np.int16) data = np.array(data, dtype=np.int16) # Create new wav file # Volume is very low so we multiply the output values by a factor wave.write("lpc_output.wav", rate, d_hat * 20) # Find the excitation signal excitation = data - d_hat plt.figure(2) plt.title("Excitation Signal") plt.plot(time, excitation, color="r") plt.show() # Needs to be > 16 bit number to prevent overflow excitation = np.array(excitation, dtype=np.int64) print( f"Sum of absolute value of excitation signal: {reduce(lambda a,b: a + abs(b), excitation)}" )
#Víctor Hugo Flores Pineda 155990 #Rebeca Baños García 157655 #Python 3 import matplotlib.pyplot as plt import numpy as np import scipy.signal from scipy.io import wavfile as wv import librosa.core as lc fs, x = wv.read('./audios/camaraA.wav') n = len(x) ham = scipy.signal.hamming(n) mult = ham * x[:, 1] y = scipy.signal.lfilter([1], [1, 0.63], mult) a = lc.lpc(y, 10) y_hat = scipy.signal.lfilter([0] + -1 * a[1:], [1], y) plt.plot(y) plt.plot(y_hat, 'g--') w, h = scipy.signal.freqz([0] + -1 * a[1:], [1], n) FFT = scipy.fftpack.fft(x) plt.figure() plt.plot(w, h, 'C1') #plt.plot(w,FFT,'g') plt.show()
def get_formants(file_path): # # Read from file. # spf = wave.open(file_path, 'r') # http://www.linguistics.ucla.edu/people/hayes/103/Charts/VChart/ae.wav # # # Get file as numpy array. # x = spf.readframes(-1) # x = np.fromstring(x, 'Int16') from scipy.io import wavfile fs, data = wavfile.read(file_path) x = np.asarray(data) try: x = x[:, 0] except: pass # Get Hamming window. N = len(x) w = np.hamming(N) # Apply window and high pass filter. x1 = x * w x1 = lfilter([1], [1., 0.63], x1) # Get LPC. ncoeff = int(fs/1000) + 2 A = lib.lpc(x1, ncoeff) # Get roots. rts = np.roots(A) rts = [r for r in rts if np.imag(r) >= 0] # Get angles. angz = np.arctan2(np.imag(rts), np.real(rts)) # Get frequencies. frqs = np.dot(angz, fs / (2 * np.pi)) l = int(len(frqs)) indices = np.zeros(l) bw = indices frqs2 = np.copy(frqs) for i in range(0, l, 1): #Determining indices of the maximum and their bandwidth if(frqs2[i] == 0): frqs2[i] = -30*i ind = np.where(frqs2 == np.amax(frqs2)) try: indices[l - 1 - i] = int(ind[0]) l2 = indices[l - 1 - i] frqs2[int(l2)] = -i - 1 # keeping all indices different to prevent different dimensions of tuples # Adding bandwidth bw[l - 1 - i] = -0.5 * (fs / (2 * np.pi)) * np.log(np.abs(rts[int(ind[0])])) except: indices[l - 1 - i] = int(ind[0][0]) l2 = indices[l - 1 - i] frqs2[int(l2)] = -i - 1 #keeping all indices different to prevent different dimensions of tuples #Adding bandwidth bw[l - 1 - i] = -0.5 * (fs / (2 * np.pi)) * np.log(np.abs(rts[int(ind[0][0])])) frqs = sorted(frqs) frqs = np.asarray(frqs) formants = np.copy(frqs) for kk in range(0,l,1): if ((frqs[kk] < 90) or (bw[kk] > 400)): formants[kk] = -1 #TO BE IGNORED for i in range(l-1,-1,-1): #Removing non-formant elements if(int(formants[i]) == -1): formants = np.delete(formants,i) return formants[0:3]