Fixed da noise mixing bug
This commit is contained in:
@@ -36,7 +36,8 @@ def main():
|
||||
da_files = ["../da_stim/stimulus/3000_da.wav"]
|
||||
story_dir = "../eeg_story_stim/stimulus"
|
||||
mat_dir = "../matrix_test/speech_components"
|
||||
noise_file = "../matrix_test/behavioural_stim/stimulus/wav/noise/noise.wav"
|
||||
noise_file = "../matrix_test/behavioural_stim/stimulus/wav/noise/noise_norm.wav"
|
||||
da_noise_file = "../da_stim/noise/wav/noise/noise_norm.wav"
|
||||
|
||||
story_wavs = globDir(story_dir, '*.wav')
|
||||
mat_wavs = globDir(mat_dir, '*.wav')
|
||||
@@ -47,9 +48,11 @@ def main():
|
||||
dir_must_exist(out_dir)
|
||||
dir_must_exist(out_red_dir)
|
||||
dir_must_exist(out_stim_dir)
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
story_coef = calc_potential_max(story_wavs, noise_file, out_red_dir, "story_red_coef")
|
||||
mat_coef = calc_potential_max(mat_wavs, noise_file, out_red_dir, "mat_red_coef")
|
||||
da_coef = calc_potential_max(da_files, noise_file, out_red_dir, "da_red_coef")
|
||||
da_coef = calc_potential_max(da_files, da_noise_file, out_red_dir, "da_red_coef")
|
||||
|
||||
mat_cal_stim = "../matrix_test/long_concat_stim/out/stim/stim_0.wav"
|
||||
da_cal_stim = "../da_stim/stimulus/wav/10min_da.wav"
|
||||
|
||||
@@ -20,9 +20,9 @@ from gen_da import gen_da_stim
|
||||
|
||||
from pathops import dir_must_exist
|
||||
try:
|
||||
from signalops import rolling_window_lastaxis, calc_rms
|
||||
from signalops import rolling_window_lastaxis, window_rms, block_process_wav
|
||||
except ImportError:
|
||||
from .signalops import rolling_window_lastaxis, block_lfilter, calc_rms
|
||||
from .signalops import rolling_window_lastaxis, block_lfilter, window_rms, block_process_wav
|
||||
|
||||
import scipy.signal as sgnl
|
||||
from scipy.stats import pearsonr
|
||||
@@ -48,6 +48,7 @@ def block_lfilter_wav(b, a, x, outfile, fmt, fs, blocksize=8192):
|
||||
sndfile = PySndfile(outfile, 'w', fmt, 1, fs)
|
||||
i = 0
|
||||
y_out = np.zeros(x.size)
|
||||
y_max = 0.0
|
||||
while i < x.size:
|
||||
print("Filtering {0} to {1} of {2}".format(i, i+blocksize, x.size))
|
||||
if i+blocksize > x.size:
|
||||
@@ -58,8 +59,9 @@ def block_lfilter_wav(b, a, x, outfile, fmt, fs, blocksize=8192):
|
||||
y, new_state = sgnl.lfilter(b,a,x[i:i+blocksize], zi=new_state)
|
||||
sndfile.write_frames(y)
|
||||
y_out[i:i+y.size] = y
|
||||
y_max = np.max([y_max, np.abs(y).max()])
|
||||
i += blocksize
|
||||
return y_out
|
||||
return y_out, y_max
|
||||
|
||||
|
||||
def synthesize_trial(wavFileMatrix, indexes):
|
||||
@@ -140,9 +142,8 @@ def gen_rms_peak(files, OutRMSDir, OutPeakDir):
|
||||
rmsFilepath = os.path.join(OutRMSDir, tail)
|
||||
print("Generating: "+rmsFilepath)
|
||||
y, fs, _ = sndio.read(file)
|
||||
y = y[:, 1]
|
||||
y_rms = calc_rms(y, round(0.02*fs))
|
||||
np.save(rmsFilepath, peak)
|
||||
y_rms = window_rms(y, round(0.02*fs))
|
||||
np.save(rmsFilepath, y_rms)
|
||||
rmsFiles.append(rmsFilepath)
|
||||
|
||||
head, tail = os.path.split(file)
|
||||
@@ -152,7 +153,6 @@ def gen_rms_peak(files, OutRMSDir, OutPeakDir):
|
||||
peakFilepath = os.path.join(OutPeakDir, tail)
|
||||
print("Generating: "+peakFilepath)
|
||||
peak = np.abs(y).max()
|
||||
pdb.set_trace()
|
||||
np.save(peakFilepath, peak)
|
||||
peakFiles.append(peakFilepath)
|
||||
return rmsFiles, peakFiles
|
||||
@@ -180,7 +180,6 @@ def calc_spectrum(files, silences, fs=44100, plot=False):
|
||||
print("Calculating LTASS...")
|
||||
for ind, file in enumerate(files):
|
||||
x, fs, _ = sndio.read(file)
|
||||
x = x[:, 1]
|
||||
f, t, Zxx = sgnl.stft(x, window=np.ones(window), nperseg=window, noverlap=0)
|
||||
sil = silences[ind]
|
||||
sTemp = np.zeros((sil.shape[0], t.size), dtype=bool)
|
||||
@@ -215,7 +214,9 @@ def gen_noise(OutDir, b, fs, s_rms):
|
||||
dir_must_exist(noiseDir)
|
||||
noiseDir = os.path.join(noiseDir, 'noise')
|
||||
dir_must_exist(noiseDir)
|
||||
y = block_lfilter_wav(b, [1.0], x, os.path.join(noiseDir, 'noise.wav'), 65538, 44100)
|
||||
y, y_max = block_lfilter_wav(b, [1.0], x, os.path.join(noiseDir, 'noise.wav'), 65538, 44100)
|
||||
block_process_wav(os.path.join(noiseDir, 'noise.wav'), os.path.join(noiseDir, 'noise_norm.wav'), lambda x: x / (y_max * 0.95))
|
||||
y = y/(np.abs(y).max() * 0.95)
|
||||
noise_rms_path = os.path.join(noiseRMSDir, 'noise_rms.npy')
|
||||
rms = np.sqrt(np.mean(y**2))
|
||||
np.save(noise_rms_path, rms)
|
||||
@@ -230,7 +231,6 @@ def calc_speech_rms(files, silences, rmsDir, fs=44100, plot=False):
|
||||
n = 0
|
||||
for wavfile, sil in zip(f, silences):
|
||||
y, fs, _ = sndio.read(wavfile)
|
||||
y = y[:, 1]
|
||||
t = np.arange(y.size)
|
||||
sTemp = np.zeros(t.size, dtype=bool)
|
||||
print("Started")
|
||||
@@ -255,7 +255,7 @@ if __name__ == "__main__":
|
||||
'training TRF decoder by concatenating '
|
||||
'matrix test materials')
|
||||
parser.add_argument('--OutDir', type=PathType(exists=None, type='dir'),
|
||||
default='./stimulus', help='Output directory')
|
||||
default='./noise', help='Output directory')
|
||||
parser.add_argument('--CalcRMS', action='store_true')
|
||||
args = {k:v for k,v in vars(parser.parse_args()).items() if v is not None}
|
||||
|
||||
@@ -266,15 +266,15 @@ if __name__ == "__main__":
|
||||
wavDir = os.path.join(args['OutDir'], "wav")
|
||||
dir_must_exist(wavDir)
|
||||
if args['CalcRMS']:
|
||||
daFile = gen_da_stim(3333, os.path.join(wavDir, '10min_da.wav'))
|
||||
daFile = globDir('./noise_source', 'male_speech_resamp.wav')[0]
|
||||
rmsFiles, peakFiles = gen_rms_peak([daFile], rmsDir, peakDir)
|
||||
rmsFile = rmsFiles[0]
|
||||
peakFile = peakFiles[0]
|
||||
else:
|
||||
daFile = globDir(wavDir, '*.wav')[0]
|
||||
daFile = globDir('./noise_source', 'male_speech_resamp.wav')[0]
|
||||
rmsFile = globDir(rmsDir, '*.npy')[0]
|
||||
peakFile = globDir(peakDir, '*.npy')[0]
|
||||
silences = detect_silences([rmsFile], 44100, None)
|
||||
s_rms = calc_speech_rms([daFile], silences, rmsDir)
|
||||
b = calc_spectrum([daFile], silences)
|
||||
#silences = detect_silences([rmsFile], 44100, None)
|
||||
s_rms = calc_speech_rms(['./stimulus/3000_da.wav'], [[]], rmsDir)
|
||||
b = calc_spectrum([daFile], [np.array([])])
|
||||
y = gen_noise(args['OutDir'], b, 44100, s_rms)
|
||||
+7
-3
@@ -40,8 +40,8 @@ class DaTestThread(BaseThread):
|
||||
'''
|
||||
def __init__(self, sessionFilepath=None,
|
||||
stimFolder='./da_stim/',
|
||||
noiseFilepath="./matrix_test/behavioural_stim/stimulus/wav/noise/noise_norm.wav",
|
||||
noiseRMSFilepath="./matrix_test/behavioural_stim/stimulus/rms/noise_rms.npy",
|
||||
noiseFilepath="./da_stim/noise/wav/noise/noise_norm.wav",
|
||||
noiseRMSFilepath="./da_stim/noise/rms/noise_rms.npy",
|
||||
daPeakFilepath="./da_stim/stimulus/peak/10min_da_peak.npy",
|
||||
red_coef="./calibration/out/reduction_coefficients/da_red_coef.npy",
|
||||
cal_coef="./calibration/out/calibration_coefficients/da_cal_coef.npy",
|
||||
@@ -157,7 +157,11 @@ class DaTestThread(BaseThread):
|
||||
out_wavpath = os.path.join(out_dir, fp)
|
||||
stim_rms = np.load(rms)
|
||||
match_ratio = stim_rms/self.noise_rms
|
||||
block_mix_wavs(wav, self.noise_path, out_wavpath, 1.*self.reduction_coef, snr_fs*match_ratio*self.reduction_coef)
|
||||
set_trace()
|
||||
block_mix_wavs(wav, self.noise_path, out_wavpath,
|
||||
1.*self.reduction_coef,
|
||||
snr_fs*match_ratio*self.reduction_coef,
|
||||
mute_left=True)
|
||||
self.stim_paths.extend([out_wavpath] * self.nTrials)
|
||||
writer.writerow([wav, snr_fs, rms, si, snr])
|
||||
# TODO: Output SI/snrs of each file to a CSV file
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
sys.path.insert(0, "../helper_modules")
|
||||
sys.path.insert(0, "../matrix_test/helper_modules/")
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
@@ -101,8 +101,14 @@ def block_process_wav(wavpath, out_wavpath, func, block_size=4096, **args):
|
||||
out_wav.write_frames(y)
|
||||
i += block_size
|
||||
|
||||
def window_rms(a, window_size):
|
||||
print("Squaring...")
|
||||
a2 = a**2
|
||||
print("Convolving...")
|
||||
window = np.ones(window_size)/float(window_size)
|
||||
return np.sqrt(np.convolve(a2, window, 'same'))
|
||||
|
||||
def block_mix_wavs(wavpath_a, wavpath_b, out_wavpath, a_gain=1., b_gain=1., block_size=4096):
|
||||
def block_mix_wavs(wavpath_a, wavpath_b, out_wavpath, a_gain=1., b_gain=1., block_size=4096, mute_left=False):
|
||||
'''
|
||||
Mix two wav files, applying gains to each
|
||||
'''
|
||||
@@ -124,6 +130,8 @@ def block_mix_wavs(wavpath_a, wavpath_b, out_wavpath, a_gain=1., b_gain=1., bloc
|
||||
y[:, 0] = x1[:, 0] + x2
|
||||
y[:, 1] = x1[:, 1] + x2
|
||||
y[:, 2] = x1[:, 2]
|
||||
if mute_left:
|
||||
y[:, 0] = 0.0
|
||||
else:
|
||||
y = x1 + x2
|
||||
out_wav.write_frames(y)
|
||||
|
||||
Reference in New Issue
Block a user