use float to calc gamma, cleanup

This commit is contained in:
TT 2016-09-28 22:48:53 +09:00
parent 90db23ffc0
commit c1decf606b
5 changed files with 35 additions and 38 deletions

20
dsp.c
View file

@ -73,7 +73,7 @@ hilbert_transform(void)
} }
} }
void calclate_gamma(void) void calclate_gamma(float *gamma)
#if 0 #if 0
{ {
__SIMD32_TYPE *r = __SIMD32_CONST(refiq_buf); __SIMD32_TYPE *r = __SIMD32_CONST(refiq_buf);
@ -103,23 +103,23 @@ void calclate_gamma(void)
int16_t *r = refiq_buf; int16_t *r = refiq_buf;
int16_t *s = samp_buf; int16_t *s = samp_buf;
int len = SAMPLE_LEN/5; int len = SAMPLE_LEN/5;
double acc_r = 0; float acc_r = 0;
double acc_i = 0; float acc_i = 0;
double acc_ref = 0; float acc_ref = 0;
int i; int i;
double rn; float rn;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
int16_t s0 = *s++; int16_t s0 = *s++;
int16_t rr = *r++; int16_t rr = *r++;
int16_t ri = *r++; int16_t ri = *r++;
acc_r += (double)(s0 * rr); acc_r += (float)(s0 * rr);
acc_i += (double)(s0 * ri); acc_i += (float)(s0 * ri);
acc_ref += (double)rr*rr + (double)ri*ri; acc_ref += (float)rr*rr + (float)ri*ri;
} }
rn = sqrtf(acc_ref / len); rn = sqrtf(acc_ref / len);
gamma_real = 16 * acc_r / rn / len; gamma[0] = 16 * acc_r / rn / len;
gamma_imag = 16 * acc_i / rn / len; gamma[1] = 16 * acc_i / rn / len;
} }
#endif #endif

View file

@ -1,3 +1,4 @@
#include <math.h>
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "nanovna.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) 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)); int curr_x = ((float)WIDTH * (freq - fstart) / (fend - fstart));
value *= 29; value *= 29;

37
main.c
View file

@ -8,6 +8,7 @@
#include <shell.h> #include <shell.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h>
RTCDateTime timespec; RTCDateTime timespec;
@ -82,6 +83,7 @@ resume_sweep(void)
static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_pause(BaseSequentialStream *chp, int argc, char *argv[])
{ {
(void)chp;
(void)argc; (void)argc;
(void)argv; (void)argv;
pause_sweep(); 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[]) static void cmd_resume(BaseSequentialStream *chp, int argc, char *argv[])
{ {
(void)chp;
(void)argc; (void)argc;
(void)argv; (void)argv;
resume_sweep(); resume_sweep();
@ -210,8 +213,7 @@ volatile int16_t wait_count = 0;
int16_t dump_selection = 0; int16_t dump_selection = 0;
int16_t dsp_disabled = FALSE; int16_t dsp_disabled = FALSE;
int32_t gamma_real; float measured[4];
int32_t gamma_imag;
@ -241,14 +243,6 @@ void i2s_end_callback(I2SDriver *i2sp, size_t offset, size_t n)
--wait_count; --wait_count;
} }
#if 0
if (request_calcgamma > 0) {
if (request_calcgamma == 1)
calclate_gamma();
--request_calcgamma;
}
#endif
#if PORT_SUPPORTS_RT #if PORT_SUPPORTS_RT
cnt_e = port_rt_get_counter_value(); cnt_e = port_rt_get_counter_value();
stat.interval_cycles = cnt_s - stat.last_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) while (wait_count)
; ;
dsp_disabled = TRUE; dsp_disabled = TRUE;
calclate_gamma(); calclate_gamma(&measured[0]);
dsp_disabled = FALSE; 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[]) static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[])
{ {
int i; int i;
int32_t freq, cur_freq, step; int32_t freq, step;
int delay; int delay;
(void)argc; (void)argc;
(void)argv; (void)argv;
@ -329,7 +323,6 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[])
delay = set_frequency(freq); delay = set_frequency(freq);
delay += 2; delay += 2;
for (i = 0; i < sweep_points; i++) { for (i = 0; i < sweep_points; i++) {
cur_freq = freq;
freq = freq + step; freq = freq + step;
wait_count = delay; wait_count = delay;
while (wait_count) while (wait_count)
@ -338,11 +331,11 @@ static void cmd_scan(BaseSequentialStream *chp, int argc, char *argv[])
__disable_irq(); __disable_irq();
delay = set_frequency(freq); delay = set_frequency(freq);
palClearPad(GPIOC, GPIOC_LED); palClearPad(GPIOC, GPIOC_LED);
calclate_gamma(); calclate_gamma(&measured[0]);
palSetPad(GPIOC, GPIOC_LED); palSetPad(GPIOC, GPIOC_LED);
//dsp_disabled = FALSE; //dsp_disabled = FALSE;
__enable_irq(); __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; cur_freq = freq;
freq = freq + step; freq = freq + step;
wait_count = delay; wait_count = delay;
tlv320aic3204_select_in3();
while (wait_count) while (wait_count)
; ;
palClearPad(GPIOC, GPIOC_LED); palClearPad(GPIOC, GPIOC_LED);
//dsp_disabled = TRUE;
__disable_irq(); __disable_irq();
delay = set_frequency(freq); delay = set_frequency(freq);
calclate_gamma(); calclate_gamma(&measured[0]);
//dsp_disabled = FALSE;
__enable_irq(); __enable_irq();
//chprintf(chp, "%d %d %d\r\n", cur_freq, gamma_real, gamma_imag);
sweep_plot(cur_freq, first); sweep_plot(cur_freq, first);
first = FALSE; first = FALSE;
palSetPad(GPIOC, GPIOC_LED); palSetPad(GPIOC, GPIOC_LED);
@ -380,6 +371,7 @@ void scan_lcd(void)
static void cmd_scan_lcd(BaseSequentialStream *chp, int argc, char *argv[]) static void cmd_scan_lcd(BaseSequentialStream *chp, int argc, char *argv[])
{ {
(void)chp;
(void)argc; (void)argc;
(void)argv; (void)argv;
pause_sweep(); 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[]) static void cmd_test(BaseSequentialStream *chp, int argc, char *argv[])
{ {
int i; int i;
(void)chp;
(void)argc; (void)argc;
(void)argv; (void)argv;
@ -505,8 +498,8 @@ static void cmd_stat(BaseSequentialStream *chp, int argc, char *argv[])
acc0 += (p[i] - ave0)*(p[i] - ave0); acc0 += (p[i] - ave0)*(p[i] - ave0);
acc1 += (p[i+1] - ave1)*(p[i+1] - ave1); acc1 += (p[i+1] - ave1)*(p[i+1] - ave1);
} }
stat.rms[0] = sqrt(acc0 / count); stat.rms[0] = sqrtf(acc0 / count);
stat.rms[1] = sqrt(acc1 / count); stat.rms[1] = sqrtf(acc1 / count);
stat.ave[0] = ave0; stat.ave[0] = ave0;
stat.ave[1] = ave1; stat.ave[1] = ave1;

View file

@ -37,11 +37,10 @@ extern int16_t samp_buf[];
//extern int16_t refq_buf[]; //extern int16_t refq_buf[];
extern int16_t refiq_buf[]; extern int16_t refiq_buf[];
extern int32_t gamma_real; extern float measured[4];
extern int32_t gamma_imag;
void dsp_process(int16_t *src, size_t len); 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); 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 ili9341_test(int mode);
void sweep_plot(int32_t freq, int first); void sweep_plot(int32_t freq, int first);
void sweep_tail(); void sweep_tail(void);
extern const uint16_t x5x7_bits []; extern const uint16_t x5x7_bits [];
extern const uint32_t numfont20x24[][24]; extern const uint32_t numfont20x24[][24];

View file

@ -296,8 +296,10 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength)
band = 2; band = 2;
} }
#if 0
if (current_band != band) if (current_band != band)
si5351_disable_output(); si5351_disable_output();
#endif
switch (band) { switch (band) {
case 0: case 0:
@ -335,11 +337,13 @@ si5351_set_frequency_with_offset(int freq, int offset, uint8_t drive_strength)
break; break;
} }
if (current_band != band) {
si5351_reset_pll(); si5351_reset_pll();
#if 0
if (current_band != band) {
si5351_enable_output(); si5351_enable_output();
delay += 5; delay += 5;
} }
#endif
current_band = band; current_band = band;
return delay; return delay;
} }