Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed improvements #74

Merged
merged 4 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions commpy/channelcoding/convcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from __future__ import division

import functools
import math
from warnings import warn

Expand Down Expand Up @@ -560,16 +561,30 @@ def conv_encode(message_bits, trellis, termination = 'term', puncture_matrix=Non
def _where_c(inarray, rows, cols, search_value, index_array):

number_found = 0
for i in range(rows):
for j in range(cols):
if inarray[i, j] == search_value:
index_array[number_found, 0] = i
index_array[number_found, 1] = j
number_found += 1
res = np.where(inarray == search_value)
i_s, j_s = res
for i, j in zip(i_s, j_s):
if inarray[i, j] == search_value:
index_array[number_found, 0] = i
index_array[number_found, 1] = j
number_found += 1

return number_found


@functools.lru_cache(maxsize=128, typed=False)
def _compute_branch_metrics(decoding_type, r_codeword, i_codeword_array):
if decoding_type == 'hard':
return hamming_dist(r_codeword.astype(int), i_codeword_array.astype(int))
elif decoding_type == 'soft':
neg_LL_0 = np.log(np.exp(r_codeword) + 1) # negative log-likelihood to have received a 0
neg_LL_1 = neg_LL_0 - r_codeword # negative log-likelihood to have received a 1
return np.where(i_codeword_array, neg_LL_1, neg_LL_0).sum()
elif decoding_type == 'unquantized':
i_codeword_array = 2 * i_codeword_array - 1
return euclid_dist(r_codeword, i_codeword_array)


def _acs_traceback(r_codeword, trellis, decoding_type,
path_metrics, paths, decoded_symbols,
decoded_bits, tb_count, t, count,
Expand Down Expand Up @@ -605,15 +620,7 @@ def _acs_traceback(r_codeword, trellis, decoding_type,
i_codeword_array = dec2bitarray(i_codeword, n)

# Compute Branch Metrics
if decoding_type == 'hard':
branch_metric = hamming_dist(r_codeword.astype(int), i_codeword_array.astype(int))
elif decoding_type == 'soft':
neg_LL_0 = np.log(np.exp(r_codeword) + 1) # negative log-likelihood to have received a 0
neg_LL_1 = neg_LL_0 - r_codeword # negative log-likelihood to have received a 1
branch_metric = np.where(i_codeword_array, neg_LL_1, neg_LL_0).sum()
elif decoding_type == 'unquantized':
i_codeword_array = 2*i_codeword_array - 1
branch_metric = euclid_dist(r_codeword, i_codeword_array)
branch_metric = _compute_branch_metrics(decoding_type, tuple(r_codeword), tuple(i_codeword_array))

# ADD operation: Add the branch metric to the
# accumulated path metric and store it in the temporary array
Expand Down
6 changes: 3 additions & 3 deletions commpy/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def link_performance(self, SNRs, send_max, err_min, send_chunk=None, code_rate=1
# Deals with MIMO channel
if isinstance(self.channel, MIMOFlatChannel):
nb_symb_vector = len(channel_output)
received_msg = np.empty(int(math.ceil(len(msg) / self.rate)))
received_msg = np.empty(int(math.ceil(len(msg) / self.rate)), dtype=np.int8)
for i in range(nb_symb_vector):
received_msg[receive_size * i:receive_size * (i + 1)] = \
self.receive(channel_output[i], self.channel.channel_gains[i],
Expand All @@ -216,9 +216,9 @@ def link_performance(self, SNRs, send_max, err_min, send_chunk=None, code_rate=1
decoded_bits = self.decoder(channel_output, self.channel.channel_gains,
self.constellation, self.channel.noise_std ** 2,
received_msg, self.channel.nb_tx * self.num_bits_symbol)
bit_err += (msg != decoded_bits[:len(msg)]).sum()
bit_err += np.bitwise_xor(msg, decoded_bits[:len(msg)]).sum()
else:
bit_err += (msg != self.decoder(received_msg)[:len(msg)]).sum()
bit_err += np.bitwise_xor(msg, self.decoder(received_msg)[:len(msg)]).sum()
bit_send += send_chunk
BERs[id_SNR] = bit_err / bit_send
if bit_err < err_min:
Expand Down
6 changes: 4 additions & 2 deletions commpy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
upsample -- Upsample by an integral factor (zero insertion).
signal_power -- Compute the power of a discrete time signal.
"""
import functools

import numpy as np

Expand Down Expand Up @@ -47,13 +48,14 @@ def dec2bitarray(in_number, bit_width):
"""

if isinstance(in_number, (np.integer, int)):
return decimal2bitarray(in_number, bit_width)
return decimal2bitarray(in_number, bit_width).copy()
result = np.zeros(bit_width * len(in_number), np.int8)
for pox, number in enumerate(in_number):
result[pox * bit_width:(pox + 1) * bit_width] = decimal2bitarray(number, bit_width)
result[pox * bit_width:(pox + 1) * bit_width] = decimal2bitarray(number, bit_width).copy()
return result


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