forked from philpem/dsd-dmr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
descramble.h
212 lines (176 loc) · 5.17 KB
/
descramble.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* descramble.h */
// Functions for processing the radio-header:
// FECdecoder
// Viterbi decoder using Traceback method.
// Original Source was written by Sho Tamaoki and Tom Wada
// See http://www.lsi.ie.u-ryukyu.ac.jp/~sho/midterm/
// Modified by Satoshi Yasuda 7m3tjz/ad6gz
// Modified by Jonathan Nayor, G4KLX (C) 2009
// Converted from C++ to C by Kristoff Bonne, ON1ARF
// (C) 2011 Jonathan Naylor G4KLX
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// This code was originally written by JOnathan Naylor, G4KLX, as part
// of the "pcrepeatercontroller" project
// More info:
// http://groups.yahoo.com/group/pcrepeatercontroller
// Changes:
// Convert C++ to C
// Version 20111106: initial release
#include <stdint.h>
#include <string.h>
// function traceBack
int traceBack (int * out, int * m_pathMemory0, int * m_pathMemory1, int * m_pathMemory2, int * m_pathMemory3) {
enum FEC_STATE { S0, S1, S2, S3 } state;
int loop;
int length=0;
state=S0;
for (loop=329; loop >= 0; loop--, length++) {
switch (state) {
case S0: // if state S0
if (m_pathMemory0[loop]) {
state = S2; // lower path
} else {
state = S0; // upper path
} // end else - if
out[loop]=0;
break;
case S1: // if state S1
if (m_pathMemory1[loop]) {
state = S2; // lower path
} else {
state = S0; // upper path
} // end else - if
out[loop]=1;
break;
case S2: // if state S2
if (m_pathMemory2[loop]) {
state = S3; // lower path
} else {
state = S1; // upper path
} // end else - if
out[loop]=0;
break;
case S3: // if state S3
if (m_pathMemory3[loop]) {
state = S3; // lower path
} else {
state = S1; // upper path
} // end else - if
out[loop]=1;
break;
} // end switch
} // end for
return(length);
} // end function
// function viterbiDecode
void viterbiDecode (int n, int *data, int *m_pathMemory0, int *m_pathMemory1, int *m_pathMemory2, int *m_pathMemory3, int *m_pathMetric) {
int tempMetric[4];
int metric[8];
int loop;
int m1;
int m2;
metric[0]=(data[1]^0)+(data[0]^0);
metric[1]=(data[1]^1)+(data[0]^1);
metric[2]=(data[1]^1)+(data[0]^0);
metric[3]=(data[1]^0)+(data[0]^1);
metric[4]=(data[1]^1)+(data[0]^1);
metric[5]=(data[1]^0)+(data[0]^0);
metric[6]=(data[1]^0)+(data[0]^1);
metric[7]=(data[1]^1)+(data[0]^0);
// Pres. state = S0, Prev. state = S0 & S2
m1=metric[0]+m_pathMetric[0];
m2=metric[4]+m_pathMetric[2];
if (m1<m2) {
m_pathMemory0[n]=0;
tempMetric[0]=m1;
} else {
m_pathMemory0[n]=1;
tempMetric[0]=m2;
} // end else - if
// Pres. state = S1, Prev. state = S0 & S2
m1=metric[1]+m_pathMetric[0];
m2=metric[5]+m_pathMetric[2];
if (m1<m2) {
m_pathMemory1[n]=0;
tempMetric[1]=m1;
} else {
m_pathMemory1[n]=1;
tempMetric[1]=m2;
} // end else - if
// Pres. state = S2, Prev. state = S2 & S3
m1=metric[2]+m_pathMetric[1];
m2=metric[6]+m_pathMetric[3];
if (m1<m2) {
m_pathMemory2[n]=0;
tempMetric[2]=m1;
} else {
m_pathMemory2[n]=1;
tempMetric[2]=m2;
}
// Pres. state = S3, Prev. state = S1 & S3
m1=metric[3]+m_pathMetric[1];
m2=metric[7]+m_pathMetric[3];
if (m1 < m2) {
m_pathMemory3[n]=0;
tempMetric[3]=m1;
} else {
m_pathMemory3[n]=1;
tempMetric[3]=m2;
} // end else - if
for (loop=0;loop<4;loop++) {
m_pathMetric[loop]=tempMetric[loop];
} // end for
} // end function ViterbiDecode
// function FECdecoder
// returns outlen
int FECdecoder (int * in, int * out) {
int outLen;
int m_pathMemory0[330];
int m_pathMemory1[330];
int m_pathMemory2[330];
int m_pathMemory3[330];
int m_pathMetric[4];
int loop,loop2;
int n=0;
memset(m_pathMemory0,0,330*sizeof(int));
memset(m_pathMemory1,0,330*sizeof(int));
memset(m_pathMemory2,0,330*sizeof(int));
memset(m_pathMemory3,0,330*sizeof(int));
for (loop=0;loop<4;loop++) {
m_pathMetric[loop]=0;
} // end for
for (loop2=0;loop2<660;loop2+=2, n++) {
int data[2];
if (in[loop2]) {
data[1]=1;
} else {
data[1]=0;
} // end else - if
if (in[loop2+1]) {
data[0]=1;
} else {
data[0]=0;
} // end else - if
viterbiDecode(n, data, m_pathMemory0, m_pathMemory1, m_pathMemory2, m_pathMemory3, m_pathMetric);
} // end for
outLen=traceBack(out, m_pathMemory0, m_pathMemory1, m_pathMemory2, m_pathMemory3);
// Swap endian-ness
// code removed (done converting bits into octets), done in main program
//for (loop=0;loop<330;loop+=8) {
// int temp;
// temp=out[loop];out[loop]=out[loop+7];out[loop+7]=temp;
// temp=out[loop+1];out[loop+1]=out[loop+6];out[loop+6]=temp;
// temp=out[loop+2];out[loop+2]=out[loop+5];out[loop+5]=temp;
// temp=out[loop+3];out[loop+3]=out[loop+4];out[loop+4]=temp;
//}
return(outLen);
} // end function FECdecoder