From aca8f2dbccd3f21399962fa02fcc3e3b7ce93db5 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Wed, 7 Oct 2020 13:20:56 +0200 Subject: [PATCH 1/3] THD measurement added --- nanovna.h | 5 +++-- plot.c | 28 ++++++++++++++++++++++++++++ ui_sa.c | 5 ++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/nanovna.h b/nanovna.h index f320635..1bea3ad 100644 --- a/nanovna.h +++ b/nanovna.h @@ -39,7 +39,7 @@ //#define __ULTRA__ // Add harmonics mode on low input. //#define __ULTRA_SA__ // Adds ADF4351 control for extra high 1st IF stage #define __SPUR__ // Does spur reduction by shifting IF -#define __USE_SERIAL_CONSOLE__ // Enable serial I/O connection (need enable HAL_USE_SERIAL as TRUE in halconf.h) +//#define __USE_SERIAL_CONSOLE__ // Enable serial I/O connection (need enable HAL_USE_SERIAL as TRUE in halconf.h) /* * main.c */ @@ -235,6 +235,7 @@ extern int32_t frequencyExtra; void set_10mhz(uint32_t); void set_modulation(int); void set_modulation_frequency(int); +int search_maximum(int m, int center, int span); //extern int setting.modulation; void set_measurement(int); // extern int settingSpeed; @@ -998,7 +999,7 @@ uint32_t calc_min_sweep_time_us(void); pureRSSI_t perform(bool b, int i, uint32_t f, int e); enum { - M_OFF, M_IMD, M_OIP3, M_PHASE_NOISE, M_STOP_BAND, M_PASS_BAND, M_LINEARITY, M_AM, M_FM + M_OFF, M_IMD, M_OIP3, M_PHASE_NOISE, M_STOP_BAND, M_PASS_BAND, M_LINEARITY, M_AM, M_FM, M_THD }; enum { diff --git a/plot.c b/plot.c index 0538340..507b8de 100644 --- a/plot.c +++ b/plot.c @@ -2081,7 +2081,35 @@ static void cell_draw_marker_info(int x0, int y0) active++; } } + if (setting.measurement == M_THD && active >= 1) + active = 2; for (int i = 0; i < MARKER_COUNT; i++) { + if (i >= 2 && setting.measurement == M_THD) { + if (i == 2) { + int old_unit = setting.unit; + setting.unit = U_WATT; + float p = value((actual_t[markers[0].index])); + int j = 2; + uint32_t f = markers[0].frequency; + float h = 0.0; + while (f * j < frequencies[sweep_points-1]) { + if (search_maximum(1, f*j, 4*j) ) // use marker 1 for searching harmonics + h += value((actual_t[markers[1].index])); + j++; + } + float thd = 100.0 * sqrt(h/p); + setting.unit = old_unit; + + plot_printf(buf, sizeof buf, "THD: %4.1f%%", thd); + j = 1; + int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(16) - y0; + cell_drawstring_7x13(buf, xpos, ypos); +// cell_drawstring(buf, xpos, ypos); + break; + } + break; + } else if (i >= 2 && setting.measurement == M_OIP3 && markers[2].enabled && markers[3].enabled) { float il = value((actual_t[markers[2].index])); float ir = value((actual_t[markers[3].index])); diff --git a/ui_sa.c b/ui_sa.c index 2f4ebc8..0808735 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -851,7 +851,6 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) break; case M_LINEARITY: set_measurement(M_LINEARITY); - ui_mode_normal(); break; case M_AM: // OIP3 reset_settings(setting.mode); @@ -882,6 +881,9 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) set_sweep_frequency(ST_SPAN, uistat.value*30); set_measurement(M_FM); break; + case M_THD: + set_measurement(M_THD); + break; } #endif // selection = -1; @@ -1682,6 +1684,7 @@ static const menuitem_t menu_measure2[] = { // { MT_ADV_CALLBACK | MT_LOW, M_LINEARITY, "LINEAR", menu_measure_acb}, { MT_ADV_CALLBACK, M_AM, "AM", menu_measure_acb}, { MT_ADV_CALLBACK, M_FM, "FM", menu_measure_acb}, + { MT_ADV_CALLBACK, M_THD, "THD", menu_measure_acb}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel }; From 2b79faaeabc185d8e51d4afb624cfac8e4060b39 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Wed, 7 Oct 2020 13:32:32 +0200 Subject: [PATCH 2/3] Protect THD --- plot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plot.c b/plot.c index 507b8de..156fe69 100644 --- a/plot.c +++ b/plot.c @@ -2085,7 +2085,7 @@ static void cell_draw_marker_info(int x0, int y0) active = 2; for (int i = 0; i < MARKER_COUNT; i++) { if (i >= 2 && setting.measurement == M_THD) { - if (i == 2) { + if (i == 2 && (markers[0].index << 5) > sweep_points ) { int old_unit = setting.unit; setting.unit = U_WATT; float p = value((actual_t[markers[0].index])); From 47d41050ca7e11f3d015b1b069044abaeaaeeb75 Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Fri, 9 Oct 2020 11:11:25 +0200 Subject: [PATCH 3/3] Change PASS BAND measurement not to ask for frequency --- plot.c | 14 ++++++++++++++ sa_core.c | 4 ++-- ui_sa.c | 20 +++++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/plot.c b/plot.c index 156fe69..a10e2cd 100644 --- a/plot.c +++ b/plot.c @@ -2084,6 +2084,20 @@ static void cell_draw_marker_info(int x0, int y0) if (setting.measurement == M_THD && active >= 1) active = 2; for (int i = 0; i < MARKER_COUNT; i++) { + if (i == 3 && setting.measurement == M_PASS_BAND) { + uint32_t f; + if (markers[2].frequency>markers[1].frequency) + f = markers[2].frequency-markers[1].frequency; + else + f = markers[1].frequency-markers[2].frequency; + plot_printf(buf, sizeof buf, "WIDTH: %8.3qHz", f); + j = 3; + int xpos = 1 + (j%2)*(WIDTH/2) + CELLOFFSETX - x0; + int ypos = 1 + (j/2)*(16) - y0; + cell_drawstring_7x13(buf, xpos, ypos); +// cell_drawstring(buf, xpos, ypos); + break; + } else if (i >= 2 && setting.measurement == M_THD) { if (i == 2 && (markers[0].index << 5) > sweep_points ) { int old_unit = setting.unit; diff --git a/sa_core.c b/sa_core.c index 23fb449..9710d84 100644 --- a/sa_core.c +++ b/sa_core.c @@ -2376,14 +2376,14 @@ sweep_again: // stay in sweep loop when output mo } else if (setting.measurement == M_PASS_BAND && markers[0].index > 10) { // ----------------Pass band measurement int t = markers[0].index; float v = actual_t[t]; - while (t > 0 && actual_t[t] > v - 6.0) // Find left -3dB point + while (t > 0 && actual_t[t] > v - 4.0) // Find left -3dB point t --; if (t > 0) { markers[1].index = t; markers[1].frequency = frequencies[t]; } t = markers[0].index; - while (t < setting._sweep_points - 1 && actual_t[t] > v - 6.0) // find right -3dB point + while (t < setting._sweep_points - 1 && actual_t[t] > v - 4.0) // find right -3dB point t ++; if (t < setting._sweep_points - 1 ) { markers[2].index = t; diff --git a/ui_sa.c b/ui_sa.c index 0808735..d62fc5e 100644 --- a/ui_sa.c +++ b/ui_sa.c @@ -833,18 +833,20 @@ static UI_FUNCTION_ADV_CALLBACK(menu_measure_acb) break; case M_PASS_BAND: // STop band measurement - reset_settings(setting.mode); +// reset_settings(setting.mode); + markers[0].enabled = M_ENABLED; + markers[0].mtype = M_REFERENCE | M_TRACKING; markers[1].enabled = M_ENABLED; markers[1].mtype = M_DELTA; markers[2].enabled = M_ENABLED; markers[2].mtype = M_DELTA; - kp_help_text = "Frequency of signal"; - ui_mode_keypad(KM_CENTER); - ui_process_keypad(); - kp_help_text = "Width of signal"; - ui_mode_keypad(KM_SPAN); - ui_process_keypad(); - set_sweep_frequency(ST_SPAN, uistat.value*2); +// kp_help_text = "Frequency of signal"; +// ui_mode_keypad(KM_CENTER); +// ui_process_keypad(); +// kp_help_text = "Width of signal"; +// ui_mode_keypad(KM_SPAN); +// ui_process_keypad(); +// set_sweep_frequency(ST_SPAN, uistat.value*2); set_measurement(M_PASS_BAND); // SetAverage(4); @@ -1695,7 +1697,7 @@ static const menuitem_t menu_measure[] = { { MT_ADV_CALLBACK, M_OIP3, "OIP3", menu_measure_acb}, { MT_ADV_CALLBACK, M_PHASE_NOISE,"PHASE\nNOISE", menu_measure_acb}, { MT_ADV_CALLBACK, M_STOP_BAND, "SNR", menu_measure_acb}, - { MT_ADV_CALLBACK, M_PASS_BAND, "-6dB\nWIDTH", menu_measure_acb}, + { MT_ADV_CALLBACK, M_PASS_BAND, "-3dB\nWIDTH", menu_measure_acb}, { MT_SUBMENU, 0, S_RARROW" MORE", menu_measure2}, { MT_CANCEL, 0, S_LARROW" BACK", NULL }, { MT_NONE, 0, NULL, NULL } // sentinel