From c1decf606bedd4eaec34715ba52e272608678274 Mon Sep 17 00:00:00 2001 From: TT Date: Wed, 28 Sep 2016 22:48:53 +0900 Subject: [PATCH] use float to calc gamma, cleanup --- dsp.c | 20 ++++++++++---------- ili9431.c | 3 ++- main.c | 37 +++++++++++++++---------------------- nanovna.h | 7 +++---- si5351.c | 6 +++++- 5 files changed, 35 insertions(+), 38 deletions(-) diff --git a/dsp.c b/dsp.c index d8fde59..b10c9c0 100644 --- a/dsp.c +++ b/dsp.c @@ -73,7 +73,7 @@ hilbert_transform(void) } } -void calclate_gamma(void) +void calclate_gamma(float *gamma) #if 0 { __SIMD32_TYPE *r = __SIMD32_CONST(refiq_buf); @@ -103,23 +103,23 @@ void calclate_gamma(void) int16_t *r = refiq_buf; int16_t *s = samp_buf; int len = SAMPLE_LEN/5; - double acc_r = 0; - double acc_i = 0; - double acc_ref = 0; + float acc_r = 0; + float acc_i = 0; + float acc_ref = 0; int i; - double rn; + float rn; for (i = 0; i < len; i++) { int16_t s0 = *s++; int16_t rr = *r++; int16_t ri = *r++; - acc_r += (double)(s0 * rr); - acc_i += (double)(s0 * ri); - acc_ref += (double)rr*rr + (double)ri*ri; + acc_r += (float)(s0 * rr); + acc_i += (float)(s0 * ri); + acc_ref += (float)rr*rr + (float)ri*ri; } rn = sqrtf(acc_ref / len); - gamma_real = 16 * acc_r / rn / len; - gamma_imag = 16 * acc_i / rn / len; + gamma[0] = 16 * acc_r / rn / len; + gamma[1] = 16 * acc_i / rn / len; } #endif diff --git a/ili9431.c b/ili9431.c index 6c2011b..38862dc 100644 --- a/ili9431.c +++ b/ili9431.c @@ -1,3 +1,4 @@ +#include #include "ch.h" #include "hal.h" #include "nanovna.h" @@ -451,7 +452,7 @@ draw_on_strut(int v0, int v1, int color) void sweep_plot(int32_t freq, int first) { - float value = 10 - log10f(gamma_real*gamma_real + gamma_imag*gamma_imag); + float value = 11 - log10f(measured[0]*measured[0] + measured[1]*measured[1]); int curr_x = ((float)WIDTH * (freq - fstart) / (fend - fstart)); value *= 29; diff --git a/main.c b/main.c index dab8837..9a9cc18 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,7 @@ #include #include #include +#include RTCDateTime timespec; @@ -82,6 +83,7 @@ resume_sweep(void) static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[]) { + (void)chp; (void)argc; (void)argv; pause_sweep(); @@ -89,6 +91,7 @@ static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_resume(BaseSequentialStream *chp, int argc, char *argv[]) { + (void)chp; (void)argc; (void)argv; resume_sweep(); @@ -210,8 +213,7 @@ volatile int16_t wait_count = 0; int16_t dump_selection = 0; int16_t dsp_disabled = FALSE; -int32_t gamma_real; -int32_t gamma_imag; +float measured[4]; @@ -241,14 +243,6 @@ void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n) --wait_count; } -#if 0 - if (request_calcgamma > 0) { - if (request_calcgamma == 1) - calclate_gamma(); - --request_calcgamma; - } -#endif - #if PORT_SUPPORTS_RT cnt_e = port_rt_get_counter_value(); stat.interval_cycles = cnt_s - stat.last_counter_value; @@ -304,10 +298,10 @@ static void cmd_gamma(BaseSequentialStream *chp, int argc, char *argv[]) while (wait_count) ; dsp_disabled = TRUE; - calclate_gamma(); + calclate_gamma(&measured[0]); dsp_disabled = FALSE; - chprintf(chp, "%d %d\r\n", gamma_real, gamma_imag); + chprintf(chp, "%d %d %d %d\r\n", measured[0], measured[1], measured[2], measured[3]); } @@ -318,7 +312,7 @@ int16_t sweep_points = 101; static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) { int i; - int32_t freq, cur_freq, step; + int32_t freq, step; int delay; (void)argc; (void)argv; @@ -329,7 +323,6 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) delay = set_frequency(freq); delay += 2; for (i = 0; i < sweep_points; i++) { - cur_freq = freq; freq = freq + step; wait_count = delay; while (wait_count) @@ -338,11 +331,11 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[]) __disable_irq(); delay = set_frequency(freq); palClearPad(GPIOC, GPIOC_LED); - calclate_gamma(); + calclate_gamma(&measured[0]); palSetPad(GPIOC, GPIOC_LED); //dsp_disabled = FALSE; __enable_irq(); - chprintf(chp, "%d %d %d\r\n", cur_freq, gamma_real, gamma_imag); + chprintf(chp, "%d %d %d %d\r\n", measured[0], measured[1], measured[2], measured[3]); } } @@ -361,16 +354,14 @@ void scan_lcd(void) cur_freq = freq; freq = freq + step; wait_count = delay; + tlv320aic3204_select_in3(); while (wait_count) ; palClearPad(GPIOC, GPIOC_LED); - //dsp_disabled = TRUE; __disable_irq(); delay = set_frequency(freq); - calclate_gamma(); - //dsp_disabled = FALSE; + calclate_gamma(&measured[0]); __enable_irq(); - //chprintf(chp, "%d %d %d\r\n", cur_freq, gamma_real, gamma_imag); sweep_plot(cur_freq, first); first = FALSE; palSetPad(GPIOC, GPIOC_LED); @@ -380,6 +371,7 @@ void scan_lcd(void) static void cmd_scan_lcd(BaseSequentialStream *chp, int argc, char *argv[]) { + (void)chp; (void)argc; (void)argv; pause_sweep(); @@ -424,6 +416,7 @@ static void cmd_sweep(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[]) { int i; + (void)chp; (void)argc; (void)argv; @@ -505,8 +498,8 @@ static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[]) acc0 += (p[i] - ave0)*(p[i] - ave0); acc1 += (p[i+1] - ave1)*(p[i+1] - ave1); } - stat.rms[0] = sqrt(acc0 / count); - stat.rms[1] = sqrt(acc1 / count); + stat.rms[0] = sqrtf(acc0 / count); + stat.rms[1] = sqrtf(acc1 / count); stat.ave[0] = ave0; stat.ave[1] = ave1; diff --git a/nanovna.h b/nanovna.h index f78c58a..f722fa6 100644 --- a/nanovna.h +++ b/nanovna.h @@ -37,11 +37,10 @@ extern int16_t samp_buf[]; //extern int16_t refq_buf[]; extern int16_t refiq_buf[]; -extern int32_t gamma_real; -extern int32_t gamma_imag; +extern float measured[4]; void dsp_process(int16_t *src, size_t len); -void calclate_gamma(void); +void calclate_gamma(float *gamma); int si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength); @@ -49,7 +48,7 @@ void ili9341_init(void); void ili9341_test(int mode); void sweep_plot(int32_t freq, int first); -void sweep_tail(); +void sweep_tail(void); extern const uint16_t x5x7_bits []; extern const uint32_t numfont20x24[][24]; diff --git a/si5351.c b/si5351.c index 5394a0b..503231d 100644 --- a/si5351.c +++ b/si5351.c @@ -296,8 +296,10 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength) band = 2; } +#if 0 if (current_band != band) si5351_disable_output(); +#endif switch (band) { case 0: @@ -335,11 +337,13 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength) break; } + si5351_reset_pll(); +#if 0 if (current_band != band) { - si5351_reset_pll(); si5351_enable_output(); delay += 5; } +#endif current_band = band; return delay; }