Updated calibration
This commit is contained in:
@@ -22,7 +22,6 @@ def main():
|
||||
v = np.array(v)
|
||||
v = v/(v.max()*2)
|
||||
v[np.isnan(v)] = 0.0
|
||||
out = {}
|
||||
for key, val in zip(vals.keys(), v):
|
||||
out_file = './out/calibration_coefficients/{}_cal_coef.npy'.format(key)
|
||||
np.save(out_file, val)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,73 +0,0 @@
|
||||
#!/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
|
||||
from shutil import copyfile
|
||||
|
||||
def calc_potential_max(wavs, noise_filepath, out_dir, out_name):
|
||||
max_wav_samp = 0
|
||||
max_wav_rms = 0
|
||||
for wav in wavs:
|
||||
x, fs, enc = sndio.read(wav)
|
||||
max_wav_samp = np.max([max_wav_samp, np.max(np.abs(x))])
|
||||
max_wav_rms = np.max([max_wav_rms, np.sqrt(np.mean(x**2))])
|
||||
x, fs, enc = sndio.read(noise_filepath)
|
||||
noise_rms = np.sqrt(np.mean(x**2))
|
||||
max_noise_samp = max(np.abs(x))
|
||||
|
||||
snr = 5.
|
||||
snr_fs = 10**(-snr/20)
|
||||
max_noise_samp *= max_wav_rms/noise_rms
|
||||
max_sampl = max_wav_samp+(max_noise_samp*snr_fs)
|
||||
reduction_coef = 1.0/max_sampl
|
||||
np.save(os.path.join(out_dir, "{}.npy".format(out_name)), reduction_coef)
|
||||
return reduction_coef
|
||||
|
||||
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_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')
|
||||
|
||||
out_dir = "./out"
|
||||
out_red_dir = os.path.join(out_dir, 'reduction_coefficients')
|
||||
out_stim_dir = os.path.join(out_dir, 'stimulus')
|
||||
dir_must_exist(out_dir)
|
||||
dir_must_exist(out_red_dir)
|
||||
dir_must_exist(out_stim_dir)
|
||||
|
||||
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, 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"
|
||||
click_cal_stim = "../click_stim/click_3000_20Hz.wav"
|
||||
story_cal_stim = "../eeg_story_stim/stimulus/odin_1_1.wav"
|
||||
|
||||
mat_out_stim = os.path.join(out_stim_dir, "mat_cal_stim.wav")
|
||||
click_out_stim = os.path.join(out_stim_dir, "click_cal_stim.wav")
|
||||
da_out_stim = os.path.join(out_stim_dir, "da_cal_stim.wav")
|
||||
story_out_stim = os.path.join(out_stim_dir, "story_cal_stim.wav")
|
||||
|
||||
block_process_wav(mat_cal_stim, mat_out_stim, lambda x: x * mat_coef)
|
||||
block_process_wav(story_cal_stim, story_out_stim, lambda x: x * story_coef)
|
||||
block_process_wav(da_cal_stim, da_out_stim, lambda x: x * da_coef)
|
||||
copyfile(click_cal_stim, click_out_stim)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,38 +0,0 @@
|
||||
#!/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
|
||||
dir_must_exist('./out/calibrated_stim/')
|
||||
sndio.write("./out/calibrated_stim/1k_tone.wav", y, fs, format='wav', enc='pcm16')
|
||||
coef = np.load('./out/calibration_coefficients/da_cal_coef.npy')
|
||||
y, fs, enc = sndio.read('./out/stimulus/da_cal_stim.wav')
|
||||
sndio.write('./out/calibrated_stim/da_cal_stim.wav', y*coef, fs, format='wav', enc='pcm16')
|
||||
coef = np.load('./out/calibration_coefficients/mat_cal_coef.npy')
|
||||
y, fs, enc = sndio.read('./out/stimulus/mat_cal_stim.wav')
|
||||
sndio.write('./out/calibrated_stim/mat_cal_stim.wav', y*coef, fs, format='wav', enc='pcm16')
|
||||
coef = np.load('./out/calibration_coefficients/story_cal_coef.npy')
|
||||
y, fs, enc = sndio.read('./out/stimulus/story_cal_stim.wav')
|
||||
sndio.write('./out/calibrated_stim/story_cal_stim.wav', y*coef, fs, format='wav', enc='pcm16')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -12,6 +12,7 @@ from shutil import copy2
|
||||
import sys
|
||||
import traceback
|
||||
from loggerops import log_exceptions
|
||||
from scipy.io import loadmat
|
||||
|
||||
from pysndfile import sndio, PySndfile
|
||||
from matrix_test.helper_modules.filesystem import globDir
|
||||
@@ -375,6 +376,8 @@ class MatTestThread(BaseThread):
|
||||
# list
|
||||
listAudiofiles = globDir(os.path.join(self.listDir, lists[ind]), "*.wav")
|
||||
listCSV = globDir(os.path.join(self.listDir, lists[ind]), "*.csv")
|
||||
levels = globDir(os.path.join(self.listDir, lists[ind]), "*.mat")
|
||||
|
||||
with open(listCSV[0]) as csv_file:
|
||||
csv_reader = csv.reader(csv_file)
|
||||
# Allocate empty lists to store audio samples, RMS and words of
|
||||
@@ -383,8 +386,9 @@ class MatTestThread(BaseThread):
|
||||
self.listsRMS.append([])
|
||||
self.listsString.append([])
|
||||
# Get data for each sentence
|
||||
for fp, words in zip(listAudiofiles, csv_reader):
|
||||
for fp, words, level_file in zip(listAudiofiles, csv_reader, levels):
|
||||
# Read in audio file and calculate it's RMS
|
||||
level = loadmat(level_file)
|
||||
x, self.fs, _ = sndio.read(fp)
|
||||
x_rms = np.sqrt(np.mean(x**2))
|
||||
self.lists[-1].append(x)
|
||||
|
||||
Reference in New Issue
Block a user