-
Notifications
You must be signed in to change notification settings - Fork 5
/
vitdecoder.c
83 lines (66 loc) · 1.94 KB
/
vitdecoder.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
82
83
#include "mex.h"
#include "matrix.h"
#include "math.h"
#define M(a) ((a) == (0) ? (a-1) : (a))
void decoder(double *r,double*Y,double*S,double*N,int n,int Ysize,int ns,double *u_out)
{
int i,j,k,z;
int bb,u1,u2,s1,s2;
double gamma[ns][2],tempgamma[2],maxgamma[2] = {0,0};
int survivors[ns][n][2];
for(z=0;z<ns;z++)
{
gamma[z][1] = gamma[z][0] = -10000;
}
gamma[0][0] = 0;
for(i=0;i<n;i++)
{
for(j=0;j<ns;j++)
{
u1 = (int)N[j];
s1 = (int)N[j+Ysize];
u2 = (int)N[j+ns];
s2 = (int)N[j+ns+Ysize];
tempgamma[0] = gamma[s1][i%2];
tempgamma[1] = gamma[s2][i%2];
for(z=0;z<2;z++)
{
tempgamma[0] += r[z+(i<<1)]*M((int)Y[u1+(s1<<1)+(z*Ysize)]);
tempgamma[1] += r[z+(i<<1)]*M((int)Y[u2+(s2<<1)+(z*Ysize)]);
}
bb = (tempgamma[0] > tempgamma[1]) ? 0 : 1;
gamma[j][(i+1)%2] = tempgamma[bb] - maxgamma[i%2];
survivors[j][i][0] = (int)N[j+ns*bb];
survivors[j][i][1] = (int)N[j+ns*bb+Ysize];
if(gamma[j][(i+1)%2] > maxgamma[i%2])
{
maxgamma[(i+1)%2] = gamma[j][(i+1)%2];
}
}
}
int s = 0;
for(z=n-1;z>=n-log2(ns);z--)
{
s = survivors[s][z][1];
}
for(z=n-log2(ns)-1;z>=0;z--)
{
u_out[z] = survivors[s][z][0];
s = survivors[s][z][1];
}
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
double *r,*Y,*S,*N,*u_out;
int n,Ysize,ns;
r = mxGetPr(prhs[0]);
Y = mxGetPr(prhs[1]);
S = mxGetPr(prhs[2]);
N = mxGetPr(prhs[3]);
n = mxGetN(prhs[0]);
Ysize = mxGetN(prhs[1]);
ns = mxGetN(prhs[1])/2; /* Number of possible states*/
plhs[0] = mxCreateDoubleMatrix(1,n-log2(ns), mxREAL);
u_out = mxGetPr(plhs[0]);
decoder(r,Y,S,N,n,Ysize,ns,u_out);
}