viterbi

package module
v0.0.0-...-38076f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2023 License: AGPL-3.0 Imports: 3 Imported by: 3

README

logo

Convolutional Encoder and Viterbi Decoder

This package implements a convolutional encoder and a Viterbi decoder. It can be used as a library or as a command line tool.

Using as a library

package main

import (
	"fmt"
	"github.com/8ff/viterbi"
)

func main() {
	// Initialize a codec.
	codec, err := viterbi.Init(viterbi.Input{Constraint: 7, Polynomials: []int{91, 109, 121}, ReversePolynomials: false})
	if err != nil {
		panic(err)
	}

	/**** Encode Bytes ****/
	input := []byte("Hello, world!")

	// Encode a message.
	encoded := codec.EncodeBytes(input)

	// Decode the message.
	decoded := codec.DecodeBytes(encoded)

	// Print input and decoded message.
	fmt.Printf("InputBytes:   %s\n", input)
	fmt.Printf("DecodedBytes: %s\n", decoded)

	/**** Encode string of bits ****/
	inputBits := "101010"

	// Encode a message.
	encodedBits := codec.Encode(inputBits)

	// Decode the message.
	decodedBits := codec.Decode(encodedBits)

	// Print input and decoded message.
	fmt.Printf("InputBits:   %s\n", inputBits)
	fmt.Printf("DecodedBits: %s\n", decodedBits)
}

Using as command line tool

cd cmd/viterbiCli

# Encode
echo 101010 | go run viterbiCli.go encode 3 5 7

# Decode
echo 10000010 | go run viterbiCli.go decode 3 5 7

# Encode/Decode
echo 101010 | go run viterbiCli.go encode 3 5 7 | go run viterbiCli.go decode 3 5 7

Error Handling

Inputs are validated, and proper error messages will be displayed.

  • The constraint should be greater than 0.
  • A generator polynomial should be greater than 0 and less than 1 << constraint.
  • The received bit sequence should be of length N * where N is an integer. Otherwise some bits must be missing during transmission. We will fill in appropriate number of trailing zeros.

Dependencies

This code contains no external dependencies.

Documentation

Index

Constants

View Source
const MaxInt = 1<<(32-1) - 1

Variables

View Source
var FLAGS_encode bool
View Source
var FLAGS_reverse_polynomials bool

Functions

func BitsToBytes

func BitsToBytes(bits string) []byte

Function that takes in a string of bits and converts it to a []byte.

func BitsToInts

func BitsToInts(bits string) []int

Function that converts a string of bits to []int

func BytesToBits

func BytesToBits(bytes []byte) string

Function that takes in []byte and converts it to a string of bits.

func HammingDistance

func HammingDistance(x string, y string) int

func IntsToBits

func IntsToBits(ints []int) string

Function that converts a []int to a string of bits.

func ParseInt

func ParseInt(s string) int

func ReverseBits

func ReverseBits(num_bits int, input int) int

Types

type Input

type Input struct {
	Constraint         int
	Polynomials        []int
	ReversePolynomials bool
}

type Trellis

type Trellis [][]int

type ViterbiCodec

type ViterbiCodec struct {
	// contains filtered or unexported fields
}

func Init

func Init(input Input) (*ViterbiCodec, error)

func (*ViterbiCodec) BranchMetric

func (v *ViterbiCodec) BranchMetric(bits string, source_state int, target_state int) int

Return the branch metric for the given source and target states.

func (*ViterbiCodec) Decode

func (v *ViterbiCodec) Decode(bits string) string

Decode a string of bits using the Viterbi algorithm.

func (*ViterbiCodec) DecodeBytes

func (v *ViterbiCodec) DecodeBytes(bits string) []byte

Decode bytes using BitsToBytes and run codec.Decode.

func (*ViterbiCodec) Encode

func (v *ViterbiCodec) Encode(bits string) string

Encode the given message bits.

func (*ViterbiCodec) EncodeBytes

func (v *ViterbiCodec) EncodeBytes(bytes []byte) string

Encode bytes using BytesToBits and run codec.Encode.

func (*ViterbiCodec) InitializeOutputs

func (v *ViterbiCodec) InitializeOutputs()

func (*ViterbiCodec) NextState

func (v *ViterbiCodec) NextState(current_state int, input int) int

func (*ViterbiCodec) Output

func (v *ViterbiCodec) Output(current_state int, input int) string

func (*ViterbiCodec) PathMetric

func (v *ViterbiCodec) PathMetric(bits string, prev_path_metrics []int, state int) [2]int

Return the path metric and the source state for the given target state.

func (*ViterbiCodec) UpdatePathMetrics

func (v *ViterbiCodec) UpdatePathMetrics(bits string, path_metrics []int, trellis Trellis) ([]int, []int)

Update the path metrics and trellis for the next bit.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL