Skip to content
Open
66 changes: 66 additions & 0 deletions sbitx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "i2cbb.h"
#include "si5351.h"
#include "ini.h"
int set_field(char *, char *); // This should be moved to a .h file

#define DEBUG 0

Expand Down Expand Up @@ -52,6 +53,10 @@ fftw_plan plan_spectrum;
float spectrum_window[MAX_BINS];
void set_rx1(int frequency);
void tr_switch(int tx_on);
float min_fft_level;
int rx_gain_slow_count = 0;
int rx_gain_changed = 0; // Flag to indicate a change in rx_gain has been called for
extern int mode_changed;

// Wisdom Defines for the FFTW and FFTWF libraries
// Options for WISDOM_MODE from least to most rigorous are FFTW_ESTIMATE, FFTW_MEASURE, FFTW_PATIENT, and FFTW_EXHAUSTIVE
Expand Down Expand Up @@ -591,7 +596,67 @@ void rx_process(int32_t *input_rx, int32_t *input_mic,

// the spectrum display is updated
spectrum_update();

// Make adjustments to IF Gain
min_fft_level = 10000; // Set high before starting to find the lowest level in the fft_bins array

for(i=1269; i<1800; i++)
{
if (fft_bins[i] < min_fft_level)
{
min_fft_level = fft_bins[i]; // new lowest level
}
}

#define TARGET_FFT_LEVEL 0.015
#define FAST_MIN_FFT_LEVEL 0.008
#define FAST_MAX_FFT_LEVEL 0.025

// Allow for fast IF Gain changes if the mode was changed (which occurs when either the band or the mode is changed)

if (((min_fft_level < FAST_MIN_FFT_LEVEL) || (min_fft_level > FAST_MAX_FFT_LEVEL)) && (mode_changed == 1))
{
rx_gain_slow_count = 0; // Reset slow IF Gain Loop counter

if (min_fft_level < FAST_MIN_FFT_LEVEL)
{
rx_gain += 5; // Plan to increase up RX Gain
rx_gain_changed = 1; // Flag to request a big RX Gain change
}
else if (min_fft_level > FAST_MAX_FFT_LEVEL)
{
rx_gain -= 5; // Plan to decrease RX Gain
rx_gain_changed = 1; // Flag to request a big RX Gain change
}
}
else
{
rx_gain_slow_count++; // Only make fine adjustments to rx_gain every second.
if (rx_gain_slow_count > 106) // Approx 106 blocks of samples per second
{
mode_changed = 0; // If the IF gain is close to correct for a second, stop fast IF Gain changes.
rx_gain_slow_count = 0;
if (min_fft_level < TARGET_FFT_LEVEL)
{
rx_gain += 1;
rx_gain_changed = 1; // Flag to request a small RX Gain change
}
else
{
rx_gain -= 1;
rx_gain_changed = 1; // Flag to request a small RX Gain change
}
}
}

if((!in_tx) && rx_gain_changed == 1)
{
// sound_mixer(audio_card, "Capture", rx_gain); // This function call is not needed
char rx_gain_buff[8];
(void) sprintf(rx_gain_buff, "%d", rx_gain);
set_field("r1:gain", rx_gain_buff);
rx_gain_changed = 0;
}

// ... back to the actual processing, after spectrum update

Expand Down Expand Up @@ -1261,6 +1326,7 @@ void sdr_request(char *request, char *response){
else if (!strcmp(cmd, "r1:freq")){
int d = atoi(value);
set_rx1(d);
mode_changed = 1; // enable the fast IF Gain Loop
//printf("Frequency set to %d\n", freq_hdr);
strcpy(response, "ok");
}
Expand Down
6 changes: 5 additions & 1 deletion sbitx_gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ char *ui_font = "Sans";
int field_font_size = 12;
int screen_width=800, screen_height=480;

extern int mode_changed; // Used by the Auto AGC code
int mode_changed = 0; // Used by the Auto AGC code

// we just use a look-up table to define the fonts used
// the struct field indexes into this table
struct font_style {
Expand Down Expand Up @@ -1314,7 +1317,7 @@ static void save_user_settings(int forced){
FILE *f = fopen(file_path, "w");
if (!f){
printf("Unable to save %s : %s\n", file_path, strerror(errno));
settings_updated = 0; // stop repeated attempts to write if file cannot be opened.
settings_updated = 0; // stop repeated attempts to write if file cannot be opened.
return;
}

Expand Down Expand Up @@ -3644,6 +3647,7 @@ void set_radio_mode(char *mode){
int i;

printf("Mode: %s\n", mode);
mode_changed = 1;
for (i = 0; i < sizeof(umode) - 1 && *mode; i++)
umode[i] = toupper(*mode++);
umode[i] = 0;
Expand Down