-
Notifications
You must be signed in to change notification settings - Fork 16
/
Hilbert.cs
361 lines (326 loc) · 9.53 KB
/
Hilbert.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
using System;
using System.Collections.Generic;
using System.Text;
// Copyright (c) 2006, 2007 by Hugh Pyle, inguzaudio.com
namespace DSPUtil
{
/// <summary>
/// FIR filter with a Dirac pulse at its center
/// </summary>
public class Dirac : SoundObj
{
private int _length;
/// <summary>
/// Constructor
/// </summary>
/// <param name="length">Length (preferably odd)</param>
public Dirac(int length)
{
if (length % 2 == 0)
{
length++;
}
_length = length;
base.NumChannels = 1;
}
/// <summary>Length of the filter</summary>
public override int Iterations
{
get { return _length; }
}
/// <summary>
/// Number of channels: always 1
/// </summary>
public override ushort NumChannels
{
get
{
return 1;
}
set
{
if (value != 1)
{
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Get an iterator for samples of the filter
/// </summary>
public override IEnumerator<ISample> Samples
{
get
{
int mid = (int)((_length - 1) / 2);
for (int j = 0; j < _length; j++)
{
double val = 0;
int n = j - mid;
if (n == 0)
{
val = 1;
}
yield return new Sample(val);
}
}
}
}
/// <summary>
/// FIR filter approximating a Hilbert transform; not windowed
/// </summary>
public class Hilbert : SoundObj
{
private int _length;
/// <summary>
/// Constructor
/// </summary>
/// <param name="length">Length (preferably odd)</param>
public Hilbert(int length)
{
if (length % 2 == 0)
{
length++;
}
_length = length;
base.NumChannels = 1;
}
/// <summary>Length of the filter</summary>
public override int Iterations
{
get { return _length; }
}
/// <summary>
/// Number of channels: always 1
/// </summary>
public override ushort NumChannels
{
get
{
return 1;
}
set
{
if (value != 1)
{
throw new ArgumentOutOfRangeException();
}
}
}
/// <summary>
/// Get an iterator for samples of the filter
/// </summary>
public override IEnumerator<ISample> Samples
{
get
{
int mid = (int)((_length - 1) / 2);
for (int j = 0; j < _length; j++)
{
int n = j - mid;
double val = 0;
if (n % 2 != 0)
{
val = 2 / (Math.PI * n);
}
yield return new Sample(val);
}
}
}
}
/// <summary>
/// Given a signal input
/// Return the (positive only) magnitude Hilbert envelope
/// </summary>
public class HilbertEnvelope : SoundObj
{
private int _length;
private ISoundObj _i;
private ISoundObj _r;
/// <summary>
/// Constructor
/// </summary>
/// <param name="length">Length for Hilbert FIR (preferably odd)</param>
public HilbertEnvelope(int length)
{
if (length % 2 == 0)
{
length++;
}
_length = length;
base.NumChannels = 1;
// Imaginary portion is generated by a Hilbert transform of the real input
_i = new FastConvolver(new Hilbert(length));
// Real portion is a delayed version of the real input
_r = new FastConvolver(new Dirac(length));
}
public override IEnumerator<ISample> Samples
{
get
{
if (_input == null)
{
yield break;
}
_i.Input = _input;
_r.Input = _input;
ushort nc = _input.NumChannels;
IEnumerator<ISample> ienum = _i.Samples;
IEnumerator<ISample> renum = _r.Samples;
bool imore = ienum.MoveNext();
bool rmore = renum.MoveNext();
while (imore && rmore)
{
ISample icurr = ienum.Current;
ISample rcurr = renum.Current;
ISample ret;
if (nc == 2)
{
ret = new Sample2();
}
else
{
ret = new Sample(nc);
}
for (ushort c = 0; c < nc; c++)
{
ret[c] = new Complex(rcurr[c], icurr[c]).Magnitude;
}
yield return ret;
imore = ienum.MoveNext();
rmore = renum.MoveNext();
}
}
}
}
/// <summary>
/// Given a signal X
/// produce a filter returning (a + jb)X
/// where 'a' is the in-phase multiplier, 'b' is the quadrature multiplier, and 'j' means a 90 degree phase shift
/// </summary>
public class PhaseMultiplier : SoundObj
{
private double _a;
private double _b;
private int _length;
private double _f;
/// <summary>
/// Constructor
/// </summary>
/// <param name="phase">in-phase and quadrature multipliers</param>
/// <param name="length">Length (preferably odd)</param>
public PhaseMultiplier(Complex phase, int length)
{
_init(phase, length, 0, 0);
}
public PhaseMultiplier(Complex phase, int length, uint sampleRate)
{
_init(phase, length, sampleRate, 0);
}
public PhaseMultiplier(Complex phase, int length, uint sampleRate, double cornerFreq)
{
_init(phase, length, sampleRate, cornerFreq);
}
private void _init(Complex phase, int length, uint sampleRate, double cornerFreq)
{
if (length % 2 == 0)
{
length++;
}
_a = phase.Re; // inphase scale
_b = phase.Im; // quadrature scale
_length = length;
SampleRate = sampleRate;
_f = cornerFreq;
base.NumChannels = 1;
}
public double PhaseRe
{
get
{
return _a;
}
set
{
_a = value;
}
}
public double PhaseIm
{
get
{
return _b;
}
set
{
_b = value;
}
}
/// <summary>Length of the filter</summary>
public override int Iterations
{
get { return _length; }
}
/// <summary>
/// Number of channels: always 1
/// </summary>
public override ushort NumChannels
{
get
{
return 1;
}
set
{
if (value != 1)
{
throw new ArgumentOutOfRangeException();
}
}
}
public override IEnumerator<ISample> Samples
{
get
{
int mid = (int)((_length - 1) / 2);
// Make a Hilbert transformation filter (for the 'j' component)
Hilbert hilbert = new Hilbert(_length);
// And a window for the filter
BlackmanHarris window = new BlackmanHarris(mid, mid);
if (_f > 0)
{
FilterProfile fp = new FilterProfile();
fp.Add(new FreqGain(0, 0));
fp.Add(new FreqGain(_f / 2, 0));
fp.Add(new FreqGain(_f * 2, -200));
FilterImpulse fi = new FilterImpulse(_length, fp, FilterInterpolation.COSINE, _sr);
FastConvolver fc = new FastConvolver(hilbert, fi);
SoundBuffer sb = new SoundBuffer(fc);
window.Input = sb.Subset(_length / 2, _length);
}
else
{
window.Input = hilbert;
}
// Return the samples
int j=0;
foreach(ISample sample in window)
{
int n = j - mid;
if(n==0)
{
// Set the in-phase component
sample[0] = _a;
}
else
{
// Scale the quadrature component
sample[0] = sample[0] * _b;
}
yield return sample;
j++;
}
}
}
}
}