<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Load signal2&nbsp;&nbsp;Compute loudness of the whole signal3&nbsp;&nbsp;Compute loudness per signal segments4&nbsp;&nbsp;Compute tonality from spectrum  How to compute acoustic Loudness according to Zwicker method for stationary signalsThis tutorial explains how to use MOSQITO to compute the acoustic loudness of a signal according to the Zwicker method for stationary signals (as per ISO 532-1 section 5). For more information on the implementation and validation of the metric, you can refer to the [documentation](../docs/loudness_zwst.md).The following commands are used to import the necessary functions.<jupyter_code># Add MOSQITO to the Python path
import sys
sys.path.append('..')

# Import numpy
import numpy as np
# Import plot function
import matplotlib.pyplot as plt
# Import spectrum computation tool
from scipy.fft import fft, fftfreq
# Import mosqito functions
from mosqito.utils import load
from mosqito.sq_metrics import loudness_zwst
from mosqito.sq_metrics import loudness_zwst_perseg
from mosqito.sq_metrics import loudness_zwst_freq

# Import MOSQITO color sheme [Optional]
from mosqito import COLORS

# To get inline plots (specific to Jupyter notebook)
%matplotlib notebook<jupyter_output><empty_output><jupyter_text>Load signalIn this tutorial, the signal is imported from a .wav file. The tutorial [Audio signal basic operations](./tuto_signal_basic_operations.ipynb) gives more information about the syntax of the import and the other supported file types. You can use any .wav file to perform the tutorial or you can download the [pink noise signal](../validations/sq_metrics/loudness_zwst/input/ISO_532-1/Test%20signal%205%20%28pinknoise%2060%20dB%29.wav) from MOSQITO that is used in the following.<jupyter_code># Define path to the .wav file
# To be replaced by your own path
path = "../validations/sq_metrics/loudness_zwst/input/ISO_532_1/Test signal 5 (pinknoise 60 dB).wav"
# load signal
sig, fs = load(path, wav_calib=2 * 2 **0.5)
# plot signal
t = np.linspace(0, (len(sig) - 1) / fs, len(sig))
plt.figure(1)
plt.plot(t, sig, color=COLORS[0])
plt.xlabel('Time [s]')
plt.ylabel('Acoustic pressure [Pa]')<jupyter_output><empty_output><jupyter_text>Compute loudness of the whole signalThe acoustic loudness is computed by using the following command line. In addition to the signal (as ndarray) and the sampling frequency, the function takes 1 input argument: "field_type" that can be set to "free" or "diffuse" depending on the environment of the audio signal recording.<jupyter_code>N, N_specific, bark_axis = loudness_zwst(sig, fs, field_type="free")<jupyter_output><empty_output><jupyter_text>The function return the Loudness of the signal :<jupyter_code>print("N_zwst = {:.1f} sone".format(N) )<jupyter_output>N_zwst = 10.4 sone<jupyter_text>The specific loudness over critical band rate is also computed :<jupyter_code>plt.figure(2)
plt.plot(bark_axis, N_specific, color=COLORS[0])
plt.xlabel("Critical band rate [Bark]")
plt.ylabel("N'_zwst [sone/Bark]")<jupyter_output><empty_output><jupyter_text>Compute loudness per signal segmentsTo compute the loudnes for successive, possibly overlaping, time segments, you can use the loudness_zwst_perseg function. It accepts two more input paramters:- nperseg: to define the length of each segment- noverlap: to define the number of points to overlap between segments<jupyter_code>N, N_specific, bark_axis, time_axis = loudness_zwst_perseg(
    sig, fs, nperseg=8192 * 2, noverlap=4096
)
plt.figure(3)
plt.plot(time_axis, N, color=COLORS[0])
plt.xlabel("Time [s]")
plt.ylabel("N_zwst [sone]")
plt.ylim((0, 15))<jupyter_output><empty_output><jupyter_text>Compute tonality from spectrumThe commands below shows how to compute the stationary loudness from a frequency spectrum either in complex values or amplitude values using the functions from MOSQITO. One should note that only stationary values can be computed from a frequency input. The input spectrum can be either 1D with size (Nfrequency) or 2D with size (fNrequency x Ntime). The corresponding time axis can be either the same for all the spectra, with size (Nfrequency) or different for each spectrum with size (Nfrequency x Ntime).One should pay attention that the input spectrum must be in RMS values !<jupyter_code># Compute spectrum
n = len(sig)
spec = np.abs(2 / np.sqrt(2) / n * fft(sig)[0:n//2])
freqs = fftfreq(n, 1/fs)[0:n//2]
# Compute Loudness
N, N_specific, bark_axis = loudness_zwst_freq(spec, freqs)

print("N_zwst = {:.1f} sone".format(N) )<jupyter_output>N_zwst = 10.4 sone<jupyter_text>---<jupyter_code>from datetime import date
print("Tutorial generation date:", date.today().strftime("%B %d, %Y"))<jupyter_output>Tutorial generation date: March 30, 2022
{'fraction_non_alphanumeric': 0.08948030176026824, 'fraction_numerical': 0.017812238055322716, 'mean_word_length': 6.071111111111111, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '409710', 'license': 'Apache-2.0', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1506, 'n_words': 541, 'path': 'tutorials/tuto_loudness_zwst.ipynb', 'repo_name': 'wantysal/MoSQITooo', 'size': '4772'}