Skip to content

Commit

Permalink
add interpolation on cal
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Sep 15, 2017
1 parent 0ce695b commit 774a11a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions dsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ calculate_gamma(float gamma[2])
float rs = acc_ref_s;
float rc = acc_ref_c;
float rr = rs * rs + rc * rc;
//rr = sqrtf(rr) * 1e8;
float ss = acc_samp_s;
float sc = acc_samp_c;
gamma[0] = (sc * rc + ss * rs) / rr;
Expand Down
14 changes: 14 additions & 0 deletions flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ caldata_recall(int id)
return 0;
}

const properties_t *
caldata_ref(int id)
{
const properties_t *src;
if (id < 0 || id >= SAVEAREA_MAX)
return NULL;
src = (const properties_t*)saveareas[id];

if (src->magic != CONFIG_MAGIC)
return NULL;
if (checksum(src, sizeof(properties_t)) != 0)
return NULL;
return src;
}

const uint32_t save_config_prop_area_size = 0x8000;

Expand Down
75 changes: 71 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ int32_t frequency = 10000000;
uint8_t drive_strength = SI5351_CLK_DRIVE_STRENGTH_2MA;
int8_t frequency_updated = FALSE;
int8_t sweep_enabled = TRUE;
int8_t cal_auto_interpolate = TRUE;

static THD_WORKING_AREA(waThread1, 768);
static THD_FUNCTION(Thread1, arg)
Expand Down Expand Up @@ -503,6 +504,9 @@ update_frequencies(void)
for (i = 0; i < sweep_points; i++)
frequencies[i] = start + span * i / (sweep_points - 1) * 100;

if (cal_auto_interpolate)
cal_interpolate(0);

frequency_updated = TRUE;
// set grid layout
update_grid();
Expand Down Expand Up @@ -897,6 +901,62 @@ cal_done(void)
cal_status |= CALSTAT_APPLY;
}

void
cal_interpolate(int s)
{
const properties_t *src = caldata_ref(s);
int i, j;
int eterm;
if (src == NULL)
return;

ensure_edit_config();

// lower than start freq of src range
for (i = 0; i < sweep_points; i++) {
if (frequencies[i] >= src->_frequencies[0])
break;

// fill cal_data at head of src range
for (eterm = 0; eterm < 5; eterm++) {
cal_data[eterm][i][0] = src->_cal_data[eterm][0][0];
cal_data[eterm][i][1] = src->_cal_data[eterm][0][1];
}
}

j = 0;
for (; i < sweep_points; i++) {
uint32_t f = frequencies[i];

for (; j < sweep_points-1; j++) {
if (src->_frequencies[j] <= f && f < src->_frequencies[j+1]) {
// found f between freqs at j and j+1
float k1 = (float)(f - src->_frequencies[j])
/ (src->_frequencies[j+1] - src->_frequencies[j]);
float k0 = 1.0 - k1;
for (eterm = 0; eterm < 5; eterm++) {
cal_data[eterm][i][0] = src->_cal_data[eterm][j][0] * k0 + src->_cal_data[eterm][j+1][0] * k1;
cal_data[eterm][i][1] = src->_cal_data[eterm][j][1] * k0 + src->_cal_data[eterm][j+1][1] * k1;
}
break;
}
}
if (j == sweep_points-1)
break;
}

// upper than end freq of src range
for (; i < sweep_points; i++) {
// fill cal_data at tail of src
for (eterm = 0; eterm < 5; eterm++) {
cal_data[eterm][i][0] = src->_cal_data[eterm][sweep_points-1][0];
cal_data[eterm][i][1] = src->_cal_data[eterm][sweep_points-1][1];
}
}

cal_status |= src->_cal_status | CALSTAT_APPLY | CALSTAT_INTERPOLATED;
}

static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[])
{
const char *items[] = { "load", "open", "short", "thru", "isoln", "Es", "Er", "Et", "cal'ed" };
Expand Down Expand Up @@ -945,8 +1005,15 @@ static void cmd_cal(BaseSequentialStream *chp, int argc, char *argv[])
chprintf(chp, "%f %f\r\n", cal_data[CAL_THRU][0][0], cal_data[CAL_THRU][0][1]);
chprintf(chp, "%f %f\r\n", cal_data[CAL_ISOLN][0][0], cal_data[CAL_ISOLN][0][1]);
return;
} else if (strcmp(cmd, "in") == 0) {
int s = 0;
if (argc > 1)
s = atoi(argv[1]);
cal_interpolate(s);
draw_cal_status();
return;
} else {
chprintf(chp, "usage: cal [load|open|short|thru|isoln|done|reset|on|off]\r\n");
chprintf(chp, "usage: cal [load|open|short|thru|isoln|done|reset|on|off|in]\r\n");
return;
}
}
Expand Down Expand Up @@ -1493,13 +1560,13 @@ int main(void)
*/
dacStart(&DACD2, &dac1cfg1);

/* initial frequencies */
update_frequencies();

/* restore frequencies and calibration properties from flash memory */
if (config.default_loadcal >= 0)
caldata_recall(config.default_loadcal);

/* initial frequencies */
update_frequencies();

redraw();

/*
Expand Down
2 changes: 2 additions & 0 deletions nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern float measured[2][101][2];
#define CALSTAT_ED CALSTAT_LOAD
#define CALSTAT_EX CALSTAT_ISOLN
#define CALSTAT_APPLY (1<<8)
#define CALSTAT_INTERPOLATED (1<<9)

#define ETERM_ED 0 /* error term directivity */
#define ETERM_ES 1 /* error term source match */
Expand Down Expand Up @@ -287,6 +288,7 @@ extern properties_t current_props;

int caldata_save(int id);
int caldata_recall(int id);
const properties_t *caldata_ref(int id);

int config_save(void);
int config_recall(void);
Expand Down
2 changes: 2 additions & 0 deletions plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,8 @@ draw_cal_status(void)
ili9341_fill(0, y, 10, 6*YSTEP, 0x0000);
if (cal_status & CALSTAT_APPLY) {
char c[3] = "C0";
if (cal_status & CALSTAT_INTERPOLATED)
c[0] = 'c';
if (active_props == &current_props)
c[1] = '*';
else
Expand Down

0 comments on commit 774a11a

Please sign in to comment.