2022-06-16 09:28:07 +00:00
|
|
|
#include "monitor.h"
|
2022-06-26 07:47:46 +00:00
|
|
|
#include <common/common.h>
|
2022-06-16 09:28:07 +00:00
|
|
|
|
|
|
|
#define LOG_LEVEL LOG_INFO
|
|
|
|
#include <ft8/debug.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static float hann_i(int i, int N)
|
|
|
|
{
|
|
|
|
float x = sinf((float)M_PI * i / N);
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
static float hamming_i(int i, int N)
|
|
|
|
{
|
|
|
|
const float a0 = (float)25 / 46;
|
|
|
|
const float a1 = 1 - a0;
|
2022-06-16 09:28:07 +00:00
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
float x1 = cosf((float)TWO_PI * i / N);
|
|
|
|
return a0 - a1 * x1;
|
|
|
|
}
|
2022-06-16 09:28:07 +00:00
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
static 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;
|
2022-06-16 09:28:07 +00:00
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
float x1 = cosf((float)TWO_PI * i / N);
|
|
|
|
float x2 = 2 * x1 * x1 - 1; // Use double angle formula
|
2022-06-16 09:28:07 +00:00
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
return a0 - a1 * x1 + a2 * x2;
|
|
|
|
}
|
2022-06-16 09:28:07 +00:00
|
|
|
|
2022-06-26 07:47:46 +00:00
|
|
|
static void waterfall_init(ftx_waterfall_t* me, int max_blocks, int num_bins, int time_osr, int freq_osr)
|
2022-06-16 09:28:07 +00:00
|
|
|
{
|
|
|
|
size_t mag_size = max_blocks * time_osr * freq_osr * num_bins * sizeof(me->mag[0]);
|
|
|
|
me->max_blocks = max_blocks;
|
|
|
|
me->num_blocks = 0;
|
|
|
|
me->num_bins = num_bins;
|
|
|
|
me->time_osr = time_osr;
|
|
|
|
me->freq_osr = freq_osr;
|
|
|
|
me->block_stride = (time_osr * freq_osr * num_bins);
|
2022-08-03 12:57:03 +00:00
|
|
|
me->mag = (WF_ELEM_T*)malloc(mag_size);
|
2022-06-16 09:28:07 +00:00
|
|
|
LOG(LOG_DEBUG, "Waterfall size = %zu\n", mag_size);
|
|
|
|
}
|
|
|
|
|
2022-06-26 07:47:46 +00:00
|
|
|
static void waterfall_free(ftx_waterfall_t* me)
|
2022-06-16 09:28:07 +00:00
|
|
|
{
|
|
|
|
free(me->mag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void monitor_init(monitor_t* me, const monitor_config_t* cfg)
|
|
|
|
{
|
2022-06-19 17:01:21 +00:00
|
|
|
float slot_time = (cfg->protocol == FTX_PROTOCOL_FT4) ? FT4_SLOT_TIME : FT8_SLOT_TIME;
|
|
|
|
float symbol_period = (cfg->protocol == FTX_PROTOCOL_FT4) ? FT4_SYMBOL_PERIOD : FT8_SYMBOL_PERIOD;
|
2022-06-16 09:28:07 +00:00
|
|
|
// Compute DSP parameters that depend on the sample rate
|
|
|
|
me->block_size = (int)(cfg->sample_rate * symbol_period); // samples corresponding to one FSK symbol
|
|
|
|
me->subblock_size = me->block_size / cfg->time_osr;
|
|
|
|
me->nfft = me->block_size * cfg->freq_osr;
|
|
|
|
me->fft_norm = 2.0f / me->nfft;
|
|
|
|
// const int len_window = 1.8f * me->block_size; // hand-picked and optimized
|
|
|
|
|
|
|
|
me->window = (float*)malloc(me->nfft * sizeof(me->window[0]));
|
|
|
|
for (int i = 0; i < me->nfft; ++i)
|
|
|
|
{
|
|
|
|
// window[i] = 1;
|
2022-08-03 12:57:03 +00:00
|
|
|
me->window[i] = me->fft_norm * hann_i(i, me->nfft);
|
2022-06-16 09:28:07 +00:00
|
|
|
// me->window[i] = blackman_i(i, me->nfft);
|
|
|
|
// me->window[i] = hamming_i(i, me->nfft);
|
|
|
|
// me->window[i] = (i < len_window) ? hann_i(i, len_window) : 0;
|
|
|
|
}
|
|
|
|
me->last_frame = (float*)calloc(me->nfft, sizeof(me->last_frame[0]));
|
|
|
|
|
|
|
|
LOG(LOG_INFO, "Block size = %d\n", me->block_size);
|
|
|
|
LOG(LOG_INFO, "Subblock size = %d\n", me->subblock_size);
|
|
|
|
|
2022-08-03 12:57:03 +00:00
|
|
|
size_t fft_work_size = 0;
|
|
|
|
kiss_fftr_alloc(me->nfft, 0, 0, &fft_work_size);
|
2022-06-16 09:28:07 +00:00
|
|
|
me->fft_work = malloc(fft_work_size);
|
|
|
|
me->fft_cfg = kiss_fftr_alloc(me->nfft, 0, me->fft_work, &fft_work_size);
|
|
|
|
|
2022-08-03 12:57:03 +00:00
|
|
|
LOG(LOG_INFO, "N_FFT = %d\n", me->nfft);
|
|
|
|
LOG(LOG_DEBUG, "FFT work area = %zu\n", fft_work_size);
|
|
|
|
|
|
|
|
#ifdef WATERFALL_USE_PHASE
|
2025-03-15 05:44:23 +00:00
|
|
|
me->nifft = 64; // Gives 100 Hz sample rate for FT8 (160ms symbol period)
|
2022-08-03 12:57:03 +00:00
|
|
|
|
|
|
|
size_t ifft_work_size = 0;
|
|
|
|
kiss_fft_alloc(me->nifft, 1, 0, &ifft_work_size);
|
|
|
|
me->ifft_work = malloc(ifft_work_size);
|
|
|
|
me->ifft_cfg = kiss_fft_alloc(me->nifft, 1, me->ifft_work, &ifft_work_size);
|
|
|
|
|
|
|
|
LOG(LOG_INFO, "N_iFFT = %d\n", me->nifft);
|
|
|
|
LOG(LOG_DEBUG, "iFFT work area = %zu\n", ifft_work_size);
|
|
|
|
#endif
|
|
|
|
|
2022-06-16 09:28:07 +00:00
|
|
|
// Allocate enough blocks to fit the entire FT8/FT4 slot in memory
|
|
|
|
const int max_blocks = (int)(slot_time / symbol_period);
|
|
|
|
// Keep only FFT bins in the specified frequency range (f_min/f_max)
|
|
|
|
me->min_bin = (int)(cfg->f_min * symbol_period);
|
|
|
|
me->max_bin = (int)(cfg->f_max * symbol_period) + 1;
|
|
|
|
const int num_bins = me->max_bin - me->min_bin;
|
|
|
|
|
|
|
|
waterfall_init(&me->wf, max_blocks, num_bins, cfg->time_osr, cfg->freq_osr);
|
|
|
|
me->wf.protocol = cfg->protocol;
|
|
|
|
|
|
|
|
me->symbol_period = symbol_period;
|
|
|
|
|
|
|
|
me->max_mag = -120.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void monitor_free(monitor_t* me)
|
|
|
|
{
|
|
|
|
waterfall_free(&me->wf);
|
|
|
|
free(me->fft_work);
|
|
|
|
free(me->last_frame);
|
|
|
|
free(me->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void monitor_reset(monitor_t* me)
|
|
|
|
{
|
|
|
|
me->wf.num_blocks = 0;
|
2022-06-19 17:01:21 +00:00
|
|
|
me->max_mag = -120.0f;
|
2022-06-16 09:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute FFT magnitudes (log wf) for a frame in the signal and update waterfall data
|
|
|
|
void monitor_process(monitor_t* me, const float* frame)
|
|
|
|
{
|
|
|
|
// Check if we can still store more waterfall data
|
|
|
|
if (me->wf.num_blocks >= me->wf.max_blocks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int offset = me->wf.num_blocks * me->wf.block_stride;
|
|
|
|
int frame_pos = 0;
|
|
|
|
|
|
|
|
// Loop over block subdivisions
|
|
|
|
for (int time_sub = 0; time_sub < me->wf.time_osr; ++time_sub)
|
|
|
|
{
|
|
|
|
kiss_fft_scalar timedata[me->nfft];
|
|
|
|
kiss_fft_cpx freqdata[me->nfft / 2 + 1];
|
|
|
|
|
|
|
|
// Shift the new data into analysis frame
|
|
|
|
for (int pos = 0; pos < me->nfft - me->subblock_size; ++pos)
|
|
|
|
{
|
|
|
|
me->last_frame[pos] = me->last_frame[pos + me->subblock_size];
|
|
|
|
}
|
|
|
|
for (int pos = me->nfft - me->subblock_size; pos < me->nfft; ++pos)
|
|
|
|
{
|
|
|
|
me->last_frame[pos] = frame[frame_pos];
|
|
|
|
++frame_pos;
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:57:03 +00:00
|
|
|
// Do DFT of windowed analysis frame
|
2022-06-16 09:28:07 +00:00
|
|
|
for (int pos = 0; pos < me->nfft; ++pos)
|
|
|
|
{
|
2022-08-03 12:57:03 +00:00
|
|
|
timedata[pos] = me->window[pos] * me->last_frame[pos];
|
2022-06-16 09:28:07 +00:00
|
|
|
}
|
|
|
|
kiss_fftr(me->fft_cfg, timedata, freqdata);
|
|
|
|
|
2022-08-03 12:57:03 +00:00
|
|
|
// Loop over possible frequency OSR offsets
|
2022-06-16 09:28:07 +00:00
|
|
|
for (int freq_sub = 0; freq_sub < me->wf.freq_osr; ++freq_sub)
|
|
|
|
{
|
|
|
|
for (int bin = me->min_bin; bin < me->max_bin; ++bin)
|
|
|
|
{
|
|
|
|
int src_bin = (bin * me->wf.freq_osr) + freq_sub;
|
|
|
|
float mag2 = (freqdata[src_bin].i * freqdata[src_bin].i) + (freqdata[src_bin].r * freqdata[src_bin].r);
|
|
|
|
float db = 10.0f * log10f(1E-12f + mag2);
|
2022-08-03 12:57:03 +00:00
|
|
|
|
|
|
|
#ifdef WATERFALL_USE_PHASE
|
|
|
|
// Save the magnitude in dB and phase in radians
|
|
|
|
float phase = atan2f(freqdata[src_bin].i, freqdata[src_bin].r);
|
|
|
|
me->wf.mag[offset].mag = db;
|
|
|
|
me->wf.mag[offset].phase = phase;
|
|
|
|
#else
|
2022-06-16 09:28:07 +00:00
|
|
|
// Scale decibels to unsigned 8-bit range and clamp the value
|
|
|
|
// Range 0-240 covers -120..0 dB in 0.5 dB steps
|
|
|
|
int scaled = (int)(2 * db + 240);
|
|
|
|
me->wf.mag[offset] = (scaled < 0) ? 0 : ((scaled > 255) ? 255 : scaled);
|
2022-08-03 12:57:03 +00:00
|
|
|
#endif
|
2022-06-16 09:28:07 +00:00
|
|
|
++offset;
|
|
|
|
|
|
|
|
if (db > me->max_mag)
|
|
|
|
me->max_mag = db;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++me->wf.num_blocks;
|
|
|
|
}
|
2022-08-03 12:57:03 +00:00
|
|
|
|
|
|
|
#ifdef WATERFALL_USE_PHASE
|
2025-03-15 05:44:23 +00:00
|
|
|
void monitor_resynth(const monitor_t* me, const ftx_candidate_t* cand, float* signal)
|
2022-08-03 12:57:03 +00:00
|
|
|
{
|
|
|
|
const int num_ifft = me->nifft;
|
|
|
|
const int num_shift = num_ifft / 2;
|
2025-03-15 05:44:23 +00:00
|
|
|
const int taper_width = 1;
|
2022-08-03 12:57:03 +00:00
|
|
|
|
2025-03-15 05:44:23 +00:00
|
|
|
// Starting offset is 1 block and 1 subblock due to how data is shifted to analysis buffer
|
|
|
|
WF_ELEM_T* el = wfall_get_element(&me->wf, 0, 0, 0, cand->freq_sub);
|
2022-08-03 12:57:03 +00:00
|
|
|
|
|
|
|
// DFT frequency data - initialize to zero
|
|
|
|
kiss_fft_cpx freqdata[num_ifft];
|
|
|
|
for (int i = 0; i < num_ifft; ++i)
|
|
|
|
{
|
|
|
|
freqdata[i].r = 0;
|
|
|
|
freqdata[i].i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
for (int num_block = 1; num_block < me->wf.num_blocks; ++num_block)
|
|
|
|
{
|
2025-03-15 05:44:23 +00:00
|
|
|
// Extract frequency data around the selected cand only
|
|
|
|
for (int i = cand->freq_offset - taper_width - 1; i < cand->freq_offset + 8 + taper_width - 1; ++i)
|
2022-08-03 12:57:03 +00:00
|
|
|
{
|
|
|
|
if ((i >= 0) && (i < me->wf.num_bins))
|
|
|
|
{
|
2025-03-15 05:44:23 +00:00
|
|
|
int tgt_bin = (me->wf.freq_osr * (i - cand->freq_offset) + num_ifft) % num_ifft;
|
2022-08-03 12:57:03 +00:00
|
|
|
float weight = 1.0f;
|
2025-03-15 05:44:23 +00:00
|
|
|
if (i < cand->freq_offset)
|
2022-08-03 12:57:03 +00:00
|
|
|
{
|
2025-03-15 05:44:23 +00:00
|
|
|
weight = ((i - cand->freq_offset) + taper_width) / (float)taper_width;
|
2022-08-03 12:57:03 +00:00
|
|
|
}
|
2025-03-15 05:44:23 +00:00
|
|
|
else if (i > cand->freq_offset + 7)
|
2022-08-03 12:57:03 +00:00
|
|
|
{
|
2025-03-15 05:44:23 +00:00
|
|
|
weight = ((cand->freq_offset + 7 - i) + taper_width) / (float)taper_width;
|
2022-08-03 12:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert (dB magnitude, phase) to (real, imaginary)
|
|
|
|
float mag = powf(10.0f, el[i].mag / 20) / 2 * weight;
|
|
|
|
freqdata[tgt_bin].r = mag * cosf(el[i].phase);
|
|
|
|
freqdata[tgt_bin].i = mag * sinf(el[i].phase);
|
|
|
|
|
|
|
|
int i2 = i + me->wf.num_bins;
|
|
|
|
tgt_bin = (tgt_bin + 1) % num_ifft;
|
|
|
|
float mag2 = powf(10.0f, el[i2].mag / 20) / 2 * weight;
|
|
|
|
freqdata[tgt_bin].r = mag2 * cosf(el[i2].phase);
|
|
|
|
freqdata[tgt_bin].i = mag2 * sinf(el[i2].phase);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute inverse DFT and overlap-add the waveform
|
|
|
|
kiss_fft_cpx timedata[num_ifft];
|
|
|
|
kiss_fft(me->ifft_cfg, freqdata, timedata);
|
|
|
|
for (int i = 0; i < num_ifft; ++i)
|
|
|
|
{
|
|
|
|
signal[pos + i] += timedata[i].i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next symbol
|
|
|
|
el += me->wf.block_stride;
|
|
|
|
pos += num_shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|