From 82ec0546b4ea8430cccdd433a9ef2c6b1e0c5a61 Mon Sep 17 00:00:00 2001 From: 30hours Date: Sat, 20 Jan 2024 03:20:51 +0000 Subject: [PATCH] Fix file saving for RspDuo/Usrp with ofstream --- config/config.yml | 14 ++++++------- config/radar4.yml | 2 +- src/capture/Source.cpp | 17 ++++++++-------- src/capture/Source.h | 5 +++-- src/capture/rspduo/RspDuo.cpp | 37 ++++++++++++----------------------- src/capture/usrp/Usrp.cpp | 9 +++++++-- 6 files changed, 38 insertions(+), 46 deletions(-) diff --git a/config/config.yml b/config/config.yml index 49f3a66..47b6c8b 100644 --- a/config/config.yml +++ b/config/config.yml @@ -15,19 +15,19 @@ process: overlap: 0 ambiguity: delayMin: -10 - delayMax: 300 - dopplerMin: -300 - dopplerMax: 300 + delayMax: 400 + dopplerMin: -200 + dopplerMax: 200 clutter: delayMin: -10 - delayMax: 300 + delayMax: 400 detection: pfa: 0.00001 - nGuard: 10 - nTrain: 20 + nGuard: 2 + nTrain: 6 minDelay: 5 minDoppler: 15 - nCentroid: 10 + nCentroid: 6 tracker: initiate: M: 3 diff --git a/config/radar4.yml b/config/radar4.yml index 8cb18c5..47b6c8b 100644 --- a/config/radar4.yml +++ b/config/radar4.yml @@ -5,7 +5,7 @@ capture: type: "RspDuo" replay: state: false - loop: true + loop: true file: '/opt/blah2/replay/file.rspduo' process: diff --git a/src/capture/Source.cpp b/src/capture/Source.cpp index c981572..16bc36c 100644 --- a/src/capture/Source.cpp +++ b/src/capture/Source.cpp @@ -20,7 +20,6 @@ Source::Source(std::string _type, uint32_t _fc, uint32_t _fs, fs = _fs; path = _path; saveIq = _saveIq; - saveIqFile = NULL; } std::string Source::open_file() @@ -39,9 +38,9 @@ std::string Source::open_file() typeLower.begin(), ::tolower); 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; exit(1); @@ -53,12 +52,12 @@ std::string Source::open_file() void Source::close_file() { - if (saveIqFile != NULL) + if (!saveIqFile.is_open()) { - fclose(saveIqFile); - } - else - { - std::cerr << "Error: Can not close file pointer." << std::endl; + saveIqFile.close(); } + + // switch member with blank file stream + std::ofstream blankFile; + std::swap(saveIqFile, blankFile); } \ No newline at end of file diff --git a/src/capture/Source.h b/src/capture/Source.h index c99b41e..fcd150e 100644 --- a/src/capture/Source.h +++ b/src/capture/Source.h @@ -8,6 +8,7 @@ #include #include +#include #include "data/IqData.h" class Source @@ -29,8 +30,8 @@ protected: /// @brief True if IQ data to be saved. bool *saveIq; - /// @brief File pointer to IQ data to be saved. - FILE *saveIqFile; + /// @brief File stream to save IQ data. + std::ofstream saveIqFile; public: diff --git a/src/capture/rspduo/RspDuo.cpp b/src/capture/rspduo/RspDuo.cpp index 88ca030..03e4321 100644 --- a/src/capture/rspduo/RspDuo.cpp +++ b/src/capture/rspduo/RspDuo.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include // class static constants @@ -36,7 +37,7 @@ sdrplay_api_CallbackFnsT cbFns; sdrplay_api_RxChannelParamsT *chParams; // global variables -FILE *out_file_fp = NULL; +//FILE *out_file_fp = NULL; FILE *file_replay = NULL; short *buffer_16_ar = NULL; struct timeval current_tm = {0, 0}; @@ -50,7 +51,8 @@ short max_b_nr = 0; bool run_fg = true; bool stats_fg = true; bool write_fg = true; -bool capture_fg = false; +bool *capture_fg; +std::ofstream* saveIqFileLocal; int wait_time_nr = 2; IqData *buffer1; IqData *buffer2; @@ -72,8 +74,9 @@ RspDuo::RspDuo(std::string _type, uint32_t _fc, uint32_t _fs, dab_notch_fg = false; chunk_time_nr = DEF_CHUNK_TIME_NR; - out_file_fp = saveIqFile; - capture_fg = &saveIq; + //out_file_fp = saveIqFile; + capture_fg = saveIq; + saveIqFileLocal = &saveIqFile; } void RspDuo::start() @@ -526,29 +529,13 @@ void RspDuo::stream_b_callback(short *xi, short *xq, sdrplay_api_StreamCbParamsT 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 - 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(buffer_16_ar), + sizeof(short) * numSamples * 4); + + if (!(*saveIqFileLocal)) { std::cerr << "Error - stream_b_callback - not enough samples received" << std::endl; free(buffer_16_ar); diff --git a/src/capture/usrp/Usrp.cpp b/src/capture/usrp/Usrp.cpp index 177238f..0422734 100644 --- a/src/capture/usrp/Usrp.cpp +++ b/src/capture/usrp/Usrp.cpp @@ -93,9 +93,14 @@ void Usrp::process(IqData *buffer1, IqData *buffer2) buffer2->unlock(); // 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( + bufferPtr), samps_per_buff * sizeof(std::complex)); + } } } }