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

Align spectrum with frequency scale #1464

Merged
merged 1 commit into from
Oct 3, 2022
Merged

Conversation

srcejon
Copy link
Collaborator

@srcejon srcejon commented Oct 3, 2022

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.

image

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):

    m_glHistogramSpectrumMatrix.scale(
        ((float) 2 * (width() - m_leftMargin - m_rightMargin)) / ((float) width() * (float)(m_nbBins - 1)),
        ((float) 2*m_histogramHeight / height()) / m_powerRange

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:

image

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:

image

@f4exb f4exb merged commit 8843a8c into f4exb:master Oct 3, 2022
@srcejon srcejon deleted the spectrum_alignment branch December 22, 2022 10:42
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.

2 participants