2018-12-27 20:33:07 +00:00
|
|
|
|
#include "decode.h"
|
|
|
|
|
#include "constants.h"
|
2021-08-12 10:43:25 +00:00
|
|
|
|
#include "crc.h"
|
|
|
|
|
#include "ldpc.h"
|
|
|
|
|
#include "unpack.h"
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-08-05 11:11:21 +00:00
|
|
|
|
#include <stdbool.h>
|
2021-08-05 10:56:35 +00:00
|
|
|
|
#include <math.h>
|
2019-01-02 18:54:18 +00:00
|
|
|
|
|
2022-06-16 09:28:07 +00:00
|
|
|
|
// #define LOG_LEVEL LOG_DEBUG
|
|
|
|
|
// #include "debug.h"
|
|
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
|
/// Compute log likelihood log(p(1) / p(0)) of 174 message bits for later use in soft-decision LDPC decoding
|
2021-12-14 08:38:07 +00:00
|
|
|
|
/// @param[in] wf Waterfall data collected during message slot
|
2021-08-12 10:43:25 +00:00
|
|
|
|
/// @param[in] cand Candidate to extract the message from
|
|
|
|
|
/// @param[in] code_map Symbol encoding map
|
|
|
|
|
/// @param[out] log174 Output of decoded log likelihoods for each of the 174 message bits
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ft4_extract_likelihood(const waterfall_t* wf, const candidate_t* cand, float* log174);
|
|
|
|
|
static void ft8_extract_likelihood(const waterfall_t* wf, const candidate_t* cand, float* log174);
|
2021-11-17 08:31:01 +00:00
|
|
|
|
|
|
|
|
|
/// Packs a string of bits each represented as a zero/non-zero byte in bit_array[],
|
|
|
|
|
/// as a string of packed bits starting from the MSB of the first byte of packed[]
|
|
|
|
|
/// @param[in] plain Array of bits (0 and nonzero values) with num_bits entires
|
|
|
|
|
/// @param[in] num_bits Number of bits (entries) passed in bit_array
|
|
|
|
|
/// @param[out] packed Byte-packed bits representing the data in bit_array
|
|
|
|
|
static void pack_bits(const uint8_t bit_array[], int num_bits, uint8_t packed[]);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
|
2018-12-27 20:33:07 +00:00
|
|
|
|
static float max2(float a, float b);
|
|
|
|
|
static float max4(float a, float b, float c, float d);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
static void heapify_down(candidate_t heap[], int heap_size);
|
|
|
|
|
static void heapify_up(candidate_t heap[], int heap_size);
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ftx_normalize_logl(float* log174);
|
|
|
|
|
static void ft4_extract_symbol(const uint8_t* wf, float* logl);
|
|
|
|
|
static void ft8_extract_symbol(const uint8_t* wf, float* logl);
|
|
|
|
|
static void ft8_decode_multi_symbols(const uint8_t* wf, int num_bins, int n_syms, int bit_idx, float* log174);
|
|
|
|
|
|
2022-05-11 12:33:26 +00:00
|
|
|
|
static const uint8_t* get_cand_mag(const waterfall_t* wf, const candidate_t* candidate)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2021-12-08 21:33:52 +00:00
|
|
|
|
int offset = candidate->time_offset;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
offset = (offset * wf->time_osr) + candidate->time_sub;
|
|
|
|
|
offset = (offset * wf->freq_osr) + candidate->freq_sub;
|
|
|
|
|
offset = (offset * wf->num_bins) + candidate->freq_offset;
|
2022-05-11 12:33:26 +00:00
|
|
|
|
return wf->mag + offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ft8_snr(const waterfall_t* wf, const candidate_t* candidate)
|
|
|
|
|
{
|
|
|
|
|
int sum_signal = 0;
|
|
|
|
|
int sum_noise = 0;
|
|
|
|
|
int num_average = 0;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 0 of the candidate
|
|
|
|
|
const uint8_t* mag_cand = get_cand_mag(wf, candidate);
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if (wf->protocol == FTX_PROTOCOL_FT4)
|
2022-05-11 12:33:26 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute average score over sync symbols (m+k = 0-7, 36-43, 72-79)
|
|
|
|
|
for (int block = 0; block < FT8_NN; ++block)
|
|
|
|
|
{
|
|
|
|
|
int block_abs = candidate->time_offset + block; // relative to the captured signal
|
|
|
|
|
// Check for time boundaries
|
|
|
|
|
if (block_abs < 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (block_abs >= wf->num_blocks)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 'block' of the candidate
|
|
|
|
|
const uint8_t* p8 = mag_cand + (block * wf->block_stride);
|
|
|
|
|
|
|
|
|
|
int k = block % FT8_SYNC_OFFSET;
|
|
|
|
|
int sm = -1;
|
|
|
|
|
if (k < 7)
|
|
|
|
|
{
|
|
|
|
|
// Check only the neighbors of the expected symbol frequency- and time-wise
|
|
|
|
|
sm = kFT8_Costas_pattern[k]; // Index of the expected bin
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int max;
|
|
|
|
|
for (int m = 0; m < 8; ++m)
|
|
|
|
|
{
|
|
|
|
|
if ((sm == -1) || (p8[m] > max))
|
|
|
|
|
{
|
|
|
|
|
sm = m;
|
|
|
|
|
max = p8[m];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sm != -1)
|
|
|
|
|
{
|
|
|
|
|
sum_signal += p8[sm];
|
|
|
|
|
sum_noise += (3 + (int)p8[0] + (int)p8[1] + (int)p8[2] + (int)p8[3] + (int)p8[4] + (int)p8[5] + (int)p8[6] + (int)p8[7] - (int)p8[sm]) / 7;
|
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// return num_average;
|
|
|
|
|
return (sum_signal - sum_noise) / num_average;
|
2021-12-08 21:33:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static int ft8_sync_score(const waterfall_t* wf, const candidate_t* candidate)
|
2021-12-08 21:33:52 +00:00
|
|
|
|
{
|
|
|
|
|
int score = 0;
|
|
|
|
|
int num_average = 0;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 0 of the candidate
|
2022-05-11 12:33:26 +00:00
|
|
|
|
const uint8_t* mag_cand = get_cand_mag(wf, candidate);
|
2021-12-08 21:33:52 +00:00
|
|
|
|
|
|
|
|
|
// Compute average score over sync symbols (m+k = 0-7, 36-43, 72-79)
|
|
|
|
|
for (int m = 0; m < FT8_NUM_SYNC; ++m)
|
|
|
|
|
{
|
|
|
|
|
for (int k = 0; k < FT8_LENGTH_SYNC; ++k)
|
|
|
|
|
{
|
|
|
|
|
int block = (FT8_SYNC_OFFSET * m) + k; // relative to the message
|
|
|
|
|
int block_abs = candidate->time_offset + block; // relative to the captured signal
|
|
|
|
|
// Check for time boundaries
|
|
|
|
|
if (block_abs < 0)
|
|
|
|
|
continue;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if (block_abs >= wf->num_blocks)
|
2021-12-08 21:33:52 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 'block' of the candidate
|
2021-12-14 08:38:07 +00:00
|
|
|
|
const uint8_t* p8 = mag_cand + (block * wf->block_stride);
|
2021-12-08 21:33:52 +00:00
|
|
|
|
|
|
|
|
|
// Weighted difference between the expected and all other symbols
|
|
|
|
|
// Does not work as well as the alternative score below
|
|
|
|
|
// score += 8 * p8[kFT8_Costas_pattern[k]] -
|
|
|
|
|
// p8[0] - p8[1] - p8[2] - p8[3] -
|
|
|
|
|
// p8[4] - p8[5] - p8[6] - p8[7];
|
|
|
|
|
// ++num_average;
|
|
|
|
|
|
|
|
|
|
// Check only the neighbors of the expected symbol frequency- and time-wise
|
|
|
|
|
int sm = kFT8_Costas_pattern[k]; // Index of the expected bin
|
|
|
|
|
if (sm > 0)
|
|
|
|
|
{
|
|
|
|
|
// look at one frequency bin lower
|
|
|
|
|
score += p8[sm] - p8[sm - 1];
|
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
if (sm < 7)
|
|
|
|
|
{
|
|
|
|
|
// look at one frequency bin higher
|
|
|
|
|
score += p8[sm] - p8[sm + 1];
|
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
if ((k > 0) && (block_abs > 0))
|
|
|
|
|
{
|
|
|
|
|
// look one symbol back in time
|
2021-12-14 08:38:07 +00:00
|
|
|
|
score += p8[sm] - p8[sm - wf->block_stride];
|
2021-12-08 21:33:52 +00:00
|
|
|
|
++num_average;
|
|
|
|
|
}
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if (((k + 1) < FT8_LENGTH_SYNC) && ((block_abs + 1) < wf->num_blocks))
|
2021-12-08 21:33:52 +00:00
|
|
|
|
{
|
|
|
|
|
// look one symbol forward in time
|
2021-12-14 08:38:07 +00:00
|
|
|
|
score += p8[sm] - p8[sm + wf->block_stride];
|
2021-12-08 21:33:52 +00:00
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num_average > 0)
|
|
|
|
|
score /= num_average;
|
|
|
|
|
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static int ft4_sync_score(const waterfall_t* wf, const candidate_t* candidate)
|
2021-12-08 21:33:52 +00:00
|
|
|
|
{
|
|
|
|
|
int score = 0;
|
|
|
|
|
int num_average = 0;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 0 of the candidate
|
2022-05-11 12:33:26 +00:00
|
|
|
|
const uint8_t* mag_cand = get_cand_mag(wf, candidate);
|
2021-12-08 21:33:52 +00:00
|
|
|
|
|
|
|
|
|
// Compute average score over sync symbols (block = 1-4, 34-37, 67-70, 100-103)
|
|
|
|
|
for (int m = 0; m < FT4_NUM_SYNC; ++m)
|
|
|
|
|
{
|
|
|
|
|
for (int k = 0; k < FT4_LENGTH_SYNC; ++k)
|
|
|
|
|
{
|
|
|
|
|
int block = 1 + (FT4_SYNC_OFFSET * m) + k;
|
|
|
|
|
int block_abs = candidate->time_offset + block;
|
|
|
|
|
// Check for time boundaries
|
|
|
|
|
if (block_abs < 0)
|
|
|
|
|
continue;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if (block_abs >= wf->num_blocks)
|
2021-12-08 21:33:52 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Get the pointer to symbol 'block' of the candidate
|
2021-12-14 08:38:07 +00:00
|
|
|
|
const uint8_t* p4 = mag_cand + (block * wf->block_stride);
|
2021-12-08 21:33:52 +00:00
|
|
|
|
|
|
|
|
|
int sm = kFT4_Costas_pattern[m][k]; // Index of the expected bin
|
2021-12-14 08:38:07 +00:00
|
|
|
|
|
|
|
|
|
// score += (4 * p4[sm]) - p4[0] - p4[1] - p4[2] - p4[3];
|
|
|
|
|
// num_average += 4;
|
|
|
|
|
|
|
|
|
|
// Check only the neighbors of the expected symbol frequency- and time-wise
|
2021-12-08 21:33:52 +00:00
|
|
|
|
if (sm > 0)
|
|
|
|
|
{
|
|
|
|
|
// look at one frequency bin lower
|
|
|
|
|
score += p4[sm] - p4[sm - 1];
|
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
if (sm < 3)
|
|
|
|
|
{
|
|
|
|
|
// look at one frequency bin higher
|
|
|
|
|
score += p4[sm] - p4[sm + 1];
|
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
if ((k > 0) && (block_abs > 0))
|
|
|
|
|
{
|
|
|
|
|
// look one symbol back in time
|
2021-12-14 08:38:07 +00:00
|
|
|
|
score += p4[sm] - p4[sm - wf->block_stride];
|
2021-12-08 21:33:52 +00:00
|
|
|
|
++num_average;
|
|
|
|
|
}
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if (((k + 1) < FT4_LENGTH_SYNC) && ((block_abs + 1) < wf->num_blocks))
|
2021-12-08 21:33:52 +00:00
|
|
|
|
{
|
|
|
|
|
// look one symbol forward in time
|
2021-12-14 08:38:07 +00:00
|
|
|
|
score += p4[sm] - p4[sm + wf->block_stride];
|
2021-12-08 21:33:52 +00:00
|
|
|
|
++num_average;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (num_average > 0)
|
|
|
|
|
score /= num_average;
|
|
|
|
|
|
|
|
|
|
return score;
|
2019-11-10 07:04:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
int ft8_find_sync(const waterfall_t* wf, int num_candidates, candidate_t heap[], int min_score)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
int heap_size = 0;
|
2021-12-08 21:33:52 +00:00
|
|
|
|
candidate_t candidate;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
|
|
|
|
// Here we allow time offsets that exceed signal boundaries, as long as we still have all data bits.
|
|
|
|
|
// I.e. we can afford to skip the first 7 or the last 7 Costas symbols, as long as we track how many
|
|
|
|
|
// sync symbols we included in the score, so the score is averaged.
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (candidate.time_sub = 0; candidate.time_sub < wf->time_osr; ++candidate.time_sub)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (candidate.freq_sub = 0; candidate.freq_sub < wf->freq_osr; ++candidate.freq_sub)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2021-12-08 21:33:52 +00:00
|
|
|
|
for (candidate.time_offset = -12; candidate.time_offset < 24; ++candidate.time_offset)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (candidate.freq_offset = 0; (candidate.freq_offset + 7) < wf->num_bins; ++candidate.freq_offset)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if (wf->protocol == FTX_PROTOCOL_FT4)
|
2021-12-14 08:38:07 +00:00
|
|
|
|
{
|
|
|
|
|
candidate.score = ft4_sync_score(wf, &candidate);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
candidate.score = ft8_sync_score(wf, &candidate);
|
2022-05-11 12:33:26 +00:00
|
|
|
|
// candidate.score = ft8_snr(wf, &candidate);
|
2021-12-14 08:38:07 +00:00
|
|
|
|
}
|
2019-11-10 07:04:00 +00:00
|
|
|
|
|
2021-12-08 21:33:52 +00:00
|
|
|
|
if (candidate.score < min_score)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
continue;
|
2019-11-12 13:36:32 +00:00
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
|
// If the heap is full AND the current candidate is better than
|
2019-11-10 07:04:00 +00:00
|
|
|
|
// the worst in the heap, we remove the worst and make space
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if ((heap_size == num_candidates) && (candidate.score > heap[0].score))
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2019-11-10 07:04:00 +00:00
|
|
|
|
--heap_size;
|
2022-06-19 17:01:21 +00:00
|
|
|
|
heap[0] = heap[heap_size];
|
2019-11-10 07:04:00 +00:00
|
|
|
|
heapify_down(heap, heap_size);
|
|
|
|
|
}
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2019-11-10 07:04:00 +00:00
|
|
|
|
// If there's free space in the heap, we add the current candidate
|
2021-08-05 10:56:35 +00:00
|
|
|
|
if (heap_size < num_candidates)
|
|
|
|
|
{
|
2021-12-08 21:33:52 +00:00
|
|
|
|
heap[heap_size] = candidate;
|
2019-11-10 07:04:00 +00:00
|
|
|
|
++heap_size;
|
|
|
|
|
heapify_up(heap, heap_size);
|
|
|
|
|
}
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 08:07:12 +00:00
|
|
|
|
// Sort the candidates by sync strength - here we benefit from the heap structure
|
|
|
|
|
int len_unsorted = heap_size;
|
|
|
|
|
while (len_unsorted > 1)
|
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// Take the top (index 0) element which is guaranteed to have the smallest score,
|
|
|
|
|
// exchange it with the last element in the heap, and decrease the heap size.
|
|
|
|
|
// Then restore the heap property in the new, smaller heap.
|
|
|
|
|
// At the end the elements will be sorted in descending order.
|
2021-08-18 08:07:12 +00:00
|
|
|
|
candidate_t tmp = heap[len_unsorted - 1];
|
|
|
|
|
heap[len_unsorted - 1] = heap[0];
|
|
|
|
|
heap[0] = tmp;
|
|
|
|
|
len_unsorted--;
|
|
|
|
|
heapify_down(heap, len_unsorted);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 20:33:07 +00:00
|
|
|
|
return heap_size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ft4_extract_likelihood(const waterfall_t* wf, const candidate_t* cand, float* log174)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2022-05-11 12:33:26 +00:00
|
|
|
|
const uint8_t* mag_cand = get_cand_mag(wf, cand);
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
|
|
|
|
// Go over FSK tones and skip Costas sync symbols
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (int k = 0; k < FT4_ND; ++k)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
// Skip either 5, 9 or 13 sync symbols
|
|
|
|
|
// TODO: replace magic numbers with constants
|
|
|
|
|
int sym_idx = k + ((k < 29) ? 5 : ((k < 58) ? 9 : 13));
|
|
|
|
|
int bit_idx = 2 * k;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
// Check for time boundaries
|
2021-08-16 17:02:52 +00:00
|
|
|
|
int block = cand->time_offset + sym_idx;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if ((block < 0) || (block >= wf->num_blocks))
|
|
|
|
|
{
|
|
|
|
|
log174[bit_idx + 0] = 0;
|
|
|
|
|
log174[bit_idx + 1] = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Pointer to 4 bins of the current symbol
|
|
|
|
|
const uint8_t* ps = mag_cand + (sym_idx * wf->block_stride);
|
|
|
|
|
|
|
|
|
|
ft4_extract_symbol(ps, log174 + bit_idx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ft8_extract_likelihood(const waterfall_t* wf, const candidate_t* cand, float* log174)
|
|
|
|
|
{
|
2022-05-11 12:33:26 +00:00
|
|
|
|
const uint8_t* mag_cand = get_cand_mag(wf, cand);
|
2021-12-14 08:38:07 +00:00
|
|
|
|
|
|
|
|
|
// Go over FSK tones and skip Costas sync symbols
|
|
|
|
|
for (int k = 0; k < FT8_ND; ++k)
|
|
|
|
|
{
|
|
|
|
|
// Skip either 7 or 14 sync symbols
|
|
|
|
|
// TODO: replace magic numbers with constants
|
|
|
|
|
int sym_idx = k + ((k < 29) ? 7 : 14);
|
|
|
|
|
int bit_idx = 3 * k;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-08-16 17:02:52 +00:00
|
|
|
|
// Check for time boundaries
|
2021-12-14 08:38:07 +00:00
|
|
|
|
int block = cand->time_offset + sym_idx;
|
|
|
|
|
if ((block < 0) || (block >= wf->num_blocks))
|
2021-08-16 17:02:52 +00:00
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
log174[bit_idx + 0] = 0;
|
|
|
|
|
log174[bit_idx + 1] = 0;
|
|
|
|
|
log174[bit_idx + 2] = 0;
|
2021-08-16 17:02:52 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Pointer to 8 bins of the current symbol
|
2021-12-14 08:38:07 +00:00
|
|
|
|
const uint8_t* ps = mag_cand + (sym_idx * wf->block_stride);
|
2021-08-16 17:02:52 +00:00
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
ft8_extract_symbol(ps, log174 + bit_idx);
|
2021-08-16 17:02:52 +00:00
|
|
|
|
}
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
2021-12-14 08:38:07 +00:00
|
|
|
|
}
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ftx_normalize_logl(float* log174)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// Compute the variance of log174
|
2021-08-05 10:56:35 +00:00
|
|
|
|
float sum = 0;
|
|
|
|
|
float sum2 = 0;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (int i = 0; i < FTX_LDPC_N; ++i)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
|
|
|
|
sum += log174[i];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
sum2 += log174[i] * log174[i];
|
|
|
|
|
}
|
2021-12-14 08:38:07 +00:00
|
|
|
|
float inv_n = 1.0f / FTX_LDPC_N;
|
2021-08-12 10:43:25 +00:00
|
|
|
|
float variance = (sum2 - (sum * sum * inv_n)) * inv_n;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
|
2021-08-12 11:32:49 +00:00
|
|
|
|
// Normalize log174 distribution and scale it with experimentally found coefficient
|
2021-08-18 08:07:12 +00:00
|
|
|
|
float norm_factor = sqrtf(24.0f / variance);
|
2021-12-14 08:38:07 +00:00
|
|
|
|
for (int i = 0; i < FTX_LDPC_N; ++i)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
log174[i] *= norm_factor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
bool ft8_decode(const waterfall_t* wf, const candidate_t* cand, int max_iterations, ftx_message_t* message, decode_status_t* status)
|
2021-08-12 10:43:25 +00:00
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
float log174[FTX_LDPC_N]; // message bits encoded as likelihood
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if (wf->protocol == FTX_PROTOCOL_FT4)
|
2021-12-14 08:38:07 +00:00
|
|
|
|
{
|
|
|
|
|
ft4_extract_likelihood(wf, cand, log174);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ft8_extract_likelihood(wf, cand, log174);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ftx_normalize_logl(log174);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
uint8_t plain174[FTX_LDPC_N]; // message bits (0/1)
|
2021-08-12 10:43:25 +00:00
|
|
|
|
bp_decode(log174, max_iterations, plain174, &status->ldpc_errors);
|
2021-08-13 09:39:37 +00:00
|
|
|
|
// ldpc_decode(log174, max_iterations, plain174, &status->ldpc_errors);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
|
|
|
|
|
if (status->ldpc_errors > 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
// Extract payload + CRC (first FTX_LDPC_K bits) packed into a byte array
|
|
|
|
|
uint8_t a91[FTX_LDPC_K_BYTES];
|
|
|
|
|
pack_bits(plain174, FTX_LDPC_K, a91);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
|
|
|
|
|
// Extract CRC and check it
|
2021-11-17 08:31:01 +00:00
|
|
|
|
status->crc_extracted = ftx_extract_crc(a91);
|
2021-08-12 11:32:49 +00:00
|
|
|
|
// [1]: 'The CRC is calculated on the source-encoded message, zero-extended from 77 to 82 bits.'
|
2021-08-12 10:43:25 +00:00
|
|
|
|
a91[9] &= 0xF8;
|
2021-08-12 11:32:49 +00:00
|
|
|
|
a91[10] &= 0x00;
|
2021-11-17 08:31:01 +00:00
|
|
|
|
status->crc_calculated = ftx_compute_crc(a91, 96 - 14);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
|
|
|
|
|
if (status->crc_extracted != status->crc_calculated)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// Reuse CRC value as a hash for the message (TODO: 14 bits only, should perhaps use full 16 or 32 bits?)
|
|
|
|
|
message->hash = status->crc_calculated;
|
|
|
|
|
|
|
|
|
|
if (wf->protocol == FTX_PROTOCOL_FT4)
|
2021-12-14 08:38:07 +00:00
|
|
|
|
{
|
|
|
|
|
// '[..] for FT4 only, in order to avoid transmitting a long string of zeros when sending CQ messages,
|
|
|
|
|
// the assembled 77-bit message is bitwise exclusive-OR’ed with [a] pseudorandom sequence before computing the CRC and FEC parity bits'
|
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
message->payload[i] = a91[i] ^ kFT4_XOR_sequence[i];
|
2021-12-14 08:38:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-19 17:01:21 +00:00
|
|
|
|
else
|
2021-08-12 10:43:25 +00:00
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
|
|
|
{
|
|
|
|
|
message->payload[i] = a91[i];
|
|
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// LOG(LOG_DEBUG, "Decoded message (CRC %04x), trying to unpack...\n", status->crc_extracted);
|
2021-08-12 10:43:25 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
|
static float max2(float a, float b)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
return (a >= b) ? a : b;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
|
static float max4(float a, float b, float c, float d)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
return max2(max2(a, b), max2(c, d));
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
|
static void heapify_down(candidate_t heap[], int heap_size)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// heapify from the root down
|
2022-06-19 17:01:21 +00:00
|
|
|
|
int current = 0; // root node
|
2021-08-05 10:56:35 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
int left = 2 * current + 1;
|
|
|
|
|
int right = left + 1;
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// Find the smallest value of (parent, left child, right child)
|
|
|
|
|
int smallest = current;
|
|
|
|
|
if ((left < heap_size) && (heap[left].score < heap[smallest].score))
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
smallest = left;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if ((right < heap_size) && (heap[right].score < heap[smallest].score))
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
|
smallest = right;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
2022-06-19 17:01:21 +00:00
|
|
|
|
|
|
|
|
|
if (smallest == current)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// Exchange the current node with the smallest child and move down to it
|
|
|
|
|
candidate_t tmp = heap[smallest];
|
|
|
|
|
heap[smallest] = heap[current];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
heap[current] = tmp;
|
2022-06-19 17:01:21 +00:00
|
|
|
|
current = smallest;
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
|
static void heapify_up(candidate_t heap[], int heap_size)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// heapify from the last node up
|
|
|
|
|
int current = heap_size - 1;
|
2021-08-05 10:56:35 +00:00
|
|
|
|
while (current > 0)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
int parent = (current - 1) / 2;
|
2022-06-19 17:01:21 +00:00
|
|
|
|
if (!(heap[current].score < heap[parent].score))
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-19 17:01:21 +00:00
|
|
|
|
// Exchange the current node with its parent and move up
|
2021-08-12 10:43:25 +00:00
|
|
|
|
candidate_t tmp = heap[parent];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
heap[parent] = heap[current];
|
|
|
|
|
heap[current] = tmp;
|
|
|
|
|
current = parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-11 05:48:15 +00:00
|
|
|
|
// Compute unnormalized log likelihood log(p(1) / p(0)) of 2 message bits (1 FSK symbol)
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ft4_extract_symbol(const uint8_t* wf, float* logl)
|
2021-12-11 05:48:15 +00:00
|
|
|
|
{
|
|
|
|
|
// Cleaned up code for the simple case of n_syms==1
|
|
|
|
|
float s2[4];
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < 4; ++j)
|
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
s2[j] = (float)wf[kFT4_Gray_map[j]];
|
2021-12-11 05:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
logl[0] = max2(s2[2], s2[3]) - max2(s2[0], s2[1]);
|
|
|
|
|
logl[1] = max2(s2[1], s2[3]) - max2(s2[0], s2[2]);
|
2021-12-11 05:48:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// Compute unnormalized log likelihood log(p(1) / p(0)) of 3 message bits (1 FSK symbol)
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ft8_extract_symbol(const uint8_t* wf, float* logl)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// Cleaned up code for the simple case of n_syms==1
|
|
|
|
|
float s2[8];
|
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
|
for (int j = 0; j < 8; ++j)
|
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
s2[j] = (float)wf[kFT8_Gray_map[j]];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:38:07 +00:00
|
|
|
|
logl[0] = max4(s2[4], s2[5], s2[6], s2[7]) - max4(s2[0], s2[1], s2[2], s2[3]);
|
|
|
|
|
logl[1] = max4(s2[2], s2[3], s2[6], s2[7]) - max4(s2[0], s2[1], s2[4], s2[5]);
|
|
|
|
|
logl[2] = max4(s2[1], s2[3], s2[5], s2[7]) - max4(s2[0], s2[2], s2[4], s2[6]);
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute unnormalized log likelihood log(p(1) / p(0)) of bits corresponding to several FSK symbols at once
|
2021-12-14 08:38:07 +00:00
|
|
|
|
static void ft8_decode_multi_symbols(const uint8_t* wf, int num_bins, int n_syms, int bit_idx, float* log174)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
const int n_bits = 3 * n_syms;
|
|
|
|
|
const int n_tones = (1 << n_bits);
|
2021-08-05 10:56:35 +00:00
|
|
|
|
|
2018-12-27 20:33:07 +00:00
|
|
|
|
float s2[n_tones];
|
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
|
for (int j = 0; j < n_tones; ++j)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
int j1 = j & 0x07;
|
2021-08-05 10:56:35 +00:00
|
|
|
|
if (n_syms == 1)
|
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
s2[j] = (float)wf[kFT8_Gray_map[j1]];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int j2 = (j >> 3) & 0x07;
|
2021-08-05 10:56:35 +00:00
|
|
|
|
if (n_syms == 2)
|
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
s2[j] = (float)wf[kFT8_Gray_map[j2]];
|
|
|
|
|
s2[j] += (float)wf[kFT8_Gray_map[j1] + 4 * num_bins];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
int j3 = (j >> 6) & 0x07;
|
2021-12-14 08:38:07 +00:00
|
|
|
|
s2[j] = (float)wf[kFT8_Gray_map[j3]];
|
|
|
|
|
s2[j] += (float)wf[kFT8_Gray_map[j2] + 4 * num_bins];
|
|
|
|
|
s2[j] += (float)wf[kFT8_Gray_map[j1] + 8 * num_bins];
|
2018-12-27 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract bit significance (and convert them to float)
|
|
|
|
|
// 8 FSK tones = 3 bits
|
2021-08-05 10:56:35 +00:00
|
|
|
|
for (int i = 0; i < n_bits; ++i)
|
|
|
|
|
{
|
2021-12-14 08:38:07 +00:00
|
|
|
|
if (bit_idx + i >= FTX_LDPC_N)
|
2021-08-05 10:56:35 +00:00
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
// Respect array size
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t mask = (n_tones >> (i + 1));
|
|
|
|
|
float max_zero = -1000, max_one = -1000;
|
2021-08-05 10:56:35 +00:00
|
|
|
|
for (int n = 0; n < n_tones; ++n)
|
|
|
|
|
{
|
|
|
|
|
if (n & mask)
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
max_one = max2(max_one, s2[n]);
|
|
|
|
|
}
|
2021-08-05 10:56:35 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-27 20:33:07 +00:00
|
|
|
|
max_zero = max2(max_zero, s2[n]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log174[bit_idx + i] = max_one - max_zero;
|
2021-08-05 10:56:35 +00:00
|
|
|
|
}
|
2019-01-02 18:54:18 +00:00
|
|
|
|
}
|
2021-11-17 08:31:01 +00:00
|
|
|
|
|
|
|
|
|
// Packs a string of bits each represented as a zero/non-zero byte in plain[],
|
|
|
|
|
// as a string of packed bits starting from the MSB of the first byte of packed[]
|
|
|
|
|
static void pack_bits(const uint8_t bit_array[], int num_bits, uint8_t packed[])
|
|
|
|
|
{
|
|
|
|
|
int num_bytes = (num_bits + 7) / 8;
|
|
|
|
|
for (int i = 0; i < num_bytes; ++i)
|
|
|
|
|
{
|
|
|
|
|
packed[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t mask = 0x80;
|
|
|
|
|
int byte_idx = 0;
|
|
|
|
|
for (int i = 0; i < num_bits; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (bit_array[i])
|
|
|
|
|
{
|
|
|
|
|
packed[byte_idx] |= mask;
|
|
|
|
|
}
|
|
|
|
|
mask >>= 1;
|
|
|
|
|
if (!mask)
|
|
|
|
|
{
|
|
|
|
|
mask = 0x80;
|
|
|
|
|
++byte_idx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|