Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dtype specification in abs2, rms #1665

Merged
merged 2 commits into from Feb 9, 2023
Merged

allow dtype specification in abs2, rms #1665

merged 2 commits into from Feb 9, 2023

Conversation

bmcfee
Copy link
Member

@bmcfee bmcfee commented Feb 9, 2023

Reference Issue

fixes #1656

What does this implement/fix? Explain your changes.

This PR allows a user to specify a target dtype in abs2 and rms calculations.

This resolves the issue with rms calculation on integer inputs noted in #1656 . For example, if dtype is not specified, we get overflow:

In [14]: x = np.arange(1, 32768, dtype=np.int16)

In [15]: librosa.util.abs2(x)
Out[15]: array([1, 4, 9, ..., 9, 4, 1], dtype=int16)

If dtype is specified, we get proper outputs:

In [16]: librosa.util.abs2(x, dtype=np.float32)
Out[16]: 
array([1.00000000e+00, 4.00000000e+00, 9.00000000e+00, ...,
       1.07354522e+09, 1.07361075e+09, 1.07367629e+09], dtype=float32)

Float32 is the default for rms now, as the result will be averaged and then sqrt'ed anyway.

Any other comments?

I've also snuck in a replacement of np.abs(S)**2 by util.abs2(S) in the rms calculation. Not sure how we missed that one before, but it's fixed now.

@bmcfee bmcfee added bug Something doesn't work like it should enhancement Does this improve existing functionality? functionality Does this add new functionality? labels Feb 9, 2023
@bmcfee bmcfee added this to the 0.10.0 milestone Feb 9, 2023
@bmcfee bmcfee self-assigned this Feb 9, 2023
@codecov
Copy link

codecov bot commented Feb 9, 2023

Codecov Report

Base: 98.79% // Head: 98.79% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (3916583) compared to base (26439ac).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1665   +/-   ##
=======================================
  Coverage   98.79%   98.79%           
=======================================
  Files          34       34           
  Lines        4559     4563    +4     
=======================================
+ Hits         4504     4508    +4     
  Misses         55       55           
Flag Coverage Δ
unittests 98.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
librosa/feature/spectral.py 99.17% <100.00%> (+<0.01%) ⬆️
librosa/util/utils.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@bmcfee bmcfee merged commit 3e2257c into main Feb 9, 2023
@bmcfee bmcfee deleted the abs2-dtype branch February 9, 2023 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something doesn't work like it should enhancement Does this improve existing functionality? functionality Does this add new functionality?
Development

Successfully merging this pull request may close these issues.

Overflow in feature.rms when dtype='int16'
1 participant