-
Notifications
You must be signed in to change notification settings - Fork 112
/
stream_filt.jl
728 lines (589 loc) · 21.8 KB
/
stream_filt.jl
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
const PFB{T} = Matrix{T} # polyphase filter bank
abstract type FIRKernel{T} end
# Single rate FIR kernel
mutable struct FIRStandard{T} <: FIRKernel{T}
h::Vector{T}
hLen::Int
end
function FIRStandard(h::Vector)
h = reverse(h)
hLen = length(h)
FIRStandard(h, hLen)
end
# Interpolator FIR kernel
mutable struct FIRInterpolator{T} <: FIRKernel{T}
pfb::PFB{T}
interpolation::Int
Nϕ::Int
tapsPerϕ::Int
inputDeficit::Int
ϕIdx::Int
hLen::Int
end
function FIRInterpolator(h::Vector, interpolation::Integer)
pfb = taps2pfb(h, interpolation)
tapsPerϕ, Nϕ = size(pfb)
interpolation = interpolation
inputDeficit = 1
ϕIdx = 1
hLen = length(h)
FIRInterpolator(pfb, interpolation, Nϕ, tapsPerϕ, inputDeficit, ϕIdx, hLen)
end
# Decimator FIR kernel
mutable struct FIRDecimator{T} <: FIRKernel{T}
h::Vector{T}
hLen::Int
decimation::Int
inputDeficit::Int
end
function FIRDecimator(h::Vector, decimation::Integer)
h = reverse(h)
hLen = length(h)
inputDeficit = 1
FIRDecimator(h, hLen, decimation, inputDeficit)
end
# Rational resampler FIR kernel
mutable struct FIRRational{T} <: FIRKernel{T}
pfb::PFB{T}
ratio::Rational{Int}
Nϕ::Int
ϕIdxStepSize::Int
tapsPerϕ::Int
ϕIdx::Int
inputDeficit::Int
hLen::Int
end
function FIRRational(h::Vector, ratio::Rational)
pfb = taps2pfb(h, numerator(ratio))
tapsPerϕ, Nϕ = size(pfb)
ϕIdxStepSize = mod(denominator(ratio), numerator(ratio))
ϕIdx = 1
inputDeficit = 1
hLen = length(h)
FIRRational(pfb, ratio, Nϕ, ϕIdxStepSize, tapsPerϕ, ϕIdx, inputDeficit, hLen)
end
FIRRational(h::Vector,ratio::Integer)=FIRRational(h,convert(Rational,ratio))
#
# Arbitrary resampler FIR kernel
#
# This kernel is different from the others in that it has two polyphase filtlter banks.
# The the second filter bank, dpfb, is the derivative of pfb. The purpose of this is to
# allow us to compute two y values, yLower & yUpper, whitout having to advance the input
# index by 1. It makes the kernel simple by not having to store extra state in the case
# when where's at the last polphase branch and the last available input sample. By using
# a derivitive filter, we can always compute the output in that scenario.
# See section 7.6.1 in [1] for a better explanation.
mutable struct FIRArbitrary{T} <: FIRKernel{T}
rate::Float64
pfb::PFB{T}
dpfb::PFB{T}
Nϕ::Int
tapsPerϕ::Int
ϕAccumulator::Float64
ϕIdx::Int
α::Float64
Δ::Float64
inputDeficit::Int
xIdx::Int
hLen::Int
end
function FIRArbitrary(h::Vector, rate_in::Real, Nϕ_in::Integer)
rate = convert(Float64, rate_in)
Nϕ = convert(Int, Nϕ_in)
dh = [diff(h); zero(eltype(h))]
pfb = taps2pfb(h, Nϕ)
dpfb = taps2pfb(dh, Nϕ)
tapsPerϕ = size(pfb, 1)
ϕAccumulator = 1.0
ϕIdx = 1
α = 0.0
Δ = Nϕ/rate
inputDeficit = 1
xIdx = 1
hLen = length(h)
FIRArbitrary(
rate,
pfb,
dpfb,
Nϕ,
tapsPerϕ,
ϕAccumulator,
ϕIdx,
α,
Δ,
inputDeficit,
xIdx,
hLen
)
end
# FIRFilter - the kernel does the heavy lifting
mutable struct FIRFilter{Tk<:FIRKernel}
kernel::Tk
history::Vector
historyLen::Int
h::Vector
end
# Constructor for single-rate, decimating, interpolating, and rational resampling filters
"""
FIRFilter(h::Vector[, ratio::Union{Integer,Rational}])
Construct a stateful FIRFilter object from the vector of filter taps `h`.
`ratio` is an optional rational integer which specifies
the input to output sample rate relationship (e.g. `147//160` for
converting recorded audio from 48 KHz to 44.1 KHz).
"""
function FIRFilter(h::Vector, resampleRatio::Union{Integer,Rational} = 1)
interpolation = numerator(resampleRatio)
decimation = denominator(resampleRatio)
historyLen = 0
if resampleRatio == 1 # single-rate
kernel = FIRStandard(h)
historyLen = kernel.hLen - 1
elseif interpolation == 1 # decimate
kernel = FIRDecimator(h, decimation)
historyLen = kernel.hLen - 1
elseif decimation == 1 # interpolate
kernel = FIRInterpolator(h, interpolation)
historyLen = kernel.tapsPerϕ - 1
else # rational
kernel = FIRRational(h, resampleRatio)
historyLen = kernel.tapsPerϕ - 1
end
history = zeros(historyLen)
FIRFilter(kernel, history, historyLen, h)
end
# Constructor for arbitrary resampling filter (polyphase interpolator w/ intra-phase linear interpolation)
"""
FIRFilter(h::Vector, rate::AbstractFloat[, Nϕ::Integer=32])
Returns a polyphase FIRFilter object from the vector of filter taps `h`.
`rate` is a floating point number that specifies the input to output
sample-rate relationship ``\\frac{fs_{out}}{fs_{in}}``. `Nϕ` is an
optional parameter which specifies the number of *phases* created from
`h`. `Nϕ` defaults to 32.
"""
function FIRFilter(h::Vector, rate::AbstractFloat, Nϕ::Integer=32)
rate > 0.0 || throw(DomainError(rate, "rate must be greater than 0"))
kernel = FIRArbitrary(h, rate, Nϕ)
historyLen = kernel.tapsPerϕ - 1
history = zeros(historyLen)
FIRFilter(kernel, history, historyLen, h)
end
# Constructor for a resampling FIR filter, where the user needs only to set the sampling rate
function FIRFilter(rate::AbstractFloat, Nϕ::Integer=32)
h = resample_filter(rate, Nϕ)
FIRFilter(h, rate)
end
function FIRFilter(rate::Union{Integer,Rational})
h = resample_filter(rate)
FIRFilter(h, rate)
end
#
# setphase! set's filter kernel phase index
#
function setphase!(kernel::FIRDecimator, ϕ::Real)
ϕ >= zero(ϕ) || throw(ArgumentError("ϕ must be >= 0"))
xThrowaway = round(Int, ϕ)
kernel.inputDeficit += xThrowaway
nothing
end
function setphase!(kernel::Union{FIRInterpolator, FIRRational}, ϕ::Real)
ϕ >= zero(ϕ) || throw(ArgumentError("ϕ must be >= 0"))
(ϕ, xThrowaway) = modf(ϕ)
kernel.inputDeficit += round(Int, xThrowaway)
kernel.ϕIdx = round(ϕ*(kernel.Nϕ) + 1.0)
nothing
end
function setphase!(kernel::FIRArbitrary, ϕ::Real)
ϕ >= zero(ϕ) || throw(ArgumentError("ϕ must be >= 0"))
(ϕ, xThrowaway) = modf(ϕ)
kernel.inputDeficit += round(Int, xThrowaway)
kernel.ϕAccumulator = ϕ*(kernel.Nϕ) + 1.0
kernel.ϕIdx = round(kernel.ϕAccumulator)
kernel.α = modf(kernel.ϕAccumulator)[1]
nothing
end
setphase!(self::FIRFilter, ϕ::Real) = setphase!(self.kernel, ϕ)
#
# reset! filter and its kernel to an initial state
#
# Generic case for FIRInterpolator and FIRStandard
function reset!(kernel::FIRKernel)
kernel
end
function reset!(kernel::FIRRational)
kernel.ϕIdx = 1
kernel.inputDeficit = 1
kernel
end
function reset!(kernel::FIRDecimator)
kernel.inputDeficit = 1
kernel
end
function reset!(kernel::FIRArbitrary)
kernel.ϕAccumulator = 1.0
kernel.ϕIdx = 1
kernel.α = 0.0
kernel.inputDeficit = 1
kernel.xIdx = 1
kernel
end
# For FIRFilter, set history vector to zeros of same type and required length
function reset!(self::FIRFilter)
fill!(self.history, 0)
reset!(self.kernel)
self
end
#
# taps2pfb
#
# Converts a vector of coefficients to a matrix. Each column is a filter.
# NOTE: also flips the matrix up/down so computing the dot product of a
# column with a signal vector is more efficient (since filter is convolution)
# Appends zeros if necessary.
# Example:
# julia> taps2pfb( [1:9], 4 )
# 3x4 Array{Int64,2}:
# 9 0 0 0
# 5 6 7 8
# 1 2 3 4
#
# In this example, the first phase, or ϕ, is [9, 5, 1].
function taps2pfb(h::Vector{T}, Nϕ::Integer) where T
hLen = length(h)
tapsPerϕ = ceil(Int, hLen/Nϕ)
pfbSize = tapsPerϕ * Nϕ
pfb = Matrix{T}(undef, tapsPerϕ, Nϕ)
hIdx = 1
for rowIdx in tapsPerϕ:-1:1, colIdx in 1:Nϕ
tap = hIdx > hLen ? zero(T) : h[hIdx]
@inbounds pfb[rowIdx,colIdx] = tap
hIdx += 1
end
return pfb
end
#
# Calculates the resulting length of a multirate filtering operation, given a
# FIRFilter{FIRRational} object and an input vector
#
# ( It's hard to explain how this works without a diagram )
#
function outputlength(inputlength::Integer, ratio::Union{Integer,Rational}, initialϕ::Integer)
interpolation = numerator(ratio)
decimation = denominator(ratio)
outLen = ((inputlength * interpolation) - initialϕ + 1) / decimation
ceil(Int, outLen)
end
function outputlength(::FIRStandard, inputlength::Integer)
inputlength
end
function outputlength(kernel::FIRInterpolator, inputlength::Integer)
outputlength(inputlength-kernel.inputDeficit+1, kernel.interpolation, kernel.ϕIdx)
end
function outputlength(kernel::FIRDecimator, inputlength::Integer)
outputlength(inputlength-kernel.inputDeficit+1, 1//kernel.decimation, 1)
end
function outputlength(kernel::FIRRational, inputlength::Integer)
outputlength(inputlength-kernel.inputDeficit+1, kernel.ratio, 1)
end
function outputlength(kernel::FIRArbitrary, inputlength::Integer)
ceil(Int, (inputlength-kernel.inputDeficit+1) * kernel.rate)
end
function outputlength(self::FIRFilter, inputlength::Integer)
outputlength(self.kernel, inputlength)
end
#
# Calculates the input length of a multirate filtering operation,
# given the output length
#
function inputlength(outputlength::Int, ratio::Union{Integer,Rational}, initialϕ::Integer)
interpolation = numerator(ratio)
decimation = denominator(ratio)
inLen = (outputlength * decimation + initialϕ - 1) / interpolation
floor(Int, inLen)
end
function inputlength(::FIRStandard, outputlength::Integer)
outputlength
end
function inputlength(kernel::FIRInterpolator, outputlength::Integer)
inLen = inputlength(outputlength, kernel.interpolation, kernel.ϕIdx)
inLen += kernel.inputDeficit - 1
end
function inputlength(kernel::FIRDecimator, outputlength::Integer)
inLen = inputlength(outputlength, 1//kernel.decimation, 1)
inLen += kernel.inputDeficit - 1
end
function inputlength(kernel::FIRRational, outputlength::Integer)
inLen = inputlength(outputlength, kernel.ratio, kernel.ϕIdx)
inLen += kernel.inputDeficit - 1
end
# TODO: figure out why this fails. Might be fine, but the filter operation might not being stepping through the phases correcty.
function inputlength(kernel::FIRArbitrary, outputlength::Integer)
inLen = floor(Int, outputlength/kernel.rate)
inLen += kernel.inputDeficit - 1
end
function inputlength(self::FIRFilter, outputlength::Integer)
inputlength(self.kernel, outputlength)
end
#
# Calculates the delay caused by the FIR filter in # samples, at the input sample rate, caused by the filter process
#
function timedelay(kernel::Union{FIRRational, FIRInterpolator, FIRArbitrary})
(kernel.hLen - 1)/(2.0*kernel.Nϕ)
end
function timedelay(kernel::Union{FIRStandard, FIRDecimator})
(kernel.hLen - 1)/2
end
function timedelay(self::FIRFilter)
timedelay(self.kernel)
end
#
# Single rate filtering
#
function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRStandard{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
kernel = self.kernel
history::Vector{Tx} = self.history
bufLen = length(buffer)
xLen = length(x)
bufLen >= xLen || throw(ArgumentError("buffer length must be >= length(x)"))
h = kernel.h
for i = 1:min(kernel.hLen-1, xLen)
@inbounds buffer[i] = unsafe_dot(h, history, x, i)
end
for i = kernel.hLen:xLen
@inbounds buffer[i] = unsafe_dot(h, x, i)
end
self.history = shiftin!(history, x)
return xLen
end
#
# Interpolation
#
function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRInterpolator{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
kernel = self.kernel
history::Vector{Tx} = self.history
interpolation = kernel.interpolation
xLen = length(x)
bufLen = length(buffer)
bufIdx = 0
if xLen < kernel.inputDeficit
self.history = shiftin!(history, x)
kernel.inputDeficit -= xLen
return bufIdx
end
inputIdx = kernel.inputDeficit
bufLen >= outputlength(self, xLen) || throw(ArgumentError("length(buffer) must be >= interpolation * length(x)"))
while inputIdx <= xLen
bufIdx += 1
if inputIdx < kernel.tapsPerϕ
accumulator = unsafe_dot(kernel.pfb, kernel.ϕIdx, history, x, inputIdx)
else
accumulator = unsafe_dot(kernel.pfb, kernel.ϕIdx, x, inputIdx)
end
buffer[bufIdx] = accumulator
(kernel.ϕIdx, inputIdx) = kernel.ϕIdx == kernel.Nϕ ? (1, inputIdx+1) : (kernel.ϕIdx+1, inputIdx)
end
kernel.inputDeficit = 1
self.history = shiftin!(history, x)
return bufIdx
end
#
# Rational resampling
#
function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRRational{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
kernel = self.kernel
history::Vector{Tx} = self.history
xLen = length(x)
bufLen = length(buffer)
bufIdx = 0
if xLen < kernel.inputDeficit
self.history = shiftin!(history, x)
kernel.inputDeficit -= xLen
return bufIdx
end
outLen = outputlength(xLen-kernel.inputDeficit+1, kernel.ratio, kernel.ϕIdx)
bufLen >= outLen || throw(ArgumentError("buffer is too small"))
interpolation = numerator(kernel.ratio)
decimation = denominator(kernel.ratio)
inputIdx = kernel.inputDeficit
while inputIdx <= xLen
bufIdx += 1
if inputIdx < kernel.tapsPerϕ
accumulator = unsafe_dot(kernel.pfb, kernel.ϕIdx, history, x, inputIdx)
else
accumulator = unsafe_dot(kernel.pfb, kernel.ϕIdx, x, inputIdx)
end
buffer[bufIdx] = accumulator
inputIdx += div(kernel.ϕIdx + decimation - 1, interpolation)
ϕIdx = kernel.ϕIdx + kernel.ϕIdxStepSize
kernel.ϕIdx = ϕIdx > interpolation ? ϕIdx - interpolation : ϕIdx
end
kernel.inputDeficit = inputIdx - xLen
self.history = shiftin!(history, x)
return bufIdx
end
#
# Decimation
#
function filt!(buffer::AbstractVector{Tb}, self::FIRFilter{FIRDecimator{Th}}, x::AbstractVector{Tx}) where {Tb,Th,Tx}
kernel = self.kernel
bufLen = length(buffer)
xLen = length(x)
history::Vector{Tx} = self.history
bufIdx = 0
if xLen < kernel.inputDeficit
self.history = shiftin!(history, x)
kernel.inputDeficit -= xLen
return bufIdx
end
outLen = outputlength(self, xLen)
inputIdx = kernel.inputDeficit
nbufout = fld(xLen - inputIdx, kernel.decimation) + 1
bufLen >= nbufout || throw(ArgumentError("buffer length insufficient"))
while inputIdx <= xLen
bufIdx += 1
if inputIdx < kernel.hLen
accumulator = unsafe_dot(kernel.h, history, x, inputIdx)
else
accumulator = unsafe_dot(kernel.h, x, inputIdx)
end
@inbounds buffer[bufIdx] = accumulator
inputIdx += kernel.decimation
end
kernel.inputDeficit = inputIdx - xLen
self.history = shiftin!(history, x)
return bufIdx
end
#
# Arbitrary resampling
#
# Updates FIRArbitrary state. See Section 7.5.1 in [1].
# [1] uses a phase accumilator that increments by Δ (Nϕ/rate)
function update!(kernel::FIRArbitrary)
kernel.ϕAccumulator += kernel.Δ
if kernel.ϕAccumulator > kernel.Nϕ
kernel.xIdx += div(kernel.ϕAccumulator-1, kernel.Nϕ)
kernel.ϕAccumulator = mod(kernel.ϕAccumulator-1, kernel.Nϕ) + 1
end
kernel.ϕIdx = floor(Int, kernel.ϕAccumulator)
kernel.α = kernel.ϕAccumulator - kernel.ϕIdx
end
function filt!(
buffer::AbstractVector{Tb},
self::FIRFilter{FIRArbitrary{Th}},
x::AbstractVector{Tx}
) where {Tb,Th,Tx}
kernel = self.kernel
pfb = kernel.pfb
dpfb = kernel.dpfb
xLen = length(x)
bufIdx = 0
history::Vector{Tx} = self.history
# Do we have enough input samples to produce one or more output samples?
if xLen < kernel.inputDeficit
self.history = shiftin!(history, x)
kernel.inputDeficit -= xLen
return bufIdx
end
# Skip over input samples that are not needed to produce output results.
# We do this by seting inputIdx to inputDeficit which was calculated in the previous run.
# InputDeficit is set to 1 when instantiation the FIRArbitrary kernel, that way the first
# input always produces an output.
kernel.xIdx = kernel.inputDeficit
while kernel.xIdx <= xLen
bufIdx += 1
if kernel.xIdx < kernel.tapsPerϕ
yLower = unsafe_dot(pfb, kernel.ϕIdx, history, x, kernel.xIdx)
yUpper = unsafe_dot(dpfb, kernel.ϕIdx, history, x, kernel.xIdx)
else
yLower = unsafe_dot(pfb, kernel.ϕIdx, x, kernel.xIdx)
yUpper = unsafe_dot(dpfb, kernel.ϕIdx, x, kernel.xIdx)
end
# Used to have @inbounds. Restore @inbounds if buffer length
# can be verified prior to access.
buffer[bufIdx] = muladd(yUpper, kernel.α, yLower)
update!(kernel)
end
kernel.inputDeficit = kernel.xIdx - xLen
self.history = shiftin!(history, x)
return bufIdx
end
function filt(self::FIRFilter{Tk}, x::AbstractVector{Tx}) where {Th,Tx,Tk<:FIRKernel{Th}}
bufLen = outputlength(self, length(x))
# In some cases when `filt(::FIRFilter{FIRArbitrary}, x)` is called
# with certain values of `x`, `filt!(buffer, ::FIRFilter{FIRArbitrary}, x)`
# tries to write one sample too many to the buffer and a `BoundsError`
# is thrown. Add one extra sample to catch these exceptional cases.
#
# See https://github.com/JuliaDSP/DSP.jl/issues/317
#
# FIXME: Remove this if and when the code in
# `filt!(buffer, ::FIRFilter{FIRArbitrary}, x)`
# is updated to properly account for pathological arbitrary rates.
if Tk <: FIRArbitrary
bufLen += 1
end
buffer = Vector{promote_type(Th,Tx)}(undef, bufLen)
samplesWritten = filt!(buffer, self, x)
samplesWritten == bufLen || resize!(buffer, samplesWritten)
return buffer
end
#
# Stateless filt implementations
#
# Single-rate, decimation, interpolation, and rational resampling.
function filt(h::Vector, x::AbstractVector, ratio::Union{Integer,Rational})
self = FIRFilter(h, ratio)
filt(self, x)
end
# Arbitrary resampling with polyphase interpolation and two neighbor linear interpolation.
function filt(h::Vector, x::AbstractVector, rate::AbstractFloat, Nϕ::Integer=32)
self = FIRFilter(h, rate, Nϕ)
filt(self, x)
end
"""
resample(x::AbstractVector, rate::Real[, coef::Vector])
Resample `x` at rational or arbitrary `rate`.
`coef` is an optional vector of FIR filter taps. If `coef`
is not provided, the taps will be computed using a Kaiser window.
Internally, `resample` uses a polyphase `FIRFilter` object,
but performs additional operations to make resampling a signal easier.
It compensates for the `FIRFilter`'s delay (ramp-up), and appends
zeros to `x`. The result is that when the input and output signals
are plotted on top of each other, they correlate very well, but one
signal will have more samples than the other.
"""
function resample(x::AbstractVector, rate::Real, h::Vector)
self = FIRFilter(h, rate)
# Get delay, in # of samples at the output rate, caused by filtering processes
τ = timedelay(self)
# Use setphase! to
# a) adjust the input samples to skip over before producing and output (integer part of τ)
# b) set the ϕ index of the PFB (fractional part of τ)
setphase!(self, τ)
# Calculate the number of 0's required
outLen = ceil(Int, length(x)*rate)
reqInlen = inputlength(self, outLen)
reqZerosLen = reqInlen - length(x)
xPadded = [x; zeros(eltype(x), reqZerosLen)]
filt(self, xPadded)
end
function resample(x::AbstractVector, rate::Real)
h = resample_filter(rate)
resample(x, rate, h)
end
"""
resample(x::AbstractArray, rate::Real, h::Vector = resample_filter(rate); dims)
Resample an array `x` along dimension `dims`.
"""
function resample(x::AbstractArray, rate::Real, h::Vector = resample_filter(rate); dims)
mapslices(x; dims) do x
resample(x, rate, h)
end
end
#
# References
#
# [1] F.J. Harris, *Multirate Signal Processing for Communication Systems*. Prentice Hall, 2004
# [2] Dick, C.; Harris, F., "Options for arbitrary resamplers in FPGA-based modulators," Signals, Systems and Computers, 2004. Conference Record of the Thirty-Eighth Asilomar Conference on , vol.1, no., pp.777,781 Vol.1, 7-10 Nov. 2004
# [3] Kim, S.C.; Plishker, W.L.; Bhattacharyya, S.S., "An efficient GPU implementation of an arbitrary resampling polyphase channelizer," Design and Architectures for Signal and Image Processing (DASIP), 2013 Conference on, vol., no., pp.231,238, 8-10 Oct. 2013
# [4] Horridge, J.P.; Frazer, Gordon J., "Accurate arbitrary resampling with exact delay for radar applications," Radar, 2008 International Conference on , vol., no., pp.123,127, 2-5 Sept. 2008
# [5] Blok, M., "Fractional delay filter design for sample rate conversion," Computer Science and Information Systems (FedCSIS), 2012 Federated Conference on , vol., no., pp.701,706, 9-12 Sept. 2012