-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFarrowFIR.hpp
More file actions
657 lines (645 loc) · 33 KB
/
FarrowFIR.hpp
File metadata and controls
657 lines (645 loc) · 33 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* *
* * Filename: FarrowDelayLine.hpp
* *
* * Description:
* * A time-varying fractional delay using Farrow's method for Lagrange interpolation.
* * instead of using the Direct Form FIR filter, we use a Farrow structure
* * to compute the fractional delay. A Farrow filter rewrites any polynomial
* * based fractional delay FIR as a sum of fixed sub-filter whore outputs are
* * combined by powers of mu. It's nice and very stable for audio-rate modulation
* * of delay lines, like pitch bends. It's less compuationally consumptive too.
* * y[n, mu] = sum_{m=0}^M (mu^m * v_m[n]), with v_m[n] = sum_{k=0}^K (c_{m,k} * x[n - D - k])
* * Note: SIMD capable.
* *
* * Where:
* * M = polynoimial order (for an N-th order Lagrance Fractional Delay M=N).
* * c_{m,k} are the fixed FIR taps (constant at run-time).
* * All time-varying cost is in the Horner-style evaluation of sum_{m=0}^M (mu^m * v_m[n]).
* * Author:
* * JEP J.Enrique Peraza
* *
*/
#pragma once
#include <array>
#include <cstddef>
#include <algorithm>
#include <complex>
#include <cmath>
#include <experimental/simd>
#include "DelayLine.hpp"
template<class pack> inline pack ipow(pack base, size_t n) noexcept
{
pack r=pack(1);
for(; n; --n) r*=base;
return r;
}
namespace sdr::mdm
{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Helper: Compile-time 2-D array for coefficients matrix (max order +1)^2
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T, size_t MaxOrder>
using CoeffMatrix=std::array<std::array<T,MaxOrder+1>,MaxOrder+1>;
using cplx=std::complex<float>;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Polynomial helpers - naive because orders are tine (<=5)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
namespace detail
{
// ~~~~~~~~~~~~~~~~~~~~~~~- //
// Expand product (x-m) for all m in the provided list, normalize by
// the denominator.
// ~~~~~~~~~~~~~~~~~~~~~~~- //
template <typename T, size_t Max>
void MultiplyMonomial( // Multiply a monomial (x-m) for all m in the provided list
std::array<T,Max>& p, // Polynomial coefficients array
size_t order, // Order of the polynomial
T root, // The root to multiply by
T denom) noexcept // The denominator to normalize by
{ // ~~~~~~~~ MultiplyMonomial ~~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~~~~ //
// Poly holds coefficients up to current degree (inclusive)
// newpoly=poly*(x-root)/denominator
// ~~~~~~~~~~~~~~~~~~~~~~ //
for (size_t i=std::min<std::size_t>(order+1,Max-1);i-->0;) // For each coefficient in the polynomial.
{ // Compute new polynomial coefficients.
p[i+1]+=p[i]/denom; // Compute the new coefficient.
p[i]*=(-root)/denom; // Update the next coefficient.
} // Done with the new polynomial coefficients.
} // ~~~~~~~~ MultiplyMonomial ~~~~~~~~~~~~ //
// Note: The generic template above works for both real and complex types,
// so a separate complex overload is unnecessary and caused a redefinition.
// ~~~~~~~~~~~~~~~~~~~~~~~- //
// Build Lagrance basis polynomial L-k(x) of degree N (0<=k<=N)
// ~~~~~~~~~~~~~~~~~~~~~~~- //
template <typename T,size_t Max>
std::array<T,Max> BuildLagrangeCoeffs(
size_t k, // The index of the Lagrange polynomial to build
size_t N) noexcept // The order of the Lagrange polynomial
{ // ~~~~~~~~ BuildLagrangeCoeffs ~~~~~~~~-- //
std::array<T,Max> coefs{}; // Coefficients of the Lagrange polynomial
coefs[0]=T(1); // Initialize the first coefficient to 1
size_t deg=0; // Degree of the polynomial
for (size_t m=0;m<=N;++m) // For the order of the polynomial
{ // Compute the coefficients of the Lagrange polynomial
if (m==k) // Is the current index equal to K?
continue; // Yes not this one.
MultiplyMonomial(coefs,deg,static_cast<T>(m),static_cast<T>(k)-static_cast<T>(m));// Multiply the monomial (x-m) for all m in the provided list
++deg; // Increment the degree of the polynomial
} // Done with the coefficients of the Lagrange polynomial
return coefs; // Return the coefficients of the Lagrange polynomial
} // ~~~~~~~~ BuildLagrangeCoeffs ~~~~~~~~-- //
} // namespace detail
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Farrow-structure Lagrange Fractional-Delay Filter (order <= MaxOrder)
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T=float,
size_t MaxLen=1024,
size_t MaxOrder=5>
class FarrowInterpolator
{
public:
using cplx=std::complex<T>;
constexpr static size_t MAXORDER=MaxOrder;
~FarrowInterpolator(void) noexcept = default; // Default destructor
void SetOrder(
size_t N) noexcept // Set the order of the Lagraange Interpolator
{ // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
order=std::min<size_t>(N,MaxOrder);
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
void SetMu( // Set the fractional part of the delay line
T mu ) noexcept // The fractional part of the delay
{ // ~~~~~~~~ SetMu ~~~~~~~~~~~~ //
this->mu=mu-std::floor(mu); // Set the fractional part of the delay
}
template<typename DL>
bool Process (
const DL& minidl, // Delay line like object
size_t D, // The total delay encoded in size_t
T* const out) noexcept // The output stream
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D<order || D>MaxLen-1) return false;
std::array<T,MaxOrder+1> v{}; // Zero the output buffer
// ~~~~~~~~~~~~~~~~~~~ //
// Calculate the coefficients using Horner's method fo mu^m
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t k=0;k<=order;++k)// Rows
v[k]=minidl->Peek(D-k); // Get the sample at index D+k from the delay line
// ~~~~~~~~~~~~~~~~~~~- //
// Polynomial accumulation: Horner's evaluation for current mu
// y=(((v_N)*mu+n_{N-1})*mu+ ...
// +v_0)*mu+v_0
// ~~~~~~~~~~~~~~~~~~~- //
T cum=coeff[order][order]*v[0]; // seed with highest µ-power term
for (int m=static_cast<int>(order-1);m>=0;--m) // For each coefficient in the Lagrange polynomial
cum=cum*mu+coeff[m][order]*v[order-m]; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
*out=cum; // Store the output sample in the output buffer
return true; // Return true to indicate success
}
bool Process(
const DelayLine<T,MaxLen>& dl, // The delay line to process
size_t D, // The fractional delay in samples
T* const y) noexcept // Output buffer.
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D<order || D > MaxLen - 1) return false;
size_t maxD=MaxLen-1-order; // Maximum delay length.
if (D>maxD) D=maxD; // Clamp the delay to the
// ~~~~~~~~~~~~~~~~~~~- //
// v_m=S_k (C[m][k]*x[n-D-k]))
// ~~~~~~~~~~~~~~~~~~~- //
std::array<T,MaxOrder+1> v{}; // Zero the output buffer
for (size_t k=0;k<=order;++k) // For each coefficient in the Lagrange polynomial
{
const T xk=dl.Peek(D-k); // Get the sample at index D+k from the delay line
for (size_t m=0;m<=order;++m)
v[m]+=coeff[m][k]*xk; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
} // Done with the output samples.
// ~~~~~~~~~~~~~~~~~~~ //
// Perform Horner's evalueation of coeffiecient expansion
// y=(((v_N)*mu+n_{N-1})*mu+ ... +v_0)*mu+v_0
// ~~~~~~~~~~~~~~~~~~~ //
*y=0; // Initialize the output sample with the last coefficient
for (int i=static_cast<int>(order);i>=0;--i) // For each coefficient in the Lagrange polynomial
*y=(*y)*mu+v[i]; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
return true; // Return true to indicate success
} // ~~~~~~~- Process ~~~~~~~~~~~ //
bool Process (
const DelayLine<cplx,MaxLen>& dl, // Delay line like object
size_t D, // The total delay encoded in size_t
cplx* const out) noexcept // The output stream
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D<order || D>MaxLen-1) return false;
std::array<cplx,MaxOrder+1> v{}; // Zero the output buffer
// ~~~~~~~~~~~~~~~~~~~ //
// Calculate the coefficients using Horner's method fo mu^m
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t k=0;k<=order;++k)// Rows
v[k]=dl.Peek(D-k); // Get the sample at index D+k from the delay line
// ~~~~~~~~~~~~~~~~~~~- //
// Polynomial accumulation: Horner's evaluation for current mu
// y=(((v_N)*mu+n_{N-1})*mu+ ...
// +v_0)*mu+v_0
// ~~~~~~~~~~~~~~~~~~~- //
cplx cum=coeff[order][order]*v[0];// seed with highest µ-power term
for (int m=static_cast<int>(order-1);m>=0;--m) // For each coefficient in the Lagrange polynomial
cum=cum*mu+coeff[m][order]*v[order-m]; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
*out=cum; // Store the output sample in the output buffer
return true; // Return true to indicate success
}
inline const std::array<T, MaxOrder+1>& operator[](size_t i) const noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeff[i];
}
inline std::array<T, MaxOrder+1>& operator[](size_t i) noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeff[i];
}
private:
size_t order{3}; // Order of the Lagrange filter, default is 3.
T mu=T(0); // Fractional part of the delay, default is 0.
CoeffMatrix<T,MaxOrder> coeff{}; // Coefficients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept // Build the coefficients for the Lagrange filter
{ // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~~~~-- //
// coeff[m][k]=polynomial coefficients of mu^m for tap k
// ~~~~~~~~~~~~~~~~~~~~~~-- //
for (size_t k=0;k<=order;++k) // For each coefficient in the interpolator
{
auto c=detail::BuildLagrangeCoeffs<T,MaxOrder+1>(k,order);
for (size_t m=0;m<=order;++m) // For the order of the filter.
coeff[m][k]=c[m]; // Store the coefficients in the matrix
} // Done with the coefficients.
} // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
}; // class FarrowInterpolator
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Complex Farrow Interpolator
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T=float,
size_t MaxLen=1024,
size_t MaxOrder=5>
class FarrowInterpolatorCplx
{
public:
using cplx=std::complex<T>;
constexpr static size_t MAXORDER=MaxOrder;
~FarrowInterpolatorCplx(void) noexcept = default; // Default destructor
void SetOrder(
size_t N) noexcept // Set the order of the Lagraange Interpolator
{ // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
order=std::min<size_t>(N,MaxOrder);
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
void SetMu( // Set the fractional part of the delay line
T mu ) noexcept // The fractional part of the delay
{ // ~~~~~~~~ SetMu ~~~~~~~~~~~~ //
this->mu=mu-std::floor(mu); // Set the fractional part of the delay
}
bool Process (
const DelayLine<cplx,MaxLen>& dl, // Delay line like object
size_t D, // The total delay encoded in size_t
cplx* const out) noexcept // The output stream
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D<order || D>MaxLen-1) return false;
std::array<cplx,MaxOrder+1> v{}; // Sub-filter outputs v_m
for (size_t k=0;k<=order;++k) // Gather P+1 integer taps
{
const cplx xk = dl.Peek(D - k);
for (size_t m=0;m<=order;++m)
v[m] += coeff[m][k] * xk; // Accumulate sub-filter outputs
}
// Horner evaluation across mu
cplx y = v[order];
for (int m=static_cast<int>(order)-1; m>=0; --m)
y = y * mu + v[m];
*out = y;
return true;
} // ~~~~~~~- Process ~~~~~~~~~~~ //
private:
size_t order{3}; // Order of the Lagrange filter, default is 3.
T mu=T(0); // Fractional part of the delay, default is 0.
CoeffMatrix<cplx,MaxOrder> coeff{}; // Coefficients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept // Build the coefficients for the Lagrange filter
{ // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~~~~-- //
// coeff[m][k]=polynomial coefficients of mu^m for tap k
// ~~~~~~~~~~~~~~~~~~~~~~-- //
for (size_t k=0;k<=order;++k) // For each coefficient in the interpolator
{
auto c=detail::BuildLagrangeCoeffs<cplx,MaxOrder+1>(k,order);
for (size_t m=0;m<=order;++m) // For the order of the filter.
coeff[m][k]=c[m]; // Store the coefficients in the matrix
} // Done with the coefficients.
} // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
}; // class FarrowInterpolatorCplx
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Farrow Deinterpolator to estimate the fractional delay of where to
// write the sample in the delay line. As opposed to the interpolator, that determins
// what the output sample iss, the deinterpolator determines where the input sample
// should be written in the delay line.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T=float,
size_t MaxLen=1024,
size_t MaxOrder=5>
class FarrowDeinterpolator
{
public:
FarrowDeinterpolator(void) noexcept
{
BuildCoefficients(); // Build the coefficients for the Lagrange filter
}
~FarrowDeinterpolator(void) noexcept = default; // Default destructor
/// Note that the order MUST be the same as in the interpolator.
void SetOrder(size_t N) noexcept // Set the deinterpolator order
{
order=std::min<size_t>(N,MaxOrder);// Clamp the order to the maximum order
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
T GetMu(void) const noexcept // Get the fractional part of the delay
{
return mu; // Return the fractional part of the delay
}
// Apply the inverse Farrow filter one write (distribute x into fractional slots)
bool Process(
T x, // The sample to write to the delay line
DelayLine<T,MaxLen>& dl, // The delay line to write to (mutable)
size_t D) noexcept // The total delay in samples
{
if (D<order || D>MaxLen-1) return false;
std::array<T,MaxOrder+1> v{}; // Zero the output buffer
// ~~~~~~~~~~~~~~~~~~~ //
// Calculate the coefficients using Horner's method fo mu^m
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t m=0;m<=order;++m)// Rows
{
for (size_t k=0;k<=order;++k)// Cols
{
if (k==0)
v[m]=coeff[m][k]*x; // Initialize the first coefficient
else
v[m]+=coeff[m][k]*std::pow(mu,k); // Compute the coefficient for the current order and tap
}
} // Done with the coefficients.
// ~~~~~~~~~~~~~~~~~~~ //
// Perform additive write to the delay line
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t k=0;k<=order;++k)
dl.WriteAt(D - k, dl.Peek(D - k) + v[k]);
return true; // Return true to indicate success
}
// Coefficient access
const auto& GetCoeff(void) const noexcept { return coeff; } // ~~~~~~~- Process ~~~~~~~~~~~ //
/// Set fractional delay parameter mu
void SetMu(T mu_val) noexcept { mu = mu_val - std::floor(mu_val); }
private:
size_t order{3}; // Order MUST be the same as in the interpolator.
T mu{0}; // Fractional part of the delay, default is 0.
CoeffMatrix<T,MaxOrder> coeff{}; // Coefficients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept
{
for (size_t k=0;k<=order;++k)
{ // For each coefficient in the filter...
auto c=detail::BuildLagrangeCoeffs<T,MaxOrder+1>(k,order);
for (size_t m=0;m<=order;++m)
coeff[m][k]=T(c[m]); // Store the coefficients in the matrix
}
}
}; // class FarrowDeinterpolator
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Farrow Deinterpolator Complex
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T=cplx,
size_t MaxLen=1024,
size_t MaxOrder=5>
class FarrowDeinterpolatorCplx
{
public:
FarrowDeinterpolatorCplx(void) noexcept
{
BuildCoefficients(); // Build the coefficients for the Lagrange filter
}
~FarrowDeinterpolatorCplx(void) noexcept = default; // Default destructor
/// Note that the order MUST be the same as in the interpolator.
void SetOrder(size_t N) noexcept // Set the deinterpolator order
{
order=std::min<size_t>(N,MaxOrder);// Clamp the order to the maximum order
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
T GetMu(void) const noexcept // Get the fractional part of the delay
{
return mu; // Return the fractional part of the delay
}
// Apply the inverse Farrow filter one write (distribute x into fractional slots)
bool Process(
T x, // The sample to write to the delay line
DelayLine<T,MaxLen>& dl, // The delay line to write to (mutable)
size_t D) noexcept // The total delay in samples
{ // ~~~~~~~~~~~~~ Process ~~~~~~~~~~~~~~ //
if (D<order || D>MaxLen-1) return false;
std::array<T,MaxOrder+1> v{}; // Zero the output buffer
// ~~~~~~~~~~~~~~~~~~~ //
// Calculate the coefficients using Horner's method fo mu^m
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t m=0;m<=order;++m)// Rows
{
for (size_t k=0;k<=order;++k)// Cols
{
if (k==0)
v[m]=coeff[m][k]*x; // Initialize the first coefficient
else
v[m]+=coeff[m][k]*std::pow(mu,k); // Compute the coefficient for the current order and tap
}
} // Done with the coefficients.
// ~~~~~~~~~~~~~~~~~~~ //
// Perform additive write to the delay line
// This equation does all the work.
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t k=0;k<=order;++k)
dl.WriteAt(D-k,dl.Peek(D-k)+v[k]);
return true; // Return true to indicate success
} // ~~~~~~~~ Process ~~~~~~~~~~~ //
// Coefficient access
const auto& GetCoeff(void) const noexcept { return coeff; }
/// Set fractional delay parameter mu
void SetMu(T mu_val) noexcept { mu = mu_val - std::floor(mu_val); }
private:
size_t order{3}; // Order MUST be the same as in the interpolator.
T mu{0}; // Fractional part of the delay, default is 0.
CoeffMatrix<T,MaxOrder> coeff{}; // Coefficients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept
{
for (size_t k=0;k<=order;++k)
{ // For each coefficient in the filter...
auto c=detail::BuildLagrangeCoeffs<T,MaxOrder+1>(k,order);
for (size_t m=0;m<=order;++m)
coeff[m][k]=T(c[m]); // Store the coefficients in the matrix
}
}
}; // class FarrowDeinterpolatorCplx
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// Variable fractional delay line using Farrow's approach to Lagrange interpolation
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
template<typename T=float,
size_t MaxLen=1024,
size_t MaxOrder=5,
typename vT = std::experimental::native_simd<T>>
class FarrowInterpolatorSIMD
{
static_assert(MaxOrder <= 5, "MaxOrder must be <= 5 for FarrowInterpolatorSIMD");
static constexpr size_t VL=vT::size();
using Coeff=std::array<std::array<vT,MaxOrder+1>,MaxOrder+1>;
public:
constexpr static size_t MAXORDER=MaxOrder;
~FarrowInterpolatorSIMD(void) noexcept = default; // Default destructor
void SetOrder(
size_t N) noexcept // Set the order of the Lagraange Interpolator
{ // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
order=std::min<size_t>(N,MaxOrder);
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
bool Prepare(
size_t order,
float f) noexcept
{
if (order==0||f<0.0||f>=static_cast<float>(order))
return false; // Sanitize input: can't have zero order or invalid delay.
this->order=std::min<size_t>(order,MaxOrder); // Set the order of the Farrow filter
mu=vT(f-std::floor(f)); // Set the fractional delay.
BuildCoefficients(); // Calculate the coefficients for the Farrow filter.
return true; // Return true if preparation was successful.
}
/**
* @brief Processes a sample(s) using the Farrow interpolator.
* @param dl The delay line to process.
* @param D The fractional delay in samples.
* @param y Pointer to output buffer
* @return 0 is good? value.
*/
inline vT ProcessFrame(
const DelayLineSIMD<T,MaxLen,vT>& dl, // The delay line to process
size_t D) noexcept // The total delay
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D>order && D<MaxLen-1) return vT(0);
std::array<vT,MaxOrder+1> v{}; // Zero the output buffer
size_t maxD=MaxLen-1-order; // Maximum delay length.
// ~~~~~~~~~~~~~~~~~~~- //
// v_m=S_k (C[m][k]*x[n-D-k])) <-Gether P+1 integer taps
// ~~~~~~~~~~~~~~~~~~~- //
for (size_t k=0;k<=order;++k) // For each coefficient in the Lagrange polynomial
{
const vT xk=dl.Peek(D-k); // Get the sample at index D+k from the delay line
for (size_t m=0;m<=order;++m)
v[m]+=coeffvT[m][k]*xk; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
} // Done with the output samples.
// ~~~~~~~~~~~~~~~~~~~ //
// Perform Horner's evalueation of coeffiecient expansion
// y=(((v_N)*mu+n_{N-1})*mu+ ... +v_0)*mu+v_0
// ~~~~~~~~~~~~~~~~~~~ //
vT y = vT(0); // Initialize the output sample to zero
for (int m=static_cast<int>(order); m>=0; --m) // For each coefficient in the Lagrange polynomial
y*=*mu+v[m]; // Accumulate using Horner's method
return y; // Return the processed output value
} // ~~~~~~~- Process ~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~ //
// Process a block of frames using the Farrow interpolator.
// ~~~~~~~~~~~~~~~~~~~ //
inline vT ProcessBlock(
const DelayLineSIMD<T,MaxLen,vT>& dl,
size_t D, // The total delay in samples
size_t nFrames, // The number of frames to process
vT* const y) noexcept // The output buffer
{ // ~~~~~~~- ProcessBlock ~~~~~~~~~~~ //
if (D<order || D>MaxLen-1) return vT(0);
for (size_t i=0;i<nFrames;++i)// For each row (frame) in the block...
y[i]=ProcessFrame(dl, D); // Process each frame using the Farrow interpolator
return vT(0); // Return zero as a placeholder
}
inline const std::array<vT, MaxOrder+1>& operator[](size_t i) const noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeffvT[i];
}
inline std::array<vT, MaxOrder+1>& operator[](size_t i) noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeffvT[i];
}
private:
size_t order{3}; // Order of the Lagrange filter, default is 3.
vT mu{vT(0)}; // Fractional part of the delay, default is 0.
Coeff coeffvT{}; // coeffvTicients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept // Build the coefficients for the Lagrange filter
{ // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
// ~~~~~~~~~~~~~~~~~~~~~~-- //
// coeff[m][k]=polynomial coefficients of mu^m for tap k
// ~~~~~~~~~~~~~~~~~~~~~~-- //
for (size_t m=0;m<=order;++m) // For each order
{
auto c=detail::BuildLagrangeCoeffs<T,MaxOrder+1>(0,order); // Build the coefficients for the Lagrange filter
for (size_t k=0;k<=order;++k) // For each tap
coeffvT[m][k]=VT(c[m][k]); // Store the coefficients in the matrix
}
}
};
template<typename T=float,
size_t MaxLen=1024,
size_t MaxOrder=5,
typename vT=std::experimental::native_simd<T>>
class FarrowDeinterpolatorSIMD
{
static_assert(MaxOrder <= 5, "MaxOrder must be <= 5 for FarrowInterpolatorSIMD");
static constexpr size_t VL=vT::size();
using Coeff=std::array<std::array<vT,MaxOrder+1>,MaxOrder+1>;
public:
constexpr static size_t MAXORDER=MaxOrder;
~FarrowDeinterpolatorSIMD(void) noexcept = default; // Default destructor
void SetOrder(
size_t N) noexcept // Set the order of the Lagraange Interpolator
{ // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
order=std::min<size_t>(N,MaxOrder);
BuildCoefficients(); // Build the coefficients for the Lagrange filter
} // ~~~~~~~~ SetOrder ~~~~~~~~~~~~ //
void SetMu(const vT& m) noexcept // Set the fract part of the dela
{
mu=m-vT(std::floor(m)); // Set the fractional part of the delay
}
vT GetMu(void) const noexcept // Get the fractional part of the delay
{
return mu; // Return the fractional part of the delay
}
// Apply the inverse Farrow filter one write (distribute x into fractional slots)
bool Process(
const vT& x, // The sample to write to the delay line
DelayLineSIMD<T,MaxLen,vT>& dl, // The delay line to write to (mutable)
size_t D) noexcept // The total delay in samples
{
if (D<order || D>MaxLen-1) return false;
std::array<vT,MaxOrder+1> v{};
// ~~~~~~~~~~~~~~~~~~~ //
// Calculate the coefficients using Horner's method fo mu^m
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t m=0;m<=order;++m)// Rows
{
for (size_t k=0;k<=order;++k)// Cols
{
if (k==0)
v[m]=coeffvT[m][k]*x; // Initialize the first coeffvTicient
else
v[m]+=coeffvT[m][k]*std::pow(mu,k); // Compute the coefficient
}
} // Done with the coefficients.
// ~~~~~~~~~~~~~~~~~~~ //
// Perform additive write to the delay line
// ~~~~~~~~~~~~~~~~~~~ //
for (size_t k=0;k<=order;++k) // For each coefficient in
dl.WriteAt(D-k,dl.Peek(D-k)+v[k]);// Additive write to the delay line
return true; // Return true to indicate success
} // ~~~~~~~- Process ~~~~~~~~~~~ //
inline vT ProcessFrame(
const DelayLineSIMD<T,MaxLen,vT>& dl, // The delay line to process
size_t D) noexcept // Output buffer.
{ // ~~~~~~~- Process ~~~~~~~~~~~ //
if (D<order || D > MaxLen - 1) return vT(0);
size_t maxD=MaxLen-1-order; // Maximum delay length.
if (D>maxD) D=maxD; // Clamp the delay to the
// ~~~~~~~~~~~~~~~~~~~- //
// v_m=S_k (C[m][k]*x[n-D-k]))
// ~~~~~~~~~~~~~~~~~~~- //
std::array<vT,MaxOrder+1> v{}; // Zero the output buffer
for (size_t k=0;k<=order;++k) // For each coefficient in the Lagrange polynomial
{
const vT xk=dl.Peek(D-k); // Get the sample at index D+k from the delay line
for (size_t m=0;m<=order;++m)
v[m]+=coeffvT[m][k]*xk; // Compute the output sample
} // Done with the output samples.
vT y=v[order];
// ~~~~~~~~~~~~~~~~~~~ //
// Perform Horner's evalueation of coeffiecient expansion
// y=(((v_N)*mu+n_{N-1})*mu+ ... +v_0)*mu+v_0
// ~~~~~~~~~~~~~~~~~~~ //
for (int m=static_cast<int>(order)-1;m>=0;--m) // For each coefficient in the Lagrange polynomial
y=y*mu+v[m]; // Compute the output sample by summing the products of the coefficients and the corresponding samples from the delay line
return y; // Return true to indicate success
} // ~~~~~~~- Process ~~~~~~~~~~~ //
inline vT ProcessBlock(
const DelayLineSIMD<T,MaxLen,vT>& dl, // Our Delay line buffer to deinterpolate to
size_t D, // The total delay in samples
size_t nFrames, // The number of frames to process
vT* const y) noexcept // The output buffer
{
if (D<order||D>MaxLen-1) return false;
// ~~~~~~~~~~~~~~~~~~~~~~- //
// A block is just a slice of arrays bro and they contain signals.
// ~~~~~~~~~~~~~~~~~~~~~~- //
for (size_t n=0;n<nFrames;++n)
y[n]=ProcessFrame(dl,D+n);
return vT(0); // Return zero as a placeholder
}
inline const std::array<vT, MaxOrder+1>& operator[](size_t i) const noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeffvT[i];
}
inline std::array<vT, MaxOrder+1>& operator[](size_t i) noexcept // ~~~~~~~~ GetCoefficients ~~~~~~~~~~~~ //
{
assert(i < MaxOrder+1);
return coeffvT[i];
}
private:
size_t order{3}; // Order MUST be the same as in the interpolator.
vT mu{vT(0)}; // Fractional part of the delay
Coeff coeffvT{}; // Coefficients for the Lagrange filter, size is order+1.
void BuildCoefficients(void) noexcept
{
for (size_t k=0;k<=order;++k)
{ // For each coefficient in the filter...
auto c=detail::BuildLagrangeCoeffs<vT,MaxOrder+1>(k,order);
for (size_t m=0;m<=order;++m)
coeffvT[m][k]=vT(c[m]); // Store the coeffvTicients in the matrix
}
} // ~~~~~~~~ BuildCoefficients ~~~~~~~~~~~~ //
};
} // namespace sig::wg