Created
January 30, 2013 15:58
-
-
Save matael/4674230 to your computer and use it in GitHub Desktop.
HDBn Decoder + test program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python2.7 | |
| #-*- coding: utf8 -*- | |
| from __future__ import print_function | |
| class HDBn: | |
| """ | |
| Classe d'implémentation d'HDBn | |
| param constr. : | |
| n -> nombre de bits avant un viol | |
| """ | |
| def __init__(self, n=3): | |
| """ Constructeur """ | |
| # nb bit avant viol | |
| self.n = n | |
| # init vals | |
| self.prev = -1 | |
| self.viol = -1 | |
| self.P = [] | |
| self.N = [] | |
| def out(self, b): | |
| """ Affichage """ | |
| if b == -1: | |
| P.append(0) | |
| N.append(1) | |
| elif b == +1: | |
| P.append(1) | |
| N.append(0) | |
| elif b == 0: | |
| P.append(0) | |
| N.append(0) | |
| def whoami(self): | |
| """ Who Am I ? """ | |
| print("i'm HDB{} decoder \\o/".format(self.n)) | |
| def encode(self, data=[]): | |
| """ Decoder """ | |
| print("D P N") | |
| i = 0 | |
| while i<len(data): | |
| if data[i] == 1: | |
| # envoi en inversant la polarité | |
| self.out(-self.prev) | |
| self.prev = -self.prev | |
| i += 1 | |
| else: # on a donc un 0 | |
| # si il ne reste pas assez de bit pour qu'un viol arrive, on envoie direct | |
| if i >= len(data)-(self.n+1): | |
| self.out(data[i]) | |
| i += 1 | |
| continue | |
| if data[i+1]: # si ce n'est pas un zéro ensuite on envoie | |
| self.out(0) | |
| i += 1 | |
| else: | |
| # sinon : on enregistre la séquence de 0 et on envoie en conséquence | |
| seq = [] | |
| for j in range(self.n+1): | |
| if data[i+j] == 0: | |
| seq.append(data[i+j]) | |
| else: break | |
| if len(seq) >= self.n+1: # on a assez de 0 | |
| # on traite le viol | |
| if self.viol == -1 and self.prev == -1: | |
| seq[0] = +1 | |
| seq[-1] = +1 | |
| elif self.viol == -1 and self.prev == 1: | |
| seq[-1] = +1 | |
| elif self.viol == 1 and self.prev == -1: | |
| seq[-1] = -1 | |
| elif self.viol == 1 and self.prev == 1: | |
| seq[0] = -1 | |
| seq[-1] = -1 | |
| self.prev = seq[-1] | |
| self.viol = seq[-1] | |
| for j in seq: self.out(j) | |
| i += len(seq) | |
| return (P,N) | |
| def self.decode(P,N): | |
| """ Decoder """ | |
| # re init | |
| self.viol = -1 | |
| self.prev = -1 | |
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| #-*- coding: utf8 -*- | |
| from hdbn import HDBn | |
| def main(): | |
| data = "10010100000011000011100" | |
| data = [int(_) for _ in data] | |
| coder = HDBn(2) | |
| coder.whoami() | |
| coder.encode(data) | |
| if __name__=='__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment