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

203 lines
7 KiB
C++
Raw Normal View History

2021-11-18 23:03:20 +01:00
#include "module_pm_demod.h"
2023-01-31 01:12:10 +01:00
#include "common/dsp/filter/firdes.h"
2021-11-18 23:03:20 +01:00
#include "logger.h"
#include "imgui/imgui.h"
2022-03-25 19:33:31 +01:00
namespace demod
2021-11-18 23:03:20 +01:00
{
2022-03-25 19:33:31 +01:00
PMDemodModule::PMDemodModule(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-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Parse params
if (parameters.count("resample_after_pll") > 0)
d_resample_after_pll = parameters["resample_after_pll"].get<bool>();
2022-03-25 19:33:31 +01:00
if (parameters.count("pll_bw") > 0)
d_pll_bw = parameters["pll_bw"].get<float>();
else
throw std::runtime_error("PLL Bw parameter must be present!");
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("pll_max_offset") > 0)
d_pll_max_offset = parameters["pll_max_offset"].get<float>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("rrc_alpha") > 0)
d_rrc_alpha = parameters["rrc_alpha"].get<float>();
else
throw std::runtime_error("RRC Alpha parameter must be present!");
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("rrc_taps") > 0)
d_rrc_taps = parameters["rrc_taps"].get<int>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("costas_bw") > 0)
d_loop_bw = parameters["costas_bw"].get<float>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("clock_gain_omega") > 0)
d_clock_gain_omega = parameters["clock_gain_omega"].get<float>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("clock_mu") > 0)
d_clock_mu = parameters["clock_mu"].get<float>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("clock_gain_mu") > 0)
d_clock_gain_mu = parameters["clock_gain_mu"].get<float>();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (parameters.count("clock_omega_relative_limit") > 0)
d_clock_omega_relative_limit = parameters["clock_omega_relative_limit"].get<float>();
2021-11-18 23:03:20 +01:00
if (parameters.count("subcarrier_offset") > 0)
d_subccarier_offset = parameters["subcarrier_offset"].get<uint64_t>();
2022-03-25 19:33:31 +01:00
name = "PM Demodulator";
2022-05-16 11:23:08 +02:00
MAX_SPS = 10; // Here we do NOT want to resample unless really necessary
2022-03-29 16:57:47 +02:00
show_freq = true;
2022-03-25 19:33:31 +01:00
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
void PMDemodModule::init()
{
2023-06-28 23:38:05 +02:00
BaseDemodModule::initb(!d_resample_after_pll);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// PLL
pll = std::make_shared<dsp::PLLCarrierTrackingBlock>(agc->output_stream, d_pll_bw, d_pll_max_offset, -d_pll_max_offset);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Domain conversion
pm_psk = std::make_shared<dsp::PMToBPSK>(pll->output_stream,
d_resample_after_pll ? d_samplerate : final_samplerate,
d_subccarier_offset == 0 ? d_symbolrate : d_subccarier_offset);
if (d_resample_after_pll)
2023-01-31 02:37:41 +01:00
resampler = std::make_shared<dsp::SmartResamplerBlock<complex_t>>(pm_psk->output_stream, final_samplerate, d_samplerate);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// RRC
rrc = std::make_shared<dsp::FIRBlock<complex_t>>(d_resample_after_pll ? resampler->output_stream : pm_psk->output_stream, dsp::firdes::root_raised_cosine(1, final_samplerate, d_symbolrate, d_rrc_alpha, d_rrc_taps));
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Costas
costas = std::make_shared<dsp::CostasLoopBlock>(rrc->output_stream, d_loop_bw, 2);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Clock recovery
2022-08-21 22:11:47 +02:00
rec = std::make_shared<dsp::MMClockRecoveryBlock<complex_t>>(costas->output_stream, final_sps, d_clock_gain_omega, d_clock_mu, d_clock_gain_mu, d_clock_omega_relative_limit);
2022-03-25 19:33:31 +01:00
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
PMDemodModule::~PMDemodModule()
2021-11-18 23:03:20 +01:00
{
2022-03-25 19:33:31 +01:00
delete[] sym_buffer;
2021-11-18 23:03:20 +01:00
}
2022-03-25 19:33:31 +01:00
void PMDemodModule::process()
2021-11-18 23:03:20 +01:00
{
2022-03-25 19:33:31 +01:00
if (input_data_type == DATA_FILE)
filesize = file_source->getFilesize();
else
filesize = 0;
if (output_data_type == DATA_FILE)
{
data_out = std::ofstream(d_output_file_hint + ".soft", std::ios::binary);
d_output_files.push_back(d_output_file_hint + ".soft");
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
logger->info("Using input baseband " + d_input_file);
logger->info("Demodulating to " + d_output_file_hint + ".soft");
2023-05-08 23:08:34 +02:00
logger->info("Buffer size : %d", d_buffer_size);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
time_t lastTime = 0;
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Start
BaseDemodModule::start();
pll->start();
pm_psk->start();
rrc->start();
costas->start();
rec->start();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
int dat_size = 0;
2023-06-27 22:28:00 +02:00
while (demod_should_run())
2021-11-18 23:03:20 +01:00
{
2022-03-25 19:33:31 +01:00
dat_size = rec->output_stream->read();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (dat_size <= 0)
2022-11-02 18:38:57 +01:00
{
rec->output_stream->flush();
2022-03-25 19:33:31 +01:00
continue;
2022-11-02 18:38:57 +01:00
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Push into constellation
constellation.pushComplex(rec->output_stream->readBuf, dat_size);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Estimate SNR
snr_estimator.update(rec->output_stream->readBuf, dat_size);
snr = snr_estimator.snr();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (snr > peak_snr)
peak_snr = snr;
2021-11-18 23:03:20 +01:00
2022-03-29 16:57:47 +02:00
// Update freq
display_freq = dsp::rad_to_hz(pll->getFreq(), final_samplerate);
2022-03-29 16:57:47 +02:00
2022-03-25 19:33:31 +01:00
for (int i = 0; i < dat_size; i++)
{
sym_buffer[i] = clamp(rec->output_stream->readBuf[i].real * 100);
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
rec->output_stream->flush();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01: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);
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (input_data_type == DATA_FILE)
progress = file_source->getPosition();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
// Update module stats
module_stats["snr"] = snr;
module_stats["peak_snr"] = peak_snr;
2022-03-29 16:57:47 +02:00
module_stats["freq"] = display_freq;
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
2023-07-06 16:05:54 +02:00
logger->info("Progress " + std::to_string(round(((float)progress / (float)filesize) * 1000.0f) / 10.0f) + "%%, SNR : " + std::to_string(snr) + "dB," + " Peak SNR: " + std::to_string(peak_snr) + "dB");
2022-03-25 19:33:31 +01:00
}
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
logger->info("Demodulation finished");
if (input_data_type == DATA_FILE)
stop();
2021-11-18 23:03:20 +01:00
}
2022-03-25 19:33:31 +01:00
void PMDemodModule::stop()
{
// Stop
BaseDemodModule::stop();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
pll->stop();
pm_psk->stop();
rrc->stop();
costas->stop();
rec->stop();
rec->output_stream->stopReader();
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
if (output_data_type == DATA_FILE)
data_out.close();
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
std::string PMDemodModule::getID()
{
return "pm_demod";
}
2021-11-18 23:03:20 +01:00
2022-03-25 19:33:31 +01:00
std::vector<std::string> PMDemodModule::getParameters()
{
std::vector<std::string> params = {"rrc_alpha", "rrc_taps", "pll_bw", "pll_max_offset", "costas_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;
}
std::shared_ptr<ProcessingModule> PMDemodModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
{
return std::make_shared<PMDemodModule>(input_file, output_file_hint, parameters);
}
}