Skip to content

Commit fc764d5

Browse files
author
Eduardo Soares
committed
Add caching on recurring calculations
Using a LRU cache. For now uses static parameters, in the future some configuration should be added.
1 parent 210b973 commit fc764d5

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

commpy/channelcoding/convcode.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from __future__ import division
77

8+
import functools
89
import math
910
from warnings import warn
1011

@@ -570,6 +571,19 @@ def _where_c(inarray, rows, cols, search_value, index_array):
570571
return number_found
571572

572573

574+
@functools.lru_cache(maxsize=128, typed=False)
575+
def _compute_branch_metrics(decoding_type, r_codeword, i_codeword_array):
576+
if decoding_type == 'hard':
577+
return hamming_dist(r_codeword.astype(int), i_codeword_array.astype(int))
578+
elif decoding_type == 'soft':
579+
neg_LL_0 = np.log(np.exp(r_codeword) + 1) # negative log-likelihood to have received a 0
580+
neg_LL_1 = neg_LL_0 - r_codeword # negative log-likelihood to have received a 1
581+
return np.where(i_codeword_array, neg_LL_1, neg_LL_0).sum()
582+
elif decoding_type == 'unquantized':
583+
i_codeword_array = 2 * i_codeword_array - 1
584+
return euclid_dist(r_codeword, i_codeword_array)
585+
586+
573587
def _acs_traceback(r_codeword, trellis, decoding_type,
574588
path_metrics, paths, decoded_symbols,
575589
decoded_bits, tb_count, t, count,
@@ -605,15 +619,7 @@ def _acs_traceback(r_codeword, trellis, decoding_type,
605619
i_codeword_array = dec2bitarray(i_codeword, n)
606620

607621
# Compute Branch Metrics
608-
if decoding_type == 'hard':
609-
branch_metric = hamming_dist(r_codeword.astype(int), i_codeword_array.astype(int))
610-
elif decoding_type == 'soft':
611-
neg_LL_0 = np.log(np.exp(r_codeword) + 1) # negative log-likelihood to have received a 0
612-
neg_LL_1 = neg_LL_0 - r_codeword # negative log-likelihood to have received a 1
613-
branch_metric = np.where(i_codeword_array, neg_LL_1, neg_LL_0).sum()
614-
elif decoding_type == 'unquantized':
615-
i_codeword_array = 2*i_codeword_array - 1
616-
branch_metric = euclid_dist(r_codeword, i_codeword_array)
622+
branch_metric = _compute_branch_metrics(decoding_type, tuple(r_codeword), tuple(i_codeword_array))
617623

618624
# ADD operation: Add the branch metric to the
619625
# accumulated path metric and store it in the temporary array

commpy/utilities.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
upsample -- Upsample by an integral factor (zero insertion).
1818
signal_power -- Compute the power of a discrete time signal.
1919
"""
20+
import functools
2021

2122
import numpy as np
2223

@@ -47,13 +48,14 @@ def dec2bitarray(in_number, bit_width):
4748
"""
4849

4950
if isinstance(in_number, (np.integer, int)):
50-
return decimal2bitarray(in_number, bit_width)
51+
return decimal2bitarray(in_number, bit_width).copy()
5152
result = np.zeros(bit_width * len(in_number), np.int8)
5253
for pox, number in enumerate(in_number):
53-
result[pox * bit_width:(pox + 1) * bit_width] = decimal2bitarray(number, bit_width)
54+
result[pox * bit_width:(pox + 1) * bit_width] = decimal2bitarray(number, bit_width).copy()
5455
return result
5556

5657

58+
@functools.lru_cache(maxsize=128, typed=False)
5759
def decimal2bitarray(number, bit_width):
5860
"""
5961
Converts a positive integer to NumPy array of the specified size containing bits (0 and 1). This version is slightly

0 commit comments

Comments
 (0)