from encdec8b10b import EncDec8B10B a = b'Hello, this is a test of encoding.' coder = EncDec8B10B() encoded = coder.encode(EncDec8B10B.COMMA) + coder.encode(a) print('8b10b encoded:', repr(encoded)) decoder = EncDec8B10B() print(repr(decoder.decode(encoded))) print('test decode:', repr(decoder.decode(''))) def jm3coder(bits, lastlvl=0): '''Takes in a string of bits, and outputs a trinary level. It is guaranteed that no two outputs repeat allowing for easy clock recovery as long as the sampling rate is faster than output rate. (e.g. 30Hz (33.3ms) sampling of a 29Hz (34.5ms) signal. Note that this does not do sync, so something like an 8b10b encoder should be used on top of this, and then transmit three dummy bits at the begining as the decoder. The three will cycle through all three states help ensuring level detection An optional paramter of lastlvl can be provided, which is the previous signal level transmitted.''' r = [] for i in bits: v = int(i, 2) lastlvl = (lastlvl + 1 + v) % 3 r.append('%d' % lastlvl) return ''.join(r) encoded = jm3coder('000' + encoded) print('jm3coded:', repr(encoded)) # make a "stretched" bit string stretched = ''.join(encoded[i:i + 50] + encoded[i:i + 50][-1] for i in range(0, len(encoded), 50)) print('stretched:', repr(stretched)) def jm3decoder(trits, lastlvl=0): '''Decodes a string encoded w/ jm3coder. lastlvl should/must be provided which is the last rx'd level (it must be 0, 1 or 2). ''' lookup = { (0, 1): 0, (0, 2): 1, (1, 2): 0, (1, 0): 1, (2, 0): 0, (2, 1): 1, } r = [] for i in trits: lvl = int(i, 3) if lvl == lastlvl: continue r.append('%d' % lookup[(lastlvl, lvl)]) #r.append('%d' % ((lvl - lastlvl + 2) % 3)) lastlvl = lvl return ''.join(r) decoder = EncDec8B10B() stretched = jm3decoder(stretched) print(repr(stretched)) print(repr(decoder.decode(stretched))) print(repr(decoder.decode(''))) print('done')