/
Generator.cs
382 lines (342 loc) · 11.7 KB
/
Generator.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections.Generic;
using System.Text;
namespace MELPeModem
{
class Quad
{
double I0, I1, Q0, Q1, Coeff;
double DeltaPhase;
public Quad()
{
I0 = 1;
Q0 = 0;
I1 = 1;
Q1 = 0;
Coeff = 0;
DeltaPhase = 0;
}
public Quad(IQ initialVector, IQ delta, int offset)
{
double initialPhase = initialVector.Phase;
double initialR = initialVector.R;
DeltaPhase = delta.Phase;
Coeff = 2 * Math.Sin(DeltaPhase);
double newPhase = initialPhase + DeltaPhase * offset;
I0 = initialR * Math.Cos(newPhase);
Q0 = initialR * Math.Sin(newPhase);
I1 = initialR * Math.Cos(newPhase + DeltaPhase);
Q1 = initialR * Math.Sin(newPhase + DeltaPhase);
}
public Quad(IQ initialVector, IQ delta)
{
double initialPhase = initialVector.Phase;
double initialR = initialVector.R;
DeltaPhase = delta.Phase;
Coeff = 2 * Math.Sin(DeltaPhase);
I0 = initialVector.I;
Q0 = initialVector.Q;
I1 = initialR * Math.Cos(initialPhase + DeltaPhase);
Q1 = initialR * Math.Sin(initialPhase + DeltaPhase);
}
public Quad(float outputFreq, float sampleFreq)
{
DeltaPhase = 2 * Math.PI * outputFreq / sampleFreq;
Coeff = 2 * Math.Sin(DeltaPhase);
double initialPhase = 0 ;
I0 = 1;
Q0 = 0;
I1 = Math.Cos(initialPhase + DeltaPhase);
Q1 = Math.Sin(initialPhase + DeltaPhase);
}
public Quad(float outputFreq, float sampleFreq, double initialPhase)
{
DeltaPhase = 2 * Math.PI * outputFreq / sampleFreq;
Coeff = 2 * Math.Sin(DeltaPhase);
I0 = Math.Cos(initialPhase);
Q0 = Math.Sin(initialPhase);
I1 = Math.Cos(initialPhase + DeltaPhase);
Q1 = Math.Sin(initialPhase + DeltaPhase);
}
public Quad(float outputFreq, float sampleFreq, Quad initialQuad)
{
DeltaPhase = 2 * Math.PI * outputFreq / sampleFreq;
Coeff = 2 * Math.Sin(DeltaPhase);
float initialPhase = initialQuad.Value.Phase;
I0 = initialQuad.Value.I;
Q0 = initialQuad.Value.Q;
I1 = Math.Cos(initialPhase + DeltaPhase);
Q1 = Math.Sin(initialPhase + DeltaPhase);
}
public static Quad operator ++(Quad a)
{
double Q2 = a.Q0 + a.Coeff * a.I1;
double I2 = a.I0 - a.Coeff * a.Q1;
a.I0 = a.I1; a.I1 = I2;
a.Q0 = a.Q1; a.Q1 = Q2;
return a;
}
public IQ Next()
{
IQ result = new IQ((float)I0, (float)Q0);
double Q2 = Q0 + Coeff * I1;
double I2 = I0 - Coeff * Q1;
I0 = I1; I1 = I2;
Q0 = Q1; Q1 = Q2;
return result;
}
public int Process(float data, out IQ outIQ)
{
outIQ.I = (float)I0 * data;
outIQ.Q = (float)Q0 * data;
double Q2 = Q0 + Coeff * I1;
double I2 = I0 - Coeff * Q1;
I0 = I1; I1 = I2;
Q0 = Q1; Q1 = Q2;
return 1;
}
public static implicit operator IQ(Quad a)
{
return new IQ((float)a.I0, (float)a.Q0);
}
public static IQ operator +(IQ a, Quad b)
{
return a + b.Value;
}
public static IQ operator +(Quad b, IQ a )
{
return a + b.Value;
}
public IQ Value
{
get { return new IQ((float)I0, (float)Q0); }
set {
double initialPhase = value.Phase;
value = value/value.R;
I0 = value.I;
Q0 = value.Q;
I1 = Math.Cos(initialPhase + DeltaPhase);
Q1 = Math.Sin(initialPhase + DeltaPhase);
}
}
public IQ NextValue
{
get { return new IQ((float)I1, (float)Q1); }
}
}
class Generator
{
double OutputFrequency;
double SamplingFrequency;
double DeltaPhase;
double S0, S1, Coeff;
public Generator()
{
}
public Generator(float FreqDest, float FreqSample) : this(FreqDest, FreqSample, 0)
{
}
public Generator(float FreqDest, float FreqSample, float PhaseDest)
{
this.SamplingFrequency = FreqSample;
this.OutputFrequency = FreqDest;
this.DeltaPhase = (2 * Math.PI * FreqDest) / SamplingFrequency;
this.Coeff = 2 * Math.Cos(DeltaPhase);
this.Phase = PhaseDest;
}
/// <summary>
/// Initializes the generator.
/// </summary>
/// <param name="FreqDest">Desired frequency in Hertz</param>
/// <param name="FreqSample">Sampling frequency in Hertz</param>
/// <param name="PhaseDest">Starting phase in radians</param>
public void Init(float FreqDest, float FreqSample, float PhaseDest)
{
this.SamplingFrequency = FreqSample;
this.OutputFrequency = FreqDest;
this.DeltaPhase = (2 * Math.PI * FreqDest) / SamplingFrequency;
this.Coeff = 2 * Math.Cos(DeltaPhase);
this.Phase = PhaseDest;
}
public float Frequency
{
get { return (float)this.OutputFrequency; }
set
{
float CurrentPhase = Phase;
this.OutputFrequency = value;
this.DeltaPhase = (2 * Math.PI * value) / SamplingFrequency;
this.Coeff = 2 * Math.Cos(DeltaPhase);
this.Phase = CurrentPhase;
}
}
public float Phase
{
get
{
S0 = Math.Max(Math.Min(S0, 1.0), -1.0);
double ph0 = Math.Asin(S0);
// Now we have to resolve a phase ambiguity
// Use the formula : cos(w) = (S1 - Cos(D)S0)/sin(D)
// cos(D) = Coeff/2, sin(D) is always positive
double CosW = S1 - Coeff * S0 / 2.0;
if (CosW < 0)
ph0 = Math.PI - ph0;
if (ph0 < 0)
ph0 += (2 * Math.PI);
return (float) ph0;
}
set
{
S0 = Math.Sin(value);
S1 = Math.Sin(value + DeltaPhase);
}
}
public void SetFrequency(float newFrequency)
{
Frequency = newFrequency;
}
public void SetPhase(float newPhase)
{
Phase = newPhase;
}
public int GenerateVoid(int NumSamples)
{
double OldValue;
for (int i = 0; i < NumSamples; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
}
return NumSamples;
}
public int Generate(float Value, float[] outputBuffer, int NumSamples)
{
double OldValue;
for (int i = 0; i < NumSamples; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i] = (float)OldValue * Value;
}
return NumSamples;
}
public int Generate(float[] outputBuffer, int NumSamples)
{
double OldValue;
for (int i = 0; i < NumSamples; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i] = (float)OldValue ;
}
return NumSamples;
}
public int Generate(float[] outputBuffer)
{
double OldValue;
for (int i = 0; i < outputBuffer.Length; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i] = (float)OldValue ;
}
return outputBuffer.Length;
}
public int Generate(float[] modulation, float[] outputBuffer)
{
double OldValue;
int i = 0;
foreach (float modval in modulation)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i++] = (float)OldValue * modval;
}
return modulation.Length;
}
public int Generate(float[] modulation, float[] outputBuffer, int startIndex)
{
double OldValue;
int i = 0;
foreach (float modval in modulation)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[startIndex + i++] = (float)OldValue * modval;
}
return modulation.Length;
}
public int Process(float Value, out float outResult)
{
double OldValue;
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outResult = (float)OldValue * Value;
return 1;
}
public int Process(float[] inputSignal, int inputIndex, float[] outputBuffer, int numSamples)
{
double OldValue;
for( int i = 0; i < numSamples; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i] = (float)OldValue * inputSignal[inputIndex + i];
}
return numSamples;
}
/// <summary>
/// Adds (mixes) the new samples into provided array
/// </summary>
/// <param name="Buffer">The buffer where samples will be added</param>
/// <param name="NumSamples">Number of sampless to add</param>
/// <returns>Resulting array</returns>
public int GenerateAdd(float Value, float[] outputBuffer, int NumSamples)
{
double OldValue;
for (int i = 0; i < NumSamples; i++)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i] += (float)OldValue * Value;
}
return NumSamples;
}
public int GenerateAdd(float[] modulation, float[] outputBuffer)
{
double OldValue;
int i = 0;
foreach (float modval in modulation)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[i++] += (float)OldValue * modval;
}
return modulation.Length;
}
public int GenerateAdd(float[] modulation, float[] outputBuffer, int startIndex)
{
double OldValue;
int i = 0;
foreach (float modval in modulation)
{
OldValue = S0;
S0 = S1;
S1 = S1 * Coeff - OldValue;
outputBuffer[startIndex + i++] += (float)OldValue * modval;
}
return modulation.Length;
}
}
}