Fixed bugs

This commit is contained in:
2019-01-17 11:50:21 +00:00
parent 4b7d0b372d
commit 9ca1f9b152
12 changed files with 161 additions and 28 deletions
+44 -2
View File
@@ -5,10 +5,10 @@
"major" : 6,
"minor" : 1,
"revision" : 10,
"architecture" : "x64"
"architecture" : "x86"
}
,
"rect" : [ 0.0, 44.0, 1012.0, 678.0 ],
"rect" : [ 0.0, 44.0, 1920.0, 990.0 ],
"bglocked" : 0,
"openinpresentation" : 0,
"default_fontsize" : 12.0,
@@ -28,6 +28,48 @@
"digest" : "",
"tags" : "",
"boxes" : [ {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"frgb" : 0.0,
"id" : "obj-17",
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 889.0, 694.0, 150.0, 20.0 ],
"text" : "0.06574271"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"frgb" : 0.0,
"id" : "obj-18",
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 408.0, 215.0, 81.75, 20.0 ],
"text" : "80 dB SPL"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
"frgb" : 0.0,
"id" : "obj-7",
"maxclass" : "comment",
"numinlets" : 1,
"numoutlets" : 0,
"patching_rect" : [ 847.0, 646.0, 150.0, 20.0 ],
"text" : "72 dB SPL / 70db HL"
}
}
, {
"box" : {
"fontname" : "Arial",
"fontsize" : 12.0,
+1 -1
View File
@@ -22,7 +22,7 @@ def calc_potential_max(wavs, noise_filepath, out_dir, out_name):
noise_rms = np.sqrt(np.mean(x**2))
max_noise_samp = max(np.abs(x))
snr = 0.
snr = -15.
snr_fs = 10**(-snr/20)
max_noise_samp *= max_wav_rms/noise_rms
max_sampl = max_wav_samp+(max_noise_samp*snr_fs)
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
sys.path.insert(0, "../matrix_test/helper_modules")
import numpy as np
from pathops import dir_must_exist
from filesystem import globDir
from pysndfile import sndio
import os
from signalops import block_process_wav
import matplotlib.pyplot as plt
def main():
'''
'''
fs = 44100
f = 1000.0
n = np.arange(fs * 60 * 5)
y = np.sin(2*np.pi*f*n/fs)
coef = np.load('./out/calibration_coefficients/click_cal_coef.npy')
y *= coef
sndio.write("./out/stimulus/1k_tone.wav", y, fs, format='wav', enc='pcm16')
if __name__ == "__main__":
main()
+26 -6
View File
@@ -129,20 +129,33 @@ def gen_indexes():
y[i] = x.copy()
return y
def gen_rms(files, OutDir):
def gen_rms_peak(files, OutRMSDir, OutPeakDir):
rmsFiles = []
peakFiles = []
for file in files:
head, tail = os.path.split(file)
tail = os.path.splitext(tail)[0]
tail = tail + "_rms.npy"
dir_must_exist(OutDir)
rmsFilepath = os.path.join(OutDir, tail)
dir_must_exist(OutRMSDir)
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, y_rms)
np.save(rmsFilepath, peak)
rmsFiles.append(rmsFilepath)
return rmsFiles
head, tail = os.path.split(file)
tail = os.path.splitext(tail)[0]
tail = tail + "_peak.npy"
dir_must_exist(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
def detect_silences(rmsFiles, fs, seg_min_size=0.02):
print("Detecting silence in wav files...")
@@ -167,6 +180,7 @@ 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)
@@ -216,6 +230,7 @@ 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")
@@ -246,14 +261,19 @@ if __name__ == "__main__":
rmsDir = os.path.join(args['OutDir'], "rms")
dir_must_exist(rmsDir)
peakDir = os.path.join(args['OutDir'], "peak")
dir_must_exist(peakDir)
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'))
rmsFiles = gen_rms([daFile], rmsDir)
rmsFiles, peakFiles = gen_rms_peak([daFile], rmsDir, peakDir)
rmsFile = rmsFiles[0]
peakFile = peakFiles[0]
else:
daFile = globDir(wavDir, '*.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)
+4 -2
View File
@@ -40,8 +40,9 @@ class DaTestThread(BaseThread):
'''
def __init__(self, sessionFilepath=None,
stimFolder='./da_stim/',
noiseFilepath="./matrix_test/behavioural_stim/stimulus/wav/noise/noise.wav",
noiseFilepath="./matrix_test/behavioural_stim/stimulus/wav/noise/noise_norm.wav",
noiseRMSFilepath="./matrix_test/behavioural_stim/stimulus/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",
nTrials=2, socketio=None, participant=None, srt_50=None,
@@ -58,7 +59,7 @@ class DaTestThread(BaseThread):
self.nTrials = nTrials
self.trial_ind = 0
self._stopevent = Event()
self.si = np.array([20.0, 35.0, 50.0, 65.0, 80.0, 90.0, 100.0])
self.si = np.array([20.0, 35.0, 50.0, 65.0, 80.0, 90.0])
super(DaTestThread, self).__init__(self.test_name,
sessionFilepath=sessionFilepath,
@@ -130,6 +131,7 @@ class DaTestThread(BaseThread):
shuffle(self.si)
x = logit(self.si * 0.01)
snrs = (x/(4*s_50))+srt_50
snrs = np.append(snrs, np.inf)
self.snr_fs = 10**(-snrs/20)
self.snr_fs[self.snr_fs == np.inf] = 0.
if (self.snr_fs == -np.inf).any():
+5 -1
View File
@@ -83,7 +83,7 @@ class EEGMatTrainThread(BaseThread):
self.socketio.on_event('submit_eeg_response', self.submitTestResponse, namespace='/main')
self.socketio.on_event('finalise_results', self.finaliseResults, namespace='/main')
self.dev_mode = True
self.dev_mode = False
def loadStimulus(self):
'''
@@ -112,7 +112,11 @@ class EEGMatTrainThread(BaseThread):
wavs = globDir(self.stim_folder, "*.wav")
questions = globDir(self.stim_folder, "stim_questions_*.csv")
if not len(questions):
raise FileNotFoundError("No question files found in {}".format(self.stim_dir))
rms_files = globDir(self.stim_folder, "stim_*_rms.npy")
if not len(rms_files):
raise FileNotFoundError("No rms files found in {}".format(self.stim_dir))
self.socketio.emit('test_stim_load', namespace='/main')
# Add noise to audio files at set SNRs and write to participant
+5 -4
View File
@@ -65,7 +65,7 @@ class EEGTestThread(BaseThread):
# Percent speech inteligibility (estimated using behavioural measure)
# to present stimuli at
self.si = np.array([20.0, 35.0, 50.0, 65.0, 80.0, 90.0, 100.0])
self.si = np.array([20.0, 35.0, 50.0, 65.0, 80.0, 90.0])
self.trial_ind = 0
self._stopevent = Event()
@@ -200,7 +200,8 @@ class EEGTestThread(BaseThread):
s_50 *= 0.01
x = logit(self.si * 0.01)
snrs = (x/(4*s_50))+srt_50
snr_map = pd.DataFrame({"speech_intel" : self.si, "snr": snrs})
snrs = np.append(snrs, np.inf)
snr_map = pd.DataFrame({"speech_intel" : np.append(self.si, 0.0), "snr": snrs})
save_dir = self.participant.data_paths['eeg_test/stimulus']
snr_map_path = os.path.join(save_dir, "snr_map.csv")
snr_map.to_csv(snr_map_path)
@@ -225,7 +226,7 @@ class EEGTestThread(BaseThread):
audio, fs, enc, fmt = sndio.read(wav, return_format=True)
speech = audio[:, :2]
#triggers = audio[:, 2]
triggers = audio[:, 2]
wf = []
for ind2, s in enumerate(snr):
start = randint(0, noise_file.frames()-speech.shape[0])
@@ -245,7 +246,7 @@ class EEGTestThread(BaseThread):
out_wav = (speech+(np.stack([noise, noise], axis=1)*snr_fs))*self.reduction_coef
except:
set_trace()
#out_wav = np.concatenate([out_wav, triggers[:, np.newaxis]], axis=1)
out_wav = np.concatenate([out_wav, triggers[:, np.newaxis]], axis=1)
sndio.write(out_wav_path, out_wav, fs, fmt, enc)
np.save(out_meta_path, snr)
wf.append(out_wav_path)
+21 -7
View File
@@ -18,9 +18,9 @@ import matplotlib.pyplot as plt
from pathops import dir_must_exist
try:
from signalops import rolling_window_lastaxis, calc_rms
from signalops import rolling_window_lastaxis, calc_rms, block_process_wav
except ImportError:
from .signalops import rolling_window_lastaxis, block_lfilter, calc_rms
from .signalops import rolling_window_lastaxis, block_lfilter, calc_rms, block_process_wav
import scipy.signal as sgnl
from scipy.stats import pearsonr
@@ -46,6 +46,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:
@@ -56,8 +57,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):
@@ -129,18 +131,27 @@ def gen_indexes():
def gen_rms(files, OutDir):
rmsFiles = []
OutPeakDir = './stimulus/peak'
for sentenceList in files:
for file in sentenceList:
head, tail = os.path.split(file)
tail = os.path.splitext(tail)[0]
tail = tail + "_rms.npy"
tail_rms = tail + "_rms.npy"
dir_must_exist(OutDir)
rmsFilepath = os.path.join(OutDir, tail)
rmsFilepath = os.path.join(OutDir, tail_rms)
print("Generating: "+rmsFilepath)
y, fs, _ = sndio.read(file)
y_rms = calc_rms(y, round(0.02*fs))
np.save(rmsFilepath, y_rms)
rmsFiles.append(rmsFilepath)
y, fs, _ = sndio.read(file)
tail_peak = tail + "_peak.npy"
dir_must_exist(OutPeakDir)
peakFilepath = os.path.join(OutPeakDir, tail_peak)
print("Generating: "+peakFilepath)
peak = np.abs(y).max()
np.save(peakFilepath, peak)
return rmsFiles
def detect_silences(rmsFiles, fs):
@@ -199,17 +210,20 @@ 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))
noise_rms_path = os.path.join(noiseRMSDir, 'noise_rms.npy')
y = y/(np.abs(y).max() * 0.95)
rms = np.sqrt(np.mean(y**2))
peak = np.abs(y).max()
np.save(noise_rms_path, rms)
np.save('./stimulus/peak/noise_peak.npy', peak)
return y
def calc_speech_rms(files, silences, rmsDir, fs=44100, plot=False):
'''
'''
pdb.set_trace()
f = sum(files, [])
sumsqrd = 0.0
n = 0
+8 -1
View File
@@ -119,7 +119,14 @@ def block_mix_wavs(wavpath_a, wavpath_b, out_wavpath, a_gain=1., b_gain=1., bloc
x2 = wav_b.read_frames(block_size)
x1 *= a_gain
x2 *= b_gain
out_wav.write_frames(x1 + x2)
if x1.shape[1] == 3:
y = np.zeros(x1.shape)
y[:, 0] = x1[:, 0] + x2
y[:, 1] = x1[:, 1] + x2
y[:, 2] = x1[:, 2]
else:
y = x1 + x2
out_wav.write_frames(y)
i += block_size
+15 -1
View File
@@ -13,6 +13,19 @@ import numpy as np
import multiprocessing
from helper import multiprocess_map
def flat_for(a, f, *args):
a = a.reshape(-1)
for i, v in enumerate(a):
print("Sample {0} of {1}".format(i, a.size))
a[i] = f(v, *args)
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 gen_rms(file, OutDir):
head, tail = os.path.split(file)
tail = os.path.splitext(tail)[0]
@@ -22,7 +35,8 @@ def gen_rms(file, OutDir):
print("Generating: "+rmsFilepath)
y, fs, _ = sndio.read(file)
y_rms = calc_rms(y, round(0.02*fs))
y = y[:, 0]
y_rms = window_rms(y, round(0.02*fs))
np.save(rmsFilepath, y_rms)
return rmsFilepath
+3 -3
View File
@@ -61,11 +61,11 @@ class MatTestThread(BaseThread):
Thread for running server side matrix test operations
'''
def __init__(self, listN=3, sessionFilepath=None,
noiseFilepath="./matrix_test/behavioural_stim/stimulus/wav/noise/noise.wav",
noiseFilepath="./matrix_test/behavioural_stim/stimulus/wav/noise/noise_norm.wav",
noiseRMSFilepath="./matrix_test/behavioural_stim/stimulus/rms/noise_rms.npy",
listFolder="./matrix_test/behavioural_stim/stimulus/wav/sentence-lists/",
red_coef="./calibration/out/reduction_coefficients/mat_red_coef.npy",
cal_coef="./calibration/out/calibration_coefficients/da_cal_coef.npy",
cal_coef="./calibration/out/calibration_coefficients/mat_cal_coef.npy",
socketio=None, participant=None):
self.listDir = listFolder
@@ -224,7 +224,6 @@ class MatTestThread(BaseThread):
sortedPC = percent_correct[sortedSNRind]
x = np.linspace(np.min(sortedSNR)-5, np.max(sortedSNR)+3, 3000)
srt_50, s_50 = res.x
set_trace()
x_y = self.logisticFunction(x, srt_50, s_50)
x_y *= 100.
# np.savez('./plot.npz', x, x_y*100., sortedSNR, sortedPC)
@@ -403,6 +402,7 @@ class MatTestThread(BaseThread):
y = x_noise
# Set speech to start 500ms after the noise, scaled to the desired SNR
sigStart = round(self.fs/2.)
set_trace()
y[sigStart:sigStart+x.size] += x*snr_fs
y *= self.reduction_coef
return y
+1
View File
@@ -265,6 +265,7 @@ class BaseThread(Thread):
Restore thread state from a saved session filepath
'''
with open(filepath, 'rb') as f:
set_trace()
self.__dict__.update(dill.load(f))