56 lines
1.9 KiB
Python
Executable File
56 lines
1.9 KiB
Python
Executable File
#!/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
|
|
|
|
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 = -15.0
|
|
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.wav"
|
|
|
|
story_wavs = globDir(story_dir, '*.wav')
|
|
mat_wavs = globDir(mat_dir, '*.wav')
|
|
|
|
out_dir = "./out"
|
|
dir_must_exist(out_dir)
|
|
story_coef = calc_potential_max(story_wavs, noise_file, out_dir, "story_red_coef")
|
|
mat_coef = calc_potential_max(mat_wavs, noise_file, out_dir, "mat_red_coef")
|
|
da_coef = calc_potential_max(da_files, noise_file, out_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"
|
|
|
|
if __name__ == "__main__":
|
|
main()
|