Fix file saving for RspDuo/Usrp with ofstream

This commit is contained in:
30hours 2024-01-20 03:20:51 +00:00
parent 703560e683
commit 82ec0546b4
6 changed files with 38 additions and 46 deletions

View file

@ -15,19 +15,19 @@ process:
overlap: 0 overlap: 0
ambiguity: ambiguity:
delayMin: -10 delayMin: -10
delayMax: 300 delayMax: 400
dopplerMin: -300 dopplerMin: -200
dopplerMax: 300 dopplerMax: 200
clutter: clutter:
delayMin: -10 delayMin: -10
delayMax: 300 delayMax: 400
detection: detection:
pfa: 0.00001 pfa: 0.00001
nGuard: 10 nGuard: 2
nTrain: 20 nTrain: 6
minDelay: 5 minDelay: 5
minDoppler: 15 minDoppler: 15
nCentroid: 10 nCentroid: 6
tracker: tracker:
initiate: initiate:
M: 3 M: 3

View file

@ -20,7 +20,6 @@ Source::Source(std::string _type, uint32_t _fc, uint32_t _fs,
fs = _fs; fs = _fs;
path = _path; path = _path;
saveIq = _saveIq; saveIq = _saveIq;
saveIqFile = NULL;
} }
std::string Source::open_file() std::string Source::open_file()
@ -39,9 +38,9 @@ std::string Source::open_file()
typeLower.begin(), ::tolower); typeLower.begin(), ::tolower);
std::string file = path + timestamp + "." + typeLower + ".iq"; std::string file = path + timestamp + "." + typeLower + ".iq";
saveIqFile = fopen(file.c_str(), "wb"); saveIqFile.open(file, std::ios::binary);
if (saveIqFile == NULL) if (!saveIqFile.is_open())
{ {
std::cerr << "Error: Can not open file: " << file << std::endl; std::cerr << "Error: Can not open file: " << file << std::endl;
exit(1); exit(1);
@ -53,12 +52,12 @@ std::string Source::open_file()
void Source::close_file() void Source::close_file()
{ {
if (saveIqFile != NULL) if (!saveIqFile.is_open())
{ {
fclose(saveIqFile); saveIqFile.close();
}
else
{
std::cerr << "Error: Can not close file pointer." << std::endl;
} }
// switch member with blank file stream
std::ofstream blankFile;
std::swap(saveIqFile, blankFile);
} }

View file

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <stdint.h> #include <stdint.h>
#include <fstream>
#include "data/IqData.h" #include "data/IqData.h"
class Source class Source
@ -29,8 +30,8 @@ protected:
/// @brief True if IQ data to be saved. /// @brief True if IQ data to be saved.
bool *saveIq; bool *saveIq;
/// @brief File pointer to IQ data to be saved. /// @brief File stream to save IQ data.
FILE *saveIqFile; std::ofstream saveIqFile;
public: public:

View file

@ -8,6 +8,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <fstream>
#include <iostream> #include <iostream>
// class static constants // class static constants
@ -36,7 +37,7 @@ sdrplay_api_CallbackFnsT cbFns;
sdrplay_api_RxChannelParamsT *chParams; sdrplay_api_RxChannelParamsT *chParams;
// global variables // global variables
FILE *out_file_fp = NULL; //FILE *out_file_fp = NULL;
FILE *file_replay = NULL; FILE *file_replay = NULL;
short *buffer_16_ar = NULL; short *buffer_16_ar = NULL;
struct timeval current_tm = {0, 0}; struct timeval current_tm = {0, 0};
@ -50,7 +51,8 @@ short max_b_nr = 0;
bool run_fg = true; bool run_fg = true;
bool stats_fg = true; bool stats_fg = true;
bool write_fg = true; bool write_fg = true;
bool capture_fg = false; bool *capture_fg;
std::ofstream* saveIqFileLocal;
int wait_time_nr = 2; int wait_time_nr = 2;
IqData *buffer1; IqData *buffer1;
IqData *buffer2; IqData *buffer2;
@ -72,8 +74,9 @@ RspDuo::RspDuo(std::string _type, uint32_t _fc, uint32_t _fs,
dab_notch_fg = false; dab_notch_fg = false;
chunk_time_nr = DEF_CHUNK_TIME_NR; chunk_time_nr = DEF_CHUNK_TIME_NR;
out_file_fp = saveIqFile; //out_file_fp = saveIqFile;
capture_fg = &saveIq; capture_fg = saveIq;
saveIqFileLocal = &saveIqFile;
} }
void RspDuo::start() void RspDuo::start()
@ -526,29 +529,13 @@ void RspDuo::stream_b_callback(short *xi, short *xq, sdrplay_api_StreamCbParamsT
write_fg = true; write_fg = true;
} }
// init file open
// if (capture_fg && write_fg && run_fg && chunk_tm.tv_sec <= current_tm.tv_sec)
// {
// if (out_file_fp != NULL)
// {
// fclose(out_file_fp);
// }
// out_file_fp = fopen(file.c_str(), "ab");
// if (out_file_fp == NULL)
// {
// std::cerr << "Error - stream_b_callback - opening output file " + file << std::endl;
// free(buffer_16_ar);
// run_fg = false;
// exit(1);
// }
// chunk_tm = current_tm;
// }
// write data to file // write data to file
if (capture_fg && write_fg) if (*capture_fg && write_fg)
{ {
if (fwrite(buffer_16_ar, sizeof(short), numSamples * 4, out_file_fp) != (size_t)numSamples * 4) saveIqFileLocal->write(reinterpret_cast<char*>(buffer_16_ar),
sizeof(short) * numSamples * 4);
if (!(*saveIqFileLocal))
{ {
std::cerr << "Error - stream_b_callback - not enough samples received" << std::endl; std::cerr << "Error - stream_b_callback - not enough samples received" << std::endl;
free(buffer_16_ar); free(buffer_16_ar);

View file

@ -93,9 +93,14 @@ void Usrp::process(IqData *buffer1, IqData *buffer2)
buffer2->unlock(); buffer2->unlock();
// save IQ data to file // save IQ data to file
if (saveIq) if (*saveIq)
{ {
for (const auto& bufferPtr : buff_ptrs)
{
// Write the buffer data to the file
saveIqFile.write(reinterpret_cast<const char*>(
bufferPtr), samps_per_buff * sizeof(std::complex<float>));
}
} }
} }
} }