Skip to content

Commit

Permalink
spectrogram tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
yiitozer committed Apr 24, 2024
1 parent ac0a9a1 commit 1a135d0
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 29 deletions.
175 changes: 163 additions & 12 deletions demo_spectrogram.ipynb

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions libsoni/core/spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def sonify_spectrogram(spectrogram: np.ndarray,
Parameters
----------
spectrogram: np.ndarray
spectrogram: np.ndarray (np.float32 / np.float64) [shape=(N, K)]
Spectrogram to be sonified.
frequency_coefficients: np.ndarray, default = None
frequency_coefficients: np.ndarray (np.float32 / np.float64) [shape=(N, )], default = None
Array containing frequency coefficients, in Hertz.
time_coefficients: np.ndarray, default = None
time_coefficients: np.ndarray (np.float32 / np.float64) [shape=(K, )], default = None
Array containing time coefficients, in seconds.
sonification_duration: int, default = None
Expand All @@ -42,13 +42,7 @@ def sonify_spectrogram(spectrogram: np.ndarray,
spectrogram_sonification: np.ndarray (np.float32 / np.float64) [shape=(M, )]
Sonified spectrogram.
"""

# Check if lengths of coefficient vectors match shape of spectrogram
assert spectrogram.shape[0] == len(frequency_coefficients),\
f'The length of frequency_coefficients must match spectrogram.shape[0]'

assert spectrogram.shape[1] == len(time_coefficients), \
f'The length of time_coefficients must match spectrogram.shape[1]'
__check_spect_shape(spectrogram, len(frequency_coefficients), len(time_coefficients))

# Calculate Hop size from time_coefficients if not explicitly given
H = int((time_coefficients[1] - time_coefficients[0]) * fs)
Expand All @@ -72,7 +66,6 @@ def sonify_spectrogram(spectrogram: np.ndarray,
spectrogram_sonification += (sinusoid * weighting_vector)

spectrogram_sonification = fade_signal(spectrogram_sonification, fs=fs, fading_duration=fading_duration)

spectrogram_sonification = normalize_signal(spectrogram_sonification) if normalize else spectrogram_sonification

return spectrogram_sonification
Expand Down Expand Up @@ -114,11 +107,7 @@ def sonify_spectrogram_multi(spectrogram: np.ndarray,
spectrogram_sonification: np.ndarray (np.float32 / np.float64) [shape=(M, )]
Sonified spectrogram.
"""

assert spectrogram.shape[0] == len(frequency_coefficients), \
f'The length of frequency_coefficients must match spectrogram.shape[0]'
assert spectrogram.shape[1] == len(time_coefficients), \
f'The length of time_coefficients must match spectrogram.shape[1]'
__check_spect_shape(spectrogram, len(frequency_coefficients), len(time_coefficients))

if num_processes is None:
num_processes = os.cpu_count() or 1
Expand Down Expand Up @@ -154,9 +143,9 @@ def sonify_spectrogram_multi(spectrogram: np.ndarray,

return spectrogram_sonification


def __sonify_chunk(args):
start, end, spectrogram_chunk, frequency_coefficients_chunk, time_coefficients, num_samples, H, fs = args

spectrogram_sonification_chunk = np.zeros(num_samples)

for i in range(spectrogram_chunk.shape[0]):
Expand All @@ -173,3 +162,14 @@ def __sonify_chunk(args):
spectrogram_sonification_chunk += (sinusoid * weighting_vector)
return spectrogram_sonification_chunk


def __check_spect_shape(spect: np.ndarray,
num_freq_bins: int,
num_time_frames: int):
# Check if lengths of coefficient vectors match shape of spectrogram
if not spect.shape[0] == num_freq_bins:
raise ValueError('The length of frequency_coefficients must match spectrogram.shape[0]')

if not spect.shape[1] == num_time_frames:
raise ValueError('The length of time_coefficients must match spectrogram.shape[1]')

0 comments on commit 1a135d0

Please sign in to comment.