Code Monkey home page Code Monkey logo

Comments (3)

bmcfee avatar bmcfee commented on July 22, 2024

What's happening here is due to the following lines which calculate the shape of the output array:

resampy/resampy/core.py

Lines 106 to 110 in 2ca177f

sample_ratio = float(sr_new) / sr_orig
# Set up the output shape
shape = list(x.shape)
shape[axis] = int(shape[axis] * sample_ratio)

If you compute the shape directly in python, it reveals a floating point roundoff:

In [1]: 15001 / 12499
Out[1]: 1.2001760140811264

In [2]: 12499 * (15001 / 12499)
Out[2]: 15000.999999999998

which then gets clamped down to 15000 when we cast to int. This is one of those times where the non-associativity of floating point arithmetic can bite you.

Now, we could have used rounding here instead of a floor/integer cast, but that would have caused problems for other input configurations, extrapolating off the end of the input array rather than interpolating.

It's worth noting that behavior with this configuration is not consistent across different resampling engines. Both resampy and samplerate produce 15000 samples in this configuration, probably because they're based on the same calculation. soxr and scipy.signal produce 15001.

Probably your best bet here, if you're really worried about it, is to explicitly construct the time grid you want to sample, and use the resample_nu function.

I'm not sure there's much else we can do, but it might be possible that a different order of operations in the length calculation here could produce more consistent results. (I'm not sure that it is possible in general, but it's worth looking into.)

from resampy.

bmcfee avatar bmcfee commented on July 22, 2024

Note: I've updated the title here because the issue is the length, not the sampling rate (which is correctly computed).

from resampy.

bmcfee avatar bmcfee commented on July 22, 2024

Confirmed that changing the order of operations here does fix the calculation. It may in turn cause other problems, but I somewhat doubt it. The fix in #113 retains integer multiplication first (which should not produce fp errors) and then divides, so there is only one step that can induce round-off error instead of two.

from resampy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.