forked from osherbakov/MELPeModem
/
OFDMSync.cs
272 lines (231 loc) · 7.82 KB
/
OFDMSync.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MELPeModem
{
class OFDMSync : DataProcessingModule
{
InputPin<IQ> DataIn;
OutputPin<IQ> DataOut;
int BlockSize;
int FFTSize;
int CPSize;
int DataSize;
int PutIndex;
int SlicingIndex = 0;
int PreviousSlicingIndex;
IQ[] DataBuffer;
float []CorrBuffer;
float[] DiffFIRBuffer;
float[] DiffIIRBuffer;
float PrevValue = 0;
FIR DiffFIR;
float DiffIIR;
float Alpha, OneMinusAlpha;
float SNRValue;
Queue<IQ> OutputData = new Queue<IQ>();
public OFDMSync(int interpDecimFactor)
{
BlockSize = interpDecimFactor;
// Calculate the size of the FFT Windows.
// Guard Time will be BlockSize - FFTSize
FFTSize = 1;
while (FFTSize < BlockSize)
{
FFTSize <<= 1;
}
FFTSize >>= 1;
CPSize = BlockSize - FFTSize;
Init();
}
public float SNR
{
get { return SNRValue; }
set { SNRValue = value; }
}
public override void Init()
{
PutIndex = 0;
SNRValue = 1.0f;
DataSize = 2 * BlockSize + CPSize;
DataBuffer = new IQ[DataSize];
Alpha = 2.0f / CPSize;
OneMinusAlpha = 1.0f - Alpha;
CorrBuffer = new float[BlockSize];
DiffFIRBuffer = new float[BlockSize];
DiffIIRBuffer = new float[BlockSize];
PrevValue = 0;
DiffIIR = 0;
float[] coeffs = new float[CPSize / 2];
for (int i = 0; i < CPSize / 2; i++)
{
coeffs[i] = 2.0f / CPSize;
}
DiffFIR = new FIR(coeffs, 1);
OutputData.Clear();
}
public void StartCorrectionProcess(int currentIndex)
{
PreviousSlicingIndex = currentIndex;
Array.Clear(DataBuffer, 0, DataSize);
Init();
}
bool CalculateIndexCorrection(out bool skipNext)
{
bool getCurrent = false;
skipNext = false;
// if points are close to each other - no correction is needed
if (Math.Abs(SlicingIndex - PreviousSlicingIndex) * 2 < BlockSize)
{
}
else if (SlicingIndex > PreviousSlicingIndex) // Samples are too far from each other
{
getCurrent = true;
}
else // Samples are too close to each other
{
skipNext = true;
}
return getCurrent;
}
void CalculateCorrections()
{
// Calculate the initial correlation value
float Gamma = 0;
float Energy = 0;
IQ Sample;
IQ ShiftedSample;
IQ PrevSample, PrevShiftedSample;
float Value;
float Diff;
int Idx = 0;
int ShiftedIdx = FFTSize;
for (; Idx < CPSize; Idx++, ShiftedIdx++)
{
Sample = DataBuffer[Idx];
ShiftedSample = DataBuffer[ShiftedIdx];
Gamma += (Sample / ShiftedSample).R2;
Energy += (Sample * Sample).R2 + (ShiftedSample * ShiftedSample).R2;
}
Value = Gamma - 0.5f * SNRValue * Energy;
Diff = Value - PrevValue;
CorrBuffer[0] = Value;
DiffFIR.Process(Diff, out DiffFIRBuffer[0]);
DiffIIR = OneMinusAlpha * DiffIIR + Alpha * Diff;
DiffIIRBuffer[0] = DiffIIR;
PrevValue = Value;
// Now, calculate all other correlations
Idx = CPSize;
ShiftedIdx = BlockSize;
for (int i = 1; i < BlockSize; i++, Idx++, ShiftedIdx++)
{
Sample = DataBuffer[Idx];
ShiftedSample = DataBuffer[ShiftedIdx];
PrevSample = DataBuffer[i-1];
PrevShiftedSample = DataBuffer[i - 1 + FFTSize];
Gamma += (Sample / ShiftedSample).R2 -
(PrevSample / PrevShiftedSample).R2;
Energy += (Sample * Sample).R2 + (ShiftedSample * ShiftedSample).R2 -
((PrevSample * PrevSample).R2 + (PrevShiftedSample * PrevShiftedSample).R2);
Value = Gamma - 0.5f * SNRValue * Energy;
Diff = Value - PrevValue;
CorrBuffer[i] = Value;
DiffFIR.Process(Diff, out DiffFIRBuffer[i]);
DiffIIR = OneMinusAlpha * DiffIIR + Alpha * Diff;
DiffIIRBuffer[i] = DiffIIR;
PrevValue = Value;
}
}
public int Process(IQ inData)
{
if (PutIndex < DataSize)
{
DataBuffer[PutIndex] = inData;
}
PutIndex++;
if (PutIndex == DataSize)
{
CalculateCorrections();
}
return OutputData.Count;
}
public void Process(CNTRL_MSG controlParam, IQ inData)
{
if (controlParam == CNTRL_MSG.DATA_IN)
{
if (PutIndex < DataSize)
{
DataBuffer[PutIndex] = inData;
}
PutIndex++;
if (PutIndex == DataSize)
{
CalculateCorrections();
foreach (IQ IQData in OutputData) DataOut.Process(IQData);
}
}
}
public int Process(IQ[] inDataArray, int startIndex, int numToProcess)
{
for (int i = 0; i < numToProcess; i++)
{
Process(inDataArray[startIndex++]);
}
return OutputData.Count;
}
public int Process(float[] inDataArrayI, float[] inDataArrayQ, int startIndex, int numToProcess)
{
IQ Data;
for (int i = 0; i < numToProcess; i++, startIndex++)
{
Data.I = inDataArrayI[startIndex];
Data.Q = inDataArrayQ[startIndex];
Process(Data);
}
return OutputData.Count;
}
public bool IsSyncReady
{
get { return (PutIndex >= DataSize); }
}
public int Count
{
get
{
return OutputData.Count;
}
}
public IQ GetData()
{
return OutputData.Dequeue();
}
public int GetData(IQ[] outputArray)
{
int ret = OutputData.Count;
OutputData.CopyTo(outputArray, 0);
OutputData.Clear();
return ret;
}
public int GetData(IQ[] outputArray, int arrayIndex)
{
int ret = OutputData.Count;
OutputData.CopyTo(outputArray, arrayIndex);
OutputData.Clear();
return ret;
}
public int SymbolCorrection
{
get
{
return ((PutIndex - 1) - SlicingIndex) % BlockSize;
}
}
public override void SetModuleParameters()
{
DataIn = new InputPin<IQ>("DataIn", this.Process);
DataOut = new OutputPin<IQ>("DataOut");
base.SetIOParameters("OFDM Timing Offset Estimator", new DataPin[] { DataIn, DataOut });
}
}
}