-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
51 lines (43 loc) · 1.15 KB
/
encoder.go
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
package viterbi
import "errors"
type Encoder struct {
GenPolys []uint
ConsLen int
NumOut int
Tailbiting bool
State uint
}
func NewEncoder(GenPolys []uint, ConsLen int, Tailbiting bool) *Encoder {
enc := new(Encoder)
enc.GenPolys = GenPolys
enc.ConsLen = ConsLen
enc.NumOut = len(GenPolys)
enc.Tailbiting = Tailbiting
return enc
}
func (enc *Encoder) Encode(input []byte) (output []byte, err error) {
if enc.Tailbiting {
tmpbuf := make([]byte, enc.ConsLen-1)
if len(input) < enc.ConsLen-1 {
err = errors.New("input too short for tailbiting encoder")
for i := range(input) {
tmpbuf[len(tmpbuf)-1-i] = input[len(input)-1-i]
}
}
for i := range(tmpbuf) {
tmpbuf[len(tmpbuf)-1-i] = input[len(input)-1-i]
}
enc.State, _ = BinToUint(tmpbuf)
}
output = make([]byte, len(input)*enc.NumOut)
for i := range(input) {
for j := 0; j < enc.NumOut; j++ {
coeffBin, _ := UintToBin(enc.GenPolys[j], uint(enc.ConsLen))
enc.State |= uint(input[i] << uint(enc.ConsLen-1))
stateBin, _ := UintToBin(enc.State, uint(enc.ConsLen))
output[i*enc.NumOut+j], _ = Correlation(stateBin, coeffBin)
}
enc.State >>= 1
}
return output, err
}