Eomys / MoSQITo

MoSQITo is a unified and modular development framework of key sound quality metrics favoring reproducible science and efficient shared scripting among engineers, teachers and researchers community.
Apache License 2.0
128 stars 43 forks source link

Filter center frequency condition not met #69

Open Antoine101 opened 9 months ago

Antoine101 commented 9 months ago

Hi,

I am trying to calculate Sharpness on 200ms temporal blocks of a signal sampled at 25600Hz. I get the following error:

File ~\Anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File d:\dev\alarm_new\identification\machine learning\postprocess_dataset.py:31
    dataset_sliced = load_slice_compute(

  File d:\dev\ml_tools\ml_tools\audio\machine_learning\load_slice_compute.py:113 in load_slice_compute
    feature_vector.compute(snapshot_norm, sampling_rate, filter_signal, Wn, btype, cepstrum_formula)

  File d:\dev\ml_tools\ml_tools\audio\machine_learning\features.py:58 in compute
    self._computation()

  File d:\dev\ml_tools\ml_tools\audio\machine_learning\features.py:146 in _computation
    result = eval(feature["function"])(self.signals[domain]["y"], function_args)

  File d:\dev\ml_tools\ml_tools\audio\machine_learning\features_functions.py:1088 in sharpness
    sharpness = sharpness_din_st(signal, args_dict["fs"], weighting="din")

  File ~\Anaconda3\lib\site-packages\mosqito\sq_metrics\sharpness\sharpness_din\sharpness_din_st.py:33 in sharpness_din_st
    N, N_specific, _ = loudness_zwst(signal, fs, field_type=field_type)

  File ~\Anaconda3\lib\site-packages\mosqito\sq_metrics\loudness\loudness_zwst\loudness_zwst.py:68 in loudness_zwst
    spec_third, _ = noct_spectrum(signal, fs, fmin=24, fmax=12600)

  File ~\Anaconda3\lib\site-packages\mosqito\sound_level_meter\noct_spectrum\noct_spectrum.py:63 in noct_spectrum
    spec.append(_n_oct_time_filter(sig, fs, fc, alpha))

  File ~\Anaconda3\lib\site-packages\mosqito\sound_level_meter\noct_spectrum\_n_oct_time_filter.py:43 in _n_oct_time_filter
    raise ValueError(

ValueError: ERROR: Design not possible. Filter center frequency shall
            verify: fc <= 0.88 * (fs / 2)

Probing noct_spectrum function with print statements, I see that the last value of fc_vec is 12589.25411794, which doesn't meet the criteria.

Why is that? Are there some requirements on the sampling rate of the signal I pass to the sharpness function? I can't found anything of the sort in the doc.

Thanks in advance!

Antoine

EDIT: After further investigation I understood that it tries to take into account all third octave bands, hence the error in my case where the sampling rate and thus max frequency is too low. Would it be possible to calculate the metrics by omitting some bands? Perhaps it could still be a valuable acoustic indicator/feature.

wantysal commented 9 months ago

Hi,

This is a great observation. During the validation procedure, all test signals are sampled at 48kHz to get a spectrum covering the whole auditory range, that's why we didn't notice. We'll have to add a check about that.

In the case of considering a few frequency bands only, we could add an option to set other values at 0 by default. However, depending on the type of signal you analyze, you could miss some information and significantly change the overall sharpness value. If the omitted bands have a non-zero loudness value, you have to be aware that omitting this noise will impact the sharpness values, and I guess the output won't be coherent with the acum definition. However, if the omitted bands have a zero loudness, this shouldn't change the result.

If your study focuses on a specific frequency range, you could also use the specific value only ;)

Cheers, Salomé

wantysal commented 8 months ago

Hello,

After further investigation, the ISO 532B standard we used to implemente loudness specifies that the sampling frequency should be of 48 kHz minimum. In case of a lower sampling frequency, the signal should be resampled. Since this loudness implementation is used into the sharpness_din_st function, the same rule applies here.

We are going to add a test at the beginning of all concerned functions to raise a warning if the sampling frequency is too low, and we'll resample the signal in that case.

Thanks for raising this issue, Salomé

EDIT : See PR#72

Antoine101 commented 8 months ago

Hi Salomé,

Thank you for reviewing this issue! I will resample my signals before computing the metrics then. For performance reasons we have them sampled at 25600Hz, and resampling them to 48000Hz would certainly be time-consuming. I was asking that because my goal is to distinguish different classes of sounds based on these metrics, so I don't really care about the accuracy of the absolute value in itself. I was thinking that accounting for bands only up to 12800Hz may be enough to show a relative difference between sounds of different classes.

wantysal commented 7 months ago

Hi Antoine,

In our case we stick to the standard requirements so that the results correspond to the defined procedure and give the same values. But in your case I guess it makes sense to use the data you have to rank your signal with the first bands only...if you're sure the distinctive information is not located at higher frequencies !

PR#72 including the sampling frequency check has been merged, you'll have to get around it in your version to use the first bands only ;)

Salomé