satdump/src-core/modules/demod/module_fsk_demod.cpp

194 lines
6.3 KiB
C++
Raw Normal View History

#include "module_fsk_demod.h"
2023-01-31 01:12:10 +01:00
#include "common/dsp/filter/firdes.h"
#include "logger.h"
#include "imgui/imgui.h"
#include <volk/volk.h>
2022-03-30 10:06:26 +02:00
namespace demod
2021-03-31 22:11:57 +02:00
{
2022-03-30 10:06:26 +02:00
FSKDemodModule::FSKDemodModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : BaseDemodModule(input_file, output_file_hint, parameters)
{
// Buffers
2022-11-13 18:22:02 +01:00
sym_buffer = new int8_t[d_buffer_size * 4];
2021-03-31 22:11:57 +02:00
2022-03-30 10:06:26 +02:00
// Parse params
2023-02-14 00:40:46 +01:00
if (parameters.count("basic_shaping") > 0)
d_basic_shaping = parameters["basic_shaping"].get<bool>();
2022-08-14 21:25:25 +02:00
if (parameters.count("rrc_alpha") > 0)
d_rrc_alpha = parameters["rrc_alpha"].get<float>();
2023-02-14 00:40:46 +01:00
else if (!d_basic_shaping)
2022-08-14 21:25:25 +02:00
throw std::runtime_error("RRC Alpha parameter must be present!");
2022-08-14 21:25:25 +02:00
if (parameters.count("rrc_taps") > 0)
d_rrc_taps = parameters["rrc_taps"].get<int>();
if (parameters.count("clock_alpha") > 0)
{
float clock_alpha = parameters["clock_alpha"].get<float>();
d_clock_gain_omega = pow(clock_alpha, 2) / 4.0;
d_clock_gain_mu = clock_alpha;
}
if (parameters.count("clock_gain_omega") > 0)
d_clock_gain_omega = parameters["clock_gain_omega"].get<float>();
if (parameters.count("clock_mu") > 0)
d_clock_mu = parameters["clock_mu"].get<float>();
if (parameters.count("clock_gain_mu") > 0)
d_clock_gain_mu = parameters["clock_gain_mu"].get<float>();
if (parameters.count("clock_omega_relative_limit") > 0)
d_clock_omega_relative_limit = parameters["clock_omega_relative_limit"].get<float>();
2022-03-30 10:06:26 +02:00
name = "FSK Demodulator";
show_freq = false;
2022-08-14 21:25:25 +02:00
constellation.d_hscale = 80.0 / 100.0;
2022-03-30 10:06:26 +02:00
constellation.d_vscale = 20.0 / 100.0;
2022-11-09 01:50:58 +01:00
// MIN_SPS = 1.1;
2022-11-09 12:17:57 +01:00
// MAX_SPS = 8.0;
2022-03-30 10:06:26 +02:00
}
void FSKDemodModule::init()
{
2023-06-28 23:38:05 +02:00
BaseDemodModule::initb();
2022-03-30 10:06:26 +02:00
// Quadrature demod
2022-11-09 12:17:57 +01:00
qua = std::make_shared<dsp::QuadratureDemodBlock>(agc->output_stream, 1.0f);
2022-11-09 01:50:58 +01:00
// DC Blocker
dcb2 = std::make_shared<dsp::CorrectIQBlock<float>>(qua->output_stream);
// Second AGC (Doppler)
agc2 = std::make_shared<dsp::AGCBlock<float>>(dcb2->output_stream, 0.1f, 0.5f, 1.0f, 65535.0f);
2022-11-09 12:17:57 +01:00
// LPF
2023-02-14 00:40:46 +01:00
std::vector<float> taps = dsp::firdes::root_raised_cosine(1, final_samplerate, d_symbolrate, d_rrc_alpha, d_rrc_taps);
if (d_basic_shaping)
{
taps.clear();
for (int i = 0; i < final_sps; i++)
taps.push_back(0.1f);
}
rrc = std::make_shared<dsp::FIRBlock<float>>(agc2->output_stream, taps);
2022-11-09 12:17:57 +01:00
2022-03-30 10:06:26 +02:00
// Clock recovery
2022-11-09 12:17:57 +01:00
rec = std::make_shared<dsp::MMClockRecoveryBlock<float>>(rrc->output_stream,
2022-08-21 22:11:47 +02:00
final_sps, d_clock_gain_omega, d_clock_mu, d_clock_gain_mu, d_clock_omega_relative_limit);
2022-03-30 10:06:26 +02:00
}
2022-03-30 10:06:26 +02:00
FSKDemodModule::~FSKDemodModule()
{
delete[] sym_buffer;
}
2022-03-30 10:06:26 +02:00
void FSKDemodModule::process()
{
2021-03-31 19:07:53 +02:00
if (input_data_type == DATA_FILE)
2022-03-30 10:06:26 +02:00
filesize = file_source->getFilesize();
else
filesize = 0;
data_out = std::ofstream(d_output_file_hint + ".soft", std::ios::binary);
d_output_files.push_back(d_output_file_hint + ".soft");
logger->info("Using input baseband " + d_input_file);
logger->info("Demodulating to " + d_output_file_hint + ".soft");
logger->info("Buffer size : " + std::to_string(d_buffer_size));
time_t lastTime = 0;
// Start
BaseDemodModule::start();
qua->start();
2022-11-09 01:50:58 +01:00
dcb2->start();
agc2->start();
2022-11-09 12:17:57 +01:00
rrc->start();
2022-03-30 10:06:26 +02:00
rec->start();
int dat_size = 0;
2023-06-27 22:28:00 +02:00
while (demod_should_run())
{
2022-03-30 10:06:26 +02:00
dat_size = rec->output_stream->read();
2022-03-30 10:06:26 +02:00
if (dat_size <= 0)
2022-11-02 18:38:57 +01:00
{
rec->output_stream->flush();
2022-03-30 10:06:26 +02:00
continue;
2022-11-02 18:38:57 +01:00
}
2022-03-30 10:06:26 +02:00
// Into const
constellation.pushFloatAndGaussian(rec->output_stream->readBuf, rec->output_stream->getDataSize());
2021-05-22 16:36:09 +02:00
2022-03-30 10:06:26 +02:00
// Estimate SNR
snr_estimator.update((complex_t *)rec->output_stream->readBuf, dat_size / 2);
snr = snr_estimator.snr();
2022-03-30 10:06:26 +02:00
if (snr > peak_snr)
peak_snr = snr;
2022-03-30 10:06:26 +02:00
for (int i = 0; i < dat_size; i++)
sym_buffer[i] = clamp(rec->output_stream->readBuf[i] * 50);
2022-03-30 10:06:26 +02:00
rec->output_stream->flush();
2022-03-30 10:06:26 +02:00
if (output_data_type == DATA_FILE)
data_out.write((char *)sym_buffer, dat_size);
else
output_fifo->write((uint8_t *)sym_buffer, dat_size);
2022-03-30 10:06:26 +02:00
if (input_data_type == DATA_FILE)
progress = file_source->getPosition();
2022-10-06 19:39:54 +02:00
// Update module stats
module_stats["snr"] = snr;
module_stats["peak_snr"] = peak_snr;
2022-03-30 10:06:26 +02:00
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
logger->info("Progress " + std::to_string(round(((double)progress / (double)filesize) * 1000.0) / 10.0) + "%%, SNR : " + std::to_string(snr) + "dB," + " Peak SNR: " + std::to_string(peak_snr) + "dB");
2022-03-30 10:06:26 +02:00
}
}
2022-03-30 10:06:26 +02:00
logger->info("Demodulation finished");
if (input_data_type == DATA_FILE)
stop();
}
void FSKDemodModule::stop()
{
// Stop
BaseDemodModule::stop();
qua->stop();
2022-11-09 01:50:58 +01:00
dcb2->stop();
agc2->stop();
2022-11-09 12:17:57 +01:00
rrc->stop();
2022-03-30 10:06:26 +02:00
rec->stop();
rec->output_stream->stopReader();
if (output_data_type == DATA_FILE)
data_out.close();
}
std::string FSKDemodModule::getID()
{
return "fsk_demod";
}
std::vector<std::string> FSKDemodModule::getParameters()
{
2022-08-14 21:25:25 +02:00
std::vector<std::string> params = {"rrc_alpha", "rrc_taps", "pll_bw", "clock_gain_omega", "clock_mu", "clock_gain_mu", "clock_omega_relative_limit"};
params.insert(params.end(), BaseDemodModule::getParameters().begin(), BaseDemodModule::getParameters().end());
return params;
2022-03-30 10:06:26 +02:00
}
std::shared_ptr<ProcessingModule> FSKDemodModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
{
return std::make_shared<FSKDemodModule>(input_file, output_file_hint, parameters);
}
}