Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/jmrplens/phonometry. Standards-conformant acoustics library for Python: measurement and metrology, docs.jmrp.io/phonometry
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768# Copyright (c) 2020. Jose M. Requena-Plens"""Octave-Band and Fractional Octave-Band filter for signals in the time domain.Implementation according to ANSI s1.11-2004 and IEC 61260-1-2014."""
from __future__ import annotations
from functools import lru_cachefrom typing import List, Tuple, overload, Literal
import numpy as np
from .calibration import CalibrationWarning, calculate_sensitivityfrom .environmental import composite_rating_level, lden, ldnfrom .compliance import verify_filter_classfrom .core import OctaveFilterBankfrom .frequencies import getansifrequencies, normalizedfreqfrom .intensity import ( FieldIndicators, IntensityResult, dynamic_capability_index, field_indicators, sound_intensity,)from .levels import laeq, lc_peak, leq, lex_8h, ln_levels, sel, sound_exposurefrom .loudness import ZwickerLoudness, loudness_zwicker, loudness_zwicker_from_spectrumfrom .loudness_ecma import EcmaLoudness, loudness_ecmafrom .loudness_moore_glasberg import ( MooreGlasbergLoudness, loudness_moore_glasberg, loudness_moore_glasberg_from_spectrum, loudness_moore_glasberg_from_third_octave,)from .loudness_moore_glasberg_time import ( MooreGlasbergTimeVaryingLoudness, loudness_moore_glasberg_time,)from .loudness_contours import equal_loudness_contour, hearing_threshold, loudness_levelfrom .sharpness import sharpness_din, sharpness_din_from_specificfrom .sti import STIResult, sti_from_impulse_response, stipa, stipa_signalfrom .tonality_ecma import EcmaTonality, tonality_ecmafrom .roughness_ecma import EcmaRoughness, roughness_ecmafrom .tonality import ToneAssessment, prominence_ratio, tone_to_noise_ratiofrom .parametric_filters import ( TimeWeighting, WeightingFilter, linkwitz_riley, time_weighting, weighting_filter,)from .insulation import ( AirborneInsulationResult, FacadeInsulationResult, ImpactInsulationResult, ImpactRatingResult, WeightedRatingResult, airborne_insulation, energy_average_level, facade_insulation, impact_insulation, weighted_impact_rating, weighted_rating,)from .lab_insulation import ( LabAirborneInsulationResult, LabImpactInsulationResult, LabInsulationWarning, background_correction, lab_airborne_insulation, lab_impact_insulation,)from .open_plan import OpenPlanResult, open_plan_metricsfrom .sound_power import ( MeteorologicalCorrection, PrecisionCriteria, PrecisionFieldIndicators, PrecisionIntensityResult, PrecisionSoundPowerResult, SoundPowerResult, SoundPowerWarning, background_noise_correction, environmental_correction, measurement_positions, meteorological_corrections, precision_background_correction, precision_field_indicators, precision_positions, precision_qualification, precision_uncertainty, sound_power_anechoic, sound_power_intensity_precision, sound_power_pressure,)from .sound_power_reverberation import ( ReverberationSoundPowerResult, sound_power_comparison, sound_power_reverberation,)from .sound_power_intensity import ( SoundPowerIntensityResult, sound_power_intensity,)from .scattering_diffusion import ( BASE_PLATE_BANDS_HZ, BASE_PLATE_MAX_SCATTERING, TWO_DIMENSIONAL_SOURCE_WEIGHTS, DiffusionResult, ScatteringDiffusionWarning, ScatteringResult, ScatteringUncertainty, absorption_coefficient_uncertainty, air_attenuation_coefficient, area_factors, base_plate_scattering, check_base_plate_scattering, directional_diffusion, directional_diffusion_coefficient, normalized_diffusion_coefficient, random_incidence_absorption, random_incidence_diffusion, reverberation_time_uncertainty, scattering_coefficient, scattering_coefficient_spectrum, scattering_coefficient_uncertainty, specular_absorption_coefficient, speed_of_sound,)from .road_absorption import ( DEFAULT_MIC_HEIGHT, DEFAULT_SOURCE_HEIGHT, DEFAULT_SPEED_OF_SOUND, PART1_FREQUENCY_RANGE, SPOT_FREQUENCY_RANGE, SPOT_NARROW_BAND_RANGE, InsituAbsorptionResult, RoadAbsorptionWarning, absorption_reference_corrected, adrienne_window, check_spot_frequency_range, geometric_spreading_factor, geometric_spreading_factor_angle, insitu_absorption_coefficient, insitu_absorption_from_reflection, insitu_absorption_spectrum, insitu_reflection_factor, max_sampled_area_radius, msa_major_axis, one_third_octave_absorption, power_reflection_coefficient, reflected_path_delay, spot_internal_loss_correction, spot_microphone_spacing_bounds, spot_tube_upper_frequency,)from .room_acoustics import ( DecayCurve, RoomAcousticsResult, decay_curve, room_parameters,)from .sound_absorption import ( AbsorptionWarning, absorption_area, absorption_coefficient, attenuation_from_alpha,)from .air_absorption import ( AtmosphericAbsorptionWarning, air_attenuation, air_attenuation_m,)from .impedance_tube import ( ImpedanceTubeResult, ImpedanceTubeWarning, TransferMatrix, absorption_from_reflection, air_density_astm, air_density_iso, air_layer_transfer_matrix, apply_mic_calibration, characteristic_impedance, face_quantities, mic_calibration_factor, normalized_surface_admittance, normalized_surface_impedance, plane_wave_frequency_range, reflection_factor, speed_of_sound_astm, speed_of_sound_iso, standing_wave_absorption, standing_wave_normalized_impedance, standing_wave_ratio_from_level, standing_wave_reflection, standing_wave_reflection_magnitude, surface_impedance, transfer_matrix_one_load, transfer_matrix_two_load, tube_attenuation_constant, tube_wavenumber, two_microphone_impedance, wave_decomposition,)from .airflow_resistance import ( AirflowResistanceWarning, StaticAirflowResult, airflow_resistance, airflow_resistivity, alternating_airflow_resistance, effective_kappa, linear_airflow_velocity, piston_volume_flow_rate, specific_airflow_resistance, static_airflow_resistance, thermal_boundary_layer_thickness,)from .absorption_rating import ( AbsorptionRatingResult, OCTAVE_BANDS_HZ, REFERENCE_CURVE, THIRD_OCTAVE_BANDS_HZ, absorption_class, practical_absorption_coefficient, weighted_absorption,)from .outdoor_propagation import ( DEFAULT_FREQUENCIES, Barrier, OutdoorAttenuation, atmospheric_absorption, barrier_attenuation, directivity_omega, geometric_divergence, ground_attenuation, ground_attenuation_alternative, meteorological_correction, outdoor_propagation_attenuation, predicted_receiver_level,)from .room_ir import ( impulse_response, inverse_filter, mls_impulse_response, mls_signal, sweep_signal,)from .building_prediction import ( AirbornePredictionResult, FlankingPath, ImpactPredictionResult, PathContribution, combine_linings, equivalent_impact_level, flanking_element, flanking_path, impact_flanking_correction, junction_min_vibration_reduction, junction_vibration_reduction, predicted_airborne_insulation, predicted_impact_insulation, standardized_impact_level,)from .building_uncertainty import ( COVERAGE_FACTORS, BandUncertainty, UncertainValue, band_uncertainty, combine_uncertainties, coverage_factor, expanded_uncertainty, maximum_repeatability_standard_deviation, prediction_input_uncertainty, reduce_by_independent_measurements, satisfies_lower_requirement, satisfies_upper_requirement, single_number_uncertainty, single_number_uncertainty_uncorrelated, uncertain_value,)from .occupational_exposure import ( COVERAGE_FACTOR, INSTRUMENT_U2, ExposureResult, ExposureWarning, Task, TaskContribution, full_day_exposure, job_based_exposure, minimum_cumulative_duration_hours, table_c4_contribution, task_based_exposure,)from ._version import __version__
# Public methods__all__ = [ "__version__", "octavefilter", "getansifrequencies", "normalizedfreq", "OctaveFilterBank", "WeightingFilter", "weighting_filter", "time_weighting", "TimeWeighting", "linkwitz_riley", "calculate_sensitivity", "leq", "laeq", "ln_levels", "lc_peak", "sel", "sound_exposure", "lex_8h", "loudness_zwicker", "loudness_zwicker_from_spectrum", "ZwickerLoudness", "loudness_ecma", "EcmaLoudness", "loudness_moore_glasberg", "loudness_moore_glasberg_from_spectrum", "loudness_moore_glasberg_from_third_octave", "MooreGlasbergLoudness", "loudness_moore_glasberg_time", "MooreGlasbergTimeVaryingLoudness", "tonality_ecma", "EcmaTonality", "roughness_ecma", "EcmaRoughness", "sharpness_din", "sharpness_din_from_specific", "equal_loudness_contour", "loudness_level", "hearing_threshold", "tone_to_noise_ratio", "prominence_ratio", "ToneAssessment", "sound_intensity", "IntensityResult", "field_indicators", "FieldIndicators", "dynamic_capability_index", "sti_from_impulse_response", "stipa", "stipa_signal", "STIResult", "sweep_signal", "inverse_filter", "impulse_response", "mls_signal", "mls_impulse_response", "room_parameters", "decay_curve", "DecayCurve", "RoomAcousticsResult", "absorption_area", "absorption_coefficient", "attenuation_from_alpha", "AbsorptionWarning", "air_attenuation", "air_attenuation_m", "AtmosphericAbsorptionWarning", # Impedance tube (ISO 10534-1/-2, ASTM E2611) "ImpedanceTubeResult", "ImpedanceTubeWarning", "TransferMatrix", "absorption_from_reflection", "air_density_astm", "air_density_iso", "air_layer_transfer_matrix", "apply_mic_calibration", "characteristic_impedance", "face_quantities", "mic_calibration_factor", "normalized_surface_admittance", "normalized_surface_impedance", "plane_wave_frequency_range", "reflection_factor", "speed_of_sound_astm", "speed_of_sound_iso", "standing_wave_absorption", "standing_wave_normalized_impedance", "standing_wave_ratio_from_level", "standing_wave_reflection", "standing_wave_reflection_magnitude", "surface_impedance", "transfer_matrix_one_load", "transfer_matrix_two_load", "tube_attenuation_constant", "tube_wavenumber", "two_microphone_impedance", "wave_decomposition", # Airflow resistance (ISO 9053-1/-2) "AirflowResistanceWarning", "StaticAirflowResult", "airflow_resistance", "airflow_resistivity", "alternating_airflow_resistance", "effective_kappa", "linear_airflow_velocity", "piston_volume_flow_rate", "specific_airflow_resistance", "static_airflow_resistance", "thermal_boundary_layer_thickness", # Absorption rating (ISO 11654) "AbsorptionRatingResult", "OCTAVE_BANDS_HZ", "REFERENCE_CURVE", "THIRD_OCTAVE_BANDS_HZ", "absorption_class", "practical_absorption_coefficient", "weighted_absorption", "DEFAULT_FREQUENCIES", "Barrier", "OutdoorAttenuation", "atmospheric_absorption", "barrier_attenuation", "directivity_omega", "geometric_divergence", "ground_attenuation", "ground_attenuation_alternative", "meteorological_correction", "outdoor_propagation_attenuation", "predicted_receiver_level", "open_plan_metrics", "OpenPlanResult", "sound_power_pressure", "measurement_positions", "background_noise_correction", "environmental_correction", "SoundPowerResult", "SoundPowerWarning", "sound_power_reverberation", "sound_power_comparison", "ReverberationSoundPowerResult", "sound_power_intensity", "SoundPowerIntensityResult", # ISO 3745 / ISO 9614-3 precision sound power "sound_power_anechoic", "PrecisionSoundPowerResult", "precision_positions", "precision_background_correction", "meteorological_corrections", "MeteorologicalCorrection", "precision_uncertainty", "sound_power_intensity_precision", "PrecisionIntensityResult", "precision_field_indicators", "PrecisionFieldIndicators", "precision_qualification", "PrecisionCriteria", # ISO 17497-1/-2 scattering & diffusion "speed_of_sound", "air_attenuation_coefficient", "random_incidence_absorption", "specular_absorption_coefficient", "scattering_coefficient", "scattering_coefficient_spectrum", "ScatteringResult", "base_plate_scattering", "BASE_PLATE_BANDS_HZ", "BASE_PLATE_MAX_SCATTERING", "check_base_plate_scattering", "reverberation_time_uncertainty", "absorption_coefficient_uncertainty", "scattering_coefficient_uncertainty", "ScatteringUncertainty", "directional_diffusion", "directional_diffusion_coefficient", "DiffusionResult", "normalized_diffusion_coefficient", "area_factors", "random_incidence_diffusion", "TWO_DIMENSIONAL_SOURCE_WEIGHTS", "ScatteringDiffusionWarning", # ISO 13472-1/-2 in-situ road-surface absorption "adrienne_window", "geometric_spreading_factor", "geometric_spreading_factor_angle", "reflected_path_delay", "insitu_reflection_factor", "insitu_absorption_from_reflection", "power_reflection_coefficient", "insitu_absorption_coefficient", "insitu_absorption_spectrum", "InsituAbsorptionResult", "absorption_reference_corrected", "one_third_octave_absorption", "max_sampled_area_radius", "msa_major_axis", "spot_tube_upper_frequency", "spot_microphone_spacing_bounds", "check_spot_frequency_range", "spot_internal_loss_correction", "DEFAULT_SOURCE_HEIGHT", "DEFAULT_MIC_HEIGHT", "DEFAULT_SPEED_OF_SOUND", "PART1_FREQUENCY_RANGE", "SPOT_FREQUENCY_RANGE", "SPOT_NARROW_BAND_RANGE", "RoadAbsorptionWarning", "airborne_insulation", "AirborneInsulationResult", "impact_insulation", "ImpactInsulationResult", "weighted_rating", "WeightedRatingResult", "weighted_impact_rating", "ImpactRatingResult", "facade_insulation", "FacadeInsulationResult", "energy_average_level", "lab_airborne_insulation", "LabAirborneInsulationResult", "lab_impact_insulation", "LabImpactInsulationResult", "background_correction", "LabInsulationWarning", "lden", "ldn", "composite_rating_level", "CalibrationWarning", "verify_filter_class", "predicted_airborne_insulation", "AirbornePredictionResult", "predicted_impact_insulation", "ImpactPredictionResult", "junction_vibration_reduction", "junction_min_vibration_reduction", "flanking_path", "flanking_element", "FlankingPath", "PathContribution", "combine_linings", "equivalent_impact_level", "impact_flanking_correction", "standardized_impact_level", "band_uncertainty", "single_number_uncertainty", "single_number_uncertainty_uncorrelated", "maximum_repeatability_standard_deviation", "coverage_factor", "expanded_uncertainty", "uncertain_value", "combine_uncertainties", "prediction_input_uncertainty", "reduce_by_independent_measurements", "satisfies_lower_requirement", "satisfies_upper_requirement", "BandUncertainty", "UncertainValue", "COVERAGE_FACTORS", "task_based_exposure", "job_based_exposure", "full_day_exposure", "minimum_cumulative_duration_hours", "table_c4_contribution", "Task", "TaskContribution", "ExposureResult", "ExposureWarning", "COVERAGE_FACTOR", "INSTRUMENT_U2",]
@lru_cache(maxsize=32)def _cached_filter_bank( fs: int, fraction: float, order: int, limits: Tuple[float, ...] | None, filter_type: str, ripple: float, attenuation: float, calibration_factor: float, dbfs: bool,) -> OctaveFilterBank: """Design (or reuse) a stateless filter bank for octavefilter().""" return OctaveFilterBank( fs=fs, fraction=fraction, order=order, limits=list(limits) if limits is not None else None, filter_type=filter_type, ripple=ripple, attenuation=attenuation, calibration_factor=calibration_factor, dbfs=dbfs, )
@overloaddef octavefilter( x: List[float] | np.ndarray, # NOSONAR - public API fs: int, fraction: float = 1, order: int = 6, limits: List[float] | None = None, show: bool = False, sigbands: Literal[False] = False, plot_file: str | None = None, detrend: bool = True, filter_type: str = "butter", ripple: float = 0.1, attenuation: float = 72.0, calibration_factor: float = 1.0, dbfs: bool = False, mode: str = "rms", nominal: Literal[False] = False,) -> Tuple[np.ndarray, List[float]]: ...
@overloaddef octavefilter( x: List[float] | np.ndarray, # NOSONAR - public API fs: int, fraction: float = 1, order: int = 6, limits: List[float] | None = None, show: bool = False, sigbands: Literal[True] = True, plot_file: str | None = None, detrend: bool = True, filter_type: str = "butter", ripple: float = 0.1, attenuation: float = 72.0, calibration_factor: float = 1.0, dbfs: bool = False, mode: str = "rms", nominal: Literal[False] = False,) -> Tuple[np.ndarray, List[float], List[np.ndarray]]: ...
@overloaddef octavefilter( x: List[float] | np.ndarray, # NOSONAR - public API fs: int, fraction: float = 1, order: int = 6, limits: List[float] | None = None, show: bool = False, sigbands: Literal[False] = False, plot_file: str | None = None, detrend: bool = True, filter_type: str = "butter", ripple: float = 0.1, attenuation: float = 72.0, calibration_factor: float = 1.0, dbfs: bool = False, mode: str = "rms", nominal: Literal[True] = ...,) -> Tuple[np.ndarray, List[str]]: ...
@overloaddef octavefilter( x: List[float] | np.ndarray, # NOSONAR - public API fs: int, fraction: float = 1, order: int = 6, limits: List[float] | None = None, show: bool = False, sigbands: Literal[True] = True, plot_file: str | None = None, detrend: bool = True, filter_type: str = "butter", ripple: float = 0.1, attenuation: float = 72.0, calibration_factor: float = 1.0, dbfs: bool = False, mode: str = "rms", nominal: Literal[True] = ...,) -> Tuple[np.ndarray, List[str], List[np.ndarray]]: ...
def octavefilter( x: List[float] | np.ndarray, # NOSONAR - public API fs: int, fraction: float = 1, order: int = 6, limits: List[float] | None = None, show: bool = False, sigbands: bool = False, plot_file: str | None = None, detrend: bool = True, filter_type: str = "butter", ripple: float = 0.1, attenuation: float = 72.0, calibration_factor: float = 1.0, dbfs: bool = False, mode: str = "rms", nominal: bool = False,) -> Tuple[np.ndarray, List[float]] | Tuple[np.ndarray, List[str]] | Tuple[np.ndarray, List[float], List[np.ndarray]] | Tuple[np.ndarray, List[str], List[np.ndarray]]: """ Filter a signal with octave or fractional octave filter bank.
This method uses a filter bank with Second-Order Sections (SOS) coefficients. To obtain the correct coefficients, automatic subsampling is applied to the signal in each filtered band.
Multichannel support: If x is 2D (channels, samples), each channel is filtered.
:param x: Input signal (1D array or 2D array [channels, samples]). :type x: Union[List[float], np.ndarray] :param fs: Sample rate in Hz. :type fs: int :param fraction: Bandwidth 'b'. Examples: 1/3-octave b=3, 1-octave b=1, 2/3-octave b=1.5. Default: 1. :type fraction: float :param order: Order of the filter. Default: 6. :type order: int :param limits: Minimum and maximum limit frequencies [f_min, f_max]. Default [12, 20000]. :type limits: Optional[List[float]] :param show: If True, plot and show the filter response. :type show: bool :param sigbands: If True, also return the signal in the time domain divided into bands. :type sigbands: bool :param plot_file: Path to save the filter response plot. :type plot_file: Optional[str] :param detrend: If True, remove DC offset before filtering. Default: True. :type detrend: bool :param filter_type: Type of filter ('butter', 'cheby1', 'cheby2', 'ellip', 'bessel'). Default: 'butter' (the only type that meets IEC 61260-1 class 1 with the default parameters). :param ripple: Passband ripple in dB (for cheby1, ellip). Default: 0.1. :param attenuation: Stopband attenuation in dB (for cheby2, ellip). Default: 72.0. For ``cheby2`` scipy pins the deep-stopband floor at exactly this value, so it must be >= 70 dB to clear the IEC 61260-1 class 1 limit (matches :class:`OctaveFilterBank`). :param calibration_factor: Calibration factor for SPL calculation. Default: 1.0. :param dbfs: If True, return results in dBFS. Default: False. :param mode: 'rms' or 'peak'. Default: 'rms'. :param nominal: If True, return IEC 61260-1 nominal frequency labels (List[str]) instead of exact floats. :return: A tuple containing (SPL_array, Frequencies_list) or (SPL_array, Frequencies_list, signals). When *nominal=True*, the frequency list contains ``List[str]`` labels instead of floats. :rtype: Union[Tuple[np.ndarray, List[float]], Tuple[np.ndarray, List[str]], Tuple[np.ndarray, List[float], List[np.ndarray]], Tuple[np.ndarray, List[str], List[np.ndarray]]] """ if show or plot_file: # Plotting has side effects: bypass the cache. filter_bank = OctaveFilterBank( fs=fs, fraction=fraction, order=order, limits=limits, filter_type=filter_type, ripple=ripple, attenuation=attenuation, show=show, plot_file=plot_file, calibration_factor=calibration_factor, dbfs=dbfs, ) else: # The bank is immutable in non-stateful mode: reuse the design. # Pass limits through as-is (tuple for hashability); the bank # constructor is the single place that validates them and owns # the default when None. limits_key = tuple(map(float, limits)) if limits is not None else None filter_bank = _cached_filter_bank( fs, fraction, order, limits_key, filter_type, ripple, attenuation, calibration_factor, dbfs, )
return filter_bank.filter(x, sigbands=sigbands, mode=mode, detrend=detrend, nominal=nominal) # type: ignore[call-overload,no-any-return]