You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Audio processing
Audio signal-to-noise ratio calculation
Copy page
Download PDF
Audio signal-to-noise ratio calculation

Operator introduction

Description

Audio signal-to-noise ratio (SNR) calculator, performs signal-noise separation based on non-negative matrix factorization (NMF)

Key features

  • Performs STFT spectral analysis on audio;
  • Uses NMF to decompose the spectrum into signal and noise components;
  • Calculates SNR (in dB) based on the ratio of the estimated energy of the reconstructed time-domain signal to that of the estimated noise.

Format support

  • Common audio formats: mp3, wav, flac, ogg, aac, m4a
  • Supports both local paths and object storage paths (tos:// or s3://)

Daft invocation

Operator parameters

Input

Input column name

Description

audio_paths

Array of audio paths (supports both local and object storage paths) - Local path: absolute or relative path - Object storage: path starting with "tos://" or "s3://"

Output

Floating-point array (float64), each row corresponds to the SNR value (in dB) of the respective audio.

Parameters

If a parameter does not have a default value, it is required.

Parameter name

Type

Default value

Description

n_components

int

2

Number of components for NMF decomposition. Default: 2 (typically, the first component is considered the signal, and the remaining components are noise)

max_iter

int

200

Maximum number of NMF iterations. Default: 200

Examples

The sample code demonstrates how to use Daft to run the operator to calculate the audio signal-to-noise ratio (SNR, in dB).

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.audio import AudioSNR
from daft.las.functions.udf import las_udf

if __name__ == "__main__":
    if os.getenv("DAFT_RUNNER", "native") == "ray":
        import logging

        import ray

        def configure_logging():
            logging.basicConfig(
                level=logging.INFO,
                format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
                datefmt="%Y-%m-d %H:%M:%S.%s".format(),
            )
            logging.getLogger("tracing.span").setLevel(logging.WARNING)
            logging.getLogger("daft_io.stats").setLevel(logging.WARNING)
            logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING)
            logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING)

        ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging})
        daft.set_runner_ray()
    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    # Construct URL using environment variables
    tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com")
    samples = {
        "audio_path": [
            f"https://{tos_dir_url}/public/shared_audio_dataset/sample.mp3"
        ],
    }

    ds = daft.from_pydict(samples)

    # Use Daft for distributed processing
    snr_udf = las_udf(
        AudioSNR,
        construct_args={
            "n_components": 2,
            "max_iter": 200,
        },
    )

    ds = ds.with_column("snr_db", snr_udf(col("audio_path")))
    ds.show()
    # ╭──────────────────────────────────────────┬──────────╮
    # │ audio_path                               ┆ snr_db   │
    # │ ---                                      ┆ ---      │
    # │ Utf8                                     ┆ Float64  │
    # ╞══════════════════════════════════════════╪══════════╡
    # │ https://las-cn-beijing-publi-…           ┆ 12.34    │
    # ╰──────────────────────────────────────────┴──────────╯
Last updated: 2026.05.12 19:06:34