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

fixed off by one error for FFTmagnitude() #24

Merged
merged 4 commits into from
Aug 22, 2020

Conversation

paradigmn
Copy link
Contributor

The output off a Real-FFT is N/2+1 due to the dc offset. Hence the last frequency bin was discarded.

The output off a Real-FFT is N/2+1 due to the dc offset. Hence the last frequency bin was discarded.
@swharden
Copy link
Owner

swharden commented Aug 21, 2020

Hi @paradigmn, thank you for spotting this and creating a PR! It would be excellent if we could point to a reference to support that this is accurate. In reviewing The Fundamentals of FFT-Based Signal Analysis and Measurement (by NI) they say:

"...to convert from a two-sided spectrum to a single-sided spectrum, discard the second
half of the array and multiply every point except for DC by two."
image

This implies the output length should be N/2, with the first element being the DC component... yet this PR suggests length should be N/2 + 1 🤔

Do you have an authoritative reference we can point to to confirm the correct behavior? Absent this I might hop over to some of the other popular libraries (like numpy for matplotlib) and see what they tend to do...

@paradigmn
Copy link
Contributor Author

I was rewriting some Numpy Python code, when I notices this issue. They explicitly mention the buffer size in their documentation: https://numpy.org/doc/stable/reference/generated/numpy.fft.rfft.html. Would that be sufficient?

@swharden
Copy link
Owner

I was rewriting some Numpy Python code, when I notices this issue. They explicitly mention the buffer size in their documentation: https://numpy.org/doc/stable/reference/generated/numpy.fft.rfft.html. Would that be sufficient?

Yeah, great reference! They say length should be N/2 + 1, and regarding that last value:

If N is even, A[-1] contains the term representing both positive and negative Nyquist frequency (+fs/2 and -fs/2), and must also be purely real.

I'll add some tests to this PR and merge it in shortly. Thanks for your help here @paradigmn!

@paradigmn
Copy link
Contributor Author

no problem, you are welcome :)

RFFT() returns an array of length N/2+1 and is now used to get the real component by FFTmagnitude() which previously returned an array of length N/2. This change will break systems that expected output to contain N/2 values...
swharden#24 verify values are identical to those produced by numpy
@swharden swharden merged commit 37f1de8 into swharden:master Aug 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants