benchmarking

package
v0.0.0-...-8ff7918 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BPSKToBits

func BPSKToBits(a mat2.Vector, boundary float64) mat.SparseVector

BPSKToBits conversts a BPSK vector [-1,1] to sparse vector [0,1]. Values >= boundary will be considered a 1, otherwise a 0.

func BitsToBPSK

func BitsToBPSK(a mat.SparseVector) mat2.Vector

BitsToBPSK converts a [0,1] matrix to a [-1,1] matrix

func BitsToErased

func BitsToErased(codeword mat.SparseVector) []bec.ErasureBit

BitsToErased creates a slice of ErasureBits from the codeword passed in

func ErasedCount

func ErasedCount(base []bec.ErasureBit) (count int)

ErasedCount returns the number of erased bits

func HammingDistanceBPSK

func HammingDistanceBPSK(a, b mat2.Vector) int

HammingDistanceBPSK calculates number of bits different. Assumes >=0 is 1 and <0 is 0 If a and b are different sizes it assumes they are both aligned with the zero index (the difference is at the end)

func HammingDistanceErasuresToBits

func HammingDistanceErasuresToBits(a []bec.ErasureBit, b mat.SparseVector) int

HammingDistanceErasuresToBits calculates number of bits different. If a and b are different sizes it assumes they are both aligned with the zero index (the difference is at the end)

func RandomErase

func RandomErase(codeword []bec.ErasureBit, probabilityOfErasure float64) []bec.ErasureBit

RandomErase creates a new slice of ErasureBits with some of them set to Erased given the probabilityOfErasure

func RandomEraseCount

func RandomEraseCount(codeword []bec.ErasureBit, numberOfBitsToFlip int) []bec.ErasureBit

RandomErase creates a copy of the codeword and randomly sets numberOfBitsToFlip of them to Erased

func RandomFlipBitCount

func RandomFlipBitCount(input mat.SparseVector, numberOfBitsToFlip int) mat.SparseVector

RandomFlipBitCount randomly flips min(numberOfBitsToFlip,len(input)) number of bits.

func RandomMessage

func RandomMessage(len int) mat.SparseVector

RandomMessage creates a random message of length len.

func RandomMessageOnesCount

func RandomMessageOnesCount(len int, onesCount int) mat.SparseVector

RandomMessage creates a random message o lenght len with a hamming weight equal to onesCount

func RandomNoiseBPSK

func RandomNoiseBPSK(bpsk mat2.Vector, E_bPerN_0 float64) mat2.Vector

RandomNoiseBPSK creates a randomizes version of the bpsk vector using the E_b/N_0 passed in

Types

type BPSKChannel

type BPSKChannel func(codeword mat2.Vector) (channelInducedCodeword mat2.Vector)

type BPSKChannelCorrection

type BPSKChannelCorrection func(originalCodeword, channelInducedCodeword mat2.Vector) (fixedChannelInducedCodeword mat2.Vector)

type BPSKChannelEncoder

type BPSKChannelEncoder func(message mat.SparseVector) (codeword mat2.Vector)

specific to BPSK

type BPSKChannelMetrics

type BPSKChannelMetrics func(originalMessage mat.SparseVector, originalCodeword, fixedChannelInducedCodeword mat2.Vector) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64)

type BinaryErasureChannel

type BinaryErasureChannel func(codeword []bec.ErasureBit) (channelInducedCodeword []bec.ErasureBit)

type BinaryErasureChannelCorrection

type BinaryErasureChannelCorrection func(originalCodeword, channelInducedCodeword []bec.ErasureBit) (fixedChannelInducedCodeword []bec.ErasureBit)

type BinaryErasureChannelEncoder

type BinaryErasureChannelEncoder func(message mat.SparseVector) (codeword []bec.ErasureBit)

specific to BEC

type BinaryErasureChannelMetrics

type BinaryErasureChannelMetrics func(originalMessage mat.SparseVector, originalCodeword, fixedChannelInducedCodeword []bec.ErasureBit) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64)

type BinaryMessageConstructor

type BinaryMessageConstructor func(trial int) (message mat.SparseVector)

type BinarySymmetricChannel

type BinarySymmetricChannel func(codeword mat.SparseVector) (channelInducedCodeword mat.SparseVector)

type BinarySymmetricChannelCorrection

type BinarySymmetricChannelCorrection func(originalCodeword, channelInducedCodeword mat.SparseVector) (fixedChannelInducedCodeword mat.SparseVector)

type BinarySymmetricChannelEncoder

type BinarySymmetricChannelEncoder func(message mat.SparseVector) (codeword mat.SparseVector)

specfic to BSC

type BinarySymmetricChannelMetrics

type BinarySymmetricChannelMetrics func(originalMessage, originalCodeword, fixedChannelInducedCodeword mat.SparseVector) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64)

type Checkpoints

type Checkpoints func(updatedStats Stats)

type Stats

type Stats struct {
	ChannelCodewordError avgstd.AvgStd // probability of a bit error after channel errors are fixed
	ChannelMessageError  avgstd.AvgStd // probability of a bit error after channel errors are fixed
	ChannelParityError   avgstd.AvgStd // probability of a bit error after channel errors are fixed
}

func BenchmarkBEC

func BenchmarkBEC(ctx context.Context,
	trials, threads int,
	createMessage BinaryMessageConstructor,
	encode BinaryErasureChannelEncoder,
	channel BinaryErasureChannel,
	codewordRepair BinaryErasureChannelCorrection,
	metrics BinaryErasureChannelMetrics,
	checkpoints Checkpoints, showBar bool) Stats
Example
linearBlock, _ := hamming.New(context.Background(), 3, 0)

createMessage := func(trial int) mat.SparseVector {
	t := trial % 64
	message := mat.CSRVec(4)
	for i := 0; i < 4; i++ {
		message.Set(i, (t&(1<<i))>>i)
	}
	return message
}
encode := func(message mat.SparseVector) (codeword []bec.ErasureBit) {
	return BitsToErased(linearBlock.Encode(message))
}

channel := func(originalCodeword []bec.ErasureBit) (erroredCodeword []bec.ErasureBit) {
	//since hamming can fix only one bit wrong under BSC but for BEC this code can fix 2 errors!!
	return RandomEraseCount(originalCodeword, 2)
}

repair := func(originalCodeword, channelInducedCodeword []bec.ErasureBit) (fixed []bec.ErasureBit) {
	alg := &iterative.Simple{
		H: linearBlock.H,
	}
	return bec.Flipping(alg, channelInducedCodeword)
}

metrics := func(originalMessage mat.SparseVector, originalCodeword, fixedChannelInducedCodeword []bec.ErasureBit) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64) {
	codewordErrors := ErasedCount(fixedChannelInducedCodeword)
	message := linearBlock.DecodeBE(fixedChannelInducedCodeword)
	messageErrors := ErasedCount(message)
	parityErrors := codewordErrors - messageErrors

	percentFixedCodewordErrors = float64(codewordErrors) / float64(linearBlock.CodewordLength())
	percentFixedMessageErrors = float64(messageErrors) / float64(linearBlock.MessageLength())
	percentFixedParityErrors = float64(parityErrors) / float64(linearBlock.ParitySymbols())
	return
}

checkpoint := func(updatedStats Stats) {
}

stats := BenchmarkBEC(context.Background(), 10000, 1, createMessage, encode, channel, repair, metrics, checkpoint, false)

fmt.Println("Bit Error Probability :", stats)
Output:

Bit Error Probability : {Codeword:0.00(+/-0.00), Message:0.00(+/-0.00), Parity:0.00(+/-0.00)}

func BenchmarkBECContinueStats

func BenchmarkBECContinueStats(
	ctx context.Context,
	trials, threads int,
	createMessage BinaryMessageConstructor,
	encode BinaryErasureChannelEncoder,
	channel BinaryErasureChannel,
	codewordRepair BinaryErasureChannelCorrection,
	metrics BinaryErasureChannelMetrics,
	checkpoints Checkpoints,
	previousStats Stats,
	showProgressBar bool) Stats

func BenchmarkBPSK

func BenchmarkBPSK(ctx context.Context,
	trials int, threads int,
	createMessage BinaryMessageConstructor,
	encode BPSKChannelEncoder,
	channel BPSKChannel,
	codewordRepair BPSKChannelCorrection,
	metrics BPSKChannelMetrics,
	checkpoints Checkpoints, showProgress bool) Stats
Example
threads := runtime.NumCPU()
linearBlock, _ := hamming.New(context.Background(), 3, threads)

createMessage := func(trial int) mat.SparseVector {
	t := trial % 64
	message := mat.CSRVec(4)
	for i := 0; i < 4; i++ {
		message.Set(i, (t&(1<<i))>>i)
	}
	return message
}

encode := func(message mat.SparseVector) (codeword mat2.Vector) {
	return BitsToBPSK(linearBlock.Encode(message))
}

channel := func(codeword mat2.Vector) (channelInducedCodeword mat2.Vector) {
	//since hamming can fix only one bit wrong, should have zero errors around 2Eb but will sometimes fail do to rounding
	return RandomNoiseBPSK(codeword, 2.0)
}

repair := func(originalCodeword, channelInducedCodeword mat2.Vector) (codeword mat2.Vector) {
	//we're going to simulate a hard decision of >=0 is 1
	// and <0 will be 0 on the output codeword

	tmp := mat.CSRVec(channelInducedCodeword.Len())
	for i := 0; i < channelInducedCodeword.Len(); i++ {
		if channelInducedCodeword.AtVec(i) >= 0 {
			tmp.Set(i, 1)
		}
	}
	alg := &harddecision.Gallager{H: linearBlock.H}
	//next we'll do the simple Gallager hard decision bit flipping with a max of 20 iterations
	tmp = harddecision.BitFlipping(alg, linearBlock.H, tmp, 20)
	return BitsToBPSK(tmp)
}

metrics := func(message mat.SparseVector, originalCodeword, fixedChannelInducedCodeword mat2.Vector) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64) {
	codewordErrors := HammingDistanceBPSK(originalCodeword, fixedChannelInducedCodeword)
	decoded := linearBlock.Decode(BPSKToBits(fixedChannelInducedCodeword, 0))
	messageErrors := decoded.HammingDistance(message)
	parityErrors := codewordErrors - messageErrors

	percentFixedCodewordErrors = float64(codewordErrors) / float64(linearBlock.CodewordLength())
	percentFixedMessageErrors = float64(messageErrors) / float64(linearBlock.MessageLength())
	percentFixedParityErrors = float64(parityErrors) / float64(linearBlock.ParitySymbols())
	return
}

checkpoint := func(updatedStats Stats) {}

stats := BenchmarkBPSK(context.Background(), 100_000, threads, createMessage, encode, channel, repair, metrics, checkpoint, false)

fmt.Println("Bit Error Probability :", stats)
Output:

Bit Error Probability : {Codeword:0.00(+/-0.04), Message:0.00(+/-0.05), Parity:0.00(+/-0.05)}

func BenchmarkBPSKContinueStats

func BenchmarkBPSKContinueStats(ctx context.Context,
	trials int, threads int,
	createMessage BinaryMessageConstructor,
	encode BPSKChannelEncoder,
	channel BPSKChannel,
	codewordRepair BPSKChannelCorrection,
	metrics BPSKChannelMetrics,
	checkpoints Checkpoints,
	previousStats Stats,
	showProgress bool) Stats

func BenchmarkBSC

func BenchmarkBSC(ctx context.Context,
	trials int, threads int,
	createMessage BinaryMessageConstructor,
	encode BinarySymmetricChannelEncoder,
	channel BinarySymmetricChannel,
	codewordRepair BinarySymmetricChannelCorrection,
	metrics BinarySymmetricChannelMetrics,
	checkpoints Checkpoints,
	showProgress bool) Stats
Example
linearBlock, _ := hamming.New(context.Background(), 3, 0)

createMessage := func(trial int) mat.SparseVector {
	t := trial % 64
	message := mat.CSRVec(4)
	for i := 0; i < 4; i++ {
		message.Set(i, (t&(1<<i))>>i)
	}
	return message
}

encode := func(message mat.SparseVector) (codeword mat.SparseVector) {
	return linearBlock.Encode(message)
}

channel := func(originalCodeword mat.SparseVector) (erroredCodeword mat.SparseVector) {
	//since hamming can fix only one bit wrong we'll just flip one bit per codeword
	return RandomFlipBitCount(originalCodeword, 1)
}
repair := func(originalCodeword, channelInducedCodeword mat.SparseVector) (fixed mat.SparseVector) {
	alg := &harddecision.Gallager{
		H: linearBlock.H,
	}

	return harddecision.BitFlipping(alg, linearBlock.H, channelInducedCodeword, 50)
}

metrics := func(originalMessage, originalCodeword, fixedChannelInducedCodeword mat.SparseVector) (percentFixedCodewordErrors, percentFixedMessageErrors, percentFixedParityErrors float64) {
	codewordErrors := originalCodeword.HammingDistance(fixedChannelInducedCodeword)
	message := linearBlock.Decode(fixedChannelInducedCodeword)
	messageErrors := message.HammingDistance(originalMessage)
	parityErrors := codewordErrors - messageErrors

	percentFixedCodewordErrors = float64(codewordErrors) / float64(linearBlock.CodewordLength())
	percentFixedMessageErrors = float64(messageErrors) / float64(linearBlock.MessageLength())
	percentFixedParityErrors = float64(parityErrors) / float64(linearBlock.ParitySymbols())
	return
}

checkpoint := func(updatedStats Stats) {}

stats := BenchmarkBSC(context.Background(), 1, 1, createMessage, encode, channel, repair, metrics, checkpoint, false)

fmt.Println("Bit Error Probability :", stats)
Output:

Bit Error Probability : {Codeword:0.00(+/-0.00), Message:0.00(+/-0.00), Parity:0.00(+/-0.00)}

func BenchmarkBSCContinueStats

func BenchmarkBSCContinueStats(ctx context.Context,
	trials int, threads int,
	createMessage BinaryMessageConstructor,
	encode BinarySymmetricChannelEncoder,
	channel BinarySymmetricChannel,
	codewordRepair BinarySymmetricChannelCorrection,
	metrics BinarySymmetricChannelMetrics,
	checkpoints Checkpoints,
	previousStats Stats,
	showProgress bool) Stats

func (Stats) String

func (s Stats) String() string

Jump to

Keyboard shortcuts

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