Corrected file handling.

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