def training(nfiltbank, orderLPC): #Get directory and list of *.wav files directory = os.getcwd() + '/train_all_speakers' wave_files = [f for f in os.listdir(directory)] nSpeaker = len(wave_files) nCentroid = 32 # original is 16 codebooks_mfcc = np.empty((nSpeaker, nfiltbank, nCentroid)) codebooks_lpc = np.empty((nSpeaker, orderLPC, nCentroid)) for i, wave_file in enumerate(wave_files): fname = '/' + wave_file print 'Speaker [' + str( i) + '] File:' + wave_file + ' Training features...' (fs, s) = read(directory + fname) mel_coeff = mfcc_p(s, fs) mel_coeff = mel_coeff.transpose() mel_coeff[0, :] = np.zeros(mel_coeff.shape[1]) lpc_coeff = lpc(s, fs, orderLPC) codebooks_mfcc[i, :, :] = lbg(mel_coeff, nCentroid) codebooks_lpc[i, :, :] = lbg(lpc_coeff, nCentroid) print('Training finished\n') return (codebooks_mfcc, codebooks_lpc)
speaker = k return speaker print "|||||STARTING TEST|||||\n" for i, wave_file in enumerate(wave_files): fname = '/' + wave_file to_print = 'Speaker [' + str( i) + '] File:' + wave_file + ' Testing features...' print to_print (fs, s) = read(directory + fname) #Passing test file to MFCC mel_coefs = mfcc_p(s, fs) mel_coefs = mel_coefs.transpose() mel_coefs[0, :] = np.zeros( mel_coefs.shape[1] ) # 0th coefficient does not carry significant information #Passing test file to LPC lpc_coefs = lpc(s, fs, orderLPC) sp_mfcc = minDistance(mel_coefs, codebooks_mfcc) sp_lpc = minDistance(lpc_coefs, codebooks_lpc) print 'Speaker [' + str(i) + '] matches Speaker [' + str( sp_mfcc) + '] ||MFCC||' print 'Speaker [' + str(i) + '] matches Speaker [' + str( sp_lpc) + '] ||LPC||\n'
def training(nfiltbank, orderLPC): nSpeaker = 8 nCentroid = 32 #original is 16 codebooks_mfcc = np.empty((nSpeaker, nfiltbank, nCentroid)) codebooks_lpc = np.empty((nSpeaker, orderLPC, nCentroid)) directory = os.getcwd() + '/train' fname = str() for i in range(nSpeaker): fname = '/s' + str(i + 1) + '.wav' print('Now speaker ', str(i + 1), 'features are being trained') (fs, s) = read(directory + fname) #altering sample rate #fs = 10000 #original was 13000 #mel_coeff = mfcc(s, fs, nfiltbank) mel_coeff = mfcc_p(s, fs) mel_coeff = mel_coeff.transpose() mel_coeff[0, :] = np.zeros(mel_coeff.shape[1]) lpc_coeff = lpc(s, fs, orderLPC) codebooks_mfcc[i, :, :] = lbg(mel_coeff, nCentroid) codebooks_lpc[i, :, :] = lbg(lpc_coeff, nCentroid) plt.figure(i) plt.title('Codebook for speaker ' + str(i + 1) + ' with ' + str(nCentroid) + ' centroids') for j in range(nCentroid): plt.subplot(211) plt.stem(codebooks_mfcc[i, :, j]) plt.ylabel('MFCC') plt.subplot(212) markerline, stemlines, baseline = plt.stem(codebooks_lpc[i, :, j]) plt.setp(markerline, 'markerfacecolor', 'r') plt.setp(baseline, 'color', 'k') plt.ylabel('LPC') plt.axis(ymin=-1, ymax=1) plt.xlabel('Number of features') plt.show() print('Training complete') # #plotting 5th and 6th dimension MFCC features on a 2D plane # #comment lines 54 to 71 if you don't want to see codebook # codebooks = np.empty((2, nfiltbank, nCentroid)) # mel_coeff = np.empty((2, nfiltbank, 68)) # # for i in range(2): # fname = '/s' + str(i+2) + '.wav' # (fs,s) = read(directory + fname) # #mel_coeff[i,:,:] = mfcc(s, fs, nfiltbank)[:,0:68] # saver = mfcc_p(s, fs) # mel_coeff[i, :, :] = saver.transpose()[:,0:68] # codebooks[i,:,:] = lbg(mel_coeff[i,:,:], nCentroid) # # # plt.figure(nSpeaker + 1) # s1 = plt.scatter(mel_coeff[0,6,:], mel_coeff[0,4,:],s = 100, color = 'r', marker = 'o') # c1 = plt.scatter(codebooks[0,6,:], codebooks[0,4,:], s = 100, color = 'r', marker = '+') # s2 = plt.scatter(mel_coeff[1,6,:], mel_coeff[1,4,:],s = 100, color = 'b', marker = 'o') # c2 = plt.scatter(codebooks[1,6,:], codebooks[1,4,:], s = 100, color = 'b', marker = '+') # plt.grid() # plt.legend((s1, s2, c1, c2), ('Sp1','Sp2','Sp1 centroids', 'Sp2 centroids'), scatterpoints = 1, loc = 'upper left') # plt.show() return (codebooks_mfcc, codebooks_lpc)