Comments (2)
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.
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)
- v0.3.0 produces other results than v0.2.2 HOT 3
- Resampy 0.3.0 is slow for multi-dimensional input HOT 5
- Issues with Numba 0.55.2 HOT 4
- Resampy 0.3.1 slows down other libraries HOT 43
- Use parallel=True by default can be problematic in multiprocessing applications HOT 8
- Bringing import time back down
- Edge Case, rounding error in length calculation HOT 3
- Missing argument in guvectorize decorator ? HOT 2
- Quality issues? HOT 5
- resampling to the same hz HOT 1
- Incompatible with latest numba HOT 3
- Best quality resampling?
- PyPI status HOT 2
- Add linting CI
- Add pypi packaging workflow HOT 1
- Drop legacy python support
- Fix documentation builds HOT 1
- Update README badges
- Improve default filters and make the whole process reproducible HOT 14
- Cache filters on load
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from resampy.