Align spectrum with frequency scale #1464
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At low FFT sizes, it is apparent that the spectrum is not quite aligned with the frequency scale. This is most obvious when peak markers / table are enabled, as the markers don't align with the peaks on the right hand side of the spectrum. (At high FFT sizes, the difference is ~1 pixel, so not visible). But it's also noticeable if you place a marker on a point, and the frequency isn't quite what you'd calculate for the centre of the bin.
This is because the frequency scale goes from -Fs/2/size to Fs/2/size (so size+1 points), but the spectrum line is drawn with size points. The code currently stretches the spectrum over the frequency range, by doing (m_nbBins - 1):
If we change this to m_nbBins, the markers align with the peaks, but we are left with a gap on the right hand side of the spectrum:
To draw up to the end of the scale, we can replicate the Nyquist point from the FFT (i.e the point at N/2 in the FFT output). Currently, we just draw this as the lowest frequency point, but it actually can be considered to be a positive frequency as well, so it can be drawn at both ends of the spectrum, giving us the extra point we need.
For a description of this, see: https://dsp.stackexchange.com/questions/72515/in-the-context-of-dft-where-does-the-nyquist-frequency-sample-belong-in-a-doubl and the FFTshift section in: https://www.gaussianwaves.com/2015/11/interpreting-fft-results-complex-dft-frequency-bins-and-fftshift/
This then gives a correctly aligned spectrum that fills the full frequency range: