Completely redid the RDS demod

This commit is contained in:
AlexandreRouma
2023-12-13 23:25:18 +01:00
parent 794d6ff5ac
commit 2432390600
12 changed files with 1027 additions and 156 deletions

View File

@ -2,11 +2,13 @@
#include "../demod.h"
#include <dsp/demod/broadcast_fm.h>
#include <dsp/clock_recovery/mm.h>
#include <dsp/clock_recovery/fd.h>
#include <dsp/loop/fast_agc.h>
#include <dsp/loop/costas.h>
#include <dsp/taps/root_raised_cosine.h>
#include <dsp/digital/binary_slicer.h>
#include <dsp/digital/manchester_decoder.h>
#include <dsp/digital/differential_decoder.h>
#include <dsp/routing/doubler.h>
#include <gui/widgets/symbol_diagram.h>
#include <fstream>
#include <rds.h>
@ -14,9 +16,9 @@
namespace demod {
class WFM : public Demodulator {
public:
WFM() {}
WFM() : diag(0.5, 4096) {}
WFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) {
WFM(std::string name, ConfigManager* config, dsp::stream<dsp::complex_t>* input, double bandwidth, double audioSR) : diag(0.5, 4096) {
init(name, config, input, bandwidth, audioSR);
}
@ -45,33 +47,66 @@ namespace demod {
if (config->conf[name][getName()].contains("rds")) {
_rds = config->conf[name][getName()]["rds"];
}
if (config->conf[name][getName()].contains("rdsInfo")) {
_rdsInfo = config->conf[name][getName()]["rdsInfo"];
}
_config->release(modified);
// Define structure
demod.init(input, bandwidth / 2.0f, getIFSampleRate(), _stereo, _lowPass, _rds);
recov.init(&demod.rdsOut, 5000.0 / 2375, omegaGain, muGain, 0.01);
slice.init(&recov.out);
manch.init(&slice.out);
diff.init(&manch.out, 2);
agc.init(&demod.rdsOut, 1.0, 1e6, 0.1);
costas.init(&agc.out, 0.005f);
taps = dsp::taps::bandPass<dsp::complex_t>(0, 2375, 100, 5000);
fir.init(&costas.out, taps);
double baudfreq = dsp::math::hzToRads(2375.0/2.0, 5000);
costas2.init(&fir.out, 0.01, 0.0, baudfreq, baudfreq - (baudfreq*0.1), baudfreq + (baudfreq*0.1));
c2r.init(&costas2.out);
recov.init(&c2r.out, 5000.0 / (2375.0 / 2.0), 1e-6, 0.01, 0.01);
slice.init(&doubler.outA);
diff.init(&slice.out, 2);
hs.init(&diff.out, rdsHandler, this);
doubler.init(&recov.out);
reshape.init(&doubler.outB, 4096, (1187 / 30) - 4096);
diagHandler.init(&reshape.out, _diagHandler, this);
diag.lines.push_back(-0.8);
diag.lines.push_back(0.8);
}
void start() {
agc.start();
costas.start();
fir.start();
costas2.start();
c2r.start();
demod.start();
recov.start();
slice.start();
manch.start();
diff.start();
hs.start();
doubler.start();
reshape.start();
diagHandler.start();
}
void stop() {
agc.stop();
costas.stop();
fir.stop();
costas2.stop();
c2r.stop();
demod.stop();
recov.stop();
slice.stop();
manch.stop();
diff.stop();
hs.stop();
c2r.stop();
reshape.stop();
diagHandler.stop();
}
void showMenu() {
@ -94,14 +129,105 @@ namespace demod {
_config->release(true);
}
// if (_rds) {
// if (rdsDecode.countryCodeValid()) { ImGui::Text("Country code: %d", rdsDecode.getCountryCode()); }
// if (rdsDecode.programCoverageValid()) { ImGui::Text("Program coverage: %d", rdsDecode.getProgramCoverage()); }
// if (rdsDecode.programRefNumberValid()) { ImGui::Text("Reference number: %d", rdsDecode.getProgramRefNumber()); }
// if (rdsDecode.programTypeValid()) { ImGui::Text("Program type: %d", rdsDecode.getProgramType()); }
// if (rdsDecode.PSNameValid()) { ImGui::Text("Program name: [%s]", rdsDecode.getPSName().c_str()); }
// if (rdsDecode.radioTextValid()) { ImGui::Text("Radiotext: [%s]", rdsDecode.getRadioText().c_str()); }
// }
// TODO: This will break when the entire radio module is disabled
if (!_rds) { ImGui::BeginDisabled(); }
if (ImGui::Checkbox(("Advanced RDS Info##_radio_wfm_rds_info_" + name).c_str(), &_rdsInfo)) {
_config->acquire();
_config->conf[name][getName()]["rdsInfo"] = _rdsInfo;
_config->release(true);
}
if (!_rds) { ImGui::EndDisabled(); }
float menuWidth = ImGui::GetContentRegionAvail().x;
if (_rds && _rdsInfo) {
ImGui::BeginTable(("##radio_wfm_rds_info_tbl_" + name).c_str(), 2, ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders);
if (rdsDecode.piCodeValid()) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("PI Code");
ImGui::TableSetColumnIndex(1);
ImGui::Text("0x%04X (%s)", rdsDecode.getPICode(), rdsDecode.getCallsign().c_str());
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Country Code");
ImGui::TableSetColumnIndex(1);
ImGui::Text("%d", rdsDecode.getCountryCode());
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Program Coverage");
ImGui::TableSetColumnIndex(1);
ImGui::Text("%s (%d)", rds::AREA_COVERAGE_TO_STR[rdsDecode.getProgramCoverage()], rdsDecode.getProgramCoverage());
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Reference Number");
ImGui::TableSetColumnIndex(1);
ImGui::Text("%d", rdsDecode.getProgramRefNumber());
}
else {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("PI Code");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("0x---- (----)");
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Country Code");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("--"); // TODO: String
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Program Coverage");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("------- (--)");
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Reference Number");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("--");
}
if (rdsDecode.programTypeValid()) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Program Type");
ImGui::TableSetColumnIndex(1);
ImGui::Text("%s (%d)", rds::PROGRAM_TYPE_US_TO_STR[rdsDecode.getProgramType()], rdsDecode.getProgramType());
}
else {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Program Type");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("------- (--)"); // TODO: String
}
if (rdsDecode.musicValid()) {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Music");
ImGui::TableSetColumnIndex(1);
ImGui::Text("%s", rdsDecode.getMusic() ? "Yes":"No");
}
else {
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::TextUnformatted("Music");
ImGui::TableSetColumnIndex(1);
ImGui::TextUnformatted("---");
}
ImGui::EndTable();
ImGui::SetNextItemWidth(menuWidth);
diag.draw();
}
}
void setBandwidth(double bandwidth) {
@ -145,6 +271,14 @@ namespace demod {
_this->rdsDecode.process(data, count);
}
// DEBUGGING ONLY
static void _diagHandler(float* data, int count, void* ctx) {
WFM* _this = (WFM*)ctx;
float* buf = _this->diag.acquireBuffer();
memcpy(buf, data, count * sizeof(float));
_this->diag.releaseBuffer();
}
static void fftRedraw(ImGui::WaterFall::FFTRedrawArgs args, void* ctx) {
WFM* _this = (WFM*)ctx;
if (!_this->_rds) { return; }
@ -186,13 +320,23 @@ namespace demod {
}
dsp::demod::BroadcastFM demod;
dsp::clock_recovery::FD recov;
dsp::loop::FastAGC<dsp::complex_t> agc;
dsp::loop::Costas<2> costas;
dsp::tap<dsp::complex_t> taps;
dsp::filter::FIR<dsp::complex_t, dsp::complex_t> fir;
dsp::loop::Costas<2> costas2;
dsp::convert::ComplexToReal c2r;
dsp::clock_recovery::MM<float> recov;
dsp::digital::BinarySlicer slice;
dsp::digital::ManchesterDecoder manch;
dsp::digital::DifferentialDecoder diff;
dsp::sink::Handler<uint8_t> hs;
EventHandler<ImGui::WaterFall::FFTRedrawArgs> fftRedrawHandler;
dsp::routing::Doubler<float> doubler;
dsp::buffer::Reshaper<float> reshape;
dsp::sink::Handler<float> diagHandler;
ImGui::SymbolDiagram diag;
rds::RDSDecoder rdsDecode;
ConfigManager* _config = NULL;
@ -200,6 +344,7 @@ namespace demod {
bool _stereo = false;
bool _lowPass = true;
bool _rds = false;
bool _rdsInfo = false;
float muGain = 0.01;
float omegaGain = (0.01*0.01)/4.0;

View File

@ -3,6 +3,8 @@
#include <map>
#include <algorithm>
#include <utils/flog.h>
namespace rds {
std::map<uint16_t, BlockType> SYNDROMES = {
{ 0b1111011000, BLOCK_TYPE_A },
@ -54,18 +56,26 @@ namespace rds {
type = (BlockType)((lastType + 1) % _BLOCK_TYPE_COUNT);
}
// Save block while correcting errors (NOT YET)
// Save block while correcting errors (NOT YET) <- idk why the "not yet is here", TODO: find why
blocks[type] = correctErrors(shiftReg, type, blockAvail[type]);
// Update continous group count
if (type == BLOCK_TYPE_A) { contGroup = 1; }
else if (type == BLOCK_TYPE_B && lastType == BLOCK_TYPE_A) { contGroup++; }
// If block type is A, decode it directly, otherwise, update continous count
if (type == BLOCK_TYPE_A) {
decodeBlockA();
}
else if (type == BLOCK_TYPE_B) { contGroup = 1; }
else if ((type == BLOCK_TYPE_C || type == BLOCK_TYPE_CP) && lastType == BLOCK_TYPE_B) { contGroup++; }
else if (type == BLOCK_TYPE_D && (lastType == BLOCK_TYPE_C || lastType == BLOCK_TYPE_CP)) { contGroup++; }
else { contGroup = 0; }
else {
// If block B is available, decode it alone.
if (contGroup == 1) {
decodeBlockB();
}
contGroup = 0;
}
// If we've got an entire group, process it
if (contGroup >= 4) {
if (contGroup >= 3) {
contGroup = 0;
decodeGroup();
}
@ -124,26 +134,45 @@ namespace rds {
return out;
}
void RDSDecoder::decodeGroup() {
void RDSDecoder::decodeBlockA() {
// If it didn't decode properly return
if (!blockAvail[BLOCK_TYPE_A]) { return; }
// Update timeout
std::lock_guard<std::mutex> lck(groupMtx);
auto now = std::chrono::high_resolution_clock::now();
anyGroupLastUpdate = now;
// Make sure blocks A and B are available
if (!blockAvail[BLOCK_TYPE_A] || !blockAvail[BLOCK_TYPE_B]) { return; }
blockALastUpdate = now;
// Decode PI code
piCode = (blocks[BLOCK_TYPE_A] >> 10) & 0xFFFF;
countryCode = (blocks[BLOCK_TYPE_A] >> 22) & 0xF;
programCoverage = (AreaCoverage)((blocks[BLOCK_TYPE_A] >> 18) & 0xF);
programRefNumber = (blocks[BLOCK_TYPE_A] >> 10) & 0xFF;
decodeCallsign();
}
void RDSDecoder::decodeBlockB() {
// If it didn't decode properly return
if (!blockAvail[BLOCK_TYPE_B]) { return; }
// Decode group type and version
uint8_t groupType = (blocks[BLOCK_TYPE_B] >> 22) & 0xF;
GroupVersion groupVer = (GroupVersion)((blocks[BLOCK_TYPE_B] >> 21) & 1);
groupType = (blocks[BLOCK_TYPE_B] >> 22) & 0xF;
groupVer = (GroupVersion)((blocks[BLOCK_TYPE_B] >> 21) & 1);
// Decode traffic program and program type
trafficProgram = (blocks[BLOCK_TYPE_B] >> 20) & 1;
programType = (ProgramType)((blocks[BLOCK_TYPE_B] >> 15) & 0x1F);
}
void RDSDecoder::decodeGroup() {
std::lock_guard<std::mutex> lck(groupMtx);
auto now = std::chrono::high_resolution_clock::now();
// Make sure blocks B is available
if (!blockAvail[BLOCK_TYPE_B]) { return; }
// Decode block B
decodeBlockB();
if (groupType == 0) {
group0LastUpdate = now;
@ -202,9 +231,33 @@ namespace rds {
}
}
bool RDSDecoder::anyGroupValid() {
void RDSDecoder::decodeCallsign() {
// Determin first better based on offset
bool w = (piCode >= 21672);
callsign = w ? 'W' : 'K';
// Base25 decode the rest
std::string restStr;
int rest = piCode - (w ? 21672 : 4096);
while (rest) {
restStr += 'A' + (rest % 26);
rest /= 26;
}
// Reorder chars
for (int i = restStr.size() - 1; i >= 0; i--) {
callsign += restStr[i];
}
}
bool RDSDecoder::blockAValid() {
auto now = std::chrono::high_resolution_clock::now();
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - anyGroupLastUpdate)).count() < 5000.0;
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - blockALastUpdate)).count() < 5000.0;
}
bool RDSDecoder::blockBValid() {
auto now = std::chrono::high_resolution_clock::now();
return (std::chrono::duration_cast<std::chrono::milliseconds>(now - blockALastUpdate)).count() < 5000.0;
}
bool RDSDecoder::group0Valid() {

View File

@ -20,22 +20,42 @@ namespace rds {
};
enum AreaCoverage {
AREA_COVERAGE_LOCAL,
AREA_COVERAGE_INTERNATIONAL,
AREA_COVERAGE_NATIONAL,
AREA_COVERAGE_SUPRA_NATIONAL,
AREA_COVERAGE_REGIONAL1,
AREA_COVERAGE_REGIONAL2,
AREA_COVERAGE_REGIONAL3,
AREA_COVERAGE_REGIONAL4,
AREA_COVERAGE_REGIONAL5,
AREA_COVERAGE_REGIONAL6,
AREA_COVERAGE_REGIONAL7,
AREA_COVERAGE_REGIONAL8,
AREA_COVERAGE_REGIONAL9,
AREA_COVERAGE_REGIONAL10,
AREA_COVERAGE_REGIONAL11,
AREA_COVERAGE_REGIONAL12
AREA_COVERAGE_INVALID = -1,
AREA_COVERAGE_LOCAL = 0,
AREA_COVERAGE_INTERNATIONAL = 1,
AREA_COVERAGE_NATIONAL = 2,
AREA_COVERAGE_SUPRA_NATIONAL = 3,
AREA_COVERAGE_REGIONAL1 = 4,
AREA_COVERAGE_REGIONAL2 = 5,
AREA_COVERAGE_REGIONAL3 = 6,
AREA_COVERAGE_REGIONAL4 = 7,
AREA_COVERAGE_REGIONAL5 = 8,
AREA_COVERAGE_REGIONAL6 = 9,
AREA_COVERAGE_REGIONAL7 = 10,
AREA_COVERAGE_REGIONAL8 = 11,
AREA_COVERAGE_REGIONAL9 = 12,
AREA_COVERAGE_REGIONAL10 = 13,
AREA_COVERAGE_REGIONAL11 = 14,
AREA_COVERAGE_REGIONAL12 = 15
};
inline const char* AREA_COVERAGE_TO_STR[] = {
"Local",
"International",
"National",
"Supra-National",
"Regional 1",
"Regional 2",
"Regional 3",
"Regional 4",
"Regional 5",
"Regional 6",
"Regional 7",
"Regional 8",
"Regional 9",
"Regional 10",
"Regional 11",
"Regional 12",
};
enum ProgramType {
@ -108,6 +128,76 @@ namespace rds {
PROGRAM_TYPE_EU_ALARM = 31
};
inline const char* PROGRAM_TYPE_EU_TO_STR[] = {
"None",
"News",
"Current Affairs",
"Information",
"Sports",
"Education",
"Drama",
"Culture",
"Science",
"Varied",
"Pop Music",
"Rock Music",
"Easy Listening Music",
"Light Classical",
"Serious Classical",
"Other Music",
"Weather",
"Finance",
"Children Program",
"Social Affairs",
"Religion",
"Phone-in",
"Travel",
"Leisure",
"Jazz Music",
"Country Music",
"National Music",
"Oldies Music",
"Folk Music",
"Documentary",
"Alarm Test",
"Alarm",
};
inline const char* PROGRAM_TYPE_US_TO_STR[] = {
"None",
"News",
"Information",
"Sports",
"Talk",
"Rock",
"Classic Rock",
"Adult Hits",
"Soft Rock",
"Top 40",
"Country",
"Oldies",
"Soft",
"Nostalgia",
"Jazz",
"Classical",
"Rythm and Blues",
"Soft Rythm and Blues",
"Foreign Language",
"Religious Music",
"Religious Talk",
"Personality",
"Public",
"College",
"Unassigned",
"Unassigned",
"Unassigned",
"Unassigned",
"Unassigned",
"Weather",
"Emergency Test",
"Emergency",
};
enum DecoderIdentification {
DECODER_IDENT_STEREO = (1 << 0),
DECODER_IDENT_ARTIFICIAL_HEAD = (1 << 1),
@ -119,13 +209,14 @@ namespace rds {
public:
void process(uint8_t* symbols, int count);
bool countryCodeValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
bool piCodeValid() { std::lock_guard<std::mutex> lck(groupMtx); return blockAValid(); }
uint16_t getPICode() { std::lock_guard<std::mutex> lck(groupMtx); return piCode; }
uint8_t getCountryCode() { std::lock_guard<std::mutex> lck(groupMtx); return countryCode; }
bool programCoverageValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
uint8_t getProgramCoverage() { std::lock_guard<std::mutex> lck(groupMtx); return programCoverage; }
bool programRefNumberValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
uint8_t getProgramRefNumber() { std::lock_guard<std::mutex> lck(groupMtx); return programRefNumber; }
bool programTypeValid() { std::lock_guard<std::mutex> lck(groupMtx); return anyGroupValid(); }
std::string getCallsign() { std::lock_guard<std::mutex> lck(groupMtx); return callsign; }
bool programTypeValid() { std::lock_guard<std::mutex> lck(groupMtx); return blockBValid(); }
ProgramType getProgramType() { std::lock_guard<std::mutex> lck(groupMtx); return programType; }
bool musicValid() { std::lock_guard<std::mutex> lck(groupMtx); return group0Valid(); }
@ -139,9 +230,14 @@ namespace rds {
private:
static uint16_t calcSyndrome(uint32_t block);
static uint32_t correctErrors(uint32_t block, BlockType type, bool& recovered);
void decodeBlockA();
void decodeBlockB();
void decodeGroup();
bool anyGroupValid();
void decodeCallsign();
bool blockAValid();
bool blockBValid();
bool group0Valid();
bool group2Valid();
@ -154,17 +250,24 @@ namespace rds {
uint32_t blocks[_BLOCK_TYPE_COUNT];
bool blockAvail[_BLOCK_TYPE_COUNT];
// All groups
// Block A (All groups)
std::mutex groupMtx;
std::chrono::time_point<std::chrono::high_resolution_clock> anyGroupLastUpdate;
std::chrono::time_point<std::chrono::high_resolution_clock> blockALastUpdate{}; // 1970-01-01
uint16_t piCode;
uint8_t countryCode;
AreaCoverage programCoverage;
uint8_t programRefNumber;
std::string callsign;
// Block B (All groups)
std::chrono::time_point<std::chrono::high_resolution_clock> blockBLastUpdate{}; // 1970-01-01
uint8_t groupType;
GroupVersion groupVer;
bool trafficProgram;
ProgramType programType;
// Group type 0
std::chrono::time_point<std::chrono::high_resolution_clock> group0LastUpdate;
std::chrono::time_point<std::chrono::high_resolution_clock> group0LastUpdate{}; // 1970-01-01
bool trafficAnnouncement;
bool music;
uint8_t decoderIdent;
@ -172,7 +275,7 @@ namespace rds {
std::string programServiceName = " ";
// Group type 2
std::chrono::time_point<std::chrono::high_resolution_clock> group2LastUpdate;
std::chrono::time_point<std::chrono::high_resolution_clock> group2LastUpdate{}; // 1970-01-01
bool rtAB = false;
std::string radioText = " ";