-
Notifications
You must be signed in to change notification settings - Fork 1
/
convolutional_encoder.py
56 lines (46 loc) · 1.44 KB
/
convolutional_encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/python
class ConvolutionalEncoder:
def __init__(self, text):
self.current_state = '00'
#self.encode(text)
self.text = text
def encode(self):
encoded_text = ""
for c in self.text:
if self.current_state == '00':
encoded_text += self.state_00(c)
elif self.current_state == '10':
encoded_text += self.state_10(c)
elif self.current_state == '11':
encoded_text += self.state_11(c)
elif self.current_state == '01':
encoded_text += self.state_01(c)
return encoded_text
def state_00(self, input):
if input == '1':
self.current_state = '10'
return '11'
elif input == '0':
self.current_state = '00'
return '00'
def state_10(self, input):
if input == '1':
self.current_state = '11'
return '00'
elif input == '0':
self.current_state = '01'
return '11'
def state_11(self, input):
if input == '1':
self.current_state = '11'
return '10'
elif input == '0':
self.current_state = '01'
return '01'
def state_01(self, input):
if input == '1':
self.current_state = '10'
return '01'
elif input == '0':
self.current_state = '00'
return '10'