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
BUG: Different results between numpy.fft.rfft and scipy.signal.freqz #17289
Comments
Thanks for reporting the issue, @happyTonakai. That's definitely a bug. It works in some cases (specifically the IIR case where
The parameter |
Yikes I think we just changed the frequency returned, not the actual values. To avoid this problem, I think we should do two things:
@AtsushiSakai are you up for giving it a shot? |
I discussed this in #15273, there is a solution proposed in one my posts there. The frequency grid is wrong, when using rfft. To illustrate, two calls of to freqz give different results:
The output:
|
@drestebon would you be up for opening a PR to fix the accuracy / grid problem? Ultimately we should make sure the performance is correct first, i.e., in a first PR with test cases showing errant behavior on |
@larsoner I will try to squeeze a couple of hours out of next week (or the following) ;) |
* TST: for issues #17289 #15273 * BUG/ENH: fix #17289 #15273 * TST: use correct assert * BENCH: Added a benchmark for signal.freqz * TST/BENCH: improved coverage #17289 #15273 * BENCH: removed vectorized input #17289 #15273 * STY: add missing whitespace [skip ci] --------- Co-authored-by: Jake Bowhay <60778417+j-bowhay@users.noreply.github.com>
* TST: for issues scipy#17289 scipy#15273 * BUG/ENH: fix scipy#17289 scipy#15273 * TST: use correct assert * BENCH: Added a benchmark for signal.freqz * TST/BENCH: improved coverage scipy#17289 scipy#15273 * BENCH: removed vectorized input scipy#17289 scipy#15273 * STY: add missing whitespace [skip ci] --------- Co-authored-by: Jake Bowhay <60778417+j-bowhay@users.noreply.github.com>
Describe your issue.
I'm using the following two functions to calculate the frequency response of an impulse response:
numpy.fft.rfft
andscipy.signal.freqz
, but I get different results. Let's simplify the problem using a small number of FFT.NFFT = 4,
rfft
calculates DFT at frequencies0, pi/2, pi
which is exactly equal tow
returned byfreqz
However
H2
is different fromH1
The DFT at
pi/2
should be real-valued for a real-valued signal, sorfft
gives the correct result.From the source code of
freqz
in line 438-455 ofscipy/signal/_filter_design.py
we can see why it gives the wrong result. It doesn't give the nyquist result at all eveninclude_nyquist
is set toTrue
.Reproducing Code Example
Error message
SciPy/NumPy/Python version information
1.9.3 1.23.4 sys.version_info(major=3, minor=9, micro=0, releaselevel='final', serial=0)
The text was updated successfully, but these errors were encountered: