-
Notifications
You must be signed in to change notification settings - Fork 6
/
calculate_pbit_mex.c
83 lines (68 loc) · 1.91 KB
/
calculate_pbit_mex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* calculate_pbit_mex.c - Compute bit-wise probabilities
*
* Usage: Pbit = calculate_pbit_mex(Pk)
* Pk := Probability of each constellation symbol
*
* Compile with: mex -R2018a -lm calculate_pbit_mex.c
* Designed for 64-bit Linux.
*
* Copyright (c) 2018-2022 Dario Pilori, Politecnico di Torino <[email protected]>
* SPDX-License-Identifier: MIT
*/
#include "mex.h"
#include <math.h>
/* Prototypes */
unsigned int insert_zero(unsigned int i, unsigned int k, unsigned int nb);
void calculate_bit_probabilities(const double *Pk, double *Pb, int m, int M);
/* Gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
size_t M, m;
double *Pb, *Pk;
/* Verify input */
if(nrhs != 1) {
mexErrMsgIdAndTxt("DspLibrary:qam_gmi_mex:nrhs",
"One input required.");
}
/* Verify output */
if(nlhs > 1) {
mexErrMsgIdAndTxt("DspLibrary:qam_gmi_mex:nlhs",
"Max one output.");
}
/* Get sizes */
M = mxGetM(prhs[0]);
m = log2(M);
/* Get probabilities */
Pk = mxGetDoubles(prhs[0]);
/* Allocate the output matrix */
plhs[0] = mxCreateDoubleMatrix(m,1,mxREAL);
Pb = mxGetDoubles(plhs[0]);
/* Call function */
calculate_bit_probabilities(Pk, Pb, m, M);
}
// Actual function
void calculate_bit_probabilities(const double *Pk, double *Pb, int m, int M)
{
int j, k;
unsigned int bj;
for(k=0; k<m; k++)
{
Pb[k] = 0.0;
for(j=0; j<M/2; j++)
{
bj = insert_zero(j, k, m);
Pb[k] += Pk[bj];
}
}
}
// Helper function to insert a zero inside a number
unsigned int insert_zero(unsigned int i, unsigned int k, unsigned int nb)
{
unsigned int b0, left, right;
left = (i<<1) & ( ( (1<<(nb-k)) - 1)<<(k+1) );
right = i & ((1<<k)-1);
b0 = left | right;
return b0;
}