From 645bc3463f4d9d742edd978aeb9ae8e9f0b3bd2c Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Tue, 28 Dec 2021 18:52:13 +0100 Subject: [PATCH] Tweaked syncing code --- blocks/costas-beacon-sync.cpp | 1 - blocks/fft-beacon-finder.cpp | 69 ++++++++++++++++++++++++----------- blocks/fft-beacon-finder.h | 5 ++- blocks/generate-testdata.grc | 2 +- blocks/test.cpp | 26 +++++++++---- 5 files changed, 72 insertions(+), 31 deletions(-) diff --git a/blocks/costas-beacon-sync.cpp b/blocks/costas-beacon-sync.cpp index 077cd84..7d1f119 100644 --- a/blocks/costas-beacon-sync.cpp +++ b/blocks/costas-beacon-sync.cpp @@ -73,7 +73,6 @@ std::complex CostasBeaconSync::work(std::complex in) { return out; } - CostasBeaconSync::~CostasBeaconSync() { firfilt_crcf_destroy(bandpass); } diff --git a/blocks/fft-beacon-finder.cpp b/blocks/fft-beacon-finder.cpp index 9ede805..5030299 100644 --- a/blocks/fft-beacon-finder.cpp +++ b/blocks/fft-beacon-finder.cpp @@ -1,5 +1,7 @@ #include "fft-beacon-finder.h" +#include + FFTBeaconFinder::FFTBeaconFinder(int samplingrate) { this->samplingrate = samplingrate; @@ -10,6 +12,9 @@ FFTBeaconFinder::FFTBeaconFinder(int samplingrate) { pos = 0; next_fft_in = 0; + + last_center = 0; + last_center_level = 0.0; } std::complex FFTBeaconFinder::work(std::complex in) { @@ -28,29 +33,52 @@ std::complex FFTBeaconFinder::work(std::complex in) { } } - float max_levels = 0; - int max_center = 0; - for (int bin = -50; bin <= 50; bin++) { - int center_idx = spectral_bin_to_fft_idx(bin); - float center_val = std::abs(fft_out[center_idx]) / fft_max; - if (center_val > 0.25) { - int left_idx = spectral_bin_to_fft_idx(bin - 127); - int right_idx = spectral_bin_to_fft_idx(bin + 127); - float left_val = std::abs(fft_out[left_idx]) / fft_max; - float right_val = std::abs(fft_out[right_idx]) / fft_max; + std::cout << std::endl; - if (center_val + left_val + right_val > max_levels) { - max_levels = center_val + left_val + right_val; - max_center = bin; + int last_center_idx = spectral_bin_to_fft_idx(last_center); + float last_center_val = + std::abs(fft_out[last_center_idx]) / fft_max; + + if (last_center_val < 0.25) { + float max_levels = 0; + int max_center = 0; + for (int bin = -50; bin <= 50; bin++) { + int center_idx = spectral_bin_to_fft_idx(bin); + float center_val = std::abs(fft_out[center_idx]) / fft_max; + if (center_val > 0.25) { + int left_idx = spectral_bin_to_fft_idx(bin - 127); + int right_idx = spectral_bin_to_fft_idx(bin + 127); + float left_val = std::abs(fft_out[left_idx]) / fft_max; + float right_val = + std::abs(fft_out[right_idx]) / fft_max; + + std::cout << bin << " " << left_val << " | " + << center_val << " | " << right_val + << std::endl; + + if (center_val + left_val + right_val > max_levels) { + max_levels = center_val + left_val + right_val; + max_center = bin; + } } } - } - if (max_levels > 0.0) { - float center_freq = max_center * samplingrate / FFT_LEN; - nco_crcf_set_frequency( - coarse_correction, - -(2 * M_PI * center_freq) / samplingrate); + if (max_levels > 0.0) { + last_center = max_center; + + float center_freq = + float(max_center) / FFT_LEN * samplingrate; + nco_crcf_set_frequency( + coarse_correction, + -(2 * M_PI * center_freq) / samplingrate); + + std::cout << "New center " << center_freq << " " + << max_center << std::endl; + } else { + std::cout << "No center found!" << std::endl; + } + } else { + std::cout << "Last center still valid" << std::endl; } next_fft_in = samplingrate / 4; @@ -74,11 +102,10 @@ int FFTBeaconFinder::spectral_bin_to_fft_idx(int bin) { } else if (bin > 0) { return bin - 1; } else { - return bin + FFT_LEN; + return FFT_LEN + bin; } } - FFTBeaconFinder::~FFTBeaconFinder() { nco_crcf_destroy(coarse_correction); fft_destroy_plan(fft); diff --git a/blocks/fft-beacon-finder.h b/blocks/fft-beacon-finder.h index 0899361..c9d79de 100644 --- a/blocks/fft-beacon-finder.h +++ b/blocks/fft-beacon-finder.h @@ -20,12 +20,15 @@ class FFTBeaconFinder { size_t pos; int next_fft_in; + int last_center; + float last_center_level; + int spectral_bin_to_fft_idx(int); public: FFTBeaconFinder(int); ~FFTBeaconFinder(); - + std::complex work(std::complex); }; diff --git a/blocks/generate-testdata.grc b/blocks/generate-testdata.grc index 6b2e1c5..d710189 100644 --- a/blocks/generate-testdata.grc +++ b/blocks/generate-testdata.grc @@ -158,7 +158,7 @@ blocks: alias: '' amp: '1' comment: '' - freq: '35000' + freq: '-10000' maxoutbuf: '0' minoutbuf: '0' offset: '0' diff --git a/blocks/test.cpp b/blocks/test.cpp index fb67763..6b4d48f 100644 --- a/blocks/test.cpp +++ b/blocks/test.cpp @@ -6,6 +6,8 @@ #include "costas-beacon-sync.h" #include "fft-beacon-finder.h" +const size_t buffer_size = 4069; + int main(int argc, char const* argv[]) { FILE* output = fopen("output.raw", "w"); FILE* input = fopen("input.raw", "r"); @@ -16,14 +18,24 @@ int main(int argc, char const* argv[]) { int sample_count = 0; clock_t start_time = clock(); - std::complex in; - while (fread(&in, sizeof(std::complex), 1, input) == 1) { - std::complex coarse_synced = finder.work(in); - std::complex sync_correction = sync.work(coarse_synced); - std::complex out = coarse_synced * sync_correction; + std::complex in_buffer[buffer_size]; + std::complex out_buffer[buffer_size]; - fwrite(&out, sizeof(std::complex), 1, output); - sample_count++; + size_t len = + fread(in_buffer, sizeof(std::complex), buffer_size, input); + while (len > 0) { + for (size_t i = 0; i < len; i++) { + std::complex coarse_synced = finder.work(in_buffer[i]); + std::complex sync_correction = sync.work(coarse_synced); + out_buffer[i] = coarse_synced * sync_correction; + } + + sample_count += len; + + fwrite(out_buffer, sizeof(std::complex), buffer_size, output); + + len = + fread(&in_buffer, sizeof(std::complex), buffer_size, input); }; clock_t end_time = clock();