Skip to content

Commit

Permalink
Merge pull request #82 from schneider42/export-menu
Browse files Browse the repository at this point in the history
Export Menu Option
  • Loading branch information
miek authored Sep 1, 2016
2 parents b79cdc2 + 78b004b commit e936e51
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 3 deletions.
3 changes: 3 additions & 0 deletions inputsource.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ class InputSource : public SampleSource<std::complex<float>>
};
void setSampleRate(off_t rate);
off_t rate();
float relativeBandwidth() {
return 1;
}
};
94 changes: 94 additions & 0 deletions plotview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@

#include "plotview.h"
#include <iostream>
#include <fstream>
#include <QApplication>
#include <QDebug>
#include <QMenu>
#include <QPainter>
#include <QScrollBar>
#include <QFileDialog>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QGridLayout>
#include <QSpinBox>
#include "plots.h"

PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0})
Expand Down Expand Up @@ -94,6 +101,17 @@ void PlotView::contextMenuEvent(QContextMenuEvent * event)
extract->setEnabled(cursorsEnabled && (src->sampleType() == typeid(float)));
menu.addAction(extract);

// Add action to export the selected samples into a file
auto save = new QAction("Export samples to file...", &menu);
connect(
save, &QAction::triggered,
this, [=]() {
exportSamples(src);
}
);
save->setEnabled(src->sampleType() == typeid(std::complex<float>));
menu.addAction(save);

if (menu.exec(event->globalPos()))
updateView(false);
}
Expand Down Expand Up @@ -193,6 +211,82 @@ void PlotView::extractSymbols(std::shared_ptr<AbstractSampleSource> src)
std::cout << std::endl << std::flush;
}

void PlotView::exportSamples(std::shared_ptr<AbstractSampleSource> src)
{
auto complexSrc = std::dynamic_pointer_cast<SampleSource<std::complex<float>>>(src);
if (!complexSrc) {
return;
}

QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setOption(QFileDialog::DontUseNativeDialog, true);

QGroupBox groupBox("Selection To Export", &dialog);
QVBoxLayout vbox(&groupBox);

QRadioButton cursorSelection("Cursor Selection", &groupBox);
QRadioButton currentView("Current View", &groupBox);
QRadioButton completeFile("Complete File (Experimental)", &groupBox);

if (cursorsEnabled) {
cursorSelection.setChecked(true);
} else {
currentView.setChecked(true);
cursorSelection.setEnabled(false);
}

vbox.addWidget(&cursorSelection);
vbox.addWidget(&currentView);
vbox.addWidget(&completeFile);
vbox.addStretch(1);

groupBox.setLayout(&vbox);

QGridLayout *l = dialog.findChild<QGridLayout*>();
l->addWidget(&groupBox, 4, 1);

QGroupBox groupBox2("Decimation");
QSpinBox decimation(&groupBox2);
decimation.setValue(1 / complexSrc->relativeBandwidth());

QVBoxLayout vbox2;
vbox2.addWidget(&decimation);

groupBox2.setLayout(&vbox2);
l->addWidget(&groupBox2, 4, 2);

if (dialog.exec()) {
QStringList fileNames = dialog.selectedFiles();

off_t start, end;
if (cursorSelection.isChecked()) {
start = selectedSamples.minimum;
end = start + selectedSamples.length();
} else if(currentView.isChecked()) {
start = viewRange.minimum;
end = start + viewRange.length();
} else {
start = 0;
end = complexSrc->count();
}

std::ofstream os (fileNames[0].toStdString(), std::ios::binary);

off_t index;
// viewRange.length() is used as some less arbitrary step value
off_t step = viewRange.length();

for (index = start; index < end; index += step) {
off_t length = std::min(step, end - index);
auto samples = complexSrc->getSamples(index, length);
for (auto i = 0; i < length; i += decimation.value()) {
os.write((const char*)&samples[i], 8);
}
}
}
}

void PlotView::invalidateEvent()
{
horizontalScrollBar()->setMinimum(0);
Expand Down
1 change: 1 addition & 0 deletions plotview.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public slots:
void addPlot(Plot *plot);
void emitTimeSelection();
void extractSymbols(std::shared_ptr<AbstractSampleSource> src);
void exportSamples(std::shared_ptr<AbstractSampleSource> src);
int plotsHeight();
off_t samplesPerLine();
void updateView(bool reCenter = false);
Expand Down
4 changes: 4 additions & 0 deletions samplebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ class SampleBuffer : public SampleSource<Tout>, public Subscriber
off_t rate() {
return src->rate();
};

float relativeBandwidth() {
return 1;
}
};
1 change: 1 addition & 0 deletions samplesource.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SampleSource : public AbstractSampleSource
virtual void invalidateEvent() { };
virtual off_t count() = 0;
virtual off_t rate() = 0;
virtual float relativeBandwidth() = 0;
std::type_index sampleType() override;
void subscribe(Subscriber *subscriber);
int subscriberCount();
Expand Down
4 changes: 2 additions & 2 deletions spectrogramplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
SpectrogramPlot::SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float>>> src) : Plot(src), inputSource(src), tuner(this)
{
setFFTSize(512);
zoomLevel = 0;
zoomLevel = 1;
powerMax = 0.0f;
powerMin = -50.0f;

Expand All @@ -45,7 +45,6 @@ SpectrogramPlot::SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float

tunerTransform = std::make_shared<TunerTransform>(src.get());
connect(&tuner, &Tuner::tunerMoved, this, &SpectrogramPlot::tunerMoved);
tunerMoved();
src->subscribe(this);
}

Expand Down Expand Up @@ -236,6 +235,7 @@ void SpectrogramPlot::tunerMoved()
{
tunerTransform->setFrequency(getTunerPhaseInc());
tunerTransform->setTaps(getTunerTaps());
tunerTransform->setRelativeBandwith(tuner.deviation() * 2.0 / getStride());

// TODO: for invalidating traceplot cache, this shouldn't really go here
QPixmapCache::clear();
Expand Down
12 changes: 11 additions & 1 deletion tunertransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <liquid/liquid.h>
#include "util.h"

TunerTransform::TunerTransform(SampleSource<std::complex<float>> *src) : SampleBuffer(src), taps{1.0f}
TunerTransform::TunerTransform(SampleSource<std::complex<float>> *src) : SampleBuffer(src), frequency(0), bandwidth(1.), taps{1.0f}
{

}
Expand Down Expand Up @@ -61,3 +61,13 @@ void TunerTransform::setTaps(std::vector<float> taps)
{
this->taps = taps;
}

float TunerTransform::relativeBandwidth() {
return bandwidth;
}

void TunerTransform::setRelativeBandwith(float bandwidth)
{
this->bandwidth = bandwidth;
}

3 changes: 3 additions & 0 deletions tunertransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ class TunerTransform : public SampleBuffer<std::complex<float>, std::complex<flo
private:
float frequency;
std::vector<float> taps;
float bandwidth;

public:
TunerTransform(SampleSource<std::complex<float>> *src);
void work(void *input, void *output, int count, off_t sampleid) override;
void setFrequency(float frequency);
void setTaps(std::vector<float> taps);
void setRelativeBandwith(float bandwidth);
float relativeBandwidth();
};

0 comments on commit e936e51

Please sign in to comment.