Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: scaling factor for window with negative value #25122

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

Merged
merged 5 commits into from
Feb 7, 2023

Conversation

gapplef
Copy link
Contributor

@gapplef gapplef commented Feb 1, 2023

PR Summary

Simply drop the np.abs() on window to fix the wrong scaling factor for window with negative value. For more detail refer to #24821

Caution: With this fix, the behavior would change for window with complex value.
With np.abs() on window, it seems can handle complex value, but I don't think it's the right way to do it. As it can't fall back to real value case for complex value with zero imaginary part and negative real part (for example -1 + 0j).
Also, I didn't understand the need for complex window, so here I simply ignore the complex case. And this is consistent with the implementation of scipy.

Closes #24821

Related to #22828 (not sure it can be closed, that seems to try to deal with the complex case as well, but is there one?).

PR Checklist

Documentation and Tests

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

Simply drop the `np.abs()` on window to fix the wrong scaling factor for window with negative value.
For more detail refer to matplotlib#24821

**Caution**: With this fix, the behavior would change for window with complex value.
With `np.abs()` on window, it seems can handle complex value, but I don't think it's the right way to do it. As it can't fall back to real value case for complex value with zero imaginary part and negative real part (for example -1 + 0j).
Also, I didn't understand the need for complex window, so here I simply ignore the complex case. And this is consistent with the implementation of [scipy](https://github.com/scipy/scipy/blob/d9f75db82fdffef06187c9d8d2f0f5b36c7a791b/scipy/signal/_spectral_py.py#L1854-L1859).
@jklymak
Copy link
Member

jklymak commented Feb 1, 2023

I think these are correct. Can you add some tests, particularly if you can somehow create a flattop w/o using scipy (we don't use it for our tests, unfortunately). For tests, I'd calculate the spectrum of random numbers and just show that its average equals an expected value, perhaps with a bit of floating point slop (np.testing.allclose)?

I don't think this needs a what's new or anything - it only applies to windows with negative co-efficients, which are relatively rare.

@jklymak jklymak added this to the v3.7.1 milestone Feb 1, 2023
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Add new test function for scale factor of flattop window.
Also remove the unnecessary `np.abs()` on window in other functions.
@gapplef
Copy link
Contributor Author

gapplef commented Feb 1, 2023

I have add the new function test_psd_window_flattop, with reference to test_psd_window_hanning and test_psd_windowarray_scale_by_freq.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks close to me. Not sure about some of the testing details so please check. Note we expect all the code in tests to be run, so please remove the dead block.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and discussion that lead to it! The lint issue needs to be dealt with, and we need a second review, but I believe this is correct and should go in...

@oscargus
Copy link
Member

oscargus commented Feb 7, 2023

Agree that the second abs in #4328 was not really on purpose (since it was not tested for) and that this should be the correct fix.

Should we close #22828?

@oscargus
Copy link
Member

oscargus commented Feb 7, 2023

Thanks @gapplef and congratulations on your first merged PR! Hope to see you around!

@oscargus oscargus merged commit baebe44 into matplotlib:main Feb 7, 2023
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 7, 2023
@gapplef
Copy link
Contributor Author

gapplef commented Feb 7, 2023

🎉 🎊 Thanks for your discussion and help! @jklymak @oscargus

@ksunden ksunden modified the milestones: v3.7.1, v3.7.0 Feb 9, 2023
@ksunden ksunden mentioned this pull request Feb 20, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[Bug]: Windows correction is not correct in mlab._spectral_helper
4 participants