Computing a LTAS with the Public API

Computing a LTAS with the Public API [1]#

As always in the Public API, the first step is to build the project.

An Instrument can be provided to the Project for the WAV data to be converted in pressure units. This will lead the resulting spectra to be expressed in dB SPL (rather than in dB FS):

from pathlib import Path

audio_folder = Path(r"_static/sample_audio/timestamped")

from osekit.public.project import Project
from osekit.core.instrument import Instrument

project = Project(
    folder=audio_folder,
    strptime_format="%y%m%d_%H%M%S",
    instrument=Instrument(end_to_end_db=150.0),
)

project.build()
	2026-03-25 15:41:48,116
Building the project...
	2026-03-25 15:41:48,117
Analyzing original audio files...
	2026-03-25 15:41:48,127
Organizing project folder...
	2026-03-25 15:41:48,131
Build done!

The Public API Project is now analyzed and organized:

print(f"{' DATASET ':#^60}")
print(f"{'Begin:':<30}{str(project.origin_dataset.begin):>30}")
print(f"{'End:':<30}{str(project.origin_dataset.end):>30}")
print(f"{'Sample rate:':<30}{str(project.origin_dataset.sample_rate):>30}\n")

print(f"{' ORIGINAL FILES ':#^60}")
import pandas as pd

pd.DataFrame(
    [
        {
            "Name": f.path.name,
            "Begin": f.begin,
            "End": f.end,
            "Sample Rate": f.sample_rate,
        }
        for f in project.origin_files
    ],
).set_index("Name")
######################### DATASET ##########################
Begin:                                   2022-09-25 22:34:50
End:                                     2022-09-25 22:36:50
Sample rate:                                           48000

###################### ORIGINAL FILES ######################
Begin End Sample Rate
Name
sample_220925_223450.wav 2022-09-25 22:34:50 2022-09-25 22:35:00 48000
sample_220925_223500.wav 2022-09-25 22:35:00 2022-09-25 22:35:10 48000
sample_220925_223510.wav 2022-09-25 22:35:10 2022-09-25 22:35:20 48000
sample_220925_223520.wav 2022-09-25 22:35:20 2022-09-25 22:35:30 48000
sample_220925_223530.wav 2022-09-25 22:35:30 2022-09-25 22:35:40 48000
sample_220925_223600.wav 2022-09-25 22:36:00 2022-09-25 22:36:10 48000
sample_220925_223610.wav 2022-09-25 22:36:10 2022-09-25 22:36:20 48000
sample_220925_223620.wav 2022-09-25 22:36:20 2022-09-25 22:36:30 48000
sample_220925_223630.wav 2022-09-25 22:36:30 2022-09-25 22:36:40 48000
sample_220925_223640.wav 2022-09-25 22:36:40 2022-09-25 22:36:50 48000

Since we will run a spectral transform, we need to define the FFT parameters:

from scipy.signal import ShortTimeFFT
from scipy.signal.windows import hamming

sample_rate = 24_000

sft = ShortTimeFFT(
    win=hamming(1024),
    hop=128,  # This will be forced to len(win) if we compute a LTAS
    fs=sample_rate,
)

To run transforms in the Public API, use the Transform class:

from osekit.utils.audio import Normalization
from osekit.public.transform import Transform, OutputType

transform = Transform(
    output_type=OutputType.SPECTROGRAM
    | OutputType.SPECTRUM,  # we want to export both the spectrogram and the sx spectrum
    nb_ltas_time_bins=3000,  # This will turn the regular spectrum computation in a LTAS
    sample_rate=sample_rate,
    normalization=Normalization.DC_REJECT,  # Removes the DC component
    fft=sft,
    v_lim=(0.0, 150.0),  # Boundaries of the spectrograms
    colormap="viridis",  # Default value
    name="LTAS",
)

Running the transform will compute the LTAS and save the output files to disk.

project.run(transform=transform)
	2026-03-25 15:41:48,182
Creating the audio data...
	2026-03-25 15:41:48,189
Running transform...
	2026-03-25 15:41:48,189
Computing and writing spectrum matrices and spectrograms...
	2026-03-25 15:41:48,996
Transform done!

As for regular spectrum transforms, the output LTAS is stored in a SpectroDataset named after transform.name:

pd.DataFrame(
    [
        {
            "Exported file": list(sd.files)[0].path.name,
            "Begin": sd.begin,
            "End": sd.end,
            "Sample Rate": sd.fft.fs,
        }
        for sd in project.get_output(transform.name).data
    ],
).set_index("Exported file")
Begin End Sample Rate
Exported file
2022_09_25_22_34_50_000000.npz 2022-09-25 22:34:50 2022-09-25 22:36:50 24000
# Reset the project to get all files back to place.
project.reset()