From 2c1010e9c2005c91fe5af0779828f43aadf1b722 Mon Sep 17 00:00:00 2001 From: lars Date: Mon, 17 Sep 2018 11:15:37 +0200 Subject: [PATCH] correct misconception about txbuffer, signal generation working --- fl2k-psk.cpp | 75 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/fl2k-psk.cpp b/fl2k-psk.cpp index 1308ac3..b84272a 100644 --- a/fl2k-psk.cpp +++ b/fl2k-psk.cpp @@ -9,9 +9,19 @@ #include #include #include +#include +#include #include "osmo-fl2k.h" +#define SIGNAL_MAX 99 // 99 * 0.70710678 = 70.00.... keeps rounding error in signal to minimum + // 17 * 0.70710678 = 12.02.... keeps rounding error low and low signal keeps rest of the spectrum relatively clean +#define SIGNAL_MIN -SIGNAL_MAX +#define SIGNAL_FREQ 28000000 +#define SF_RATIO 4 // {2,4,8} samplerate / signal frequency ratio (max sample rate is about 140 MS/s on USB3) + +#define SIN_1_4_PI 0.70710678 // sin(1/4*pi) + int8_t *tx_buffer = nullptr; int phase_curr = 0; @@ -36,20 +46,21 @@ void fl2k_callback(fl2k_data_info_t *data_info) { cout << "WARNING: device error" << endl; } - // sample rate is twice the frequency, so half a wavelength is in de buffer - phase_curr = (phase_curr + 180) % 360; - int idx_txbuf = ((phase_curr/180) * FL2K_BUF_LEN); - data_info->sampletype_signed = 1; - data_info->r_buf = (char *)tx_buffer + idx_txbuf; + data_info->r_buf = (char *)tx_buffer; } void init_txbuffer() { - tx_buffer = (int8_t*)malloc(FL2K_BUF_LEN * 3); // put 1.5 wavelengths in buffer + int8_t sin14pi = (int8_t)(SIGNAL_MAX * SIN_1_4_PI); + map> sines; + sines[2] = {SIGNAL_MIN, SIGNAL_MAX}; + sines[4] = {0, SIGNAL_MIN, 0, SIGNAL_MAX}; + sines[8] = {0, (int8_t)-sin14pi, SIGNAL_MIN, (int8_t)-sin14pi, 0, sin14pi, SIGNAL_MAX, sin14pi}; + + tx_buffer = (int8_t*)malloc(FL2K_BUF_LEN); + auto sine = sines[SF_RATIO]; for(int i = 0; i < FL2K_BUF_LEN; i++) { - tx_buffer[i] = (sin(((double)i/FL2K_BUF_LEN) * M_PI) * 127) +.5; // add .5 because integers round down - tx_buffer[i + FL2K_BUF_LEN] = -tx_buffer[i]; - tx_buffer[i + (FL2K_BUF_LEN*2)] = tx_buffer[i]; + tx_buffer[i] = sine[i % SF_RATIO]; } } @@ -67,7 +78,7 @@ void attach_sighandlers() { } void set_freq_carrier(uint32_t freq) { - uint32_t samplerate = freq * 2; // sample rate twice the frequency gives the smoothest output (TODO: experiment) + uint32_t samplerate = freq * SF_RATIO; // Set the sample rate int r = fl2k_set_sample_rate(fl2k_dev, samplerate); @@ -77,7 +88,7 @@ void set_freq_carrier(uint32_t freq) { /* read back actual frequency */ samplerate = fl2k_get_sample_rate(fl2k_dev); - cout << "Actual {sample rate,frequency} = {" << samplerate << "," << samplerate/2 << "}" << endl; + cout << "Actual {sample rate,frequency} = {" << samplerate << "," << samplerate/SF_RATIO << "}" << endl; } @@ -93,28 +104,28 @@ int main(int argc, char **argv) { cout << "Opened device" << endl; int r = fl2k_start_tx(fl2k_dev, fl2k_callback, nullptr, 0); - set_freq_carrier(28000000); - } - - cout << "Press u,d,q to raise or lower frequency, or quit: " << flush; - char c; - while(cin.get(c)) { - switch(c) { - case 'u': - set_freq_carrier((fl2k_get_sample_rate(fl2k_dev) /2) +1000000); - break; - case 'd': - set_freq_carrier((fl2k_get_sample_rate(fl2k_dev) /2) -1000000); - break; - case 'q': - exit(0); - break; - default: - break; + set_freq_carrier(SIGNAL_FREQ); + + cout << "Press u,d,q to raise or lower frequency, or quit: " << flush; + char c; + while(cin.get(c)) { + switch(c) { + case 'u': + set_freq_carrier((fl2k_get_sample_rate(fl2k_dev) /SF_RATIO) +1000000); + break; + case 'd': + set_freq_carrier((fl2k_get_sample_rate(fl2k_dev) /SF_RATIO) -1000000); + break; + case 'q': + exit(0); + break; + default: + break; + } + + cin.ignore(); + cout << "> " << flush; } - - cin.ignore(); - cout << "> " << flush; }