Skip to content

Commit

Permalink
add average mode
Browse files Browse the repository at this point in the history
  • Loading branch information
qrp73 committed Nov 18, 2019
1 parent 7aad290 commit e679893
Showing 1 changed file with 33 additions and 65 deletions.
98 changes: 33 additions & 65 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,52 +676,40 @@ static void ensure_edit_config(void)
cal_status = 0;
}

static void sweep_sample(float data[2], uint16_t avg)
{
(*sample_func)(data); // calculate reflection coeficient
if (avg <= 1)
return;
float acc[2];
for (uint32_t j=1; j < avg; j++) {
wait_dsp(1);
(*sample_func)(acc);
data[0] += acc[0];
data[1] += acc[1];
}
data[0] /= avg;
data[1] /= avg;
}

#define SWEEP_AVG_COUNT 10

// main loop for measurement
static bool sweep(bool break_on_operation)
{
pll_lock_failed = false;
float acc[2];
for (int i = 0; i < sweep_points; i++) {
int delay = set_frequency(frequencies[i]);
delay = delay < 3 ? 3 : delay;
delay = delay > 8 ? 8 : delay;

if (!sweep_avg) {
tlv320aic3204_select(0); // CH0:REFLECT
wait_dsp(delay);
(*sample_func)(measured[0][i]); // calculate reflection coeficient

tlv320aic3204_select(1); // CH1:TRANSMISSION
wait_dsp(delay);
(*sample_func)(measured[1][i]); // calculate transmission coeficient
} else {
#define SWEEP_AVG_COUNT 10
tlv320aic3204_select(0); // CH0:REFLECT
wait_dsp(delay);
measured[0][i][0] = 0.0;
measured[0][i][1] = 0.0;
for (int j=0; j < SWEEP_AVG_COUNT; j++) {
wait_dsp(1);
(*sample_func)(acc);
measured[0][i][0] += acc[0];
measured[0][i][1] += acc[1];
}
measured[0][i][0] /= SWEEP_AVG_COUNT;
measured[0][i][1] /= SWEEP_AVG_COUNT;

tlv320aic3204_select(1); // CH1:TRANSMISSION
wait_dsp(delay);
measured[1][i][0] = 0.0;
measured[1][i][1] = 0.0;
for (int j=0; j < SWEEP_AVG_COUNT; j++) {
wait_dsp(1);
(*sample_func)(acc);
measured[1][i][0] += acc[0];
measured[1][i][1] += acc[1];
}
measured[1][i][0] /= SWEEP_AVG_COUNT;
measured[1][i][1] /= SWEEP_AVG_COUNT;
}
tlv320aic3204_select(0); // CH0:REFLECT
wait_dsp(delay);
sweep_sample(measured[0][i], sweep_avg ? SWEEP_AVG_COUNT : 1);

tlv320aic3204_select(1); // CH1:TRANSMISSION
wait_dsp(delay);
sweep_sample(measured[1][i], sweep_avg ? SWEEP_AVG_COUNT : 1);

if (cal_status & CALSTAT_APPLY)
apply_error_term_at(i);
Expand All @@ -739,32 +727,6 @@ static bool sweep(bool break_on_operation)
}

#ifdef __SCANRAW_CMD__
static void measure_gamma_avg(uint8_t channel, uint32_t freq, uint16_t avg_count, float* gamma) {
int delay = set_frequency(freq);
delay = delay < 3 ? 3 : delay;
delay = delay > 8 ? 8 : delay;

tlv320aic3204_select(channel);
wait_dsp(delay);

gamma[0] = 0.0;
gamma[1] = 0.0;
float gamma_acc[2] = { 0, 0 };
for (int j = 0; j < avg_count; j++) {

wait_dsp(1);
/* calculate reflection/transmission coeficient */
(*sample_func)(gamma);

if (avg_count == 1) break;
gamma_acc[0] += gamma[0];
gamma_acc[1] += gamma[1];
}
if (avg_count > 1) {
gamma[0] = gamma_acc[0] / avg_count;
gamma[1] = gamma_acc[1] / avg_count;
}
}

static void cmd_scanraw(BaseSequentialStream *chp, int argc, char *argv[])
{
Expand Down Expand Up @@ -799,9 +761,15 @@ static void cmd_scanraw(BaseSequentialStream *chp, int argc, char *argv[])
palClearPad(GPIOC, GPIOC_LED); // disable led and wait for voltage stabilization
chThdSleepMilliseconds(10);

float gamma[2];
for (int i = 0; i < count; i++, freq += step) {
float gamma[2];
measure_gamma_avg(chan, freq, avg_count, gamma);
int delay = set_frequency(freq);
delay = delay < 3 ? 3 : delay;
delay = delay > 8 ? 8 : delay;

tlv320aic3204_select(chan);
wait_dsp(delay);
sweep_sample(gamma, avg_count);

#ifndef __USE_STDIO__
// WARNING: chprintf doesn't support proper float formatting
Expand Down

0 comments on commit e679893

Please sign in to comment.