Skip to content

Commit

Permalink
adding fft skip / zoom out
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Gilbert <[email protected]>
  • Loading branch information
Jacob Gilbert committed Jul 17, 2021
1 parent a4e4ad2 commit c478c2a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/plotview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,12 @@ void PlotView::setFFTAndZoom(int size, int zoom)
spectrogramPlot->setFFTSize(size);

// Set new zoom level
zoomLevel = zoom;
if (spectrogramPlot != nullptr)
spectrogramPlot->setZoomLevel(zoom);
zoomLevel = std::max(1,zoom);
nfftSkip = std::max(1,-1*zoom);
if (spectrogramPlot != nullptr) {
spectrogramPlot->setZoomLevel(zoomLevel);
spectrogramPlot->setSkip(nfftSkip);
}

// Update horizontal (time) scrollbar
horizontalScrollBar()->setSingleStep(10);
Expand Down Expand Up @@ -524,7 +527,7 @@ void PlotView::resizeEvent(QResizeEvent * event)

size_t PlotView::samplesPerColumn()
{
return fftSize / zoomLevel;
return fftSize * nfftSkip / zoomLevel;
}

void PlotView::scrollContentsBy(int dx, int dy)
Expand Down
1 change: 1 addition & 0 deletions src/plotview.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public slots:

int fftSize = 1024;
int zoomLevel = 1;
int nfftSkip = 1;
int powerMin;
int powerMax;
bool cursorsEnabled;
Expand Down
10 changes: 8 additions & 2 deletions src/spectrogramcontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ SpectrogramControls::SpectrogramControls(const QString & title, QWidget * parent
layout->addRow(new QLabel(tr("FFT size:")), fftSizeSlider);

zoomLevelSlider = new QSlider(Qt::Horizontal, widget);
zoomLevelSlider->setRange(0, 10);
zoomLevelSlider->setRange(-6, 10);
zoomLevelSlider->setPageStep(1);

layout->addRow(new QLabel(tr("Zoom:")), zoomLevelSlider);
Expand Down Expand Up @@ -139,7 +139,13 @@ void SpectrogramControls::setDefaults()
void SpectrogramControls::fftOrZoomChanged(void)
{
int fftSize = pow(2, fftSizeSlider->value());
int zoomLevel = std::min(fftSize, (int)pow(2, zoomLevelSlider->value()));
int zoomLevel = zoomLevelSlider->value();
if (zoomLevel >= 0)
// zooming in by power-of-two steps
zoomLevel = std::min(fftSize, (int)pow(2, zoomLevel));
else
// zooming out (skipping FFTs) by power-of-two steps
zoomLevel = -1*std::min(fftSize, (int)pow(2, -1*zoomLevel));
emit fftOrZoomChanged(fftSize, zoomLevel);
}

Expand Down
16 changes: 11 additions & 5 deletions src/spectrogramplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SpectrogramPlot::SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float
{
setFFTSize(fftSize);
zoomLevel = 1;
nfftSkip = 1;
powerMax = 0.0f;
powerMin = -50.0f;
sampleRate = 0;
Expand Down Expand Up @@ -222,7 +223,7 @@ void SpectrogramPlot::paintMid(QPainter &painter, QRect &rect, range_t<size_t> s

QPixmap* SpectrogramPlot::getPixmapTile(size_t tile)
{
QPixmap *obj = pixmapCache.object(TileCacheKey(fftSize, zoomLevel, tile));
QPixmap *obj = pixmapCache.object(TileCacheKey(fftSize, zoomLevel, nfftSkip, tile));
if (obj != 0)
return obj;

Expand All @@ -241,13 +242,13 @@ QPixmap* SpectrogramPlot::getPixmapTile(size_t tile)
}
}
obj->convertFromImage(image);
pixmapCache.insert(TileCacheKey(fftSize, zoomLevel, tile), obj);
pixmapCache.insert(TileCacheKey(fftSize, zoomLevel, nfftSkip, tile), obj);
return obj;
}

float* SpectrogramPlot::getFFTTile(size_t tile)
{
std::array<float, tileSize>* obj = fftCache.object(TileCacheKey(fftSize, zoomLevel, tile));
std::array<float, tileSize>* obj = fftCache.object(TileCacheKey(fftSize, zoomLevel, nfftSkip, tile));
if (obj != nullptr)
return obj->data();

Expand All @@ -259,7 +260,7 @@ float* SpectrogramPlot::getFFTTile(size_t tile)
sample += getStride();
ptr += fftSize;
}
fftCache.insert(TileCacheKey(fftSize, zoomLevel, tile), destStorage);
fftCache.insert(TileCacheKey(fftSize, zoomLevel, nfftSkip, tile), destStorage);
return destStorage->data();
}

Expand Down Expand Up @@ -296,7 +297,7 @@ void SpectrogramPlot::getLine(float *dest, size_t sample)

int SpectrogramPlot::getStride()
{
return fftSize / zoomLevel;
return fftSize * nfftSkip / zoomLevel;
}

float SpectrogramPlot::getTunerPhaseInc()
Expand Down Expand Up @@ -377,6 +378,11 @@ void SpectrogramPlot::setZoomLevel(int zoom)
zoomLevel = zoom;
}

void SpectrogramPlot::setSkip(int skip)
{
nfftSkip = skip;
}

void SpectrogramPlot::setSampleRate(double rate)
{
sampleRate = rate;
Expand Down
7 changes: 6 additions & 1 deletion src/spectrogramplot.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public slots:
void setPowerMax(int power);
void setPowerMin(int power);
void setZoomLevel(int zoom);
void setSkip(int skip);
void tunerMoved();

private:
Expand All @@ -69,6 +70,7 @@ public slots:

int fftSize;
int zoomLevel;
int nfftSkip;
float powerMax;
float powerMin;
double sampleRate;
Expand All @@ -92,19 +94,22 @@ class TileCacheKey
{

public:
TileCacheKey(int fftSize, int zoomLevel, size_t sample) {
TileCacheKey(int fftSize, int zoomLevel, int nfftSkip, size_t sample) {
this->fftSize = fftSize;
this->zoomLevel = zoomLevel;
this->nfftSkip = nfftSkip;
this->sample = sample;
}

bool operator==(const TileCacheKey &k2) const {
return (this->fftSize == k2.fftSize) &&
(this->zoomLevel == k2.zoomLevel) &&
(this->nfftSkip == k2.nfftSkip) &&
(this->sample == k2.sample);
}

int fftSize;
int zoomLevel;
int nfftSkip;
size_t sample;
};

0 comments on commit c478c2a

Please sign in to comment.