forked from xukmin/viterbi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
*.o | ||
.DS_Store | ||
.vscode | ||
*.so | ||
viterbi_main | ||
viterbi_test |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from viterbicodec import ViterbiCodec, reverse_bits | ||
|
||
all = ["Viterbi"] | ||
|
||
|
||
def list2str(bits): | ||
return "".join(map(lambda x: "1" if x > 0 else "0", bits)) | ||
|
||
|
||
def str2list(bits): | ||
return list(map(lambda x: 1 if x == "1" else 0, bits)) | ||
|
||
|
||
class Viterbi(ViterbiCodec): | ||
def __init__(self, constraint, polynomials): | ||
for i in range(len(polynomials)): | ||
polynomials[i] = reverse_bits(constraint, polynomials[i]) | ||
self.constraint = constraint | ||
self.polynomials = polynomials | ||
ViterbiCodec.__init__(self, constraint, polynomials) | ||
|
||
def encode(self, bits): | ||
bits = list2str(bits) | ||
output = ViterbiCodec.encode(self, bits)[: -2 * (self.constraint - 1)] | ||
return str2list(output) | ||
|
||
def decode(self, bits): | ||
bits = list2str(bits) | ||
bits += "0" * (2 * (self.constraint - 1)) | ||
output = ViterbiCodec.decode(self, bits) | ||
return str2list(output) | ||
|
||
|
||
if __name__ == "__main__": | ||
bits = [1] * 100 | ||
codec = Viterbi(7, [0o133, 0o171]) | ||
a = codec.encode(bits) | ||
print(a) | ||
b = codec.decode(a) | ||
print(b) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include "viterbi.h" | ||
|
||
PYBIND11_MODULE(viterbicodec, m) { | ||
pybind11::class_<ViterbiCodec>(m, "ViterbiCodec") | ||
.def(pybind11::init<int, const std::vector<int>&>()) | ||
.def("encode", &ViterbiCodec::Encode) | ||
.def("decode", &ViterbiCodec::Decode); | ||
|
||
m.def("reverse_bits", &ReverseBits); | ||
} |