Tweaked syncing code

This commit is contained in:
Sebastian 2021-12-28 18:52:13 +01:00
parent 7e899d01e0
commit 645bc3463f
5 changed files with 72 additions and 31 deletions

View File

@ -73,7 +73,6 @@ std::complex<float> CostasBeaconSync::work(std::complex<float> in) {
return out;
}
CostasBeaconSync::~CostasBeaconSync() {
firfilt_crcf_destroy(bandpass);
}

View File

@ -1,5 +1,7 @@
#include "fft-beacon-finder.h"
#include <iostream>
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<float> FFTBeaconFinder::work(std::complex<float> in) {
@ -28,29 +33,52 @@ std::complex<float> FFTBeaconFinder::work(std::complex<float> 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);

View File

@ -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<float> work(std::complex<float>);
};

View File

@ -158,7 +158,7 @@ blocks:
alias: ''
amp: '1'
comment: ''
freq: '35000'
freq: '-10000'
maxoutbuf: '0'
minoutbuf: '0'
offset: '0'

View File

@ -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<float> in;
while (fread(&in, sizeof(std::complex<float>), 1, input) == 1) {
std::complex<float> coarse_synced = finder.work(in);
std::complex<float> sync_correction = sync.work(coarse_synced);
std::complex<float> out = coarse_synced * sync_correction;
std::complex<float> in_buffer[buffer_size];
std::complex<float> out_buffer[buffer_size];
fwrite(&out, sizeof(std::complex<float>), 1, output);
sample_count++;
size_t len =
fread(in_buffer, sizeof(std::complex<float>), buffer_size, input);
while (len > 0) {
for (size_t i = 0; i < len; i++) {
std::complex<float> coarse_synced = finder.work(in_buffer[i]);
std::complex<float> sync_correction = sync.work(coarse_synced);
out_buffer[i] = coarse_synced * sync_correction;
}
sample_count += len;
fwrite(out_buffer, sizeof(std::complex<float>), buffer_size, output);
len =
fread(&in_buffer, sizeof(std::complex<float>), buffer_size, input);
};
clock_t end_time = clock();