Add real time frequency calculation

multi_trace
DiSlord 5 years ago
parent d874d7fe88
commit 69aba76535

@ -105,8 +105,6 @@ uint16_t adc_single_read(uint32_t chsel)
uint16_t adc1_single_read(uint32_t chsel)
{
/* ADC setup */
// adcStart(&ADCD2, NULL);
adcgrpcfgXY.sqr[0] = ADC_SQR1_SQ1_N(ADC_CHANNEL_IN1);
adcConvert(&ADCD1, &adcgrpcfgVersion, samples, 1);
return(samples[0]);
}

@ -145,7 +145,7 @@ static THD_FUNCTION(Thread1, arg)
#ifdef __LISTEN__
} else if (sweep_mode & SWEEP_LISTEN) {
if (markers[active_marker].enabled == M_ENABLED) {
perform(false,0,frequencies[markers[active_marker].index], false);
perform(false, 0, getFrequency(markers[active_marker].index), false);
SI4432_Listen(MODE_SELECT(setting.mode));
}
#endif
@ -195,7 +195,7 @@ static THD_FUNCTION(Thread1, arg)
int i = marker_search_max(active_marker);
if (i != -1 && active_marker != MARKER_INVALID) {
markers[active_marker].index = i;
markers[active_marker].frequency = frequencies[i];
markers[active_marker].frequency = getFrequency(i);
redraw_request |= REDRAW_MARKER;
}
@ -995,7 +995,7 @@ VNA_SHELL_FUNCTION(cmd_scan)
uint16_t mask = my_atoui(argv[3]);
if (mask) {
for (i = 0; i < sweep_points; i++) {
if (mask & 1) shell_printf("%U ", frequencies[i]);
if (mask & 1) shell_printf("%U ", getFrequency(i));
if (mask & 2) shell_printf("%f %f ", value(measured[TRACE_ACTUAL][i]), 0.0);
if (mask & 4) shell_printf("%f %f ", value(measured[TRACE_STORED][i]), 0.0);
if (mask & 8) shell_printf("%f %f ", value(measured[TRACE_TEMP][i]), 0.0);
@ -1083,8 +1083,8 @@ update_marker_index(void)
else { // Search frequency index for marker frequency
#if 1
for (idx = 1; idx < sweep_points; idx++) {
if (frequencies[idx] <= f) continue;
if (f < (frequencies[idx-1]/2 + frequencies[idx]/2)) idx--; // Correct closest idx
if (getFrequency(idx) <= f) continue;
if (f < (getFrequency(idx-1)/2 + getFrequency(idx)/2)) idx--; // Correct closest idx
break;
}
#else
@ -1093,7 +1093,7 @@ update_marker_index(void)
#endif
}
markers[m].index = idx;
markers[m].frequency = frequencies[idx];
markers[m].frequency = getFrequency(idx);
}
}
@ -1103,9 +1103,9 @@ void set_marker_frequency(int m, freq_t f)
return;
int i = 1;
markers[m].mtype &= ~M_TRACKING;
freq_t s = (frequencies[1] - frequencies[0])/2;
freq_t s = (getFrequency(1) - getFrequency(0))/2;
while (i< sweep_points - 2){
if (frequencies[i]-s <= f && f < frequencies[i+1]-s) { // Avoid rounding error in s!!!!!!!
if (getFrequency(i)-s <= f && f < getFrequency(i+1)-s) { // Avoid rounding error in s!!!!!!!
markers[m].index = i;
markers[m].frequency = f;
return;
@ -1126,6 +1126,11 @@ void set_marker_time(int m, float f)
markers[m].frequency = 0;
}
/*
* Frequency list functions
*/
#ifdef __USE_FREQ_TABLE__
static freq_t frequencies[POINTS_COUNT];
static void
set_frequencies(freq_t start, freq_t stop, uint16_t points)
{
@ -1137,11 +1142,7 @@ set_frequencies(freq_t start, freq_t stop, uint16_t points)
freq_t f = start, df = step>>1;
for (i = 0; i <= step; i++, f+=delta) {
frequencies[i] = f;
df+=error;
if (df >=step) {
f++;
df -= step;
}
if ((df+=error) >= step) {f++; df-= step;}
}
// disable at out of sweep range
for (; i < POINTS_COUNT; i++)
@ -1149,6 +1150,27 @@ set_frequencies(freq_t start, freq_t stop, uint16_t points)
setting.frequency_step = delta;
dirty = true;
}
freq_t getFrequency(uint16_t idx) {return frequencies[idx];}
#else
static freq_t _f_start;
static freq_t _f_delta;
static freq_t _f_error;
static uint16_t _f_step;
static void
set_frequencies(freq_t start, freq_t stop, uint16_t points)
{
freq_t span = stop - start;
_f_start = start;
_f_step = (points - 1);
_f_delta = span / _f_step;
_f_error = span % _f_step;
setting.frequency_step = _f_delta;
dirty = true;
}
freq_t getFrequency(uint16_t idx) {return _f_start + _f_delta * idx + (_f_step / 2 + _f_error * idx) / _f_step;}
#endif
void
update_frequencies(void)
@ -1470,7 +1492,7 @@ VNA_SHELL_FUNCTION(cmd_marker)
int i = marker_search_max(active_marker);
if (i == -1) i = 0;
markers[active_marker].index = i;
markers[active_marker].frequency = frequencies[i];
markers[active_marker].frequency = getFrequency(i);
goto display_marker;
default:
// select active marker and move to index or frequency
@ -1482,7 +1504,7 @@ VNA_SHELL_FUNCTION(cmd_marker)
set_marker_frequency(active_marker, value);
else {
markers[t].index = value;
markers[t].frequency = frequencies[value];
markers[t].frequency = getFrequency(value);
}
return;
}
@ -1522,10 +1544,8 @@ VNA_SHELL_FUNCTION(cmd_frequencies)
int i;
(void)argc;
(void)argv;
for (i = 0; i < sweep_points; i++) {
if (frequencies[i] != 0)
shell_printf("%U\r\n", frequencies[i]);
}
for (i = 0; i < sweep_points; i++)
shell_printf("%U\r\n", getFrequency(i));
}
VNA_SHELL_FUNCTION(cmd_test)

@ -96,6 +96,7 @@
#else
#define __HARMONIC__
#endif
//#define __USE_FREQ_TABLE__ // Enable use table for frequency list
#ifdef TINYSA3
#define VARIANT(X,Y) (X)
@ -1068,7 +1069,6 @@ enum {W_OFF, W_SMALL, W_BIG};
#define REPEAT_TIME 111 // Time per extra repeat in uS
#define MEASURE_TIME 127 // Time per single point measurement with vbwstep =1 without step delay in uS
extern freq_t frequencies[POINTS_COUNT];
extern const float unit_scale_value[];
extern const char unit_scale_text[];
#ifdef TINYSA4
@ -1156,6 +1156,8 @@ extern int16_t lastsaveid;
//extern properties_t current_props;
freq_t getFrequency(uint16_t idx);
//#define frequency0 current_props._frequency0
//#define frequency1 current_props._frequency1
#define sweep_points setting._sweep_points

@ -1625,7 +1625,8 @@ static void cell_draw_marker_info(int x0, int y0)
int search_gate = 10;
if (markers[0].index < search_gate)
search_gate = markers[0].index * 2 / 3;
while (f * h_i < frequencies[sweep_points-1]) {
freq_t stop = getFrequency(sweep_points-1);
while (f * h_i < stop) {
if (search_maximum(1, f*h_i, search_gate) ) { // use marker 1 for searching harmonics
h_count++;
f = (f * 3 + markers[1].frequency / h_i) >> 2;

@ -54,7 +54,6 @@ bool debug_avoid_second = false;
int current_index = -1;
setting_t setting;
freq_t frequencies[POINTS_COUNT];
uint16_t actual_rbw_x10 = 0;
freq_t frequency_step_x10 = 0;
@ -903,7 +902,7 @@ void limits_update(void)
{
if (setting.limits[i].enabled) {
active = true;
while (j < sweep_points && (frequencies[j] < setting.limits[i].frequency || setting.limits[i].frequency == 0))
while (j < sweep_points && (getFrequency(j) < setting.limits[i].frequency || setting.limits[i].frequency == 0))
stored_t[j++] = setting.limits[i].level;
}
}
@ -2153,10 +2152,10 @@ done:
int binary_search_frequency(freq_t f) // Search which index in the frequency tabled matches with frequency f using actual_rbw
{
int L = 0;
int frequency_seatch_gate = (frequencies[1] - frequencies[0]) >> 1;
if (f < frequencies[0])
int frequency_seatch_gate = (getFrequency(1) - getFrequency(0)) >> 1;
if (f < getFrequency(0))
return -1;
if (f > frequencies[sweep_points-1])
if (f > getFrequency(sweep_points-1))
return -1;
// int R = (sizeof frequencies)/sizeof(int) - 1;
int R = sweep_points - 1;
@ -2164,9 +2163,10 @@ int binary_search_frequency(freq_t f) // Search which index in the frequenc
freq_t fplus = f + frequency_seatch_gate; // actual_rbw_x10 * frequency_seatch_gate;
while (L <= R) {
int m = (L + R) / 2;
if (frequencies[m] < fmin)
freq_t f = getFrequency(m);
if (f < fmin)
L = m + 1;
else if (frequencies[m] > fplus)
else if (f > fplus)
R = m - 1;
else
return m; // index is m
@ -2176,14 +2176,14 @@ int binary_search_frequency(freq_t f) // Search which index in the frequenc
int index_of_frequency(freq_t f) // Search which index in the frequency tabled matches with frequency f using actual_rbw
{
freq_t f_step = frequencies[1] - frequencies[0];
freq_t f_step = getFrequency(1) - getFrequency(0);
if (f_step == 0)
return 0;
if (f < frequencies[0])
if (f < getFrequency(0))
return -1;
if (f > frequencies[sweep_points-1])
if (f > getFrequency(sweep_points-1))
return -1;
int i = ((f - frequencies[0] ) + (f_step >> 1)) / f_step;
int i = ((f - getFrequency(0) ) + (f_step >> 1)) / f_step;
return i;
#if 0
// int R = (sizeof frequencies)/sizeof(int) - 1;
@ -2193,9 +2193,10 @@ int index_of_frequency(freq_t f) // Search which index in the frequency tab
freq_t fplus = f + frequency_seatch_gate; // actual_rbw_x10 * frequency_seatch_gate;
while (L <= R) {
int m = (L + R) / 2;
if (frequencies[m] < fmin)
freq_t f = getFrequency(m);
if (f < fmin)
L = m + 1;
else if (frequencies[m] > fplus)
else if (f > fplus)
R = m - 1;
else
return m; // index is m
@ -2212,11 +2213,11 @@ void interpolate_maximum(int m)
ref_marker_levels = stored_t;
else
ref_marker_levels = actual_t;
const int idx = markers[m].index;
markers[m].frequency = frequencies[idx];
const int idx = markers[m].index;
markers[m].frequency = getFrequency(idx);
if (idx > 0 && idx < sweep_points-1)
{
const int32_t delta_Hz = (int64_t)frequencies[idx + 0] - frequencies[idx + 1];
const int32_t delta_Hz = (int64_t)getFrequency(idx + 0) - getFrequency(idx + 1);
#ifdef TINYSA4
#define INTER_TYPE double
#else
@ -3456,12 +3457,13 @@ again: // Spur redu
} else
f_low = f_high = real_old_freq[SI4463_RX] + real_offset;
float f_error_low, f_error_high;
float freq = getFrequency(i);
if (setting.frequency_step == 0) {
f_error_low = ((float)frequencies[i] - (float)f_low);
f_error_high = ((float)f_high-(float)frequencies[i]);
f_error_low = (freq - f_low);
f_error_high = (f_high - freq);
} else {
f_error_low = ((float)f_low-(float)frequencies[i])/setting.frequency_step;
f_error_high = ((float)f_high-(float)frequencies[i])/setting.frequency_step;
f_error_low = (f_low - freq)/setting.frequency_step;
f_error_high = (f_high- freq)/setting.frequency_step;
}
char spur = ' ';
int delta=0;
@ -3782,8 +3784,9 @@ static bool sweep(bool break_on_operation)
debug_avoid_second = false;
debug_avoid_label:
debug_avoid_second = debug_avoid_second;
freq_t current_freq = getFrequency(i);
// --------------------- measure -------------------------
pureRSSI_t rssi = perform(break_on_operation, i, frequencies[i], setting.tracking); // Measure RSSI for one of the frequencies
pureRSSI_t rssi = perform(break_on_operation, i, current_freq, setting.tracking); // Measure RSSI for one of the frequencies
#ifdef TINYSA4
if (rssi == IGNORE_RSSI)
RSSI = -174.0;
@ -3820,12 +3823,12 @@ static bool sweep(bool break_on_operation)
#define AGC_RSSI_THRESHOLD (-55+get_attenuation())
float local_rssi = RSSI +setting.external_gain;
if (local_rssi > AGC_RSSI_THRESHOLD && local_rssi > agc_prev_rssi) {
agc_peak_freq = frequencies[i];
agc_peak_freq = current_freq;
agc_peak_rssi = agc_prev_rssi = local_rssi;
}
if (local_rssi < AGC_RSSI_THRESHOLD)
agc_prev_rssi = -150;
freq_t delta_freq = frequencies[i] - agc_peak_freq;
freq_t delta_freq = current_freq - agc_peak_freq;
if (agc_peak_freq != 0 && delta_freq < 2000000) {
int max_gain = (-25 - agc_peak_rssi ) / 4;
auto_set_AGC_LNA(false, 16 + delta_freq * max_gain / 2000000 ); // enable LNA and stepwise gain
@ -3946,7 +3949,7 @@ static bool sweep(bool break_on_operation)
float d_offset = 0.0;
int d_start = 0;
if (setting.average == AV_DECONV && setting.frequency_step != 0) {
d_width = (sweep_points * (actual_rbw_x10 * 250) / (frequencies[sweep_points-1] - frequencies[0]));
d_width = (sweep_points * (actual_rbw_x10 * 250) / get_sweep_frequency(ST_SPAN));
d_start = sweep_points/2 - d_width/2;
d_offset = stored_t[d_start];
for (int i=0; i<d_width; i++)
@ -4143,7 +4146,7 @@ static volatile int dummy;
// START_PROFILE
if (i == 0 || frequencies[i] < actual_rbw_x10 * 200) { // Prepare peak finding
if (i == 0 || getFrequency(i) < actual_rbw_x10 * 200) { // Prepare peak finding
cur_max = 0; // Always at least one maximum
temppeakIndex = 0;
temppeakLevel = actual_t[0];
@ -4476,7 +4479,7 @@ static volatile int dummy;
while (m < MARKERS_MAX) { // Insufficient maxima found
if (markers[m].enabled && markers[m].mtype & M_TRACKING) { // More available markers found
markers[m].index = 0; // Enabled but no max so set to left most frequency
markers[m].frequency = frequencies[0];
markers[m].frequency = getFrequency(0);
}
m++; // Try next marker
}
@ -4492,7 +4495,7 @@ static volatile int dummy;
#define H_SPACING 4
#endif
for (int i=1; i < MARKER_COUNT;i++)
markers[i].enabled = search_maximum(i, frequencies[markers[0].index]*(i+1), (i+1)*H_SPACING);
markers[i].enabled = search_maximum(i, getFrequency(markers[0].index)*(i+1), (i+1)*H_SPACING);
#ifdef TINYSA4
} else if (setting.measurement == M_AM && markers[0].index > 10) { // ----------AM measurement
int l = markers[1].index;
@ -4503,8 +4506,8 @@ static volatile int dummy;
markers[1].index = l;
markers[2].index = r;
}
freq_t lf = frequencies[l];
freq_t rf = frequencies[r];
freq_t lf = getFrequency(l);
freq_t rf = getFrequency(r);
markers[1].frequency = lf;
markers[2].frequency = rf;
#endif
@ -4517,8 +4520,8 @@ static volatile int dummy;
markers[0].index = l;
markers[1].index = r;
}
freq_t lf = frequencies[l];
freq_t rf = frequencies[r];
freq_t lf = getFrequency(l);
freq_t rf = getFrequency(r);
markers[0].frequency = lf;
markers[1].frequency = rf;
@ -4526,7 +4529,7 @@ static volatile int dummy;
markers[3].enabled = search_maximum(3, rf + (rf - lf), 12);
} else if (setting.measurement == M_PHASE_NOISE && markers[0].index > 10) { // ------------Phase noise measurement
markers[1].index = markers[0].index + (setting.mode == M_LOW ? WIDTH/4 : -WIDTH/4); // Position phase noise marker at requested offset
markers[1].frequency = frequencies[markers[1].index];
markers[1].frequency = getFrequency(markers[1].index);
} else if ((setting.measurement == M_PASS_BAND || setting.measurement == M_FM) && markers[0].index > 10) { // ----------------Pass band measurement
int t = 0;
float v = actual_t[markers[0].index] - (in_selftest ? 6.0 : 3.0);
@ -4534,14 +4537,14 @@ static volatile int dummy;
t++;
if (t< markers[0].index) {
markers[1].index = t;
markers[1].frequency = frequencies[t];
markers[1].frequency = getFrequency(t);
}
t = setting._sweep_points-1;;
while (t > markers[0].index && actual_t[t-1] < v) // find right -3dB point
t--;
if (t > markers[0].index) {
markers[2].index = t;
markers[2].frequency = frequencies[t];
markers[2].frequency = getFrequency(t);
}
} else if (setting.measurement == M_AM) { // ----------------AM measurement
if (S_IS_AUTO(setting.agc )) {
@ -4585,7 +4588,7 @@ static volatile int dummy;
peakLevel = actual_t[peakIndex];
cur_max = 1;
}
peakFreq = frequencies[peakIndex];
peakFreq = getFrequency(peakIndex);
min_level = temp_min_level;
}
// } while (MODE_OUTPUT(setting.mode) && setting.modulation != MO_NONE); // Never exit sweep loop while in output mode with modulation

10
ui.c

@ -633,7 +633,7 @@ get_marker_frequency(int marker)
return 0;
if (!markers[marker].enabled)
return 0;
return frequencies[markers[marker].index];
return getFrequency(markers[marker].index);
}
static UI_FUNCTION_CALLBACK(menu_marker_op_cb)
@ -734,7 +734,7 @@ static UI_FUNCTION_CALLBACK(menu_marker_search_cb)
if (data > 1) // Maximum related
interpolate_maximum(active_marker);
else
markers[active_marker].frequency = frequencies[i];
markers[active_marker].frequency = getFrequency(i);
}
redraw_marker(active_marker);
// if (data == 4)
@ -1632,7 +1632,7 @@ lever_move_marker(int status)
if (idx > sweep_points-1) idx = sweep_points-1 ;
}
markers[active_marker].index = idx;
markers[active_marker].frequency = frequencies[idx];
markers[active_marker].frequency = getFrequency(idx);
redraw_marker(active_marker);
markers[active_marker].mtype &= ~M_TRACKING; // Disable tracking when dragging marker
step++;
@ -1976,7 +1976,7 @@ drag_marker(int t, int m)
index = search_nearest_index(touch_x, touch_y, t);
if (index >= 0) {
markers[m].index = index;
markers[m].frequency = frequencies[index];
markers[m].frequency = getFrequency(index);
redraw_marker(m);
}
} while (touch_check()!= EVT_TOUCH_RELEASED);
@ -2137,7 +2137,7 @@ void save_to_sd(int mask)
if (res == FR_OK) {
for (int i = 0; i < sweep_points; i++) {
char *buf = (char *)spi_buffer;
if (mask & 1) buf += plot_printf(buf, 100, "%U, ", frequencies[i]);
if (mask & 1) buf += plot_printf(buf, 100, "%U, ", getFrequency(i));
if (mask & 2) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_ACTUAL][i]));
if (mask & 4) buf += plot_printf(buf, 100, "%f ", value(measured[TRACE_STORED][i]));
if (mask & 8) buf += plot_printf(buf, 100, "%f", value(measured[TRACE_TEMP][i]));

@ -561,7 +561,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_restart_acb){
if(b){
if (current_index >= 0 && setting.sweep) {
float current_level = setting.level + ((float)current_index)* setting.level_sweep / (float)sweep_points;
plot_printf(b->text, sizeof b->text, "STOP %5.3QHz %+.1fdBm", frequencies[current_index], current_level);
plot_printf(b->text, sizeof b->text, "STOP %5.3QHz %+.1fdBm", getFrequency(current_index), current_level);
}
else
plot_printf(b->text, sizeof b->text, "START SWEEP");
@ -1563,7 +1563,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_select_acb)
}
markers[data-1].enabled = true;
// interpolate_maximum(data-1); // possibly not a maximum
markers[data-1].frequency = frequencies[markers[data-1].index];
markers[data-1].frequency = getFrequency(markers[data-1].index);
active_marker_select(data-1);
menu_push_submenu(menu_marker_modify);
redraw_marker(active_marker);
@ -1649,7 +1649,7 @@ static UI_FUNCTION_ADV_CALLBACK(menu_marker_ref_select_acb)
}
markers[data-1].enabled = true;
// interpolate_maximum(data-1); // possibly not a maximum
markers[data-1].frequency = frequencies[markers[data-1].index];
markers[data-1].frequency = getFrequency(markers[data-1].index);
markers[active_marker].ref = data-1;
redraw_marker(active_marker);
menu_move_back(false);

Loading…
Cancel
Save

Powered by TurnKey Linux.