Skip to content

Commit b5e86a6

Browse files
committed
Fixed Viterbi decoder.
As it was using the wrong utility function for euclidean distance calculation due to the recent migration from Cython based implementation to pure Python implementation.
1 parent 0f788d0 commit b5e86a6

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

commpy/channelcoding/convcode.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from matplotlib.collections import PatchCollection
2424
import matplotlib.patches as mpatches
2525

26-
from commpy.utilities import dec2bitarray, bitarray2dec, hamming_dist
26+
from commpy.utilities import dec2bitarray, bitarray2dec, hamming_dist, euclid_dist
2727
#from commpy.channelcoding.acstb import acs_traceback
2828

2929
__all__ = ['Trellis', 'conv_encode', 'viterbi_decode']
@@ -445,7 +445,7 @@ def _acs_traceback(r_codeword, trellis, decoding_type,
445445
pass
446446
elif decoding_type == 'unquantized':
447447
i_codeword_array = 2*i_codeword_array - 1
448-
branch_metric = euclid_dist_c(r_codeword, i_codeword_array, n)
448+
branch_metric = euclid_dist(r_codeword, i_codeword_array)
449449
else:
450450
pass
451451

@@ -474,7 +474,6 @@ def _acs_traceback(r_codeword, trellis, decoding_type,
474474

475475
dec_symbol = decoded_symbols[current_state, j]
476476
previous_state = paths[current_state, j]
477-
#dec2bitarray_c(dec_symbol, k, decoded_bitarray)
478477
decoded_bitarray = dec2bitarray(dec_symbol, k)
479478
decoded_bits[(t-tb_depth-1)+(j+1)*k+count:(t-tb_depth-1)+(j+2)*k+count] = \
480479
decoded_bitarray
@@ -504,8 +503,10 @@ def viterbi_decode(coded_bits, trellis, tb_depth=None, decoding_type='hard'):
504503
tb_length : int
505504
Traceback depth (Typically set to 5*(M+1)).
506505
507-
decoding_type : str {'hard', 'soft', 'unquantized'}
506+
decoding_type : str {'hard', 'unquantized'}
508507
The type of decoding to be used.
508+
'hard' option is used for hard inputs (bits) to the decoder, e.g., BSC channel.
509+
'unquantized' option is used for soft inputs (real numbers) to the decoder, e.g., BAWGN channel.
509510
510511
Returns
511512
-------
@@ -536,15 +537,15 @@ def viterbi_decode(coded_bits, trellis, tb_depth=None, decoding_type='hard'):
536537
L = int(len(coded_bits)*rate)
537538

538539
path_metrics = np.empty([number_states, 2])
539-
path_metrics[:, :] = 10000
540+
path_metrics[:, :] = 1000000
540541
path_metrics[0][0] = 0
541542
paths = np.empty([number_states, tb_depth], 'int')
542-
paths[:, :] = 10000
543+
paths[:, :] = 1000000
543544
paths[0][0] = 0
544545

545546
decoded_symbols = np.zeros([number_states, tb_depth], 'int')
546-
decoded_bits = np.empty(L+tb_depth+k, 'int')
547-
r_codeword = np.empty(n, 'int')
547+
decoded_bits = np.zeros(L+tb_depth+k, 'int')
548+
r_codeword = np.zeros(n, 'int')
548549

549550
tb_count = 1
550551
count = 0
@@ -561,7 +562,7 @@ def viterbi_decode(coded_bits, trellis, tb_depth=None, decoding_type='hard'):
561562
pass
562563
elif decoding_type == 'unquantized':
563564
r_codeword[:] = 0
564-
r_codeword = r_codeword - 1
565+
r_codeword = 2*r_codeword - 1
565566
else:
566567
pass
567568

@@ -583,4 +584,3 @@ def viterbi_decode(coded_bits, trellis, tb_depth=None, decoding_type='hard'):
583584
current_number_states = 1
584585

585586
return decoded_bits[0:len(decoded_bits)-tb_depth-1]
586-
#return decoded_bits[0:len(decoded_bits)-tb_depth-total_memory-total_memory%k-k]

commpy/utilities.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Copyright 2012 Veeresh Taranalli <[email protected]>
33
#
4-
# This file is part of CommPy.
4+
# This file is part of CommPy.
55
#
66
# CommPy is free software: you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -22,29 +22,29 @@
2222

2323
def dec2bitarray(in_number, bit_width):
2424
"""
25-
Converts a positive integer to NumPy array of the specified size containing
25+
Converts a positive integer to NumPy array of the specified size containing
2626
bits (0 and 1).
27-
27+
2828
Parameters
2929
----------
3030
in_number : int
3131
Positive integer to be converted to a bit array.
32-
32+
3333
bit_width : int
3434
Size of the output bit array.
35-
35+
3636
Returns
3737
-------
3838
bitarray : 1D ndarray of ints
3939
Array containing the binary representation of the input decimal.
40-
40+
4141
"""
4242

4343
binary_string = bin(in_number)
4444
length = len(binary_string)
4545
bitarray = np.zeros(bit_width, 'int')
4646
for i in xrange(length-2):
47-
bitarray[bit_width-i-1] = int(binary_string[length-i-1])
47+
bitarray[bit_width-i-1] = int(binary_string[length-i-1])
4848

4949
return bitarray
5050

@@ -62,7 +62,7 @@ def bitarray2dec(in_bitarray):
6262

6363
for i in xrange(len(in_bitarray)):
6464
number = number + in_bitarray[i]*pow(2, len(in_bitarray)-1-i)
65-
65+
6666
return number
6767

6868
def hamming_dist(in_bitarray_1, in_bitarray_2):
@@ -79,7 +79,7 @@ def hamming_dist(in_bitarray_1, in_bitarray_2):
7979
"""
8080

8181
distance = np.bitwise_xor(in_bitarray_1, in_bitarray_2).sum()
82-
82+
8383
return distance
8484

8585
def euclid_dist(in_array1, in_array2):
@@ -88,20 +88,20 @@ def euclid_dist(in_array1, in_array2):
8888
8989
Parameters
9090
----------
91-
in_bitarray_1: 1-D ndarray of ints
91+
in_array1: 1-D ndarray of floats
9292
NumPy array of real values.
9393
94-
in_bitarray_2: 1-D ndarray of ints
94+
in_array2: 1-D ndarray of floats
9595
NumPy array of real values.
9696
"""
9797
distance = ((in_array1 - in_array2)*(in_array1 - in_array2)).sum()
98-
98+
9999
return distance
100-
100+
101101
def upsample(x, n):
102102
"""
103-
Upsample the input array by a factor of n
104-
103+
Upsample the input array by a factor of n
104+
105105
Adds n-1 zeros between consecutive samples of x
106106
107107
Parameters
@@ -119,5 +119,3 @@ def upsample(x, n):
119119
y[i::n] = zero_array
120120

121121
return y
122-
123-

0 commit comments

Comments
 (0)