Improved decode by extending search in time and better windowing

This commit is contained in:
Karlis Goba 2018-12-25 14:01:51 +02:00
parent 70837f4cbc
commit 0c2e35b998

View file

@ -10,6 +10,12 @@
#include "common/wave.h" #include "common/wave.h"
#include "fft/kiss_fftr.h" #include "fft/kiss_fftr.h"
const int kMax_candidates = 100;
const int kLDPC_iterations = 20;
const int kMax_decoded_messages = 50;
const int kMax_message_length = 20;
void usage() { void usage() {
printf("Decode a 15-second WAV file.\n"); printf("Decode a 15-second WAV file.\n");
} }
@ -21,10 +27,32 @@ float hann_i(int i, int N) {
} }
float hamming_i(int i, int N) {
const float a0 = (float)25 / 46;
const float a1 = 1 - a0;
float x1 = cosf(2 * (float)M_PI * i / (N - 1));
return a0 - a1*x1;
}
float blackman_i(int i, int N) {
const float alpha = 0.16f; // or 2860/18608
const float a0 = (1 - alpha) / 2;
const float a1 = 1.0f / 2;
const float a2 = alpha / 2;
float x1 = cosf(2 * (float)M_PI * i / (N - 1));
float x2 = cosf(4 * (float)M_PI * i / (N - 1));
return a0 - a1*x1 + a2*x2;
}
struct Candidate { struct Candidate {
int16_t score; int16_t score;
uint16_t time_offset; int16_t time_offset;
uint16_t freq_offset; int16_t freq_offset;
uint8_t time_sub; uint8_t time_sub;
uint8_t freq_sub; uint8_t freq_sub;
}; };
@ -79,21 +107,26 @@ int find_sync(const uint8_t *power, int num_blocks, int num_bins, const uint8_t
int heap_size = 0; int heap_size = 0;
for (int alt = 0; alt < 4; ++alt) { for (int alt = 0; alt < 4; ++alt) {
for (int time_offset = 0; time_offset < num_blocks - FT8_NN; ++time_offset) { for (int time_offset = -7; time_offset < num_blocks - FT8_NN + 7; ++time_offset) {
for (int freq_offset = 0; freq_offset < num_bins - 8; ++freq_offset) { for (int freq_offset = 0; freq_offset < num_bins - 8; ++freq_offset) {
int score = 0; int score = 0;
// Compute score over Costas symbols (0-7, 36-43, 72-79) // Compute score over Costas symbols (0-7, 36-43, 72-79)
int num_scores = 0;
for (int m = 0; m <= 72; m += 36) { for (int m = 0; m <= 72; m += 36) {
for (int k = 0; k < 7; ++k) { for (int k = 0; k < 7; ++k) {
if (time_offset + k + m < 0) continue;
if (time_offset + k + m >= num_blocks) break;
int offset = ((time_offset + k + m) * 4 + alt) * num_bins + freq_offset; int offset = ((time_offset + k + m) * 4 + alt) * num_bins + freq_offset;
score += 8 * (int)power[offset + sync_map[k]] - score += 8 * (int)power[offset + sync_map[k]] -
power[offset + 0] - power[offset + 1] - power[offset + 0] - power[offset + 1] -
power[offset + 2] - power[offset + 3] - power[offset + 2] - power[offset + 3] -
power[offset + 4] - power[offset + 5] - power[offset + 4] - power[offset + 5] -
power[offset + 6] - power[offset + 7]; power[offset + 6] - power[offset + 7];
++num_scores;
} }
} }
score /= num_scores;
// If the heap is full AND the current candidate is better than // If the heap is full AND the current candidate is better than
// the worst in the heap, we remove the worst and make space // the worst in the heap, we remove the worst and make space
@ -130,11 +163,8 @@ void extract_power(const float * signal, int num_blocks, int num_bins, uint8_t *
float window[nfft]; float window[nfft];
for (int i = 0; i < nfft; ++i) { for (int i = 0; i < nfft; ++i) {
window[i] = hann_i(i, nfft); window[i] = blackman_i(i, nfft);
} }
// for (int i = 0; i < nfft; ++i) {
// window[i] = (i < block_size) ? 2 * hann_i(i, block_size) : 0.0f;
// }
size_t fft_work_size; size_t fft_work_size;
kiss_fftr_alloc(nfft, 0, 0, &fft_work_size); kiss_fftr_alloc(nfft, 0, 0, &fft_work_size);
@ -187,18 +217,17 @@ void extract_power(const float * signal, int num_blocks, int num_bins, uint8_t *
} }
printf("Max magnitude: %.1f dB\n", max_mag); printf("Max magnitude: %.1f dB\n", max_mag);
free(fft_work); free(fft_work);
} }
uint8_t max2(uint8_t a, uint8_t b) { float max2(float a, float b) {
return (a >= b) ? a : b; return (a >= b) ? a : b;
} }
uint8_t max4(uint8_t a, uint8_t b, uint8_t cand, uint8_t d) { float max4(float a, float b, float c, float d) {
return max2(max2(a, b), max2(cand, d)); return max2(max2(a, b), max2(c, d));
} }
@ -207,45 +236,61 @@ uint8_t max4(uint8_t a, uint8_t b, uint8_t cand, uint8_t d) {
void extract_likelihood(const uint8_t *power, int num_bins, const Candidate & cand, const uint8_t *code_map, float *log174) { void extract_likelihood(const uint8_t *power, int num_bins, const Candidate & cand, const uint8_t *code_map, float *log174) {
int offset = (cand.time_offset * 4 + cand.time_sub * 2 + cand.freq_sub) * num_bins + cand.freq_offset; int offset = (cand.time_offset * 4 + cand.time_sub * 2 + cand.freq_sub) * num_bins + cand.freq_offset;
int k = 0;
// Go over FSK tones and skip Costas sync symbols // Go over FSK tones and skip Costas sync symbols
for (int i = 7; i < FT8_NN - 7; ++i) { const int n_syms = 1;
if (i == 36) i += 7; const int n_bits = 3 * n_syms;
const int n_tones = (1 << n_bits);
for (int k = 0; k < FT8_ND; k += n_syms) {
int sym_idx = (k < FT8_ND / 2) ? (k + 7) : (k + 14);
// Pointer to 8 bins of the current symbol // Pointer to 8 bins of the current symbol
const uint8_t * ps = power + (offset + i * 4 * num_bins); const uint8_t *ps = power + (offset + sym_idx * 4 * num_bins);
uint8_t s2[8]; float s2[n_tones];
for (int i = 0; i < 8; ++i) { for (int j = 0; j < n_tones; ++j) {
s2[i] = ps[code_map[i]]; int j1 = j & 0x07;
s2[j] = (float)ps[code_map[j1]];
//int j2 = (j >> 3) & 0x07;
//s2[j] = (float)ps[code_map[j2]];
//s2[j] += (float)ps[code_map[j1] + 4 * num_bins];
} }
// Extract bit significance (and convert them to float) // Extract bit significance (and convert them to float)
// 8 FSK tones = 3 bits // 8 FSK tones = 3 bits
log174[k + 0] = (int)max4(s2[4], s2[5], s2[6], s2[7]) - (int)max4(s2[0], s2[1], s2[2], s2[3]); int bit_idx = 3 * k;
log174[k + 1] = (int)max4(s2[2], s2[3], s2[6], s2[7]) - (int)max4(s2[0], s2[1], s2[4], s2[5]); for (int i = 0; i < n_bits; ++i) {
log174[k + 2] = (int)max4(s2[1], s2[3], s2[5], s2[7]) - (int)max4(s2[0], s2[2], s2[4], s2[6]); uint16_t mask = (n_tones >> (i + 1));
// printf("%d %d %d %d %d %d %d %d : %.0f %.0f %.0f\n",
// ps[0], ps[1], ps[2], ps[3], ps[4], ps[5], ps[6], ps[7],
// log174[k + 0], log174[k + 1], log174[k + 2]);
k += 3; float max_zero = -1000, max_one = -1000;
for (int n = 0; n < n_tones; ++n) {
if (n & mask) {
max_one = max2(max_one, s2[n]);
}
else {
max_zero = max2(max_zero, s2[n]);
}
}
if (bit_idx + i >= 174) break;
log174[bit_idx + i] = max_one - max_zero;
}
// log174[bit_idx + 0] = max4(s2[4], s2[5], s2[6], s2[7]) - max4(s2[0], s2[1], s2[2], s2[3]);
// log174[bit_idx + 1] = max4(s2[2], s2[3], s2[6], s2[7]) - max4(s2[0], s2[1], s2[4], s2[5]);
// log174[bit_idx + 2] = max4(s2[1], s2[3], s2[5], s2[7]) - max4(s2[0], s2[2], s2[4], s2[6]);
} }
// Compute the variance of log174 // Compute the variance of log174
float sum = 0; float sum = 0;
float sum2 = 0; float sum2 = 0;
float inv_n = 1.0f / (3 * FT8_ND); float inv_n = 1.0f / FT8_N;
for (int i = 0; i < 3 * FT8_ND; ++i) { for (int i = 0; i < FT8_N; ++i) {
sum += log174[i]; sum += log174[i];
sum2 += log174[i] * log174[i]; sum2 += log174[i] * log174[i];
} }
float variance = (sum2 - sum * sum * inv_n) * inv_n; float variance = (sum2 - sum * sum * inv_n) * inv_n;
// Normalize log174 such that sigma = 2.83 (Why? It's in WSJT-X) // Normalize log174 such that sigma = 2.83 (Why? It's in WSJT-X)
float norm_factor = 2.83f / sqrtf(variance); float norm_factor = 3.83f / sqrtf(variance);
for (int i = 0; i < FT8_N; ++i) {
for (int i = 0; i < 3 * FT8_ND; ++i) {
log174[i] *= norm_factor; log174[i] *= norm_factor;
//printf("%.1f ", log174[i]); //printf("%.1f ", log174[i]);
} }
@ -300,61 +345,65 @@ int main(int argc, char ** argv) {
return -1; return -1;
} }
const float fsk_dev = 6.25f; const float fsk_dev = 6.25f; // tone deviation in Hz and symbol rate
// Compute DSP parameters that depend on the sample rate
const int num_bins = (int)(sample_rate / (2 * fsk_dev)); const int num_bins = (int)(sample_rate / (2 * fsk_dev));
const int block_size = 2 * num_bins; const int block_size = 2 * num_bins;
const int num_blocks = (num_samples - (block_size/2) - block_size) / block_size; const int num_blocks = (num_samples - (block_size/2) - block_size) / block_size;
uint8_t power[num_blocks * 4 * num_bins]; // [num_blocks][4][num_bins] ~ 200 KB
printf("%d blocks, %d bins\n", num_blocks, num_bins); printf("%d blocks, %d bins\n", num_blocks, num_bins);
// Compute FFT over the whole signal and store it
uint8_t power[num_blocks * 4 * num_bins];
extract_power(signal, num_blocks, num_bins, power); extract_power(signal, num_blocks, num_bins, power);
int num_candidates = 100; Candidate heap[kMax_candidates];
Candidate heap[num_candidates]; char decoded[kMax_decoded_messages][kMax_message_length];
int num_decoded = 0;
find_sync(power, num_blocks, num_bins, kCostas_map, num_candidates, heap); int num_candidates = find_sync(power, num_blocks, num_bins, kCostas_map, kMax_candidates, heap);
for (int idx = 0; idx < num_candidates; ++idx) { for (int idx = 0; idx < num_candidates; ++idx) {
Candidate &cand = heap[idx]; Candidate &cand = heap[idx];
float log174[3 * FT8_ND]; float log174[FT8_N];
extract_likelihood(power, num_bins, cand, kGray_map, log174); extract_likelihood(power, num_bins, cand, kGray_map, log174);
const int num_iters = 25; // bp_decode() produces better decodes, uses way less memory
uint8_t plain[3 * FT8_ND]; uint8_t plain[FT8_N];
int n_errors = 0; int n_errors = 0;
bp_decode(log174, kLDPC_iterations, plain, &n_errors);
//ldpc_decode(log174, num_iters, plain, &n_errors);
if (n_errors > 0) {
//printf("ldpc_decode() = %d\n", n_errors);
continue;
}
float freq_hz = (cand.freq_offset + cand.freq_sub / 2.0f) * fsk_dev; float freq_hz = (cand.freq_offset + cand.freq_sub / 2.0f) * fsk_dev;
float time_sec = (cand.time_offset + cand.time_sub / 2.0f) / fsk_dev; float time_sec = (cand.time_offset + cand.time_sub / 2.0f) / fsk_dev;
printf("%03d: score = %d freq = %.1f time = %.2f\n", idx, // printf("%03d: score = %d freq = %.1f time = %.2f\n", idx,
cand.score, freq_hz, time_sec); // cand.score, freq_hz, time_sec);
bp_decode(log174, num_iters, plain, &n_errors);
//ldpc_decode(log174, num_iters, plain, &n_errors);
printf("ldpc_decode() = %d\n", n_errors);
if (n_errors == 0) {
//print_tones(kGray_map, log174); //print_tones(kGray_map, log174);
// Extract payload + CRC // Extract payload + CRC
uint8_t a91[12]; uint8_t a91[12];
uint8_t mask = 0x80; uint8_t mask = 0x80;
uint8_t position = 0; int byte_idx = 0;
for (int i = 0; i < 12; ++i) { for (int i = 0; i < 12; ++i) {
a91[i] = 0; a91[i] = 0;
} }
for (int i = 0; i < FT8_K; ++i) { for (int i = 0; i < FT8_K; ++i) {
if (plain[i]) { if (plain[i]) {
a91[position] |= mask; a91[byte_idx] |= mask;
} }
mask >>= 1; mask >>= 1;
if (!mask) { if (!mask) {
mask = 0x80; mask = 0x80;
++position; ++byte_idx;
} }
} }
@ -365,13 +414,28 @@ int main(int argc, char ** argv) {
// } // }
// printf("\n"); // printf("\n");
char message[20]; char message[kMax_message_length];
unpack77(a91, message); unpack77(a91, message);
// Check for duplicate messages
bool found = false;
for (int i = 0; i < num_decoded; ++i) {
if (0 == strcmp(decoded[i], message)) {
found = true;
break;
}
}
if (!found && num_decoded < kMax_decoded_messages) {
strcpy(decoded[num_decoded], message);
++num_decoded;
// Fake WSJT-X-like output for now // Fake WSJT-X-like output for now
printf("000000 0 %4.1f %4d ~ %s\n", time_sec, (int)(freq_hz + 0.5f), message); int snr = 0; // TODO: compute SNR
printf("000000 %3d %4.1f %4d ~ %s\n", snr, time_sec, (int)(freq_hz + 0.5f), message);
} }
} }
printf("Decoded %d messages\n", num_decoded);
return 0; return 0;
} }