Captioned appendix figures.
Polished some figures. Shortened existing figure captions.
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
import glob
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from thunderhopper.filetools import search_files
|
||||
from thunderhopper.modeltools import load_data
|
||||
from thunderhopper.filters import sosfilter
|
||||
from IPython import embed
|
||||
|
||||
# GENERAL SETTINGS:
|
||||
target = 'Omocestus_rufipes'
|
||||
data_path = glob.glob(f'../data/processed/{target}*.npz')[0]
|
||||
data_path = search_files(target, dir='../data/processed/')[0]
|
||||
save_path = '../data/inv/noise_env/'
|
||||
|
||||
# ANALYSIS SETTINGS:
|
||||
scales = np.geomspace(0.1, 10000, 200)
|
||||
scales = np.geomspace(0.01, 1000, 100)
|
||||
sd_inputs = np.array([1.0])
|
||||
n_trials = 10
|
||||
tol_to_one = 0.1
|
||||
n_trials = 100
|
||||
|
||||
# EXECUTION:
|
||||
|
||||
# Load signal data:
|
||||
data, config = load_data(data_path, files='filt')
|
||||
signal, rate = data['filt'], config['rate']
|
||||
data, config = load_data(data_path, files='raw')
|
||||
signal, rate = data['raw'], config['rate']
|
||||
|
||||
# Reduce to song segment and normalize:
|
||||
time = np.arange(signal.shape[0]) / rate
|
||||
@@ -28,67 +26,60 @@ start, end = data['songs_0'].ravel()
|
||||
segment = (time >= start) & (time <= end)
|
||||
signal /= signal[segment].std()
|
||||
|
||||
# Get rescaled signals (time, scale):
|
||||
# Rescale signal (time, scale):
|
||||
signal = signal[:, None] * scales[None, :]
|
||||
|
||||
# Prepare storage:
|
||||
if sd_inputs.size > 1:
|
||||
current_match = 0
|
||||
storage = dict(
|
||||
scales=scales,
|
||||
n_trials=n_trials,
|
||||
sd_factor=np.array([0.]),
|
||||
trials=np.zeros((scales.size, n_trials), dtype=float),
|
||||
mean=np.zeros(scales.size, dtype=float),
|
||||
spread=np.zeros(scales.size, dtype=float),
|
||||
)
|
||||
sd_noise = np.zeros((scales.size, n_trials, sd_inputs.size), dtype=float)
|
||||
|
||||
# Analyze piece-wise:
|
||||
rng = np.random.default_rng()
|
||||
for i, sigma in enumerate(sd_inputs):
|
||||
print(f'Testing SD: {sigma:.3f} ...')
|
||||
|
||||
# Add Gaussian noise of given SD to rescaled signals (time, scale, trial):
|
||||
mix = signal[..., None] + rng.normal(0, sigma, (*signal.shape, n_trials))
|
||||
# Prepare trial storage:
|
||||
sd_trials = np.zeros((segment.sum(), scales.size, n_trials), dtype=float)
|
||||
|
||||
# Get mixture envelopes (time, scale, trial):
|
||||
mix = sosfilter(np.abs(mix), rate, config['env_fcut'], 'lp',
|
||||
padtype='even', padlen=config['padlen'])[segment, ...]
|
||||
# Run trials:
|
||||
for j in range(n_trials):
|
||||
# Mix signals with white noise of target SD:
|
||||
mix = signal + rng.normal(0, sigma, signal.shape)
|
||||
|
||||
# Process mixture:
|
||||
mix = sosfilter(mix, rate, config['bp_fcut'], 'bp',
|
||||
padtype='fixed', padlen=config['padlen'])
|
||||
mix = sosfilter(np.abs(mix), rate, config['env_fcut'], 'lp',
|
||||
padtype='even', padlen=config['padlen'])
|
||||
|
||||
# Log current trial:
|
||||
sd_trials[..., j] = mix[segment, :]
|
||||
|
||||
# Get noise remainders of mean over trials:
|
||||
mix -= mix.mean(axis=-1, keepdims=True)
|
||||
sd_trials -= sd_trials.mean(axis=-1, keepdims=True)
|
||||
|
||||
# Estimate noise SD:
|
||||
sd = mix.std(axis=0)
|
||||
# Average SD over trials:
|
||||
mean_sd = sd.mean(axis=-1)
|
||||
sd_noise[:, :, i] = sd_trials.std(axis=0)
|
||||
|
||||
# Log single-run results:
|
||||
if sd_inputs.size == 1:
|
||||
storage = dict(
|
||||
scales=scales,
|
||||
n_trials=n_trials,
|
||||
sd_factor=sigma,
|
||||
trials=sd,
|
||||
mean=mean_sd,
|
||||
spread=sd.std(axis=-1),
|
||||
)
|
||||
break
|
||||
# # Add Gaussian noise of given SD to rescaled signals (time, scale, trial):
|
||||
# mix = signal[..., None] + rng.normal(0, sigma, (*signal.shape, n_trials))
|
||||
|
||||
# Update multi-run results if better than previous:
|
||||
n_match = (np.abs(1 - mean_sd) <= tol_to_one).sum()
|
||||
if n_match > current_match:
|
||||
print(f'Found better SD: {sigma:.3f} with {n_match} matches (previous: {current_match})')
|
||||
storage['sd_factor'][0] = sigma
|
||||
storage['trials'][:, :] = sd
|
||||
storage['mean'][:] = mean_sd
|
||||
storage['spread'][:] = sd.std(axis=-1)
|
||||
current_match = n_match
|
||||
del mix
|
||||
del signal
|
||||
# # Get mixture envelopes (time, scale, trial):
|
||||
# mix = sosfilter(np.abs(mix), rate, config['env_fcut'], 'lp',
|
||||
# padtype='even', padlen=config['padlen'])[segment, ...]
|
||||
|
||||
# # Get noise remainders of mean over trials:
|
||||
# mix -= mix.mean(axis=-1, keepdims=True)
|
||||
|
||||
# # Estimate noise SD:
|
||||
# sd_noise[:, :, i] = mix.std(axis=0)
|
||||
|
||||
if save_path is not None:
|
||||
np.savez(save_path + 'sd_conversion.npz', **storage)
|
||||
archive = dict(
|
||||
scales=scales,
|
||||
sd_input=sd_inputs,
|
||||
sd_noise=sd_noise,
|
||||
)
|
||||
np.savez(save_path + 'sd_conversion.npz', **archive)
|
||||
|
||||
print('Done.')
|
||||
embed()
|
||||
|
||||
Reference in New Issue
Block a user