added prepare()

This commit is contained in:
Ahmet Inan 2021-10-08 11:30:20 +02:00
commit 0fac5fc62b
2 changed files with 108 additions and 119 deletions

171
decode.cc
View file

@ -260,52 +260,34 @@ struct Decoder
}
cmplx mod_map(code_type *b)
{
switch (oper_mode) {
case 6:
case 7:
case 10:
case 11:
return PhaseShiftKeying<8, cmplx, code_type>::map(b);
case 8:
case 9:
case 12:
case 13:
switch (mod_bits) {
case 2:
return PhaseShiftKeying<4, cmplx, code_type>::map(b);
case 3:
return PhaseShiftKeying<8, cmplx, code_type>::map(b);
}
return 0;
}
void mod_hard(code_type *b, cmplx c)
{
switch (oper_mode) {
case 6:
case 7:
case 10:
case 11:
PhaseShiftKeying<8, cmplx, code_type>::hard(b, c);
break;
case 8:
case 9:
case 12:
case 13:
switch (mod_bits) {
case 2:
PhaseShiftKeying<4, cmplx, code_type>::hard(b, c);
break;
case 3:
PhaseShiftKeying<8, cmplx, code_type>::hard(b, c);
break;
}
}
void mod_soft(code_type *b, cmplx c, value precision)
{
switch (oper_mode) {
case 6:
case 7:
case 10:
case 11:
PhaseShiftKeying<8, cmplx, code_type>::soft(b, c, precision);
break;
case 8:
case 9:
case 12:
case 13:
switch (mod_bits) {
case 2:
PhaseShiftKeying<4, cmplx, code_type>::soft(b, c, precision);
break;
case 3:
PhaseShiftKeying<8, cmplx, code_type>::soft(b, c, precision);
break;
}
}
const cmplx *next_sample()
@ -316,65 +298,8 @@ struct Decoder
tmp = hilbert(blockdc(tmp.real()));
return input_hist(tmp);
}
Decoder(uint8_t *out, DSP::ReadPCM<value> *pcm, int skip_count) :
pcm(pcm), correlator(mls0_seq()), crc0(0xA8F4), crc1(0xD419CC15)
bool prepare()
{
CODE::BoseChaudhuriHocquenghemGenerator<255, 71>::matrix(genmat, true, {
0b100011101, 0b101110111, 0b111110011, 0b101101001,
0b110111101, 0b111100111, 0b100101011, 0b111010111,
0b000010011, 0b101100101, 0b110001011, 0b101100011,
0b100011011, 0b100111111, 0b110001101, 0b100101101,
0b101011111, 0b111111001, 0b111000011, 0b100111001,
0b110101001, 0b000011111, 0b110000111, 0b110110001});
blockdc.samples(2*(symbol_len+guard_len));
DSP::Phasor<cmplx> osc;
const cmplx *buf;
bool okay;
do {
okay = false;
do {
if (!pcm->good())
return;
buf = next_sample();
} while (!correlator(buf));
symbol_pos = correlator.symbol_pos;
cfo_rad = correlator.cfo_rad;
std::cerr << "symbol pos: " << symbol_pos << std::endl;
std::cerr << "coarse cfo: " << cfo_rad * (rate / Const::TwoPi()) << " Hz " << std::endl;
osc.omega(-cfo_rad);
for (int i = 0; i < symbol_len; ++i)
tdom[i] = buf[i+symbol_pos+(symbol_len+guard_len)] * osc();
fwd(fdom, tdom);
CODE::MLS seq1(mls1_poly);
for (int i = 0; i < mls1_len; ++i)
fdom[bin(i+mls1_off)] *= nrz(seq1());
int8_t soft[mls1_len];
uint8_t data[(mls1_len+7)/8];
for (int i = 0; i < mls1_len; ++i)
soft[i] = std::min<value>(std::max<value>(
std::nearbyint(127 * demod_or_erase(
fdom[bin(i+mls1_off)], fdom[bin(i-1+mls1_off)]).real()),
-128), 127);
bool unique = osddec(data, soft, genmat);
if (!unique) {
std::cerr << "OSD error." << std::endl;
continue;
}
uint64_t md = 0;
for (int i = 0; i < 55; ++i)
md |= (uint64_t)CODE::get_be_bit(data, i) << i;
uint16_t cs = 0;
for (int i = 0; i < 16; ++i)
cs |= (uint16_t)CODE::get_be_bit(data, i+55) << i;
crc0.reset();
if (crc0(md<<9) != cs) {
std::cerr << "header CRC error." << std::endl;
continue;
}
oper_mode = md & 255;
switch (oper_mode) {
case 6:
cons_cols = 432;
@ -441,10 +366,74 @@ struct Decoder
frozen_bits = frozen_64512_43072;
break;
default:
return false;
}
cons_cnt = cons_bits / mod_bits;
return true;
}
Decoder(uint8_t *out, DSP::ReadPCM<value> *pcm, int skip_count) :
pcm(pcm), correlator(mls0_seq()), crc0(0xA8F4), crc1(0xD419CC15)
{
CODE::BoseChaudhuriHocquenghemGenerator<255, 71>::matrix(genmat, true, {
0b100011101, 0b101110111, 0b111110011, 0b101101001,
0b110111101, 0b111100111, 0b100101011, 0b111010111,
0b000010011, 0b101100101, 0b110001011, 0b101100011,
0b100011011, 0b100111111, 0b110001101, 0b100101101,
0b101011111, 0b111111001, 0b111000011, 0b100111001,
0b110101001, 0b000011111, 0b110000111, 0b110110001});
blockdc.samples(2*(symbol_len+guard_len));
DSP::Phasor<cmplx> osc;
const cmplx *buf;
bool okay;
do {
okay = false;
do {
if (!pcm->good())
return;
buf = next_sample();
} while (!correlator(buf));
symbol_pos = correlator.symbol_pos;
cfo_rad = correlator.cfo_rad;
std::cerr << "symbol pos: " << symbol_pos << std::endl;
std::cerr << "coarse cfo: " << cfo_rad * (rate / Const::TwoPi()) << " Hz " << std::endl;
osc.omega(-cfo_rad);
for (int i = 0; i < symbol_len; ++i)
tdom[i] = buf[i+symbol_pos+(symbol_len+guard_len)] * osc();
fwd(fdom, tdom);
CODE::MLS seq1(mls1_poly);
for (int i = 0; i < mls1_len; ++i)
fdom[bin(i+mls1_off)] *= nrz(seq1());
int8_t soft[mls1_len];
uint8_t data[(mls1_len+7)/8];
for (int i = 0; i < mls1_len; ++i)
soft[i] = std::min<value>(std::max<value>(
std::nearbyint(127 * demod_or_erase(
fdom[bin(i+mls1_off)], fdom[bin(i-1+mls1_off)]).real()),
-128), 127);
bool unique = osddec(data, soft, genmat);
if (!unique) {
std::cerr << "OSD error." << std::endl;
continue;
}
uint64_t md = 0;
for (int i = 0; i < 55; ++i)
md |= (uint64_t)CODE::get_be_bit(data, i) << i;
uint16_t cs = 0;
for (int i = 0; i < 16; ++i)
cs |= (uint16_t)CODE::get_be_bit(data, i+55) << i;
crc0.reset();
if (crc0(md<<9) != cs) {
std::cerr << "header CRC error." << std::endl;
continue;
}
oper_mode = md & 255;
if (!prepare()) {
std::cerr << "operation mode " << oper_mode << " unsupported." << std::endl;
continue;
}
cons_cnt = cons_bits / mod_bits;
std::cerr << "oper mode: " << oper_mode << std::endl;
if ((md>>8) == 0 || (md>>8) >= 129961739795077L) {
std::cerr << "call sign unsupported." << std::endl;

View file

@ -184,29 +184,15 @@ struct Encoder
}
cmplx mod_map(code_type *b)
{
switch (oper_mode) {
case 6:
case 7:
case 10:
case 11:
return PhaseShiftKeying<8, cmplx, code_type>::map(b);
case 8:
case 9:
case 12:
case 13:
switch (mod_bits) {
case 2:
return PhaseShiftKeying<4, cmplx, code_type>::map(b);
case 3:
return PhaseShiftKeying<8, cmplx, code_type>::map(b);
}
return 0;
}
Encoder(DSP::WritePCM<value> *pcm, const uint8_t *inp, int freq_off, uint64_t call_sign, int oper_mode) :
pcm(pcm), crc0(0xA8F4), crc1(0xD419CC15), bchenc({
0b100011101, 0b101110111, 0b111110011, 0b101101001,
0b110111101, 0b111100111, 0b100101011, 0b111010111,
0b000010011, 0b101100101, 0b110001011, 0b101100011,
0b100011011, 0b100111111, 0b110001101, 0b100101101,
0b101011111, 0b111111001, 0b111000011, 0b100111001,
0b110101001, 0b000011111, 0b110000111, 0b110110001}),
oper_mode(oper_mode)
bool prepare()
{
switch (oper_mode) {
case 6:
@ -274,10 +260,24 @@ struct Encoder
frozen_bits = frozen_64512_43072;
break;
default:
return;
return false;
}
cons_cnt = cons_bits / mod_bits;
cons_rows = cons_cnt / cons_cols;
return true;
}
Encoder(DSP::WritePCM<value> *pcm, const uint8_t *inp, int freq_off, uint64_t call_sign, int oper_mode) :
pcm(pcm), crc0(0xA8F4), crc1(0xD419CC15), bchenc({
0b100011101, 0b101110111, 0b111110011, 0b101101001,
0b110111101, 0b111100111, 0b100101011, 0b111010111,
0b000010011, 0b101100101, 0b110001011, 0b101100011,
0b100011011, 0b100111111, 0b110001101, 0b100101101,
0b101011111, 0b111111001, 0b111000011, 0b100111001,
0b110101001, 0b000011111, 0b110000111, 0b110110001}),
oper_mode(oper_mode)
{
if (!prepare())
return;
int offset = (freq_off * symbol_len) / rate;
code_off = offset - cons_cols / 2;
mls0_off = offset - mls0_len + 1;