update crc

This commit is contained in:
Junzi Sun
2018-01-16 15:37:04 +01:00
parent 8a9045e730
commit 6baa218596
2 changed files with 28 additions and 17 deletions
+1
View File
@@ -33,6 +33,7 @@ New features in v2.0
- ADS-B and EHS data streaming - ADS-B and EHS data streaming
- Active aircraft viewing (in terminal) - Active aircraft viewing (in terminal)
- More advanced BDS identification in Enhanced Mode-S - More advanced BDS identification in Enhanced Mode-S
- Optimize decoding speed
Source code Source code
+27 -17
View File
@@ -19,17 +19,13 @@ Common functions for ADS-B and Mode-S EHS decoder
""" """
import math import numpy as np
# the polynominal generattor code for CRC
GENERATOR = "1111111111111010000001001"
def hex2bin(hexstr): def hex2bin(hexstr):
"""Convert a hexdecimal string to binary string, with zero fillings. """ """Convert a hexdecimal string to binary string, with zero fillings. """
scale = 16 num_of_bits = len(hexstr) * 4
num_of_bits = len(hexstr) * math.log(scale, 2) binstr = bin(int(hexstr, 16))[2:].zfill(int(num_of_bits))
binstr = bin(int(hexstr, scale))[2:].zfill(int(num_of_bits))
return binstr return binstr
@@ -43,6 +39,16 @@ def hex2int(hexstr):
return int(hexstr, 16) return int(hexstr, 16)
def bin2np(binstr):
"""Convert a binary string to numpy array. """
return np.fromstring(binstr, 'u1') - ord('0')
def np2bin(npbin):
"""Convert a binary numpy array to string. """
return np.array2string(npbin, separator='')[1:-1]
def df(msg): def df(msg):
"""Decode Downlink Format vaule, bits 1 to 5.""" """Decode Downlink Format vaule, bits 1 to 5."""
msgbin = hex2bin(msg) msgbin = hex2bin(msg)
@@ -59,21 +65,25 @@ def crc(msg, encode=False):
string: message checksum, or partity bits (encoder) string: message checksum, or partity bits (encoder)
""" """
msgbin = list(hex2bin(msg)) # the polynominal generattor code for CRC [1111111111111010000001001]
generator = np.array([1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1,0,0,1])
ng = len(generator)
msgnpbin = bin2np(hex2bin(msg))
if encode: if encode:
msgbin[-24:] = ['0'] * 24 msgnpbin[-24:] = [0] * 24
# loop all bits, except last 24 piraty bits # loop all bits, except last 24 piraty bits
for i in range(len(msgbin)-24): for i in range(len(msgnpbin)-24):
# if 1, perform modulo 2 multiplication, if msgnpbin[i] == 0:
if msgbin[i] == '1': continue
for j in range(len(GENERATOR)):
# modulo 2 multiplication = XOR # perform XOR, when 1
msgbin[i+j] = str((int(msgbin[i+j]) ^ int(GENERATOR[j]))) msgnpbin[i:i+ng] = np.bitwise_xor(msgnpbin[i:i+ng], generator)
# last 24 bits # last 24 bits
reminder = ''.join(msgbin[-24:]) reminder = np2bin(msgnpbin[-24:])
return reminder return reminder
@@ -84,7 +94,7 @@ def floor(x):
eg.: floor(3.6) = 3, while floor(-3.6) = -4 eg.: floor(3.6) = 3, while floor(-3.6) = -4
""" """
return int(math.floor(x)) return int(np.floor(x))
def gray2int(graystr): def gray2int(graystr):