diff --git a/libs/spandsp/src/lpc10_encdecs.h b/libs/spandsp/src/lpc10_encdecs.h index 621b15a83a..05dd87f118 100644 --- a/libs/spandsp/src/lpc10_encdecs.h +++ b/libs/spandsp/src/lpc10_encdecs.h @@ -78,7 +78,7 @@ static __inline__ int32_t pow_ii(int32_t x, int32_t n) if (n == 0 || x == 1) return 1; if (x != -1) - return (x == 0) ? 1/x : 0; + return (x != 0) ? 1/x : 0; n = -n; } u = n; diff --git a/libs/spandsp/src/v17rx.c b/libs/spandsp/src/v17rx.c index 4b61af1409..6372c5bb58 100644 --- a/libs/spandsp/src/v17rx.c +++ b/libs/spandsp/src/v17rx.c @@ -1252,6 +1252,7 @@ SPAN_DECLARE_NONSTD(int) v17_rx(v17_rx_state_t *s, const int16_t amp[], int len) complexf_t sample; float v; #endif + int32_t root_power; int32_t power; for (i = 0; i < len; i++) @@ -1309,11 +1310,15 @@ SPAN_DECLARE_NONSTD(int) v17_rx(v17_rx_state_t *s, const int16_t amp[], int len) { /* Only AGC until we have locked down the setting. */ if (s->agc_scaling_save == FP_SCALE(0.0f)) + { + if ((root_power = fixed_sqrt32(power)) == 0) + root_power = 1; #if defined(SPANDSP_USE_FIXED_POINTx) - s->agc_scaling = saturate16(((int32_t) (FP_SCALE(2.17f)*1024.0f))/fixed_sqrt32(power)); + s->agc_scaling = saturate16(((int32_t) (FP_SCALE(2.17f)*1024.0f))/root_power); #else - s->agc_scaling = (FP_SCALE(2.17f)/RX_PULSESHAPER_GAIN)/sqrtf(power); + s->agc_scaling = (FP_SCALE(2.17f)/RX_PULSESHAPER_GAIN)/root_power; #endif + } /* Pulse shape while still at the carrier frequency, using a quadrature pair of filters. This results in a properly bandpass filtered complex signal, which can be brought directly to baseband by complex mixing. diff --git a/libs/spandsp/src/v22bis_rx.c b/libs/spandsp/src/v22bis_rx.c index aba3dd2980..64d2148013 100644 --- a/libs/spandsp/src/v22bis_rx.c +++ b/libs/spandsp/src/v22bis_rx.c @@ -799,6 +799,7 @@ SPAN_DECLARE_NONSTD(int) v22bis_rx(v22bis_state_t *s, const int16_t amp[], int l float ii; float qq; #endif + int32_t root_power; int32_t power; for (i = 0; i < len; i++) @@ -860,10 +861,12 @@ SPAN_DECLARE_NONSTD(int) v22bis_rx(v22bis_state_t *s, const int16_t amp[], int l if (s->rx.training == V22BIS_RX_TRAINING_STAGE_SYMBOL_ACQUISITION) { /* Only AGC during the initial symbol acquisition, and then lock the gain. */ + if ((root_power = fixed_sqrt32(power)) == 0) + root_power = 1; #if defined(SPANDSP_USE_FIXED_POINT) - s->rx.agc_scaling = saturate16(((int32_t) (FP_SCALE(0.18f)*FP_SCALE(3.60f)))/fixed_sqrt32(power)); + s->rx.agc_scaling = saturate16(((int32_t) (FP_SCALE(0.18f)*FP_SCALE(3.60f)))/root_power); #else - s->rx.agc_scaling = FP_SCALE(0.18f)*FP_SCALE(3.60f)/fixed_sqrt32(power); + s->rx.agc_scaling = FP_SCALE(0.18f)*FP_SCALE(3.60f)/root_power; #endif } /* Pulse shape while still at the carrier frequency, using a quadrature diff --git a/libs/spandsp/src/v27ter_rx.c b/libs/spandsp/src/v27ter_rx.c index 3451d66026..d00c779c7f 100644 --- a/libs/spandsp/src/v27ter_rx.c +++ b/libs/spandsp/src/v27ter_rx.c @@ -834,6 +834,7 @@ SPAN_DECLARE_NONSTD(int) v27ter_rx(v27ter_rx_state_t *s, const int16_t amp[], in complexf_t sample; float v; #endif + int32_t root_power; int32_t power; if (s->bit_rate == 4800) @@ -858,10 +859,12 @@ SPAN_DECLARE_NONSTD(int) v27ter_rx(v27ter_rx_state_t *s, const int16_t amp[], in if (s->training_stage == TRAINING_STAGE_SYMBOL_ACQUISITION) { /* Only AGC during the initial training */ + if ((root_power = fixed_sqrt32(power)) == 0) + root_power = 1; #if defined(SPANDSP_USE_FIXED_POINT) - s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.414f)*1024.0f))/fixed_sqrt32(power)); + s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.414f)*1024.0f))/root_power); #else - s->agc_scaling = (FP_SCALE(1.414f)/RX_PULSESHAPER_4800_GAIN)/fixed_sqrt32(power); + s->agc_scaling = (FP_SCALE(1.414f)/RX_PULSESHAPER_4800_GAIN)/root_power; #endif } /* Pulse shape while still at the carrier frequency, using a quadrature @@ -920,10 +923,12 @@ SPAN_DECLARE_NONSTD(int) v27ter_rx(v27ter_rx_state_t *s, const int16_t amp[], in if (s->training_stage == TRAINING_STAGE_SYMBOL_ACQUISITION) { /* Only AGC during the initial training */ + if ((root_power = fixed_sqrt32(power)) == 0) + root_power = 1; #if defined(SPANDSP_USE_FIXED_POINT) - s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.414f)*1024.0f))/fixed_sqrt32(power)); + s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.414f)*1024.0f))/root_power); #else - s->agc_scaling = (FP_SCALE(1.414f)/RX_PULSESHAPER_2400_GAIN)/fixed_sqrt32(power); + s->agc_scaling = (FP_SCALE(1.414f)/RX_PULSESHAPER_2400_GAIN)/root_power; #endif } /* Pulse shape while still at the carrier frequency, using a quadrature diff --git a/libs/spandsp/src/v29rx.c b/libs/spandsp/src/v29rx.c index 8431ffbfd7..9f37644de1 100644 --- a/libs/spandsp/src/v29rx.c +++ b/libs/spandsp/src/v29rx.c @@ -908,6 +908,7 @@ SPAN_DECLARE_NONSTD(int) v29_rx(v29_rx_state_t *s, const int16_t amp[], int len) complexf_t sample; float v; #endif + int32_t root_power; int32_t power; for (i = 0; i < len; i++) @@ -970,10 +971,12 @@ SPAN_DECLARE_NONSTD(int) v29_rx(v29_rx_state_t *s, const int16_t amp[], int len) /* Only AGC until we have locked down the setting. */ if (s->agc_scaling_save == FP_SCALE(0.0f)) { + if ((root_power = fixed_sqrt32(power)) == 0) + root_power = 1; #if defined(SPANDSP_USE_FIXED_POINT) - s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.25f)*1024.0f))/fixed_sqrt32(power)); + s->agc_scaling = saturate16(((int32_t) (FP_SCALE(1.25f)*1024.0f))/root_power); #else - s->agc_scaling = (FP_SCALE(1.25f)/RX_PULSESHAPER_GAIN)/fixed_sqrt32(power); + s->agc_scaling = (FP_SCALE(1.25f)/RX_PULSESHAPER_GAIN)/root_power; #endif } /* Pulse shape while still at the carrier frequency, using a quadrature