Code Monkey home page Code Monkey logo

Comments (2)

bmcfee avatar bmcfee commented on June 20, 2024

Following using resamply with default arguments, the signal amplitude is sometimes larger than the maximum energy. For a waveform, the amplitude sometimes is smaller / larger than [-1, 1].

This is intended behavior. Sample rate conversion does not assume a maximum amplitude; it's just resampling the signal by interpolation after applying a low-pass filter.

Often, there are different use cases for sample rate conversion that call for different scalings. For instance, you might want the total energy (in the sum-squared sense) of the signal to be conserved after resampling, which would not be achieved by preserving the peak amplitude. (If this is confusing, imagine reducing the sampling rate, so that you have fewer samples, but the total energy must add up to the original: you'd have to scale things up to make it fit.) Other times, you actually want the peak amplitude to be preserved.

If you want easier control over this behavior, I'd suggest using librosa.resample which wraps resampy but provides a scale=[True|False] parameter to give you direct control over this. Here's a quick example:

In [1]: import numpy as np

In [2]: import librosa

In [3]: y, sr = librosa.load(librosa.util.example_audio_file(), sr=None)

In [4]: y2 = librosa.resample(y, sr, sr * 0.5, scale=False)  # Preserve peaks

In [5]: y3 = librosa.resample(y, sr, sr * 0.5, scale=True)  # Preserve energy

In [6]: np.max(np.abs(y)), np.max(np.abs(y2)), np.max(np.abs(y3))
Out[6]: (0.710968, 0.7105063, 1.0048077)

In [7]: np.sum(y**2), np.sum(y2**2), np.sum(y3**2)
Out[7]: (31691.62, 15843.37, 31686.74)

The numbers aren't exact because this is all finite-sample approximation, but they're pretty close.

What is a safe way of dealing with this without messing up the quality of the audio?

What do you mean by "quality" in this context? The signal's peak amplitude might change, but the noise due to sampling artifacts should generally be low.

from resampy.

PetrochukM avatar PetrochukM commented on June 20, 2024

Thank you!

Following further research, librosa does not handle the problem. It does include a scale parameter but that parameter does not guarantee a min or max amplitude.

The solution I found, supported by the popular library SoX, is either selectively lowering the volume (gain) when resampling causes a large min/max or to clip the amplitude at [-1, 1].

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.