'rasampy' output ZeroDivisionError

172 views
Skip to first unread message

taityo

unread,
Nov 26, 2019, 9:29:32 AM11/26/19
to librosa
I use Google Translation.
I'm sorry that this sentence may be wrong.

I use raspberry pi zero.
Installing librosa on raspberry pi was successful.
However, when resampling by the program, the following error was output.
Please help me.

Traceback (most recent call last):
  File "comp.py", line 33, in <module>
    x,fs = librosa.load(path,DEFAULT_FS)
  File "/usr/local/lib/python3.7/dist-packages/librosa/core/audio.py", line 156, in load
    y = resample(y, sr_native, sr, res_type=res_type)
  File "/usr/local/lib/python3.7/dist-packages/librosa/core/audio.py", line 532, in resample
    y_hat = resampy.resample(y, orig_sr, target_sr, filter=res_type, axis=-1)
  File "/usr/local/lib/python3.7/dist-packages/resampy/core.py", line 120, in resample
    resample_f(x_2d, y_2d, sample_ratio, interp_win, interp_delta, precision)
ZeroDivisionError: integer division by zero

__________________________________________________________________
Python version : 3.7.3

pip list

Package       Version
------------- ------------
audioread     2.1.8
certifi       2018.8.24
cffi          1.13.2
chardet       3.0.4
Click         7.0
Cython        0.29.14
decorator     4.3.0
Flask         1.1.1
idna          2.6
itsdangerous  1.1.0
Jinja2        2.10.3
joblib        0.14.0
librosa       0.7.1
llvmlite      0.30.0
MarkupSafe    1.1.1
numba         0.46.0
numpy         1.17.4
olefile       0.46
Pillow        5.4.1
pip           18.1
pycparser     2.19
pydub         0.23.1
PyGObject     3.30.4
python-apt    1.8.4
requests      2.21.0
resampy       0.2.2
scikit-learn  0.21.3
scipy         1.3.3
setuptools    40.8.0
six           1.13.0
SoundFile     0.10.3.post1
ssh-import-id 5.7
urllib3       1.24.1
Werkzeug      0.16.0
wheel         0.33.6

Program    comp.py
import time
from enum import Enum
import numpy as np
import librosa

PATH_LIST = "wav_list.txt"

DEFAULT_FS = 8000

class Feature_Types(Enum):
    SPECTRUM = 1
    SPECTRUM_CENTROID = 2
    MFCC = 3

feature_type = Feature_Types.MFCC

print("#1 [Wav files read]")

with open(PATH_LIST) as f:
    path_list = [line.strip() for line in f.readlines()] 

x_and_fs_list = []
for path in path_list:
    x, fs = librosa.load(path, DEFAULT_FS)
    x_and_fs_list.append((x, fs))

print("> | {} : {}".format("Index""Path"))
for index in range(len(path_list)):
    print("> | {} : {}".format(index + 1, path_list[index]))

print("")

print("#2 [Feature extraction]")

print("> Selected feature type : {}".format(feature_type.name))

feature_list = []
for x_and_fs in x_and_fs_list:
    x = x_and_fs[0]
    fs = x_and_fs[1]
    if feature_type == Feature_Types.SPECTRUM:
        feature = np.abs(librosa.stft(x))
    elif feature_type == Feature_Types.SPECTRUM_CENTROID:
        feature = librosa.feature.spectral_centroid(x, fs)
    elif feature_type == Feature_Types.MFCC:
        feature = librosa.feature.mfcc(x, fs)
    feature_list.append(feature)

print("")

start = time.time()

print("#3 [Evaluation]")

reference_index = 0
reference_feature = feature_list[reference_index]
print("> Reference : {} ({})".format(reference_index + 1, path_list[reference_index]))

eval_list = []
for target_feature in feature_list:
    ac, wp = librosa.sequence.dtw(reference_feature, target_feature)
    eval = 1 - (ac[-1][-1] / np.array(ac).max())
    eval_list.append(eval)

print("> | {} , {} : {}".format("Reference""Target""Score"))
for target_index in range(len(eval_list)):
    eval = eval_list[target_index]
    print("> | {} , {} : {}".format(reference_index + 1, target_index + 1round(eval4)))

print("")

