Skip to content

Commit 8541f32

Browse files
author
Alexandre Marquet
committed
Make example script quicker.
1 parent d24f121 commit 8541f32

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

examples/75_cc.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from matplotlib import pyplot as plt
55

66
import numpy
7+
import scipy.special
78

89
#Quick implementation of a (7,5) convolutive code encoder
910
def encode75(msg):
@@ -29,8 +30,10 @@ def viterbi_branch_metrics(bits_rcvd, nbits_cw):
2930
cw = numpy.array([[0.0,0.0], [0.0,1.0], [1.0,0.0], [1.0,1.0]]) #The 4 different codewords
3031

3132
bits_rcvd = numpy.array(bits_rcvd).reshape((K, nbits_cw))
32-
for k in range(0, K):
33-
ret_val[k][:] = numpy.sum(numpy.abs(bits_rcvd[k][:]-cw)**2, axis=1)
33+
i=0
34+
for cw0 in cw:
35+
ret_val[:,i] = numpy.sum(numpy.abs(bits_rcvd-cw0)**2, axis=1)
36+
i += 1
3437

3538
return ret_val.flatten()
3639

@@ -42,8 +45,10 @@ def log_bcjr_branch_metrics(bits_rcvd, nbits_cw, sigma_b2):
4245
cw = numpy.array([[0.0,0.0], [0.0,1.0], [1.0,0.0], [1.0,1.0]]) #The 4 different codewords
4346

4447
bits_rcvd = numpy.array(bits_rcvd).reshape((K, nbits_cw))
45-
for k in range(0, K):
46-
ret_val[k][:] = -1.0/sigma_b2 * numpy.sum(numpy.abs(bits_rcvd[k][:]-cw)**2, axis=1)
48+
i=0
49+
for cw0 in cw:
50+
ret_val[:,i] = -1.0/sigma_b2 * numpy.sum(numpy.abs(bits_rcvd-cw0)**2, axis=1)
51+
i += 1
4752

4853
return ret_val.flatten()
4954

@@ -54,8 +59,10 @@ def max_log_bcjr_branch_metrics(bits_rcvd, nbits_cw, sigma_b2):
5459
cw = numpy.array([[0.0,0.0], [0.0,1.0], [1.0,0.0], [1.0,1.0]]) #The 4 different codewords
5560

5661
bits_rcvd = numpy.array(bits_rcvd).reshape((K, nbits_cw))
57-
for k in range(0, K):
58-
ret_val[k][:] = -numpy.sum(numpy.abs(bits_rcvd[k][:]-cw)**2, axis=1)
62+
i=0
63+
for cw0 in cw:
64+
ret_val[:,i] = -numpy.sum(numpy.abs(bits_rcvd-cw0)**2, axis=1)
65+
i += 1
5966

6067
return ret_val.flatten()
6168

@@ -64,9 +71,8 @@ def log_bcjr_compute_llr(app, K, S):
6471
llr = numpy.zeros(K, dtype=numpy.float32)
6572

6673
app = app.reshape((K, S, 2))
67-
for k in range(0, K):
68-
#We need copy() to make the vectors C-contiguous
69-
llr[k] = bcjr.max_star(app[k,:,0].copy()) - bcjr.max_star(app[k,:,1].copy())
74+
llr = scipy.special.logsumexp(app[:,:,0], axis=1) \
75+
- scipy.special.logsumexp(app[:,:,1], axis=1)
7076

7177
return llr
7278

@@ -75,8 +81,6 @@ def max_log_bcjr_compute_llr(app, K, S):
7581

7682
app = app.reshape((K, S, 2))
7783
llr = numpy.max(app[:,:,0], axis=1) - numpy.max(app[:,:,1], axis=1)
78-
#for k in range(0, K):
79-
# llr[k] = numpy.max(app[k,:,0]) - numpy.max(app[k,:,1])
8084

8185
return llr
8286

0 commit comments

Comments
 (0)