From b2e4655bf2ecf476113cb2d2297e45c8f3978587 Mon Sep 17 00:00:00 2001 From: Joseph Keshet Date: Mon, 27 Jun 2016 12:50:27 -0500 Subject: [PATCH] Corrected file handling. --- Extract_Features.py | 70 ++++++++++++++++++++++++--------------------- formants.py | 27 +++++++---------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/Extract_Features.py b/Extract_Features.py index 039f388..ce37d0b 100644 --- a/Extract_Features.py +++ b/Extract_Features.py @@ -1,4 +1,5 @@ __author__ = 'shua' + import argparse import numpy as np import wave @@ -12,9 +13,11 @@ from copy import deepcopy from scipy.fftpack import fft, ifft from scikits.talkbox.linpred import lpc import shutil + epsilon = 0.0000000001 prefac = .97 + def build_data(wav,begin=None,end=None): wav_in_file = wave.Wave_read(wav) wav_in_num_samples = wav_in_file.getnframes() @@ -29,6 +32,7 @@ def build_data(wav,begin=None,end=None): X.append(data[i:i + 480]) return X + def periodogram(x, nfft=None, fs=1): """Compute the periodogram of the given signal, with the given fft size. @@ -89,6 +93,7 @@ def periodogram(x, nfft=None, fs=1): fgrid = np.linspace(0, fs * 0.5, pn) return pxx[:pn] / (n * fs), fgrid + def arspec(x, order, nfft=None, fs=1): """Compute the spectral density using an AR model. @@ -140,6 +145,7 @@ def arspec(x, order, nfft=None, fs=1): fx = np.linspace(0, fs * 0.5, pxx.size) return pxx, fx + def taper(n, p=0.1): """Return a split cosine bell taper (or window) @@ -165,6 +171,7 @@ def taper(n, p=0.1): return w + def atal(x, order, num_coefs): x = np.atleast_1d(x) n = x.size @@ -184,10 +191,12 @@ def atal(x, order, num_coefs): c[m] += (float(k)/float(m)-1)*a[k]*c[m-k] return c + def preemp(input, p): """Pre-emphasis filter.""" return lfilter([1., -p], 1, input) + def arspecs(input_wav,order,Atal=False): epsilon = 0.0000000001 data = input_wav @@ -207,6 +216,7 @@ def arspecs(input_wav,order,Atal=False): ar = dct(mspec1, type=2, norm='ortho', axis=-1) return ar[:30] + def specPS(input_wav,pitch): N = len(input_wav) samps = N/pitch @@ -233,41 +243,37 @@ def specPS(input_wav,pitch): # Use the DCT to 'compress' the coefficients (spectrum -> cepstrum domain) ceps = dct(mspec, type=2, norm='ortho', axis=-1) return ceps[:50] -def build_single_feature_row(data,Atal): - lpcs = [8,9,10,11,12,13,14,15,16,17] - arr = [] - periodo = specPS(data,50) - arr.extend(periodo) - for j in lpcs: - if Atal: - ars = arspecs(data, j, Atal=True) - else: - ars = arspecs(data, j) - arr.extend(ars) - for i in range(len(arr)): - if np.isnan(np.float(arr[i])): - arr[i] = 0.0 - return arr -def Create_features(input_wav,feature_file_name, begin=None,end=None,Atal=False): - X = build_data(input_wav,begin,end) - full_path = os.path.realpath(__file__) - output_directory = os.path.dirname(full_path)+'/Features/' - if Atal: - feature_file = output_directory+"ATAL_features_"+feature_file_name+'.txt' - else: - feature_file = output_directory+"features_"+feature_file_name+'.txt' + +def build_single_feature_row(data, Atal): + lpcs = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17] + arr = [] + periodo = specPS(data, 50) + arr.extend(periodo) + for j in lpcs: + if Atal: + ars = arspecs(data, j, Atal=True) + else: + ars = arspecs(data, j) + arr.extend(ars) + for i in range(len(arr)): + if np.isnan(np.float(arr[i])): + arr[i] = 0.0 + return arr + + +def create_features(input_wav_filename, feature_filename, begin=None, end=None, Atal=False): + X = build_data(input_wav_filename, begin, end) if begin is not None and end is not None: - arr = [input_wav.replace('.wav','')] - arr.extend(build_single_feature_row(X,Atal)) - np.savetxt(feature_file,np.asarray([arr]),delimiter=",",fmt="%s") - return arr + arr = [input_wav_filename] + arr.extend(build_single_feature_row(X, Atal)) + np.savetxt(feature_filename, np.asarray([arr]), delimiter=",", fmt="%s") + return arr arcep_mat = [] for i in range(len(X)): - arr = [input_wav.replace('.wav','_PART_')+str(i)] - arr.extend(build_single_feature_row(X[i], Atal)) - arcep_mat.append(arr) - np.savetxt(feature_file,np.asarray(arcep_mat),delimiter=",",fmt="%s") + arr = [input_wav_filename + str(i)] + arr.extend(build_single_feature_row(X[i], Atal)) + arcep_mat.append(arr) + np.savetxt(feature_filename, np.asarray(arcep_mat), delimiter=",", fmt="%s") return arcep_mat - diff --git a/formants.py b/formants.py index 0d496b4..cae8fc9 100644 --- a/formants.py +++ b/formants.py @@ -1,14 +1,13 @@ + import Extract_Features as features from subprocess import call -import os import sys -import shlex import argparse +import tempfile -def easy_call(command, debug_mode=False): +def easy_call(command, debug_mode=True): try: - #command = "time " + command if debug_mode: print >>sys.stderr, command call(command, shell=True) @@ -28,20 +27,14 @@ if __name__ == "__main__": parser.add_argument('--begin', help="beginning time in the WAV file", default=0.0, type=float) parser.add_argument('--end', help="end time in the WAV file", default=-1.0, type=float) args = parser.parse_args() - full_path = os.path.realpath(__file__) - if not os.path.exists(os.path.dirname(full_path)+'/Features/'): - os.makedirs(os.path.dirname(full_path)+'/Features/') - if not os.path.exists(os.path.dirname(full_path)+'/Predictions/'): - os.makedirs(os.path.dirname(full_path)+'/Predictions/') + + tmp_features_filename = tempfile._get_default_tempdir() + "/" + next(tempfile._get_candidate_names()) + ".txt" + print tmp_features_filename if args.begin > 0.0 or args.end > 0.0: - Data = features.Create_features(args.wav_file, args.formants_file, args.begin, args.end) - ff = str(os.path.dirname(os.path.realpath(__file__))+'/Features/features_' + args.formants_file+'.txt') - pf = str(os.path.dirname(os.path.realpath(__file__))+'/Predictions/' +args.formants_file+'.csv') - easy_call("th load_estimation_model.lua " + ff + ' ' + pf) + features.create_features(args.wav_file, tmp_features_filename, args.begin, args.end) + easy_call("th load_estimation_model.lua " + tmp_features_filename + ' ' + args.formants_file) else: - Data = features.Create_features(args.wav_file, args.formants_file) - ff = str(os.path.dirname(os.path.realpath(__file__))+'/Features/features_' + args.formants_file+'.txt') - pf = str(os.path.dirname(os.path.realpath(__file__))+'/Predictions/' +args.formants_file+'.csv') - easy_call("th load_tracking_model.lua " + ff + ' ' + pf) + features.create_features(args.wav_file, tmp_features_filename) + easy_call("th load_tracking_model.lua " + tmp_features_filename + ' ' + args.formants_file)