added mode 14 for short messages

using SPC(2048, 1392), QPSK, 1600 Hz BW and about 1 second long
This commit is contained in:
Ahmet Inan 2022-05-18 23:14:26 +02:00
commit 41795780cb
5 changed files with 62 additions and 304 deletions

8
README.md vendored
View file

@ -3,10 +3,10 @@
Quick start:
Create file ```uncoded.dat``` with ```43040``` bits of random data:
Create file ```uncoded.dat``` with ```1360``` bits of random data:
```
dd if=/dev/urandom of=uncoded.dat bs=1 count=5380
dd if=/dev/urandom of=uncoded.dat bs=1 count=170
```
Encode file ```uncoded.dat``` to ```encoded.wav``` [WAV](https://en.wikipedia.org/wiki/WAV) audio file with 8000 Hz sample rate, 16 bits and only 1 (real) channel:
@ -15,10 +15,10 @@ Encode file ```uncoded.dat``` to ```encoded.wav``` [WAV](https://en.wikipedia.or
./encode encoded.wav 8000 16 1 uncoded.dat
```
Start recording to ```recorded.wav``` audio file and stop after 20 seconds:
Start recording to ```recorded.wav``` audio file and stop after 5 seconds:
```
arecord -c 1 -f S16_LE -r 8000 -d 20 recorded.wav
arecord -c 1 -f S16_LE -r 8000 -d 5 recorded.wav
```
Start playing ```encoded.wav``` audio file:

181
decode.cc
View file

@ -167,17 +167,18 @@ struct Decoder
typedef SIMD<code_type, 16 / sizeof(code_type)> mesg_type;
#endif
typedef DSP::Const<value> Const;
static const int code_order = 11;
static const int mod_bits = 2;
static const int code_len = 1 << code_order;
static const int symbol_len = (1280 * rate) / 8000;
static const int filter_len = (((21 * rate) / 8000) & ~3) | 1;
static const int guard_len = symbol_len / 8;
static const int data_bits = 43040;
static const int data_bits = 1360;
static const int crc_bits = data_bits + 32;
static const int mod_min = 2;
static const int mod_max = 3;
static const int cons_max = 64800 / mod_min;
static const int cols_min = 256;
static const int cols_max = 512;
static const int rows_max = cons_max / cols_min;
static const int cons_cols = 256;
static const int cons_rows = 4;
static const int cons_total = cons_rows * cons_cols;
static const int code_off = - cons_cols / 2;
static const int mls0_len = 127;
static const int mls0_off = - mls0_len + 1;
static const int mls0_poly = 0b10001001;
@ -191,29 +192,21 @@ struct Decoder
DSP::BlockDC<value, value> blockdc;
DSP::Hilbert<cmplx, filter_len> hilbert;
DSP::BipBuffer<cmplx, buffer_len> input_hist;
DSP::TheilSenEstimator<value, cols_max> tse;
DSP::TheilSenEstimator<value, cons_cols> tse;
SchmidlCox<value, cmplx, search_pos, symbol_len/2, guard_len> correlator;
CODE::CRC<uint16_t> crc0;
CODE::CRC<uint32_t> crc1;
CODE::OrderedStatisticsDecoder<255, 71, 4> osddec;
CODE::PolarEncoder<mesg_type> polarenc;
CODE::PolarListDecoder<mesg_type, 16> polardec;
CODE::PolarListDecoder<mesg_type, code_order> polardec;
int8_t genmat[255*71];
mesg_type mesg[44096], mess[65536];
code_type code[65536];
cmplx cons[cons_max], prev[cols_max];
mesg_type mesg[crc_bits], mess[code_len];
code_type code[code_len];
cmplx cons[cons_total], prev[cons_cols];
cmplx fdom[symbol_len], tdom[symbol_len];
value index[cols_max], phase[cols_max];
value index[cons_cols], phase[cons_cols];
value cfo_rad, sfo_rad;
const uint32_t *frozen_bits;
int code_order;
int symbol_pos;
int oper_mode;
int mod_bits;
int cons_cnt;
int cons_cols;
int cons_bits;
int mesg_bits;
static int bin(int carrier)
{
@ -241,54 +234,25 @@ struct Decoder
fdom[(i+mls0_off/2+symbol_len/2)%(symbol_len/2)] = nrz(seq0());
return fdom;
}
void lengthen()
{
int code_bits = 1 << code_order;
for (int i = code_bits-1, j = cons_bits-1, k = mesg_bits-1; i >= 0; --i)
if ((frozen_bits[i/32] >> (i%32)) & 1 || k-- < crc_bits)
code[i] = code[j--];
else
code[i] = CODE::PolarHelper<code_type>::quant(9000);
}
void systematic()
{
polarenc(mess, mesg, frozen_bits, code_order);
polarenc(mess, mesg, frozen_2048_1392, code_order);
int code_bits = 1 << code_order;
for (int i = 0, j = 0; i < code_bits && j < mesg_bits; ++i)
if (!((frozen_bits[i/32] >> (i%32)) & 1))
for (int i = 0, j = 0; i < code_bits && j < crc_bits; ++i)
if (!((frozen_2048_1392[i/32] >> (i%32)) & 1))
mesg[j++] = mess[i];
}
cmplx mod_map(code_type *b)
{
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;
return PhaseShiftKeying<4, cmplx, code_type>::map(b);
}
void mod_hard(code_type *b, cmplx c)
{
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;
}
PhaseShiftKeying<4, cmplx, code_type>::hard(b, c);
}
void mod_soft(code_type *b, cmplx c, value precision)
{
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;
}
PhaseShiftKeying<4, cmplx, code_type>::soft(b, c, precision);
}
const cmplx *next_sample()
{
@ -298,81 +262,8 @@ struct Decoder
tmp = hilbert(blockdc(tmp.real()));
return input_hist(tmp);
}
bool prepare()
{
switch (oper_mode) {
case 6:
cons_cols = 432;
mod_bits = 3;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 7:
cons_cols = 400;
mod_bits = 3;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 8:
cons_cols = 400;
mod_bits = 2;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 9:
cons_cols = 360;
mod_bits = 2;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 10:
cons_cols = 512;
mod_bits = 3;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 11:
cons_cols = 384;
mod_bits = 3;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 12:
cons_cols = 384;
mod_bits = 2;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 13:
cons_cols = 256;
mod_bits = 2;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
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)
pcm(pcm), correlator(mls0_seq()), crc0(0xA8F4), crc1(0x8F6E37A0)
{
CODE::BoseChaudhuriHocquenghemGenerator<255, 71>::matrix(genmat, true, {
0b100011101, 0b101110111, 0b111110011, 0b101101001,
@ -429,8 +320,8 @@ struct Decoder
std::cerr << "header CRC error." << std::endl;
continue;
}
oper_mode = md & 255;
if (!prepare()) {
int oper_mode = md & 255;
if (oper_mode != 14) {
std::cerr << "operation mode " << oper_mode << " unsupported." << std::endl;
continue;
}
@ -449,16 +340,15 @@ struct Decoder
if (!okay)
return;
int cons_rows = cons_cnt / cons_cols;
int code_off = - cons_cols / 2;
for (int i = 0; i < symbol_pos+2*(symbol_len+guard_len); ++i)
for (int i = 0; i < symbol_pos+(symbol_len+guard_len); ++i)
buf = next_sample();
for (int i = 0; i < symbol_len; ++i)
tdom[i] = buf[i] * osc();
for (int i = 0; i < guard_len; ++i)
osc();
fwd(fdom, tdom);
for (int i = 0; i < cons_cols; ++i)
prev[i] = fdom[bin(i+code_off)];
std::cerr << "demod ";
for (int j = 0; j < cons_rows; ++j) {
for (int i = 0; i < symbol_len+guard_len; ++i)
@ -467,11 +357,11 @@ struct Decoder
tdom[i] = buf[i] * osc();
for (int i = 0; i < guard_len; ++i)
osc();
for (int i = 0; i < cons_cols; ++i)
prev[i] = fdom[bin(i+code_off)];
fwd(fdom, tdom);
for (int i = 0; i < cons_cols; ++i)
cons[cons_cols*j+i] = demod_or_erase(fdom[bin(i+code_off)], prev[i]);
for (int i = 0; i < cons_cols; ++i)
prev[i] = fdom[bin(i+code_off)];
std::cerr << ".";
}
std::cerr << " done" << std::endl;
@ -479,7 +369,7 @@ struct Decoder
value sum_slope = 0, sum_yint = 0;
for (int j = 0; j < cons_rows; ++j) {
for (int i = 0; i < cons_cols; ++i) {
code_type tmp[mod_max];
code_type tmp[mod_bits];
mod_hard(tmp, cons[cons_cols*j+i]);
index[i] = i + code_off;
phase[i] = arg(cons[cons_cols*j+i] * conj(mod_map(tmp)));
@ -506,7 +396,7 @@ struct Decoder
value sp = 0, np = 0;
for (int j = 0; j < cons_rows; ++j) {
for (int i = 0; i < cons_cols; ++i) {
code_type tmp[mod_max];
code_type tmp[mod_bits];
mod_hard(tmp, cons[cons_cols*j+i]);
cmplx hard = mod_map(tmp);
cmplx error = cons[cons_cols*j+i] - hard;
@ -517,17 +407,16 @@ struct Decoder
value snr = DSP::decibel(precision);
std::cerr << " " << snr;
for (int i = 0; i < cons_cols; ++i)
mod_soft(code+mod_bits*(cons_cols*j+i), cons[cons_cols*j+i], precision);
mod_soft(code+2*(cons_cols*j+i), cons[cons_cols*j+i], precision);
}
std::cerr << std::endl;
} else {
value precision = 8;
for (int i = 0; i < cons_cnt; ++i)
for (int i = 0; i < cons_total; ++i)
mod_soft(code+mod_bits*i, cons[i], precision);
}
lengthen();
CODE::PolarHelper<mesg_type>::PATH metric[mesg_type::SIZE];
polardec(metric, mesg, code, frozen_bits, code_order);
polardec(metric, mesg, code, frozen_2048_1392, code_order);
systematic();
int order[mesg_type::SIZE];
for (int k = 0; k < mesg_type::SIZE; ++k)
@ -549,7 +438,7 @@ struct Decoder
}
int flips = 0;
for (int i = 0, j = 0; i < data_bits; ++i, ++j) {
while ((frozen_bits[j / 32] >> (j % 32)) & 1)
while ((frozen_2048_1392[j / 32] >> (j % 32)) & 1)
++j;
bool received = code[j] < 0;
bool decoded = mesg[i].v[best] < 0;
@ -588,7 +477,7 @@ int main(int argc, char **argv)
if (argc > 3)
skip_count = std::atoi(argv[3]);
const int data_len = 43040 / 8;
const int data_len = 1360 / 8;
uint8_t *output_data = new uint8_t[data_len];
switch (input_file.rate()) {

171
encode.cc
View file

@ -27,10 +27,16 @@ template <typename value, typename cmplx, int rate>
struct Encoder
{
typedef int8_t code_type;
static const int oper_mode = 14;
static const int mod_bits = 2;
static const int code_order = 11;
static const int code_len = 1 << code_order;
static const int symbol_len = (1280 * rate) / 8000;
static const int guard_len = symbol_len / 8;
static const int data_bits = 43040;
static const int data_bits = 1360;
static const int crc_bits = data_bits + 32;
static const int cons_cols = 256;
static const int cons_rows = 4;
static const int mls0_len = 127;
static const int mls0_poly = 0b10001001;
static const int mls1_len = 255;
@ -44,21 +50,12 @@ struct Encoder
CODE::CRC<uint32_t> crc1;
CODE::BoseChaudhuriHocquenghemEncoder<255, 71> bchenc;
CODE::PolarSysEnc<code_type> polarenc;
code_type code[65536], mesg[44096];
code_type code[code_len], mesg[crc_bits];
cmplx fdom[symbol_len], fdom4[4*symbol_len];
cmplx tdom[symbol_len], tdom4[4*symbol_len];
cmplx temp[symbol_len];
cmplx guard[guard_len];
cmplx papr_min, papr_max;
const uint32_t *frozen_bits;
int code_order;
int oper_mode;
int mod_bits;
int cons_cnt;
int cons_cols;
int cons_rows;
int cons_bits;
int mesg_bits;
int code_off;
int mls0_off;
int mls1_off;
@ -175,109 +172,19 @@ struct Encoder
fdom[bin(i+mls1_off)] *= nrz(seq4());
symbol();
}
void shorten()
{
int code_bits = 1 << code_order;
for (int i = 0, j = 0, k = 0; i < code_bits; ++i)
if ((frozen_bits[i/32] >> (i%32)) & 1 || k++ < crc_bits)
code[j++] = code[i];
}
cmplx mod_map(code_type *b)
{
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;
return PhaseShiftKeying<4, cmplx, code_type>::map(b);
}
bool prepare()
{
switch (oper_mode) {
case 6:
cons_cols = 432;
mod_bits = 3;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 7:
cons_cols = 400;
mod_bits = 3;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 8:
cons_cols = 400;
mod_bits = 2;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 9:
cons_cols = 360;
mod_bits = 2;
code_order = 16;
cons_bits = 64800;
mesg_bits = 43808;
frozen_bits = frozen_64800_43072;
break;
case 10:
cons_cols = 512;
mod_bits = 3;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 11:
cons_cols = 384;
mod_bits = 3;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 12:
cons_cols = 384;
mod_bits = 2;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
case 13:
cons_cols = 256;
mod_bits = 2;
code_order = 16;
cons_bits = 64512;
mesg_bits = 44096;
frozen_bits = frozen_64512_43072;
break;
default:
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({
Encoder(DSP::WritePCM<value> *pcm, const uint8_t *inp, int freq_off, uint64_t call_sign) :
pcm(pcm), crc0(0xA8F4), crc1(0x8F6E37A0), 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)
0b110101001, 0b000011111, 0b110000111, 0b110110001})
{
if (!prepare())
return;
int offset = (freq_off * symbol_len) / rate;
code_off = offset - cons_cols / 2;
mls0_off = offset - mls0_len + 1;
@ -286,7 +193,6 @@ struct Encoder
pilot_block();
schmidl_cox();
meta_data((call_sign << 8) | oper_mode);
pilot_block();
for (int i = 0; i < data_bits; ++i)
mesg[i] = nrz(CODE::get_le_bit(inp, i));
crc1.reset();
@ -294,10 +200,7 @@ struct Encoder
crc1(inp[i]);
for (int i = 0; i < 32; ++i)
mesg[i+data_bits] = nrz((crc1()>>i)&1);
for (int i = crc_bits; i < mesg_bits; ++i)
mesg[i] = 1;
polarenc(code, mesg, frozen_bits, code_order);
shorten();
polarenc(code, mesg, frozen_2048_1392, code_order);
for (int j = 0; j < cons_rows; ++j) {
for (int i = 0; i < cons_cols; ++i)
fdom[bin(i+code_off)] *=
@ -332,8 +235,8 @@ long long int base37_encoder(const char *str)
int main(int argc, char **argv)
{
if (argc < 6 || argc > 9) {
std::cerr << "usage: " << argv[0] << " OUTPUT RATE BITS CHANNELS INPUT [OFFSET] [CALLSIGN] [MODE]" << std::endl;
if (argc < 6 || argc > 8) {
std::cerr << "usage: " << argv[0] << " OUTPUT RATE BITS CHANNELS INPUT [OFFSET] [CALLSIGN]" << std::endl;
return 1;
}
@ -360,39 +263,7 @@ int main(int argc, char **argv)
return 1;
}
int oper_mode = 6;
if (argc >= 9)
oper_mode = std::atoi(argv[8]);
if (oper_mode < 6 || oper_mode > 13) {
std::cerr << "Unsupported operation mode." << std::endl;
return 1;
}
int band_width;
switch (oper_mode) {
case 6:
band_width = 2700;
break;
case 7:
case 8:
band_width = 2500;
break;
case 9:
band_width = 2250;
break;
case 10:
band_width = 3200;
break;
case 11:
case 12:
band_width = 2400;
break;
case 13:
band_width = 1600;
break;
default:
return 1;
}
const int band_width = 1600;
if ((output_chan == 1 && freq_off < band_width / 2) || freq_off < band_width / 2 - output_rate / 2 || freq_off > output_rate / 2 - band_width / 2) {
std::cerr << "Unsupported frequency offset." << std::endl;
@ -412,7 +283,7 @@ int main(int argc, char **argv)
std::cerr << "Couldn't open file \"" << input_name << "\" for reading." << std::endl;
return 1;
}
const int data_len = 43040 / 8;
const int data_len = 1360 / 8;
uint8_t *input_data = new uint8_t[data_len];
for (int i = 0; i < data_len; ++i)
input_data[i] = input_file.get();
@ -424,16 +295,16 @@ int main(int argc, char **argv)
output_file.silence(output_rate);
switch (output_rate) {
case 8000:
delete new Encoder<value, cmplx, 8000>(&output_file, input_data, freq_off, call_sign, oper_mode);
delete new Encoder<value, cmplx, 8000>(&output_file, input_data, freq_off, call_sign);
break;
case 16000:
delete new Encoder<value, cmplx, 16000>(&output_file, input_data, freq_off, call_sign, oper_mode);
delete new Encoder<value, cmplx, 16000>(&output_file, input_data, freq_off, call_sign);
break;
case 44100:
delete new Encoder<value, cmplx, 44100>(&output_file, input_data, freq_off, call_sign, oper_mode);
delete new Encoder<value, cmplx, 44100>(&output_file, input_data, freq_off, call_sign);
break;
case 48000:
delete new Encoder<value, cmplx, 48000>(&output_file, input_data, freq_off, call_sign, oper_mode);
delete new Encoder<value, cmplx, 48000>(&output_file, input_data, freq_off, call_sign);
break;
default:
std::cerr << "Unsupported sample rate." << std::endl;

View file

@ -33,7 +33,6 @@ void code(int N, int K)
int main()
{
code<16>(64512, 43040+32);
code<16>(64800, 43040+32);
code<11>(2048, 1360+32);
return 0;
}

File diff suppressed because one or more lines are too long