2018-11-07 21:50:55 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cmath>
|
|
|
|
|
2018-12-24 20:22:26 +08:00
|
|
|
#include "ft8/unpack_v2.h"
|
2018-11-07 21:50:55 +08:00
|
|
|
#include "ft8/ldpc.h"
|
2018-12-28 04:33:07 +08:00
|
|
|
#include "ft8/decode.h"
|
2018-12-24 20:22:26 +08:00
|
|
|
#include "ft8/constants.h"
|
2019-01-03 02:24:44 +08:00
|
|
|
#include "ft8/encode_v2.h"
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-24 20:22:26 +08:00
|
|
|
#include "common/wave.h"
|
2018-12-29 02:42:22 +08:00
|
|
|
#include "common/debug.h"
|
2018-12-24 20:22:26 +08:00
|
|
|
#include "fft/kiss_fftr.h"
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
#define LOG_LEVEL LOG_INFO
|
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
const int kMax_candidates = 100;
|
|
|
|
const int kLDPC_iterations = 20;
|
|
|
|
|
|
|
|
const int kMax_decoded_messages = 50;
|
|
|
|
const int kMax_message_length = 20;
|
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
|
2018-11-07 21:50:55 +08:00
|
|
|
void usage() {
|
2018-12-29 02:42:22 +08:00
|
|
|
fprintf(stderr, "Decode a 15-second WAV file.\n");
|
2018-11-07 21:50:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float hann_i(int i, int N) {
|
|
|
|
float x = sinf((float)M_PI * i / (N - 1));
|
|
|
|
return x*x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
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));
|
2018-12-28 04:33:07 +08:00
|
|
|
//float x2 = cosf(4 * (float)M_PI * i / (N - 1));
|
|
|
|
float x2 = 2*x1*x1 - 1; // Use double angle formula
|
2018-12-25 20:01:51 +08:00
|
|
|
|
|
|
|
return a0 - a1*x1 + a2*x2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-13 19:04:36 +08:00
|
|
|
// Compute FFT magnitudes (log power) for each timeslot in the signal
|
2018-12-28 04:33:07 +08:00
|
|
|
void extract_power(const float signal[], int num_blocks, int num_bins, uint8_t power[]) {
|
2018-11-12 18:49:35 +08:00
|
|
|
const int block_size = 2 * num_bins; // Average over 2 bins per FSK tone
|
|
|
|
const int nfft = 2 * block_size; // We take FFT of two blocks, advancing by one
|
2018-12-29 02:42:22 +08:00
|
|
|
const float fft_norm = 2.0f / nfft;
|
2018-11-12 18:49:35 +08:00
|
|
|
|
|
|
|
float window[nfft];
|
|
|
|
for (int i = 0; i < nfft; ++i) {
|
2018-12-25 20:01:51 +08:00
|
|
|
window[i] = blackman_i(i, nfft);
|
2018-11-12 18:49:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fft_work_size;
|
|
|
|
kiss_fftr_alloc(nfft, 0, 0, &fft_work_size);
|
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_INFO, "N_FFT = %d\n", nfft);
|
|
|
|
LOG(LOG_INFO, "FFT work area = %lu\n", fft_work_size);
|
2018-11-12 18:49:35 +08:00
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
void *fft_work = malloc(fft_work_size);
|
2018-11-12 18:49:35 +08:00
|
|
|
kiss_fftr_cfg fft_cfg = kiss_fftr_alloc(nfft, 0, fft_work, &fft_work_size);
|
|
|
|
|
|
|
|
int offset = 0;
|
2018-12-24 20:22:26 +08:00
|
|
|
float max_mag = -100.0f;
|
2018-11-12 18:49:35 +08:00
|
|
|
for (int i = 0; i < num_blocks; ++i) {
|
|
|
|
// Loop over two possible time offsets (0 and block_size/2)
|
2018-11-13 19:04:36 +08:00
|
|
|
for (int time_sub = 0; time_sub <= block_size/2; time_sub += block_size/2) {
|
2018-11-12 18:49:35 +08:00
|
|
|
kiss_fft_scalar timedata[nfft];
|
|
|
|
kiss_fft_cpx freqdata[nfft/2 + 1];
|
|
|
|
float mag_db[nfft/2 + 1];
|
|
|
|
|
|
|
|
// Extract windowed signal block
|
|
|
|
for (int j = 0; j < nfft; ++j) {
|
2018-11-13 19:04:36 +08:00
|
|
|
timedata[j] = window[j] * signal[(i * block_size) + (j + time_sub)];
|
2018-11-12 18:49:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kiss_fftr(fft_cfg, timedata, freqdata);
|
|
|
|
|
|
|
|
// Compute log magnitude in decibels
|
|
|
|
for (int j = 0; j < nfft/2 + 1; ++j) {
|
2018-12-29 02:42:22 +08:00
|
|
|
float mag2 = (freqdata[j].i * freqdata[j].i + freqdata[j].r * freqdata[j].r);
|
|
|
|
mag_db[j] = 10.0f * log10f(1E-10f + mag2 * fft_norm * fft_norm);
|
2018-11-12 18:49:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over two possible frequency bin offsets (for averaging)
|
2018-11-13 19:04:36 +08:00
|
|
|
for (int freq_sub = 0; freq_sub < 2; ++freq_sub) {
|
2018-11-12 18:49:35 +08:00
|
|
|
for (int j = 0; j < num_bins; ++j) {
|
2018-11-13 19:04:36 +08:00
|
|
|
float db1 = mag_db[j * 2 + freq_sub];
|
|
|
|
float db2 = mag_db[j * 2 + freq_sub + 1];
|
2018-11-12 18:49:35 +08:00
|
|
|
float db = (db1 + db2) / 2;
|
|
|
|
|
2018-12-24 20:22:26 +08:00
|
|
|
// Scale decibels to unsigned 8-bit range and clamp the value
|
2018-12-29 02:42:22 +08:00
|
|
|
int scaled = (int)(2 * (db + 120));
|
2018-11-12 18:49:35 +08:00
|
|
|
power[offset] = (scaled < 0) ? 0 : ((scaled > 255) ? 255 : scaled);
|
|
|
|
++offset;
|
2018-12-24 20:22:26 +08:00
|
|
|
|
|
|
|
if (db > max_mag) max_mag = db;
|
2018-11-12 18:49:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_INFO, "Max magnitude: %.1f dB\n", max_mag);
|
2018-11-12 18:49:35 +08:00
|
|
|
free(fft_work);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
void normalize_signal(float *signal, int num_samples) {
|
|
|
|
float max_amp = 1E-5f;
|
|
|
|
for (int i = 0; i < num_samples; ++i) {
|
|
|
|
float amp = fabsf(signal[i]);
|
|
|
|
if (amp > max_amp) {
|
|
|
|
max_amp = amp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < num_samples; ++i) {
|
|
|
|
signal[i] /= max_amp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-24 20:22:26 +08:00
|
|
|
void print_tones(const uint8_t *code_map, const float *log174) {
|
|
|
|
for (int k = 0; k < 3 * FT8_ND; k += 3) {
|
|
|
|
uint8_t max = 0;
|
|
|
|
if (log174[k + 0] > 0) max |= 4;
|
|
|
|
if (log174[k + 1] > 0) max |= 2;
|
|
|
|
if (log174[k + 2] > 0) max |= 1;
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_DEBUG, "%d", code_map[max]);
|
2018-12-24 20:22:26 +08:00
|
|
|
}
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_DEBUG, "\n");
|
2018-12-24 20:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
int main(int argc, char **argv) {
|
2018-11-07 21:50:55 +08:00
|
|
|
// Expect one command-line argument
|
|
|
|
if (argc < 2) {
|
|
|
|
usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
const char *wav_path = argv[1];
|
2018-11-07 21:50:55 +08:00
|
|
|
|
|
|
|
int sample_rate = 12000;
|
|
|
|
int num_samples = 15 * sample_rate;
|
|
|
|
float signal[num_samples];
|
|
|
|
|
|
|
|
int rc = load_wav(signal, num_samples, sample_rate, wav_path);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-29 02:42:22 +08:00
|
|
|
normalize_signal(signal, num_samples);
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
const float fsk_dev = 6.25f; // tone deviation in Hz and symbol rate
|
2018-11-13 19:04:36 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
// Compute DSP parameters that depend on the sample rate
|
2018-11-13 19:04:36 +08:00
|
|
|
const int num_bins = (int)(sample_rate / (2 * fsk_dev));
|
2018-11-12 18:49:35 +08:00
|
|
|
const int block_size = 2 * num_bins;
|
|
|
|
const int num_blocks = (num_samples - (block_size/2) - block_size) / block_size;
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_INFO, "%d blocks, %d bins\n", num_blocks, num_bins);
|
2018-11-13 19:04:36 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
// Compute FFT over the whole signal and store it
|
|
|
|
uint8_t power[num_blocks * 4 * num_bins];
|
2018-11-13 19:04:36 +08:00
|
|
|
extract_power(signal, num_blocks, num_bins, power);
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
// Find top candidates by Costas sync score and localize them in time and frequency
|
|
|
|
Candidate candidate_list[kMax_candidates];
|
|
|
|
int num_candidates = find_sync(power, num_blocks, num_bins, kCostas_map, kMax_candidates, candidate_list);
|
2018-11-07 21:50:55 +08:00
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
// TODO: sort the candidates by strongest sync first?
|
2018-11-13 19:04:36 +08:00
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
// Go over candidates and attempt to decode messages
|
|
|
|
char decoded[kMax_decoded_messages][kMax_message_length];
|
|
|
|
int num_decoded = 0;
|
2018-11-14 23:06:32 +08:00
|
|
|
for (int idx = 0; idx < num_candidates; ++idx) {
|
2018-12-28 04:33:07 +08:00
|
|
|
Candidate &cand = candidate_list[idx];
|
|
|
|
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;
|
2018-11-13 23:16:24 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
float log174[FT8_N];
|
2018-12-24 20:22:26 +08:00
|
|
|
extract_likelihood(power, num_bins, cand, kGray_map, log174);
|
2018-11-13 23:16:24 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
// bp_decode() produces better decodes, uses way less memory
|
|
|
|
uint8_t plain[FT8_N];
|
|
|
|
int n_errors = 0;
|
|
|
|
bp_decode(log174, kLDPC_iterations, plain, &n_errors);
|
2018-12-28 04:33:07 +08:00
|
|
|
//ldpc_decode(log174, kLDPC_iterations, plain, &n_errors);
|
2018-12-25 20:01:51 +08:00
|
|
|
|
|
|
|
if (n_errors > 0) {
|
2019-01-03 02:24:44 +08:00
|
|
|
LOG(LOG_DEBUG, "ldpc_decode() = %d (%.0f Hz)\n", n_errors, freq_hz);
|
2018-12-25 20:01:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
// Extract payload + CRC (first FT8_K bits)
|
2019-01-03 02:24:44 +08:00
|
|
|
uint8_t a91[FT8_K_BYTES];
|
2018-12-28 04:33:07 +08:00
|
|
|
pack_bits(plain, FT8_K, a91);
|
2018-12-25 20:01:51 +08:00
|
|
|
|
2019-01-03 02:24:44 +08:00
|
|
|
// Extract CRC and check it
|
|
|
|
uint16_t chksum = ((a91[9] & 0x07) << 11) | (a91[10] << 3) | (a91[11] >> 5);
|
|
|
|
a91[9] &= 0xF8;
|
|
|
|
a91[10] = 0;
|
|
|
|
a91[11] = 0;
|
|
|
|
uint16_t chksum2 = ft8_v2::ft8_crc(a91, 96 - 14);
|
|
|
|
if (chksum != chksum2) {
|
|
|
|
LOG(LOG_DEBUG, "Checksum: message = %04x, CRC = %04x\n", chksum, chksum2);
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-24 20:22:26 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
char message[kMax_message_length];
|
|
|
|
unpack77(a91, message);
|
|
|
|
|
2018-12-28 04:33:07 +08:00
|
|
|
// Check for duplicate messages (TODO: use hashing)
|
2018-12-25 20:01:51 +08:00
|
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < num_decoded; ++i) {
|
|
|
|
if (0 == strcmp(decoded[i], message)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 23:04:05 +08:00
|
|
|
|
2018-12-25 20:01:51 +08:00
|
|
|
if (!found && num_decoded < kMax_decoded_messages) {
|
|
|
|
strcpy(decoded[num_decoded], message);
|
|
|
|
++num_decoded;
|
2018-11-20 23:04:05 +08:00
|
|
|
|
2018-12-24 20:22:26 +08:00
|
|
|
// Fake WSJT-X-like output for now
|
2018-12-25 20:01:51 +08:00
|
|
|
int snr = 0; // TODO: compute SNR
|
2018-12-29 02:42:22 +08:00
|
|
|
printf("000000 %3d %4.1f %4d ~ %s\n", cand.score, time_sec, (int)(freq_hz + 0.5f), message);
|
2018-11-13 23:16:24 +08:00
|
|
|
}
|
2018-11-13 19:04:36 +08:00
|
|
|
}
|
2018-12-29 02:42:22 +08:00
|
|
|
LOG(LOG_INFO, "Decoded %d messages\n", num_decoded);
|
2018-11-13 19:04:36 +08:00
|
|
|
|
2018-11-07 21:50:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|