end = time.time()

print("Total elapsed time : {}[sec]".format(round(end - start, 4)))

Brian McFee

unread,
Nov 26, 2019, 9:42:15 AM11/26/19
to librosa
Thanks for the detailed report.  There are only two divisions that happen inside the resampy sinc interpolation function: one where the ratio of sampling rates is computed (fs_new / fs_old) and one where the linear interpolation points are computed: https://github.com/bmcfee/resampy/blob/master/resampy/interpn.py#L12  (the index_step variable is the only divisor used inside this function).

My guess is that the calculation of index_step is going to 0 for some reason, either because the ratio of sampling rates is too extreme (unlikely) or possibly because of a data encoding problem in the pre-computed filter lengths.

It would be helpful if you could produce a minimal example with a generated test signal that triggers the failure you're observing, so we can rule out any problems relating to the underlying data.

taityo

unread,
Nov 26, 2019, 11:57:04 AM11/26/19
to librosa
Thank you your answer.

I'm so sorry to forget to say.
I have already tried with ubuntu18.04 and confirmed that it works.
However, the same program did not work on Raspberry pi.
Does Librosa not work with Raspbian buster?

2019年11月26日火曜日 23時29分32秒 UTC+9 taityo:

Brian McFee

unread,
Nov 26, 2019, 2:57:18 PM11/26/19
to librosa
It ought to work, but we don't have a rasperry pi machine as part of our continuous integration testing, so it's not on the "officially supported" list.

As I said above, it would be most helpful if you could make a minimal example code snippet that reproduces the error without relying on external data files.

If that's not possible, could you report the native sampling rate of the file that causes the error?

Additionally, if you can run

>>> import resampy
>>> resampy.filters.get_filter('kaiser_best')

and report the results, it could help.  You should see something like:

(array([9.47593717e-01, 9.47588371e-01, 9.47572335e-01, ...,
        1.66052480e-08, 1.65032915e-08, 1.64011831e-08]),
 array(512),
 array(0.94759372))


If the second value of the tuple (array(512)) is off, that could cause the error you're seeing.  If that still looks good on your end though, I think the problem might be in numba and out of our control.

taityo

unread,
Nov 26, 2019, 10:13:22 PM11/26/19
to librosa
I tried your suggestion.

First,I made  a minimal example code snippet that reproduces the error without relying on external data files.

Code:
import resampy
import numpy as np
# Generate a sine wave at 440 Hz for 5 seconds
sr_orig = 44100.0
x = np.sin(2 * np.pi * 440.0 / sr_orig * np.arange(5 * sr_orig))
# Resample to 22050 with default parameters
resampy.resample(x, sr_orig, 22050)

Error:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>

File "/usr/local/lib/python3.7/dist-packages/resampy/core.py", line 120, in resample
resample_f(x_2d, y_2d, sample_ratio, interp_win, interp_delta, precision)
ZeroDivisionError: integer division by zero


Second,I tried to use audio data with a sampling rate of 44100Hz

Third,I tried  resampy.filters.get_filter('kaiser_best').
The output is shown below:
(array([9.47593717e-01, 9.47588371e-01, 9.47572335e-01, ...,
1.66052480e-08, 1.65032915e-08, 1.64011831e-08]), array(512, dtype=int64), array(0.94759372))

I think this output is almost the same as yours.

So I think this error is by Numba.
Is n’t it?


2019年11月26日火曜日 23時29分32秒 UTC+9 taityo:

Brian McFee

unread,
Nov 27, 2019, 12:09:25 PM11/27/19
to librosa
Thanks for the additional information.  I think this is probably an error in numba.

Digging into your example a bit more, the "sample_ratio" parameter in the interpolator will be calculated outside of numba, and evaluate to

float(22050) / 44100.0  = 0.5  (explicitly floating point)

the "index_step" parameter in resampy should then evaluate to "int(0.5 * 512) = 256". (explicitly integer valued).

These are the only two divisors inside the interpolator, and there really shouldn't be any problems with type inference here that cast down to 0.

One more question: does it also happen if you try to resample from 22050 to 44100?  If so, then the problem has to be with the "sample_ratio" calculation, and that would help isolate the bug.
Reply all
Reply to author
Forward
0 new messages