Skip to content

Commit

Permalink
Merge pull request #1856 from das-Iro/fix-fft-overlap
Browse files Browse the repository at this point in the history
Fix fft overlap
  • Loading branch information
f4exb authored Oct 23, 2023
2 parents dc7dd0c + 277b2b4 commit c9a4d54
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
10 changes: 6 additions & 4 deletions sdrbase/dsp/spectrumvis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ void SpectrumVis::feed(const ComplexVector::const_iterator& cbegin, const Comple
while (begin < end)
{
std::size_t todo = end - begin;
std::size_t samplesNeeded = m_refillSize - m_fftBufferFill;
std::size_t samplesNeeded = m_settings.m_fftSize - m_fftBufferFill;

if (todo >= samplesNeeded)
{
Expand All @@ -338,6 +338,7 @@ void SpectrumVis::feed(const ComplexVector::const_iterator& cbegin, const Comple
processFFT(positiveOnly);

// advance buffer respecting the fft overlap factor
// undefined bahavior if the memory regions overlap, valid code for 50% overlap
std::copy(m_fftBuffer.begin() + m_refillSize, m_fftBuffer.end(), m_fftBuffer.begin());

// start over
Expand Down Expand Up @@ -377,7 +378,7 @@ void SpectrumVis::feed(const SampleVector::const_iterator& cbegin, const SampleV
while (begin < end)
{
std::size_t todo = end - begin;
std::size_t samplesNeeded = m_refillSize - m_fftBufferFill;
std::size_t samplesNeeded = m_settings.m_fftSize - m_fftBufferFill;

if (todo >= samplesNeeded)
{
Expand All @@ -391,6 +392,7 @@ void SpectrumVis::feed(const SampleVector::const_iterator& cbegin, const SampleV
processFFT(positiveOnly);

// advance buffer respecting the fft overlap factor
// undefined bahavior if the memory regions overlap, valid code for 50% overlap
std::copy(m_fftBuffer.begin() + m_refillSize, m_fftBuffer.end(), m_fftBuffer.begin());

// start over
Expand Down Expand Up @@ -890,8 +892,8 @@ void SpectrumVis::applySettings(const SpectrumSettings& settings, bool force)
if ((fftSize != m_settings.m_fftSize)
|| (settings.m_fftOverlap != m_settings.m_fftOverlap) || force)
{
m_overlapSize = settings.m_fftOverlap < 0 ? 0 :
settings.m_fftOverlap < fftSize/2 ? settings.m_fftOverlap : (fftSize/2) - 1;
m_overlapSize = settings.m_fftOverlap < 0 ? 0 :
settings.m_fftOverlap < fftSize ? settings.m_fftOverlap : (fftSize - 1);
m_refillSize = fftSize - m_overlapSize;
m_fftBufferFill = m_overlapSize;
}
Expand Down
8 changes: 3 additions & 5 deletions sdrgui/gui/glspectrumgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,7 @@ void GLSpectrumGUI::setAveragingToolitp()
{
QString s;
int averagingIndex = m_settings.m_averagingMode == SpectrumSettings::AvgModeNone ? 0 : m_settings.m_averagingIndex;
float halfSize = m_settings.m_fftSize / 2;
float overlapFactor = (halfSize - m_settings.m_fftOverlap) / halfSize;
float overlapFactor = (m_settings.m_fftSize - m_settings.m_fftOverlap) / m_settings.m_fftSize;
float averagingTime = (m_settings.m_fftSize * (SpectrumSettings::getAveragingValue(averagingIndex, m_settings.m_averagingMode) == 0 ?
1 :
SpectrumSettings::getAveragingValue(averagingIndex, m_settings.m_averagingMode))) / (float) m_glSpectrum->getSampleRate();
Expand Down Expand Up @@ -905,11 +904,10 @@ void GLSpectrumGUI::setFFTSize(int log2FFTSize)

void GLSpectrumGUI::setMaximumOverlap()
{
int halfSize = m_settings.m_fftSize/2;
ui->fftOverlap->setMaximum((halfSize)-1);
ui->fftOverlap->setMaximum(m_settings.m_fftSize -1);
int value = ui->fftOverlap->value();
ui->fftOverlap->setValue(value);
ui->fftOverlap->setToolTip(tr("FFT overlap %1 %").arg((value/(float)halfSize)*100.0f));
ui->fftOverlap->setToolTip(tr("FFT overlap %1 %").arg((value/(float)m_settings.m_fftSize)*100.0f));

if (m_glSpectrum) {
m_glSpectrum->setFFTOverlap(value);
Expand Down
12 changes: 5 additions & 7 deletions sdrgui/gui/glspectrumview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2733,10 +2733,9 @@ void GLSpectrumView::applyChanges()
if (m_sampleRate > 0)
{
float timeScaleDiv = ((float)m_sampleRate / (float)m_timingRate);
float halfFFTSize = m_fftSize / 2;

if (halfFFTSize > m_fftOverlap) {
timeScaleDiv *= halfFFTSize / (halfFFTSize - m_fftOverlap);
if (m_fftSize > m_fftOverlap) {
timeScaleDiv *= m_fftSize / (m_fftSize - m_fftOverlap);
}

if (!m_invertedWaterfall) {
Expand Down Expand Up @@ -2827,10 +2826,9 @@ void GLSpectrumView::applyChanges()
if (m_sampleRate > 0)
{
float timeScaleDiv = ((float)m_sampleRate / (float)m_timingRate);
float halfFFTSize = m_fftSize / 2;

if (halfFFTSize > m_fftOverlap) {
timeScaleDiv *= halfFFTSize / (halfFFTSize - m_fftOverlap);
if (m_fftSize > m_fftOverlap) {
timeScaleDiv *= m_fftSize / (m_fftSize - m_fftOverlap);
}

if (!m_invertedWaterfall) {
Expand Down Expand Up @@ -4454,7 +4452,7 @@ void GLSpectrumView::timeZoom(bool zoomInElseOut)
return;
}

if (zoomInElseOut && (m_fftOverlap == m_fftSize/2 - 1)) {
if (zoomInElseOut && (m_fftOverlap == m_fftSize - 1)) {
return;
}

Expand Down

0 comments on commit c9a4d54

Please sign in to comment.