mayeranalytics / pySX127x

This is a python interface to the Semtech SX127x, HopeRF RFM9x, Microchip RN2483 long range, low power transceiver families.
GNU Affero General Public License v3.0
171 stars 116 forks source link

Calculating SNR #34

Closed niclas0219 closed 4 years ago

niclas0219 commented 4 years ago

I am wondering if the calculation of the SNR is correct. The datasheet (sx1278) says the following:

Estimation of SNR on last packet received.In two’s compliment format multiplied by 4 SNR dB = PacketSnr [twos complement] / 4

This is achieved with the following code: def get_pkt_snr_value(self): v = self.spi.xfer([REG.LORA.PKT_SNR_VALUE, 0])[1] return float(256-v) / 4.

This is a bit over my head but when i look at the RAW value that i get from the register i see "0b101100" = decimal 44. 256 - 44 = 212 212 / 4 = 53.

What does the 256 - v do?

niclas0219 commented 4 years ago

Found some code on this site: [https://blog.michaelyin.info/convert-8bit-byte-to-signed-int/]

if v > 127: return (256-v) * (-1) else: return v

This would seem to give a better result considering the SNR in the SX1278 register is not always negative.

mayeranalytics commented 4 years ago

You are absolutley right, the if v>127 ... is missing.