From ebec13ebdb519c08e269b8a1ff00a3d9e03452fd Mon Sep 17 00:00:00 2001 From: c vw Date: Thu, 10 Jun 2021 12:40:59 +0200 Subject: [PATCH] avoid div. by zero when calculating SWR --- transmitter.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/transmitter.c b/transmitter.c index d751cbf..6d4dcd5 100644 --- a/transmitter.c +++ b/transmitter.c @@ -570,6 +570,7 @@ static gboolean update_display(gpointer data) { fwd_average=fwd_average-fwd_cal_offset; rev_average=rev_average-fwd_cal_offset; if (rev_average < 0) rev_average=0; + if (fwd_average < 0) fwd_average=0; break; case NEW_PROTOCOL: @@ -634,6 +635,7 @@ static gboolean update_display(gpointer data) { fwd_average=fwd_average-fwd_cal_offset; rev_average=rev_average-fwd_cal_offset; if (rev_average < 0) rev_average=0; + if (fwd_average < 0) fwd_average=0; break; @@ -663,8 +665,10 @@ static gboolean update_display(gpointer data) { // tx->swr can be used in other parts of the program to // implement SWR protection etc. // The SWR is calculated from the (time-averaged) forward and reverse voltages. + // Take care that no division by zero can happen, since otherwise the moving + // exponential average cannot survive from a "nan". // - if (tx->fwd > 0.1) { + if (tx->fwd > 0.1 && fwd_average > 0.01) { // // SWR means VSWR (voltage based) but we have the forward and // reflected power, so correct for that -- 2.45.2