mirror of
https://github.com/aicodix/modem.git
synced 2026-04-27 14:30:34 +00:00
Compare commits
7 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d43a58b19 | ||
|
|
567319214c | ||
|
|
2039af90a2 | ||
|
|
fdc11fcec7 | ||
|
|
fd43c54e56 | ||
|
|
7d104033e1 | ||
|
|
81c02e5207 |
4 changed files with 92 additions and 85 deletions
2
Makefile
vendored
2
Makefile
vendored
|
|
@ -11,7 +11,7 @@ CXX = clang++ -stdlib=libc++ -march=native
|
||||||
all: encode decode
|
all: encode decode
|
||||||
|
|
||||||
test: encode decode
|
test: encode decode
|
||||||
$(QEMU) ./encode encoded.wav 8000 8 1 /dev/urandom
|
$(QEMU) ./encode encoded.wav 8000 8 1 2000 6 ANONYMOUS /dev/urandom
|
||||||
$(QEMU) ./decode /dev/null encoded.wav
|
$(QEMU) ./decode /dev/null encoded.wav
|
||||||
|
|
||||||
encode: encode.cc
|
encode: encode.cc
|
||||||
|
|
|
||||||
4
README.md
vendored
4
README.md
vendored
|
|
@ -12,7 +12,7 @@ dd if=/dev/urandom of=uncoded.dat bs=1 count=5380
|
||||||
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:
|
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:
|
||||||
|
|
||||||
```
|
```
|
||||||
./encode encoded.wav 8000 16 1 uncoded.dat
|
./encode encoded.wav 8000 16 1 2000 6 CALLSIGN uncoded.dat
|
||||||
```
|
```
|
||||||
|
|
||||||
Start recording to ```recorded.wav``` audio file and stop after 20 seconds:
|
Start recording to ```recorded.wav``` audio file and stop after 20 seconds:
|
||||||
|
|
@ -46,7 +46,7 @@ Prerequisite: [disorders](https://github.com/aicodix/disorders)
|
||||||
Encode ```uncoded.dat``` to [analytic](https://en.wikipedia.org/wiki/Analytic_signal) audio signal, add [multipath](https://en.wikipedia.org/wiki/Multipath_propagation), [CFO, SFO](https://en.wikipedia.org/wiki/Carrier_frequency_offset), [AWGN](https://en.wikipedia.org/wiki/Additive_white_Gaussian_noise), decode and compare:
|
Encode ```uncoded.dat``` to [analytic](https://en.wikipedia.org/wiki/Analytic_signal) audio signal, add [multipath](https://en.wikipedia.org/wiki/Multipath_propagation), [CFO, SFO](https://en.wikipedia.org/wiki/Carrier_frequency_offset), [AWGN](https://en.wikipedia.org/wiki/Additive_white_Gaussian_noise), decode and compare:
|
||||||
|
|
||||||
```
|
```
|
||||||
./encode - 8000 16 2 uncoded.dat 2000 | ../disorders/multipath - - ../disorders/multipath.txt 10 | ../disorders/cfo - - 234.567 | ../disorders/sfo - - 147 | ../disorders/awgn - - -30 | ./decode - - | diff -s uncoded.dat -
|
./encode - 8000 16 2 2000 6 CALLSIGN uncoded.dat | ../disorders/multipath - - ../disorders/multipath.txt 10 | ../disorders/cfo - - 234.567 | ../disorders/sfo - - 147 | ../disorders/awgn - - -30 | ./decode - - | diff -s uncoded.dat -
|
||||||
```
|
```
|
||||||
|
|
||||||
### Reading
|
### Reading
|
||||||
|
|
|
||||||
60
decode.cc
60
decode.cc
|
|
@ -7,6 +7,7 @@ Copyright 2021 Ahmet Inan <inan@aicodix.de>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
namespace DSP { using std::abs; using std::min; using std::cos; using std::sin; }
|
namespace DSP { using std::abs; using std::min; using std::cos; using std::sin; }
|
||||||
#include "bip_buffer.hh"
|
#include "bip_buffer.hh"
|
||||||
|
|
@ -501,41 +502,40 @@ struct Decoder
|
||||||
std::cerr << "coarse sfo: " << 1000000 * sfo_rad / Const::TwoPi() << " ppm" << std::endl;
|
std::cerr << "coarse sfo: " << 1000000 * sfo_rad / Const::TwoPi() << " ppm" << std::endl;
|
||||||
std::cerr << "finer cfo: " << cfo_rad * (rate / Const::TwoPi()) << " Hz " << std::endl;
|
std::cerr << "finer cfo: " << cfo_rad * (rate / Const::TwoPi()) << " Hz " << std::endl;
|
||||||
}
|
}
|
||||||
value precision = 16;
|
|
||||||
if (1) {
|
if (1) {
|
||||||
|
std::cerr << "Es/N0 (dB):";
|
||||||
value sp = 0, np = 0;
|
value sp = 0, np = 0;
|
||||||
for (int i = 0; i < cons_cnt; ++i) {
|
for (int j = 0; j < cons_rows; ++j) {
|
||||||
code_type tmp[mod_max];
|
for (int i = 0; i < cons_cols; ++i) {
|
||||||
mod_hard(tmp, cons[i]);
|
code_type tmp[mod_max];
|
||||||
cmplx hard = mod_map(tmp);
|
mod_hard(tmp, cons[cons_cols*j+i]);
|
||||||
cmplx error = cons[i] - hard;
|
cmplx hard = mod_map(tmp);
|
||||||
sp += norm(hard);
|
cmplx error = cons[cons_cols*j+i] - hard;
|
||||||
np += norm(error);
|
sp += norm(hard);
|
||||||
|
np += norm(error);
|
||||||
|
}
|
||||||
|
value precision = sp / np;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
value snr = DSP::decibel(sp / np);
|
std::cerr << std::endl;
|
||||||
std::cerr << "init Es/N0: " << snr << " dB" << std::endl;
|
} else {
|
||||||
// $LLR=log(\frac{p(x=+1|y)}{p(x=-1|y)})$
|
value precision = 8;
|
||||||
// $p(x|\mu,\sigma)=\frac{1}{\sqrt{2\pi}\sigma}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$
|
for (int i = 0; i < cons_cnt; ++i)
|
||||||
value sigma = std::sqrt(np / (2 * sp));
|
mod_soft(code+mod_bits*i, cons[i], precision);
|
||||||
precision = 1 / (sigma * sigma);
|
|
||||||
}
|
}
|
||||||
for (int i = 0; i < cons_cnt; ++i)
|
|
||||||
mod_soft(code+mod_bits*i, cons[i], precision);
|
|
||||||
lengthen();
|
lengthen();
|
||||||
CODE::PolarHelper<mesg_type>::PATH metric[mesg_type::SIZE];
|
polardec(nullptr, mesg, code, frozen_bits, code_order);
|
||||||
polardec(metric, mesg, code, frozen_bits, code_order);
|
|
||||||
systematic();
|
systematic();
|
||||||
int order[mesg_type::SIZE];
|
|
||||||
for (int k = 0; k < mesg_type::SIZE; ++k)
|
|
||||||
order[k] = k;
|
|
||||||
std::sort(order, order+mesg_type::SIZE, [metric](int a, int b){ return metric[a] < metric[b]; });
|
|
||||||
int best = -1;
|
int best = -1;
|
||||||
for (int k = 0; k < mesg_type::SIZE; ++k) {
|
for (int k = 0; k < mesg_type::SIZE; ++k) {
|
||||||
crc1.reset();
|
crc1.reset();
|
||||||
for (int i = 0; i < crc_bits; ++i)
|
for (int i = 0; i < crc_bits; ++i)
|
||||||
crc1(mesg[i].v[order[k]] < 0);
|
crc1(mesg[i].v[k] < 0);
|
||||||
if (crc1() == 0) {
|
if (crc1() == 0) {
|
||||||
best = order[k];
|
best = k;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -543,8 +543,16 @@ struct Decoder
|
||||||
std::cerr << "payload decoding error." << std::endl;
|
std::cerr << "payload decoding error." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < data_bits; ++i)
|
int flips = 0;
|
||||||
CODE::set_le_bit(out, i, mesg[i].v[best] < 0);
|
for (int i = 0, j = 0; i < data_bits; ++i, ++j) {
|
||||||
|
while ((frozen_bits[j / 32] >> (j % 32)) & 1)
|
||||||
|
++j;
|
||||||
|
bool received = code[j] < 0;
|
||||||
|
bool decoded = mesg[i].v[best] < 0;
|
||||||
|
flips += received != decoded;
|
||||||
|
CODE::set_le_bit(out, i, decoded);
|
||||||
|
}
|
||||||
|
std::cerr << "bit flips: " << flips << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
111
encode.cc
111
encode.cc
|
|
@ -6,6 +6,7 @@ Copyright 2021 Ahmet Inan <inan@aicodix.de>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstdint>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include "xorshift.hh"
|
#include "xorshift.hh"
|
||||||
#include "complex.hh"
|
#include "complex.hh"
|
||||||
|
|
@ -30,6 +31,7 @@ struct Encoder
|
||||||
static const int symbol_len = (1280 * rate) / 8000;
|
static const int symbol_len = (1280 * rate) / 8000;
|
||||||
static const int guard_len = symbol_len / 8;
|
static const int guard_len = symbol_len / 8;
|
||||||
static const int data_bits = 43040;
|
static const int data_bits = 43040;
|
||||||
|
static const int data_bytes = data_bits / 8;
|
||||||
static const int crc_bits = data_bits + 32;
|
static const int crc_bits = data_bits + 32;
|
||||||
static const int mls0_len = 127;
|
static const int mls0_len = 127;
|
||||||
static const int mls0_poly = 0b10001001;
|
static const int mls0_poly = 0b10001001;
|
||||||
|
|
@ -266,7 +268,7 @@ struct Encoder
|
||||||
cons_rows = cons_cnt / cons_cols;
|
cons_rows = cons_cnt / cons_cols;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Encoder(DSP::WritePCM<value> *pcm, const uint8_t *inp, int freq_off, uint64_t call_sign, int oper_mode) :
|
Encoder(DSP::WritePCM<value> *pcm, const uint8_t *inp, int count, int freq_off, uint64_t call_sign, int oper_mode) :
|
||||||
pcm(pcm), crc0(0xA8F4), crc1(0xD419CC15), bchenc({
|
pcm(pcm), crc0(0xA8F4), crc1(0xD419CC15), bchenc({
|
||||||
0b100011101, 0b101110111, 0b111110011, 0b101101001,
|
0b100011101, 0b101110111, 0b111110011, 0b101101001,
|
||||||
0b110111101, 0b111100111, 0b100101011, 0b111010111,
|
0b110111101, 0b111100111, 0b100101011, 0b111010111,
|
||||||
|
|
@ -284,25 +286,27 @@ struct Encoder
|
||||||
mls1_off = offset - mls1_len / 2;
|
mls1_off = offset - mls1_len / 2;
|
||||||
papr_min = cmplx(1000, 1000), papr_max = cmplx(-1000, -1000);
|
papr_min = cmplx(1000, 1000), papr_max = cmplx(-1000, -1000);
|
||||||
pilot_block();
|
pilot_block();
|
||||||
schmidl_cox();
|
for (int k = 0; k < count; ++k) {
|
||||||
meta_data((call_sign << 8) | oper_mode);
|
schmidl_cox();
|
||||||
pilot_block();
|
meta_data((call_sign << 8) | oper_mode);
|
||||||
for (int i = 0; i < data_bits; ++i)
|
pilot_block();
|
||||||
mesg[i] = nrz(CODE::get_le_bit(inp, i));
|
for (int i = 0; i < data_bits; ++i)
|
||||||
crc1.reset();
|
mesg[i] = nrz(CODE::get_le_bit(inp+k*data_bytes, i));
|
||||||
for (int i = 0; i < data_bits / 8; ++i)
|
crc1.reset();
|
||||||
crc1(inp[i]);
|
for (int i = 0; i < data_bytes; ++i)
|
||||||
for (int i = 0; i < 32; ++i)
|
crc1(inp[i+k*data_bytes]);
|
||||||
mesg[i+data_bits] = nrz((crc1()>>i)&1);
|
for (int i = 0; i < 32; ++i)
|
||||||
for (int i = crc_bits; i < mesg_bits; ++i)
|
mesg[i+data_bits] = nrz((crc1()>>i)&1);
|
||||||
mesg[i] = 1;
|
for (int i = crc_bits; i < mesg_bits; ++i)
|
||||||
polarenc(code, mesg, frozen_bits, code_order);
|
mesg[i] = 1;
|
||||||
shorten();
|
polarenc(code, mesg, frozen_bits, code_order);
|
||||||
for (int j = 0; j < cons_rows; ++j) {
|
shorten();
|
||||||
for (int i = 0; i < cons_cols; ++i)
|
for (int j = 0; j < cons_rows; ++j) {
|
||||||
fdom[bin(i+code_off)] *=
|
for (int i = 0; i < cons_cols; ++i)
|
||||||
mod_map(code+mod_bits*(cons_cols*j+i));
|
fdom[bin(i+code_off)] *=
|
||||||
symbol();
|
mod_map(code+mod_bits*(cons_cols*j+i));
|
||||||
|
symbol();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < symbol_len; ++i)
|
for (int i = 0; i < symbol_len; ++i)
|
||||||
fdom[i] = 0;
|
fdom[i] = 0;
|
||||||
|
|
@ -332,8 +336,8 @@ long long int base37_encoder(const char *str)
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
if (argc < 6 || argc > 9) {
|
if (argc < 9) {
|
||||||
std::cerr << "usage: " << argv[0] << " OUTPUT RATE BITS CHANNELS INPUT [OFFSET] [CALLSIGN] [MODE]" << std::endl;
|
std::cerr << "usage: " << argv[0] << " OUTPUT RATE BITS CHANNELS OFFSET MODE CALLSIGN INPUT.." << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -343,30 +347,18 @@ int main(int argc, char **argv)
|
||||||
int output_rate = std::atoi(argv[2]);
|
int output_rate = std::atoi(argv[2]);
|
||||||
int output_bits = std::atoi(argv[3]);
|
int output_bits = std::atoi(argv[3]);
|
||||||
int output_chan = std::atoi(argv[4]);
|
int output_chan = std::atoi(argv[4]);
|
||||||
const char *input_name = argv[5];
|
|
||||||
if (input_name[0] == '-' && input_name[1] == 0)
|
|
||||||
input_name = "/dev/stdin";
|
|
||||||
|
|
||||||
int freq_off = output_chan == 1 ? 2000 : 0;
|
int freq_off = std::atoi(argv[5]);
|
||||||
if (argc >= 7)
|
int oper_mode = std::atoi(argv[6]);
|
||||||
freq_off = std::atoi(argv[6]);
|
|
||||||
|
|
||||||
long long int call_sign = base37_encoder("ANONYMOUS");
|
|
||||||
if (argc >= 8)
|
|
||||||
call_sign = base37_encoder(argv[7]);
|
|
||||||
|
|
||||||
if (call_sign <= 0 || call_sign >= 129961739795077L) {
|
|
||||||
std::cerr << "Unsupported call sign." << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int oper_mode = 6;
|
|
||||||
if (argc >= 9)
|
|
||||||
oper_mode = std::atoi(argv[8]);
|
|
||||||
if (oper_mode < 6 || oper_mode > 13) {
|
if (oper_mode < 6 || oper_mode > 13) {
|
||||||
std::cerr << "Unsupported operation mode." << std::endl;
|
std::cerr << "Unsupported operation mode." << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
long long int call_sign = base37_encoder(argv[7]);
|
||||||
|
if (call_sign <= 0 || call_sign >= 129961739795077L) {
|
||||||
|
std::cerr << "Unsupported call sign." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int band_width;
|
int band_width;
|
||||||
switch (oper_mode) {
|
switch (oper_mode) {
|
||||||
|
|
@ -407,33 +399,40 @@ int main(int argc, char **argv)
|
||||||
typedef float value;
|
typedef float value;
|
||||||
typedef DSP::Complex<value> cmplx;
|
typedef DSP::Complex<value> cmplx;
|
||||||
|
|
||||||
std::ifstream input_file(input_name, std::ios::binary);
|
|
||||||
if (input_file.bad()) {
|
const int input_count = argc - 8;
|
||||||
std::cerr << "Couldn't open file \"" << input_name << "\" for reading." << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const int data_len = 43040 / 8;
|
const int data_len = 43040 / 8;
|
||||||
uint8_t *input_data = new uint8_t[data_len];
|
uint8_t *input_data = new uint8_t[data_len*input_count];
|
||||||
for (int i = 0; i < data_len; ++i)
|
for (int j = 0; j < input_count; ++j) {
|
||||||
input_data[i] = input_file.get();
|
const char *input_name = argv[j+8];
|
||||||
CODE::Xorshift32 scrambler;
|
if (argc == 9 && input_name[0] == '-' && input_name[1] == 0)
|
||||||
for (int i = 0; i < data_len; ++i)
|
input_name = "/dev/stdin";
|
||||||
input_data[i] ^= scrambler();
|
std::ifstream input_file(input_name, std::ios::binary);
|
||||||
|
if (input_file.bad()) {
|
||||||
|
std::cerr << "Couldn't open file \"" << input_name << "\" for reading." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < data_len; ++i)
|
||||||
|
input_data[j*data_len+i] = input_file.get();
|
||||||
|
CODE::Xorshift32 scrambler;
|
||||||
|
for (int i = 0; i < data_len; ++i)
|
||||||
|
input_data[j*data_len+i] ^= scrambler();
|
||||||
|
}
|
||||||
|
|
||||||
DSP::WriteWAV<value> output_file(output_name, output_rate, output_bits, output_chan);
|
DSP::WriteWAV<value> output_file(output_name, output_rate, output_bits, output_chan);
|
||||||
output_file.silence(output_rate);
|
output_file.silence(output_rate);
|
||||||
switch (output_rate) {
|
switch (output_rate) {
|
||||||
case 8000:
|
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, input_count, freq_off, call_sign, oper_mode);
|
||||||
break;
|
break;
|
||||||
case 16000:
|
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, input_count, freq_off, call_sign, oper_mode);
|
||||||
break;
|
break;
|
||||||
case 44100:
|
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, input_count, freq_off, call_sign, oper_mode);
|
||||||
break;
|
break;
|
||||||
case 48000:
|
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, input_count, freq_off, call_sign, oper_mode);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "Unsupported sample rate." << std::endl;
|
std::cerr << "Unsupported sample rate." << std::endl;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue