From 342c5ff6694d4016530cee1da127adb1e2a795e5 Mon Sep 17 00:00:00 2001 From: TT Date: Sun, 8 Sep 2019 18:44:33 +0900 Subject: [PATCH] add trace format of group delay --- main.c | 2 +- plot.c | 37 ++++++++++++++++++++++++++++++++++--- ui.c | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 7a933b3..1bb87a0 100644 --- a/main.c +++ b/main.c @@ -1438,7 +1438,7 @@ const struct { } trace_info[] = { { "LOGMAG", 7, 10 }, { "PHASE", 4, 90 }, - { "DELAY", 4, 1 }, + { "DELAY", 4, 1e-9 }, { "SMITH", 0, 1 }, { "POLAR", 0, 1 }, { "LINEAR", 0, 0.125 }, diff --git a/plot.c b/plot.c index 234ca94..dff5354 100644 --- a/plot.c +++ b/plot.c @@ -439,6 +439,24 @@ float phase(float *v) return 2 * atan2f(v[1], v[0]) / M_PI * 90; } +/* + * calculate groupdelay + */ +float groupdelay(float *v, float deltaf) +{ + float *w = &v[2]; // point to next coeff +#if 1 + // w = w[0]/w[1] + // v = v[0]/v[1] + // atan(w)-atan(v) = atan((w-v)/(1+wv)) + float r = w[0]*v[1] - w[1]*v[0]; + float i = w[0]*v[0] + w[1]*v[1]; + return atan2f(r, i) / (2 * M_PI * deltaf); +#else + return (atan2f(w[0], w[1]) - atan2f(v[0], v[1])) / (2 * M_PI * deltaf); +#endif +} + /* * calculate abs(gamma) */ @@ -502,6 +520,12 @@ trace_into_index(int x, int t, int i, float coeff[2]) case TRC_PHASE: v = refpos - phase(coeff) * scale; break; + case TRC_DELAY: + if (i != 100) { + float deltaf = frequencies[i+1] - frequencies[i]; + v = refpos - groupdelay(coeff, deltaf) * scale; + } + break; case TRC_LINEAR: v = refpos + linear(coeff) * scale; break; @@ -631,7 +655,7 @@ gamma2reactance(char *buf, int len, const float coeff[2]) } static void -trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequency) +trace_get_value_string(int t, char *buf, int len, float coeff[2], int i) { float v; switch (trace[t].type) { @@ -646,6 +670,13 @@ trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequ v = phase(coeff); chsnprintf(buf, len, "%.2f" S_DEGREE, v); break; + case TRC_DELAY: + { + float deltaf = frequencies[i+1] - frequencies[i]; + v = groupdelay(coeff, deltaf); + string_value_with_prefix(buf, len, v, 's'); + } + break; case TRC_LINEAR: v = linear(coeff); chsnprintf(buf, len, "%.2f", v); @@ -655,7 +686,7 @@ trace_get_value_string(int t, char *buf, int len, float coeff[2], uint32_t frequ chsnprintf(buf, len, "%.2f", v); break; case TRC_SMITH: - gamma2imp(buf, len, coeff, frequency); + gamma2imp(buf, len, coeff, frequencies[i]); break; case TRC_REAL: chsnprintf(buf, len, "%.2f", coeff[0]); @@ -1379,7 +1410,7 @@ cell_draw_marker_info(int m, int n, int w, int h) trace_get_info(t, buf, sizeof buf); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); xpos += 64; - trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel][idx], frequencies[idx]); + trace_get_value_string(t, buf, sizeof buf, measured[trace[t].channel][idx], idx); cell_drawstring_5x7(w, h, buf, xpos, ypos, config.trace_color[t]); j++; } diff --git a/ui.c b/ui.c index 3cbac63..d369429 100644 --- a/ui.c +++ b/ui.c @@ -1221,7 +1221,7 @@ const keypads_t * const keypads_mode_tbl[] = { keypads_freq, // span keypads_freq, // cw freq keypads_scale, // scale - keypads_scale, // respos + keypads_scale, // refpos keypads_time, // electrical delay keypads_scale // velocity factor };