2021-08-05 11:07:31 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdbool.h>
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2019-01-02 18:54:18 +00:00
|
|
|
#include "ft8/unpack.h"
|
2018-11-07 13:50:55 +00:00
|
|
|
#include "ft8/ldpc.h"
|
2018-12-27 20:33:07 +00:00
|
|
|
#include "ft8/decode.h"
|
2018-12-24 12:22:26 +00:00
|
|
|
#include "ft8/constants.h"
|
2019-01-02 18:54:18 +00:00
|
|
|
#include "ft8/encode.h"
|
2021-08-12 10:43:25 +00:00
|
|
|
#include "ft8/crc.h"
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2021-11-01 11:08:26 +00:00
|
|
|
#include "common/common.h"
|
2018-12-24 12:22:26 +00:00
|
|
|
#include "common/wave.h"
|
2018-12-28 18:42:22 +00:00
|
|
|
#include "common/debug.h"
|
2018-12-24 12:22:26 +00:00
|
|
|
#include "fft/kiss_fftr.h"
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
#define LOG_LEVEL LOG_INFO
|
2018-12-28 18:42:22 +00:00
|
|
|
|
2021-08-18 08:07:12 +00:00
|
|
|
const int kMin_score = 10; // Minimum sync score threshold for candidates
|
2021-08-16 17:02:52 +00:00
|
|
|
const int kMax_candidates = 120;
|
2021-08-18 08:07:12 +00:00
|
|
|
const int kLDPC_iterations = 20;
|
2018-12-25 12:01:51 +00:00
|
|
|
|
|
|
|
const int kMax_decoded_messages = 50;
|
2019-11-10 07:04:00 +00:00
|
|
|
|
|
|
|
const int kFreq_osr = 2;
|
|
|
|
const int kTime_osr = 2;
|
2018-12-25 12:01:51 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
const float kFSK_dev = 6.25f; // tone deviation in Hz and symbol rate
|
2018-12-28 18:42:22 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
void usage()
|
|
|
|
{
|
2021-08-13 09:39:37 +00:00
|
|
|
fprintf(stderr, "Decode a 15-second (or slighly shorter) WAV file.\n");
|
2018-11-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
float hann_i(int i, int N)
|
|
|
|
{
|
2021-08-18 08:07:12 +00:00
|
|
|
float x = sinf((float)M_PI * i / N);
|
2021-08-05 10:56:35 +00:00
|
|
|
return x * x;
|
2018-11-07 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
float hamming_i(int i, int N)
|
|
|
|
{
|
2018-12-25 12:01:51 +00:00
|
|
|
const float a0 = (float)25 / 46;
|
|
|
|
const float a1 = 1 - a0;
|
|
|
|
|
2021-08-18 08:07:12 +00:00
|
|
|
float x1 = cosf(2 * (float)M_PI * i / N);
|
2021-08-05 10:56:35 +00:00
|
|
|
return a0 - a1 * x1;
|
2018-12-25 12:01:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
float blackman_i(int i, int N)
|
|
|
|
{
|
2018-12-25 12:01:51 +00:00
|
|
|
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;
|
|
|
|
|
2021-08-18 08:07:12 +00:00
|
|
|
float x1 = cosf(2 * (float)M_PI * i / N);
|
2021-08-05 10:56:35 +00:00
|
|
|
float x2 = 2 * x1 * x1 - 1; // Use double angle formula
|
2018-12-25 12:01:51 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
return a0 - a1 * x1 + a2 * x2;
|
2018-12-25 12:01:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
static float max2(float a, float b)
|
|
|
|
{
|
2019-11-10 07:04:00 +00:00
|
|
|
return (a >= b) ? a : b;
|
|
|
|
}
|
2018-12-25 12:01:51 +00:00
|
|
|
|
2018-11-13 11:04:36 +00:00
|
|
|
// Compute FFT magnitudes (log power) for each timeslot in the signal
|
2021-08-13 09:39:37 +00:00
|
|
|
void extract_power(const float signal[], waterfall_t *power, int block_size)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2019-11-10 07:04:00 +00:00
|
|
|
const int subblock_size = block_size / power->time_osr;
|
2021-08-13 09:39:37 +00:00
|
|
|
const int nfft = block_size * power->freq_osr;
|
2018-12-28 18:42:22 +00:00
|
|
|
const float fft_norm = 2.0f / nfft;
|
2021-08-18 08:07:12 +00:00
|
|
|
const int len_window = 1.8f * block_size; // hand-picked and optimized
|
2018-11-12 10:49:35 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
float window[nfft];
|
|
|
|
for (int i = 0; i < nfft; ++i)
|
|
|
|
{
|
2021-08-13 09:39:37 +00:00
|
|
|
// window[i] = 1;
|
2021-08-18 08:07:12 +00:00
|
|
|
// window[i] = hann_i(i, nfft);
|
2021-08-12 10:43:25 +00:00
|
|
|
// window[i] = blackman_i(i, nfft);
|
|
|
|
// window[i] = hamming_i(i, nfft);
|
2021-08-18 08:07:12 +00:00
|
|
|
window[i] = (i < len_window) ? hann_i(i, len_window) : 0;
|
2018-11-12 10:49:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
size_t fft_work_size;
|
2018-11-12 10:49:35 +00:00
|
|
|
kiss_fftr_alloc(nfft, 0, 0, &fft_work_size);
|
|
|
|
|
2019-11-10 07:04:00 +00:00
|
|
|
LOG(LOG_INFO, "Block size = %d\n", block_size);
|
|
|
|
LOG(LOG_INFO, "Subblock size = %d\n", subblock_size);
|
2018-12-28 18:42:22 +00:00
|
|
|
LOG(LOG_INFO, "N_FFT = %d\n", nfft);
|
|
|
|
LOG(LOG_INFO, "FFT work area = %lu\n", fft_work_size);
|
2018-11-12 10:49:35 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
void *fft_work = malloc(fft_work_size);
|
2018-11-12 10:49:35 +00:00
|
|
|
kiss_fftr_cfg fft_cfg = kiss_fftr_alloc(nfft, 0, fft_work, &fft_work_size);
|
|
|
|
|
|
|
|
int offset = 0;
|
2021-08-18 08:07:12 +00:00
|
|
|
float max_mag = -120.0f;
|
2021-08-12 10:43:25 +00:00
|
|
|
for (int idx_block = 0; idx_block < power->num_blocks; ++idx_block)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2018-11-12 10:49:35 +00:00
|
|
|
// Loop over two possible time offsets (0 and block_size/2)
|
2021-08-05 10:56:35 +00:00
|
|
|
for (int time_sub = 0; time_sub < power->time_osr; ++time_sub)
|
|
|
|
{
|
2018-11-12 10:49:35 +00:00
|
|
|
kiss_fft_scalar timedata[nfft];
|
2021-08-05 10:56:35 +00:00
|
|
|
kiss_fft_cpx freqdata[nfft / 2 + 1];
|
|
|
|
float mag_db[nfft / 2 + 1];
|
2018-11-12 10:49:35 +00:00
|
|
|
|
|
|
|
// Extract windowed signal block
|
2021-08-12 10:43:25 +00:00
|
|
|
for (int pos = 0; pos < nfft; ++pos)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 12:04:15 +00:00
|
|
|
timedata[pos] = window[pos] * signal[(idx_block * block_size) + (time_sub * subblock_size) + pos];
|
2018-11-12 10:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kiss_fftr(fft_cfg, timedata, freqdata);
|
|
|
|
|
|
|
|
// Compute log magnitude in decibels
|
2021-08-12 10:43:25 +00:00
|
|
|
for (int idx_bin = 0; idx_bin < nfft / 2 + 1; ++idx_bin)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
float mag2 = (freqdata[idx_bin].i * freqdata[idx_bin].i) + (freqdata[idx_bin].r * freqdata[idx_bin].r);
|
2021-08-18 08:07:12 +00:00
|
|
|
mag_db[idx_bin] = 10.0f * log10f(1E-12f + mag2 * fft_norm * fft_norm);
|
2018-11-12 10:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over two possible frequency bin offsets (for averaging)
|
2021-08-05 10:56:35 +00:00
|
|
|
for (int freq_sub = 0; freq_sub < power->freq_osr; ++freq_sub)
|
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
for (int pos = 0; pos < power->num_bins; ++pos)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
float db = mag_db[pos * power->freq_osr + freq_sub];
|
2018-12-24 12:22:26 +00:00
|
|
|
// Scale decibels to unsigned 8-bit range and clamp the value
|
2021-08-18 08:07:12 +00:00
|
|
|
// Range 0-240 covers -120..0 dB in 0.5 dB steps
|
|
|
|
int scaled = (int)(2 * db + 240);
|
2021-08-10 10:37:54 +00:00
|
|
|
|
2019-11-10 07:04:00 +00:00
|
|
|
power->mag[offset] = (scaled < 0) ? 0 : ((scaled > 255) ? 255 : scaled);
|
2018-11-12 10:49:35 +00:00
|
|
|
++offset;
|
2018-12-24 12:22:26 +00:00
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
if (db > max_mag)
|
|
|
|
max_mag = db;
|
2018-11-12 10:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:42:22 +00:00
|
|
|
LOG(LOG_INFO, "Max magnitude: %.1f dB\n", max_mag);
|
2018-11-12 10:49:35 +00:00
|
|
|
free(fft_work);
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:56:35 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-11-07 13:50:55 +00:00
|
|
|
// Expect one command-line argument
|
2021-08-05 10:56:35 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
2018-11-07 13:50:55 +00:00
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-25 12:01:51 +00:00
|
|
|
const char *wav_path = argv[1];
|
2018-11-07 13:50:55 +00:00
|
|
|
|
|
|
|
int sample_rate = 12000;
|
|
|
|
int num_samples = 15 * sample_rate;
|
|
|
|
float signal[num_samples];
|
|
|
|
|
2021-08-05 11:07:31 +00:00
|
|
|
int rc = load_wav(signal, &num_samples, &sample_rate, wav_path);
|
2021-08-05 10:56:35 +00:00
|
|
|
if (rc < 0)
|
|
|
|
{
|
2018-11-07 13:50:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-25 12:01:51 +00:00
|
|
|
// Compute DSP parameters that depend on the sample rate
|
2021-08-13 09:39:37 +00:00
|
|
|
const int num_bins = (int)(sample_rate / (2 * kFSK_dev)); // number bins of FSK tone width that the spectrum can be divided into
|
|
|
|
const int block_size = (int)(sample_rate / kFSK_dev); // samples corresponding to one FSK symbol
|
2019-11-10 07:04:00 +00:00
|
|
|
const int subblock_size = block_size / kTime_osr;
|
|
|
|
const int nfft = block_size * kFreq_osr;
|
|
|
|
const int num_blocks = (num_samples - nfft + subblock_size) / block_size;
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2019-11-10 07:04:00 +00:00
|
|
|
LOG(LOG_INFO, "Sample rate %d Hz, %d blocks, %d bins\n", sample_rate, num_blocks, num_bins);
|
2018-11-13 11:04:36 +00:00
|
|
|
|
2018-12-25 12:01:51 +00:00
|
|
|
// Compute FFT over the whole signal and store it
|
2019-11-10 07:04:00 +00:00
|
|
|
uint8_t mag_power[num_blocks * kFreq_osr * kTime_osr * num_bins];
|
2021-08-12 10:43:25 +00:00
|
|
|
waterfall_t power = {
|
2021-08-05 10:56:35 +00:00
|
|
|
.num_blocks = num_blocks,
|
2019-11-10 07:04:00 +00:00
|
|
|
.num_bins = num_bins,
|
|
|
|
.time_osr = kTime_osr,
|
|
|
|
.freq_osr = kFreq_osr,
|
2021-08-05 10:56:35 +00:00
|
|
|
.mag = mag_power};
|
2021-08-13 09:39:37 +00:00
|
|
|
extract_power(signal, &power, block_size);
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2018-12-27 20:33:07 +00:00
|
|
|
// Find top candidates by Costas sync score and localize them in time and frequency
|
2021-08-12 10:43:25 +00:00
|
|
|
candidate_t candidate_list[kMax_candidates];
|
2021-08-06 06:57:08 +00:00
|
|
|
int num_candidates = find_sync(&power, kMax_candidates, candidate_list, kMin_score);
|
2018-11-07 13:50:55 +00:00
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
// Hash table for decoded messages (to check for duplicates)
|
2021-08-05 10:56:35 +00:00
|
|
|
int num_decoded = 0;
|
2021-08-12 10:43:25 +00:00
|
|
|
message_t decoded[kMax_decoded_messages];
|
|
|
|
message_t *decoded_hashtable[kMax_decoded_messages];
|
|
|
|
|
|
|
|
// Initialize hash table pointers
|
|
|
|
for (int i = 0; i < kMax_decoded_messages; ++i)
|
|
|
|
{
|
|
|
|
decoded_hashtable[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go over candidates and attempt to decode messages
|
2021-08-05 10:56:35 +00:00
|
|
|
for (int idx = 0; idx < num_candidates; ++idx)
|
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
const candidate_t *cand = &candidate_list[idx];
|
2021-08-05 11:07:31 +00:00
|
|
|
if (cand->score < kMin_score)
|
2021-08-05 10:56:35 +00:00
|
|
|
continue;
|
2019-11-14 11:44:19 +00:00
|
|
|
|
2021-08-05 11:07:31 +00:00
|
|
|
float freq_hz = (cand->freq_offset + (float)cand->freq_sub / kFreq_osr) * kFSK_dev;
|
|
|
|
float time_sec = (cand->time_offset + (float)cand->time_sub / kTime_osr) / kFSK_dev;
|
2018-11-13 15:16:24 +00:00
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
message_t message;
|
|
|
|
decode_status_t status;
|
|
|
|
if (!decode(&power, cand, &message, kLDPC_iterations, &status))
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
if (status.ldpc_errors > 0)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "LDPC decode: %d errors\n", status.ldpc_errors);
|
2021-08-05 10:56:35 +00:00
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
else if (status.crc_calculated != status.crc_extracted)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "CRC mismatch!\n");
|
2021-08-05 10:56:35 +00:00
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
else if (status.unpack_status != 0)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "Error while unpacking!\n");
|
2021-08-05 10:56:35 +00:00
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-24 12:22:26 +00:00
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "Checking hash table for %4.1fs / %4.1fHz [%d]...\n", time_sec, freq_hz, cand->score);
|
2021-08-18 08:07:12 +00:00
|
|
|
int idx_hash = message.hash % kMax_decoded_messages;
|
2021-08-12 10:43:25 +00:00
|
|
|
bool found_empty_slot = false;
|
|
|
|
bool found_duplicate = false;
|
|
|
|
do
|
|
|
|
{
|
2021-08-18 08:07:12 +00:00
|
|
|
if (decoded_hashtable[idx_hash] == NULL)
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "Found an empty slot\n");
|
|
|
|
found_empty_slot = true;
|
2021-08-05 10:56:35 +00:00
|
|
|
}
|
2021-08-18 08:07:12 +00:00
|
|
|
else if ((decoded_hashtable[idx_hash]->hash == message.hash) && (0 == strcmp(decoded_hashtable[idx_hash]->text, message.text)))
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "Found a duplicate [%s]\n", message.text);
|
|
|
|
found_duplicate = true;
|
2021-08-05 10:56:35 +00:00
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
else
|
2021-08-05 10:56:35 +00:00
|
|
|
{
|
2021-08-12 10:43:25 +00:00
|
|
|
LOG(LOG_DEBUG, "Hash table clash!\n");
|
|
|
|
// Move on to check the next entry in hash table
|
2021-08-18 08:07:12 +00:00
|
|
|
idx_hash = (idx_hash + 1) % kMax_decoded_messages;
|
2018-12-25 12:01:51 +00:00
|
|
|
}
|
2021-08-12 10:43:25 +00:00
|
|
|
} while (!found_empty_slot && !found_duplicate);
|
2018-11-20 15:04:05 +00:00
|
|
|
|
2021-08-12 10:43:25 +00:00
|
|
|
if (found_empty_slot)
|
|
|
|
{
|
|
|
|
// Fill the empty hashtable slot
|
2021-08-18 08:07:12 +00:00
|
|
|
memcpy(&decoded[idx_hash], &message, sizeof(message));
|
|
|
|
decoded_hashtable[idx_hash] = &decoded[idx_hash];
|
2021-08-12 10:43:25 +00:00
|
|
|
++num_decoded;
|
|
|
|
|
|
|
|
// Fake WSJT-X-like output for now
|
|
|
|
int snr = 0; // TODO: compute SNR
|
2021-08-18 08:07:12 +00:00
|
|
|
printf("000000 %3d %+4.2f %4.0f ~ %s\n", cand->score, time_sec, freq_hz, message.text);
|
2018-11-13 15:16:24 +00:00
|
|
|
}
|
2018-11-13 11:04:36 +00:00
|
|
|
}
|
2018-12-28 18:42:22 +00:00
|
|
|
LOG(LOG_INFO, "Decoded %d messages\n", num_decoded);
|
2018-11-13 11:04:36 +00:00
|
|
|
|
2018-11-07 13:50:55 +00:00
|
|
|
return 0;
|
2019-11-12 13:36:32 +00:00
|
|
|
}
|