Skip to content

Align spectrum with frequency scale#1464

Merged
f4exb merged 1 commit intof4exb:masterfrom
srcejon:spectrum_alignment
Oct 3, 2022
Merged

Align spectrum with frequency scale#1464
f4exb merged 1 commit intof4exb:masterfrom
srcejon:spectrum_alignment

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
Copilot AI added a commit that referenced this pull request Feb 14, 2026
Co-authored-by: f4exb <6192319+f4exb@users.noreply.github.com>
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