Skip to content

Commit

Permalink
correct misconception about txbuffer, signal generation working
Browse files Browse the repository at this point in the history
  • Loading branch information
biemster committed Sep 17, 2018
1 parent 72faad9 commit 2c1010e
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions fl2k-psk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
#include <iostream>
#include <signal.h>
#include <math.h>
#include <map>
#include <vector>

#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;

Expand All @@ -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<int,vector<int8_t>> 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];
}
}

Expand All @@ -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);
Expand All @@ -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;
}


Expand All @@ -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;
}


Expand Down

0 comments on commit 2c1010e

Please sign in to comment.