Skip to content

Commit 5c082c5

Browse files
authored
Merge branch 'main' into hotfix/test-audiodataset
2 parents 2a8aa9d + e1627eb commit 5c082c5

28 files changed

Lines changed: 928 additions & 71 deletions

docs/source/api.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
:maxdepth: 2
66

77
publicapi
8-
coreapi
8+
coreapi
9+
utils

docs/source/coreapi_usage.rst

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,59 @@ The data is fetched seamlessly on-demand from the audio file(s). The opening/clo
110110

111111
Eventual time gap between audio items are filled with ``0.`` values.
112112

113+
Normalization
114+
"""""""""""""
115+
116+
The fetched audio data can be normalized according to the presets given by the :class:`osekit.utils.audio_utils.Normalization` flag:
117+
118+
.. list-table:: Normalization presets
119+
:widths: 10 10
120+
:header-rows: 1
121+
122+
* - Name
123+
- Description
124+
* - ``Normalization.RAW``
125+
- :math:`x`
126+
* - ``Normalization.DC_REJECT``
127+
- :math:`x-\overline{ x }`
128+
* - ``Normalization.PEAK``
129+
- :math:`\frac{x}{x_\text{max}}`
130+
* - ``Normalization.ZSCORE``
131+
- :math:`\frac{ x-\overline{x} }{\sigma (x)}`
132+
133+
To normalize the data, simply set the :attr:`osekit.core_api.audio_data.AudioData.normalization` property to the
134+
requested normalization flag:
135+
136+
.. code-block:: python
137+
138+
from osekit.core_api.audio_data.AudioData import AudioData
139+
from osekit.utils.audio_utils.normalization import Normalization
140+
141+
ad = AudioData(...)
142+
ad.normalization = Normalization.ZSCORE # Note: normalization also is a parameter of the AudioData initializer
143+
144+
v = ad.get_value() # The fetched data will then be normalized
145+
146+
.. note::
147+
148+
The ``Normalization.DC_REJECT`` normalization can be combined with any single other normalization:
149+
150+
.. code-block:: python
151+
152+
from osekit.utils.audio_utils.normalization import Normalization
153+
154+
dc_peak = Normalization.DC_REJECT | Normalization.PEAK
155+
156+
.. warning::
157+
158+
Instantiating another combination of normalizations will raise an error:
159+
160+
.. code-block:: python
161+
162+
from osekit.utils.audio_utils.normalization import Normalization
163+
164+
incorrect_normalization = Normalization.RAW | Normalization.PEAK
165+
incorrect_normalization = Normalization.DC_REJECT | Normalization.RAW | Normalization.PEAK
113166
114167
Calibration
115168
"""""""""""
@@ -124,8 +177,8 @@ allows for retrieving the data in the shape of the recorded acoustic pressure.
124177

125178
.. code-block:: python
126179
127-
from osekit.core_api.instrument import Instrument
128180
from osekit.core_api.audio_data import AudioData
181+
from osekit.core_api.instrument import Instrument
129182
import numpy as np
130183
131184
instrument = Instrument(end_to_end_db = 150) # The raw 1. WAV value equals 150 dB SPL re 1 uPa
@@ -170,6 +223,7 @@ an ``AudioDataset`` from a given folder containing audio files:
170223
171224
from pathlib import Path
172225
from osekit.core_api.audio_dataset import AudioDataset
226+
from osekit.core_api.instrument import Instrument
173227
from pandas import Timestamp, Timedelta
174228
175229
folder = Path(r"...")
@@ -179,7 +233,9 @@ an ``AudioDataset`` from a given folder containing audio files:
179233
strptime_format="%y_%m_%d_%H_%M_%S", # To parse the files begin Timestamp
180234
begin=Timestamp("2009-01-06 12:00:00"),
181235
end=Timestamp("2009-01-06 14:00:00"),
182-
data_duration=Timedelta("10s")
236+
data_duration=Timedelta("10s"),
237+
instrument=Instrument(end_to_end_db=150),
238+
normalization="dc_reject"
183239
)
184240
185241
The resulting ``AudioDataset`` will contain 10s-long ``AudioData`` ranging from ``2009-01-06 12:00:00`` to ``2009-01-06 14:00:00``.
@@ -366,4 +422,4 @@ should be provided:
366422
ltas.plot()
367423
plt.show()
368424
369-
A ``SpectroData`` object can be turned into a ``LTASData`` thanks to the :meth:`osekit.core_api.ltas_data.LTASData.from_spectro_data` method.
425+
A ``SpectroData`` object can be turned into a ``LTASData`` thanks to the :meth:`osekit.core_api.ltas_data.LTASData.from_spectro_data` method.

docs/source/example_ltas.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This LTAS will:
1313
* Start at the begin of the first audio file
1414
* End at the end of the last audio file
1515
* Be downsampled at ``24 kHz``
16+
* Have its DC component removed
1617

1718
| The FFT used for computing the spectrograms will use a ``1024 samples``-long hamming window.
1819
| The ``hop`` of LTAS ``ShortTimeFFT`` objects is forced to the size of the window (no overlap).

docs/source/example_ltas_core.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"audio_folder = Path(r\"_static/sample_audio\")\n",
5252
"\n",
5353
"from osekit.core_api.audio_dataset import AudioDataset\n",
54+
"from osekit.utils.audio_utils import Normalization\n",
5455
"from osekit.core_api.instrument import Instrument\n",
5556
"\n",
5657
"audio_data = AudioDataset.from_folder(\n",
@@ -60,7 +61,10 @@
6061
").data[0]\n",
6162
"\n",
6263
"# Resampling at 24 kHz\n",
63-
"audio_data.sample_rate = 24_000"
64+
"audio_data.sample_rate = 24_000\n",
65+
"\n",
66+
"# Removing the DC component\n",
67+
"audio_data.normalization = Normalization.DC_REJECT"
6468
]
6569
},
6670
{

docs/source/example_ltas_public.ipynb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@
141141
"metadata": {},
142142
"outputs": [],
143143
"source": [
144+
"from osekit.utils.audio_utils import Normalization\n",
144145
"from osekit.public_api.analysis import Analysis, AnalysisType\n",
145146
"\n",
146147
"analysis = Analysis(\n",
147148
" analysis_type=AnalysisType.SPECTROGRAM\n",
148149
" | AnalysisType.MATRIX, # we want to export both the spectrogram and the sx matrix\n",
149150
" nb_ltas_time_bins=3000, # This will turn the regular spectrum computation in a LTAS\n",
150151
" sample_rate=sample_rate,\n",
152+
" normalization=Normalization.DC_REJECT, # Removes the DC component\n",
151153
" fft=sft,\n",
152154
" v_lim=(0.0, 150.0), # Boundaries of the spectrograms\n",
153155
" colormap=\"viridis\", # Default value\n",
@@ -203,7 +205,11 @@
203205
"cell_type": "code",
204206
"execution_count": null,
205207
"id": "e05d653bc1e8bfe2",
206-
"metadata": {},
208+
"metadata": {
209+
"tags": [
210+
"remove-cell"
211+
]
212+
},
207213
"outputs": [],
208214
"source": [
209215
"# Reset the dataset to get all files back to place.\n",

docs/source/example_multiple_spectrograms.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ In this example, we want to export spectrograms drawn from the sample audio data
99
* Last spectrogram ends at ``2022-09-25 22:36:25``
1010
* Spectrograms represent ``5 s``-long audio data
1111
* Audio data are downsampled sampled at ``24 kHz`` before spectrograms are computed
12+
* The DC component of the audio data is rejected before spectrograms are computed
1213
* Spectrograms that are in the gap between recordings should be skipped
1314

1415
The FFT used for computing the spectrograms will use a ``1024 samples``-long hamming window, with a ``128 samples``-long hop.

docs/source/example_multiple_spectrograms_core.ipynb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"\n",
5353
"from osekit.core_api.audio_dataset import AudioDataset\n",
5454
"from osekit.core_api.instrument import Instrument\n",
55+
"from osekit.utils.audio_utils import Normalization\n",
5556
"from pandas import Timestamp, Timedelta\n",
5657
"\n",
5758
"audio_dataset = AudioDataset.from_folder(\n",
@@ -61,6 +62,8 @@
6162
" end=Timestamp(\"2022-09-25 22:36:25\"),\n",
6263
" data_duration=Timedelta(seconds=5),\n",
6364
" instrument=Instrument(end_to_end_db=150.0),\n",
65+
" sample_rate=24_000,\n",
66+
" normalization=Normalization.DC_REJECT,\n",
6467
")"
6568
]
6669
},
@@ -192,7 +195,7 @@
192195
"source": [
193196
"import matplotlib.pyplot as plt\n",
194197
"\n",
195-
"spectro_dataset.data[0].plot()\n",
198+
"spectro_dataset.data[1].plot()\n",
196199
"plt.show()"
197200
]
198201
},

docs/source/example_multiple_spectrograms_public.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"outputs": [],
141141
"source": [
142142
"from osekit.public_api.analysis import Analysis, AnalysisType\n",
143+
"from osekit.utils.audio_utils import Normalization\n",
143144
"from pandas import Timestamp, Timedelta\n",
144145
"\n",
145146
"analysis = Analysis(\n",
@@ -150,6 +151,7 @@
150151
" end=Timestamp(\"2022-09-25 22:36:25\"),\n",
151152
" data_duration=Timedelta(seconds=5),\n",
152153
" sample_rate=sample_rate,\n",
154+
" normalization=Normalization.DC_REJECT,\n",
153155
" fft=sft,\n",
154156
" v_lim=(0.0, 150.0), # Boundaries of the spectrograms\n",
155157
" colormap=\"viridis\", # Default value\n",

docs/source/example_reshaping_multiple_files.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ In this example, we want to export reshaped files from the sample audio dataset
99
* Last file ends at ``2022-09-25 22:36:25``
1010
* Files are ``5 s``-long
1111
* Files are sampled at ``24 kHz``
12+
* Files are DC-filtered
1213
* Files that are in the gap between recordings should be skipped
1314

1415
.. toctree::

docs/source/example_reshaping_multiple_files_core.ipynb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"audio_folder = Path(r\"_static/sample_audio\")\n",
4848
"\n",
4949
"from osekit.core_api.audio_dataset import AudioDataset\n",
50+
"from osekit.utils.audio_utils import Normalization\n",
5051
"from pandas import Timestamp, Timedelta\n",
5152
"\n",
5253
"audio_dataset = AudioDataset.from_folder(\n",
@@ -55,6 +56,8 @@
5556
" begin=Timestamp(\"2022-09-25 22:35:15\"),\n",
5657
" end=Timestamp(\"2022-09-25 22:36:25\"),\n",
5758
" data_duration=Timedelta(seconds=5),\n",
59+
" sample_rate=24_000,\n",
60+
" normalization=Normalization.DC_REJECT,\n",
5861
")"
5962
],
6063
"outputs": [],

0 commit comments

Comments
 (0)