Design of Fractional Delay FIR Filters
Fractional delay filters are aimed at shifting a digital sequence by a noninteger value, through interpolation and resampling combined into a single convolution filter. This example demonstrates the design and implementation of fractional delay FIR filters using tools available in the DSP System Toolbox™.
Delay as a Convolution System
Integer Delays Delays
Consider the delay of a digital signal, where D is an integer. This operation can be represented as a convolution filter , with a finite impulse response . The corresponding transfer function is , and the frequeny response is . Programmatically, you can implement such an integer delay filter using the following MATLAB® code.
% Create the FIR D = 3; % Delay value h = [zeros(1,D) 1]
h = 1×4 0 0 0 1
Shift a sequence by filtering it through the FIR
h. Note the leading zeros at the beginning of the output, those signify the initial condition that is inherent to such filters.
x = (1:10)'; dfir = dsp.FIRFilter(h); y = dfir(x)'
y = 1×10 0 0 0 1 2 3 4 5 6 7
Non-Integer Delays via D/A Interpolation
Delaying a sequence is not defined whenever D is not an integer. To make such fractional delays sensible, one needs to add an intermediate D/A interpolation stage so as to sample the output on the continuum. That is, where denotes some D/A interpolation of the input sequnce . The D/A interpolating function could depend on with n, and could be thought of as a representation of an underlying analog signal model from which the sequence was sampled. This strategy is used in other resampling problems such as rate conversion.
This example will feature fractional delay filters using two interpolation models, both of which are offered as a part of the DSP Systems Toolbox.
The sinc-based interpolation model, which uses a bandlimited reconstruction for .
The Lagrange-based interpolation model, which uses a polynomial reconstruction for .
Bandlimited Fractional Delay Filters
The Shannon-Whittaker interpolation formula models bandlimited signals. That is, the intermediate D/A conversion is a bandlimited reconstruction of the input sequence. For a delay value D, the fractional delay , which uses the same for every n, can be represented as a convolution filter. This filter is called the ideal bandlimited fractional delay filter, and its impulse response is
The corresponding frequency response (that is, DTFT) is given by .
Causal FIR Approximation of the Ideal Bandlimited Shift Filter
The ideal sinc shift filter described in the previous section is an all-pass filter (i.e. ), but it has an infinite and non-causal impulse response . In MATLAB, it cannot be represented as a vector, but rather as a function of an index k.
% Ideal Filter sequence D = 0.4; hIdeal = @(k) sinc(k-D);
For practical and computational purposes, the ideal filter can be truncated on a finite index window, at a cost of some bandwidth loss. For a target delay value of and a desired length of N, the window of indices satisfying is symmetric about , and captures the main lobe of the ideal filter. For where and an integer , the explicit window indices are. The integer is referred to as the integer latency, and can be chosen arbitrarily. To make the FIR causal, set , so the index window is . The code below depicts the rationale behind the causal FIR approximation.
% FIR approximation with causal shift N = 6; idxWindow = (-floor((N-1)/2):floor(N/2))'; i0 = -idxWindow(1); % Causal latency hApprox = hIdeal(idxWindow); plot_causal_fir("sinc",D,N,i0,hApprox,hIdeal);
Truncation of a sinc filter causes a ripple in the frequency response, which can be addressed by applying weights (such as Kaiser or Hamming) to the FIR coefficients .
Finally, the resulting FIR approximation model of the ideal bandlimited fractional delay filter is given below.
Lagrange-based Fractional Delay Filters
Lagrange-based fractional delay filters use polynmial fitting on a moving window of input samples. That is, is a polynomial of some fixed degree K. Like the sinc-based delay filters, Lagrange-based delay filters can be formulated as a causal FIR convolution (i.e. ) of the length N=K+1, and supported on the index window. Similarly to the sinc-based model, apply the causal latency . Given a fractional delay , the FIR coefficients of the (causal shifted) Lagrange delay filter can be obtained by solving a system of linear equaions, as written below. Those equations describe a standard Lagrange polynomial fitting problem.
Here, are the enumerated indices of the sample window. The implementation is straightforward.
% Filter parameters FD = 0.4; K = 7; % Polynomial degree N = K+1; % FIR Length idxWindow = (-floor((N-1)/2):floor(N/2))'; % Define and solve Lagrange interpolation equations V = idxWindow.^(0:K); % Vandermonde structure C = FD.^(0:K); hLagrange = C/V; % Solve for the coefficients i0 = -idxWindow(1); % Causal latency plot_causal_fir("Lagrange",FD,N,i0,hLagrange);
This model can be implemented as a direct-form FIR filter if the delay value FD is fixed, or using a Farrow structure if the delay value is varying. There is a section below dedicated to the implementaiton of Lagrange interpolation using
Design And Implement sinc-Based Fractional Delay FIR Filters
The following section is focuses on designing and implementing sinc-based fractional delay filters.
designFracDelayFIR in Length-based Design Mode
designFracDelayFIR provides a simple interface to design a fractional delay FIR filter of delay value
FD and of length
FD = 0.32381; N = 10; h = designFracDelayFIR(FD,N)
h = 1×10 0.0046 -0.0221 0.0635 -0.1664 0.8198 0.3926 -0.1314 0.0552 -0.0200 0.0042
The filter implementation can be done using any standard FIR filter, such as the
dsp.FIRFilter System object.
% Create an FIR filter object fdfir = dsp.FIRFilter(h);
Delay a signal by filtering it through the designed filter.
% Generate some input n = (1:100)'; x = gen_input_signal(n); % Filter the input signal y = fdfir(x); plot_sequences(n,x, n,y); legend('Filter Output','Original Sequence') title('Raw Filter Output v.s. Input Sequence')
Notice that the actual filter delay is not , but rather because of the causal integer latency
. That latency is returned from the
designFracDelayFIR function as a second output argument.
[h,i0] = designFracDelayFIR(FD,N);
The overall delay is merely the sum of the desired fractional delay and the incurred integer latency.
Dtotal = i0+FD
Dtotal = 4.3238
This total delay is also the group delay of the FIR filter at low frequencies. Verify that by using the
[Doutput,~,~] = fdfir.outputDelay(Fc=0)
Doutput = 4.3238
Shift the plot of the input sequence by the total delay to align the filter output with the expected result.
plot_sequences(n+Dtotal,x, n,y); legend('Filter Output','Input Sequence (shifted by FD+i0)') title('Filter Output v.s. Time Adjusted Input Sequence')
Note that the shifted input markers located at generally do not coincide with output samples markers , because falls on noninteger values on the x-axis, whereas n is integer. Rather, the shifted input samples fall approximately on a line connecting each two consecutive output samples.
plot_sequences(n+i0+FD,x, n,y,'with line'); legend('Filter Output','Input Sequence (shifted by FD+i0)') title('Output Samples v.s. Shifted Input Samples ') xlim([20,30])
dsp.VariableFractionalDelay System Object in
dsp.VariableFractionalDelay object can also design sinc-based delay filters when used with the '
FIR' inteprolation mode. Begin by creating an instance of the System object. The FIR length is always even, and is specified as a half-length parameter.
vfd_fir = dsp.VariableFractionalDelay('InterpolationMethod','FIR','FilterHalfLength',N/2); i0_vfd_fir = vfd_fir.FilterHalfLength; % Interger latency
Pass the desired fractional delay as the second input argument to the object call. Make sure that the delay value you specify includes the integer latency.
y = vfd_fir(x,i0+FD); release(vfd_fir) plot_sequences(n+i0+FD,x, n,y); legend('Filter Output','Input Sequence (shifted by FD+i0)') title('dsp.VariableFractionalDelay in FIR Mode')
dsp.VariableFractionalDelay in 'FIR' Mode
'FIR' mode provide sinc-based fractional delay filters, but their implementations are different.
dsp.VariableFractionalDelayapproximates the delay value by a rational number up to some tolerance, and then samples the fractional delay as the k-th phase of a (long) interpolation filter of length L. This requires increased memory use, and yields less accurate delay.
designFracDelayFIRgenerates the FIR coefficients directly, rather than sampling them from a longer FIR. This gives the precise fractional delay value, and costs less memory.
designFracDelayFIRhas a simple function interface returning the FIR coefficients, leaving the filter implementation to the the user. The
dsp.VariableFractionalDelayis System object meant to encapsualte the filter design and implementation entirely.
The use of
designFractionalDelayFIR is preferred over
'FIR' mode for its simplicity, better performance, and efficiency. In the figure below, the filter designed with
dsp.VariableFractionalDelay has a shorter bandwidth, and its group delay is off by ~0.02 from the nominal value.
% Obtain the FIR coefficients from the dsp.VariableFractionalDelay object h_vfd_fir = vfd_fir([1;zeros(31,1)],i0_vfd_fir+FD); release(vfd_fir); plot_freq_and_gd(h,i0,,"designFracDelayFIR", h_vfd_fir,i0_vfd_fir,,"dsp.VariableFractionalDelay FIR mode"); hold on; yline(FD,'DisplayName','Target Fractional Delay'); ylim([-0.1,0.4])
Design And Implement Lagrange-based Delay Filters
Lagrange-based fractional delay filter are computationally cheap and can be implemented efficiently using the Farrow structure. The Farrow filter is a special type of FIR that is implemented using only elementary algebraic operations, such as scalar additions and multiplications. Unlike the sinc-based designs, Farrow filters do not require specialized functions (such as sinc or Bessel) to compute the delay FIR coefficients. This makes Farrow fractional delay filters particularly simple to implement on a basic hardware.
On the downside, Lagrange-based delay filters are limited to low orders, due to the highly unstable nature of polynomial approximations of high degree. This usually results with a lower bandwidth, when compared with a sinc-based filter.
The System Object
Use the system object
'Farrow' mode to create and implement Farrow delay filters. Begin by creating an instance of the system object:
vfd = dsp.VariableFractionalDelay('InterpolationMethod','Farrow','FilterLength',8); i0var = floor(vfd.FilterLength/2) % Interger latency of the filter
i0var = 4
Apply the created object on the input signal, and plot the result.
y = vfd(x,i0var+FD); plot_sequences(n+i0var+FD,x, n,y); legend('Farrow Fractional Delay Output','Input Sequence (shifted by FD+i0)') title('dsp.VariableFractionalDelay in Farrow Mode')
You can also vary the fractional delay values. The code below operates on frames of 20 samples, while increasing the delay value with each frame. Note the increase of the delay in the output graph, corresponding to the changes in the delay values.
release(vfd) FDs = i0var+5*(0:0.2:0.8); % Fractional delays vector xsource = dsp.SignalSource(x,20); ysink = dsp.AsyncBuffer; for FD=FDs xk = xsource(); yk = vfd(xk, FD); write(ysink,yk); end y = read(ysink); plot_sequences(n+i0var,x, n,y); legend('Variable Fractional Delay Output','Original Sequence (shifted by i0)') title('dsp.VariableFractionalDelay in Farrow Mode, Varying Delay')
Bandwidth of FIR Fractional Delay Filters: Analysis and Design
Longer filters give better approximation of the ideal delay filter. Indeed, in terms of raw quadratic norms it is the case. However, we need a metric that is more practically meaningful, such as the bandwidth. The function
designFracDelayFIR measures combined bandwidth, which is defined as the frequency range in which both the gain and the group delay are within 1% of their nominal values. The measured combined bandwidth can be obtained as a return value of the
designFracDelayFIR function. Compare a filter of length 16 (blue) with a filter of length 256 (red) in the figure below. As expected, the longer filter have significantly higher combined bandwidth.
FD = 0.3; N1 = 16; N2 = 256; [h1,i1,bw1] = designFracDelayFIR(FD, N1); [h2,i2,bw2] = designFracDelayFIR(FD, N2); plot_freq_and_gd(h1,i1,bw1,"N="+num2str(N1), h2,i2,bw2,"N="+num2str(N2)); ylim([-0.2,0.6])
designFracDelayFIR in Bandwidth Design Mode
The bandwidth design mode of
designFracDelayFIR can determine the required length for a given bandwidth. Specify the delay value and the desired target bandwidth as inputs to the function, and the function will find the appropriate length.
FD = 0.3; bwLower = 0.9; % Target bandwidth lower limit [h,i0fixed,bw] = designFracDelayFIR(FD,bwLower); fdfir = dsp.FIRFilter(h); info(fdfir)
ans = 6x35 char array 'Discrete-Time FIR Filter (real) ' '------------------------------- ' 'Filter Structure : Direct-Form FIR' 'Filter Length : 52 ' 'Stable : Yes ' 'Linear Phase : No '
bwLower is merely a lower bound for the combined bandwidth. The function returns a filter whose combined bandwidth is at least the value specified in
Distortion in High Bandwidth Signals
In this section, we compare the performance of the two design points (long sinc v.s. short Lagrange) with a high bandwidth input. The
dsp.VariableFractionalDelay in the previous section is an 8-degree Farrow structure, effectively an FIR of length 9. The filter obtained by
designFracDelayFIR(FD,0.9) has a length of 52 samples. Putting the two FIR frequency responses together on the same graph demonstrates the bandwidth difference between the two.
release(vfd); hvar = vfd([1;zeros(31,1)],i0var+FD); plot_freq_and_gd(h,i0fixed,bw,"Sinc-based", hvar,i0var,,"Farrow"); ylim([-0.2,0.6])
Apply the two filters on a high bandwidth signal, as compared in figure below. Sinc on the left column, Farrow on the right. Time domain on top row, frequency on the bottom. The results are, as expected:
The longer sinc filter has a higher bandwidth. The shorter Farrow filter has lower bandwidth.
Signal distortion is virtually nonexistent using the longer sinc filter, but easily noticeable in the shorter Farrow filter.
The higher accuracy comes at the expense of longer latency: approximately 25 samples v.s. only 4 in the shorter filter.
n=(1:80)'; x = high_bw_signal(n); y1 = fdfir(x); y2 = vfd(x,i0var+FD); plot_signal_comparison(n,x,y1,y2,h,hvar,i0fixed,i0var,FD);
Which should be used:
This decision is largely based on filter requirements and the target platform.
For a high bandwidth and accurate group delay response, use the
designFracDelayFIRfunction. Keep in mind that this design process is more computationally intensive. Therefore, is it better suited to be deployed on a higher-end hardware, especially if realtime tuning of the delay value is desired. It is also suitable for lower-end hardware deployment, if the delay value is fixed, and the design can be done offline.
For time-varying delay filters aimed at low-performance computational apparatus, use