Skip to content

Commit

Permalink
baseline and metadata improvements
Browse files Browse the repository at this point in the history
code cleanup around binary matrix output metadata;
bugfix: cropping option was not properly managed in baseline subtraction
code section;
added actualBINS variable to properly check for correct number of
baseline values (when cropping)
added current hop counter for proper baseline offset calculation (when
cropping)
  • Loading branch information
mariocannistra committed Aug 19, 2016
1 parent 90a3b63 commit 8b625bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
46 changes: 32 additions & 14 deletions src/acquisition.cxx
Expand Up @@ -54,13 +54,17 @@ std::vector<T> read_inputfile(std::istream* stream) {
// As long as we end up with the right number of values, we're game.
if (valuesRead > 0)
values.push_back(value);

}
return values;
}

AuxData::AuxData(const Params& params) {
std::istream* stream;
std::ifstream fs;

int suitableValues;

// Window function and baseline correction
// If both are read from stdin, we read it all in one go,
// and see if the total number of values adds up to what we need.
Expand Down Expand Up @@ -141,13 +145,22 @@ AuxData::AuxData(const Params& params) {
stream = &fs;
}
baseline_values = read_inputfile<double>(stream);

if(hops>1) {
suitableValues = ( params.N - int( params.cropPercentage * params.N / 100 ) ) * hops;
}
else
{
suitableValues = params.N - int( params.cropPercentage * params.N / 100 );
}

// Check for suitability.
if ((int)baseline_values.size() == params.N) {
if ((int)baseline_values.size() == suitableValues) {
std::cerr << "Succesfully read " << baseline_values.size() << " baseline points." << std::endl;
}
else {
throw RPFexception(
"Error reading baseline. Expected " + std::to_string(params.N)
"Error reading baseline. Expected " + std::to_string(suitableValues)
+ " values, found " + std::to_string(baseline_values.size()) + ".",
ReturnValue::InvalidInput);
}
Expand Down Expand Up @@ -225,6 +238,7 @@ Plan::Plan(Params& params_, int actual_samplerate_) :
}

void Plan::print() const {
std::cerr << "Planned hops: " << hops << std::endl;
std::cerr << "Number of bins: " << params.N << std::endl;
std::cerr << "Total number of (complex) samples to collect: " << (int64_t)params.N*params.repeats << std::endl;
std::cerr << "Buffer length: " << params.buf_length << std::endl;
Expand Down Expand Up @@ -280,7 +294,8 @@ void Acquisition::run() {
data.acquisition_finished = false;
data.repeats_done = 0;

std::thread t(&Datastore::fftThread, std::ref(data));
//std::thread t(&Datastore::fftThread, std::ref(data));
std::thread t(&Datastore::fftThread, &data);

// Record the start-of-acquisition timestamp.
startAcqTimestamp = currentDateTime();
Expand Down Expand Up @@ -391,7 +406,7 @@ void Acquisition::write_data() const {
double pwrdb = 0.0;
float fpwrdb = 0.0;
double freq = 0.0;
int initialBIN, finalBIN;
int initialBIN, finalBIN, baselineOffset;

if(!params.matrixMode) {
// Print the header
Expand Down Expand Up @@ -422,41 +437,47 @@ void Acquisition::write_data() const {
- in function Plan(): only if hops are performed: calculate a frequency offset by which the center frequency
of each hop scan should be lowered in order to produce a continuous and not overlapping
series of bins for the overall frequency range
- calculate the index offset when subtracting the baseline values
*/
if( (params.freq_hopping_isSet) && (params.cropPercentage > 0) )
{
excludedBINS = int( params.cropPercentage * params.N / 100 ) / 2;
initialBIN = excludedBINS + 1;
initialBIN = excludedBINS;
finalBIN = params.N - excludedBINS;

actualBINS = (params.N - (excludedBINS * 2)) * hops;
baselineOffset = (params.N - (excludedBINS * 2)) * (currentHopNumber - 1);
}
else
{
excludedBINS = 0;
initialBIN = 0;
finalBIN = params.N ;

actualBINS = params.N;
// 0 offset for single hop scans
baselineOffset = 0;
}

//for (int i = 0; i < params.N; i++) {
for (int i = initialBIN; i < finalBIN; i++) {

freq = tuned_freq + (i - params.N/2.0) * actual_samplerate / params.N;

if( params.linear ) {
pwrdb = (data.pwr[i] / data.repeats_done / params.N / actual_samplerate)
- (params.baseline ? aux.baseline_values[i] : 0);
- (params.baseline ? aux.baseline_values[baselineOffset+i-excludedBINS] : 0);
}
else {
pwrdb = 10*log10(data.pwr[i] / data.repeats_done / params.N / actual_samplerate)
- (params.baseline ? aux.baseline_values[i] : 0);
pwrdb = (10*log10(data.pwr[i] / data.repeats_done / params.N / actual_samplerate))
- (params.baseline ? aux.baseline_values[baselineOffset+i-excludedBINS] : 0);
}
if( params.matrixMode ) {
// we are accumulating a double, so 8 bytes, removed the sizeof()
// we are writing a float, so 4 bytes
// binary file size for 15 mins with N=100 now 43.4 MB instead of 86.8 MB )
fpwrdb=pwrdb;
binfile.write( (char*)&fpwrdb, 4 );
if(metaRows==1) {
metaCols = metaCols + 1;
}
}
else
{
Expand All @@ -471,9 +492,6 @@ void Acquisition::write_data() const {

if( params.matrixMode ) {
binfile.close();
if( tuned_freq >= params.finalfreq ) {
metaRows = metaRows + 1;
}
}

if( !params.matrixMode ) {
Expand Down
2 changes: 2 additions & 0 deletions src/metadata.h
Expand Up @@ -28,12 +28,14 @@
extern int metaCols, metaRows;
extern int startFreq, endFreq, stepFreq;
extern int hops;
extern int currentHopNumber;
extern time_t scanEnd, scanBeg;
extern float avgScanDur, sumScanDur;
extern std::string firstAcqTimestamp, lastAcqTimestamp;
extern int cntTimeStamps;
extern int actual_samplerate;
extern int excludedBINS;
extern int actualBINS;
extern double cropFreqOffset;

#endif // METADATA_H
1 change: 1 addition & 0 deletions src/params.cxx
Expand Up @@ -19,6 +19,7 @@

#include <iostream>
#include <list>
#include <math.h>
#include <string>
#include <tclap/CmdLine.h>

Expand Down
29 changes: 23 additions & 6 deletions src/rtl_power_fftw.cxx
Expand Up @@ -37,18 +37,20 @@
#include <time.h>

std::ofstream binfile, metafile;
int metaRows = 1;
int metaRows = 0;
int metaCols = 0;
float avgScanDur = 0.0;
float sumScanDur = 0.0;
time_t scanEnd, scanBeg;
int tunfreq;
int startFreq, endFreq, stepFreq;
int hops;
int currentHopNumber;
std::string firstAcqTimestamp, lastAcqTimestamp;
int cntTimeStamps;
int actual_samplerate;
int excludedBINS = 0;
int actualBINS = 0;
double cropFreqOffset = 0.0;
int got_sighup = false;
int got_sigint = false;
Expand All @@ -65,8 +67,6 @@ int main(int argc, char **argv)
try {
// Parse command line arguments.
Params params(argc, argv);
// Read auxiliary data for window function and baseline correction.
AuxData auxData(params);

// Set up RTL-SDR device.
Rtlsdr rtldev(params.dev_index);
Expand Down Expand Up @@ -115,6 +115,13 @@ int main(int argc, char **argv)
// Print info on capture time and associated specifics.
plan.print();

// moved this step here since we need to know the number of planned hops
// in order to properly account for the number of needed baseline values
// matching the number of bins after the cropping if any:

// Read auxiliary data for window function and baseline correction.
AuxData auxData(params);

//Begin the work: prepare data buffers
Datastore data(params, auxData.window_values);

Expand All @@ -138,8 +145,17 @@ int main(int argc, char **argv)
params.finalfreq = plan.freqs_to_tune.back();
//Read from device and do FFT
do {
currentHopNumber = 0;
for (auto iter = plan.freqs_to_tune.begin(); iter != plan.freqs_to_tune.end();) {
// Begin a new data acquisition.
// we need to keep track of the current hop number
// to subtract from the corresponding section of baseline data
// in a multi-hop scenario
currentHopNumber = currentHopNumber + 1;

if( (params.outcnt == 0 && params.talkless) || (params.talkless==false) )
std::cerr << "\nHop # " << currentHopNumber << std::endl;

// Begin a new data acquisition.
Acquisition acquisition(params, auxData, rtldev, data, actual_samplerate, *iter);
try {
// Read the required amount of data and process it.
Expand Down Expand Up @@ -172,6 +188,7 @@ int main(int argc, char **argv)

// Write the gathered data to stdout.
acquisition.write_data();
metaRows = metaRows + 1;

// Print the histogram of the queue length to stderr.
if( (params.outcnt == 0 && params.talkless) || (params.talkless==false) ) data.printQueueHistogram();
Expand Down Expand Up @@ -214,8 +231,8 @@ int main(int argc, char **argv)

if(params.matrixMode) {
metafile.open(params.meta_file, std::ios::out | std::ios::trunc );
metafile << metaCols << " # frequency bins (columns)" << std::endl;
metaRows = metaRows - 1; // let's fix the rows count since we start from 1
metafile << actualBINS << " # frequency bins (columns)" << std::endl;
metaRows = metaRows / hops; // in multi-hop scenarios every tune counts as an output row
metafile << metaRows << " # scans (rows)" << std::endl;
metafile << startFreq << " # startFreq (Hz)" << std::endl;
metafile << endFreq << " # endFreq (Hz)" << std::endl;
Expand Down

0 comments on commit 8b625bb

Please sign in to comment.