Theoretical description
What is it?
Filters are used widely in biologging to separate low and high frequency components in sensor signals, to smooth signals, to improve the detectability of particular activities, to change sampling rates, and to time-align signals. You may be familiar with the idea of designing a filter that emphasizes or de-emphasizes certain frequency bands. But less well-known is the fact that every filter delays the signal it is acting on. The amount of delay depends on the steepness of a filter: a steeper filter adds more delay.
For many filters, the delay also depends on frequency so that some components in a signal are delayed by more than others. In biologging, we often want to know exactly when an event occurs so we can compare behavioral signatures recorded by multiple sensors in the same tag, or even on different animals. The delays added by filters, some of which might be operating invisibly in functions such as decimators or smoothers, increase the complexity of these comparisons and the chance of making an error.
To avoid this problem, the animaltags tools use a particular type of filter that has a precisely predictable delay, and then correct for this delay by trimming the signal that is being filtered. Consequently, the tools produce processed signals that can be directly compared in terms of timing to other signals that have been processed in different ways.
How is it measured?
The animaltags tools use a type of filter called a symmetric finite impulse response (FIR) filter. This filter is a generalization of the centralized moving average that you might be familiar with from statistics. The impulse response of a filter is just the output of the filter when the input is a single short pulse. If the impulse response is symmetrical around some center time, the delay it generates is exactly equal to that center time. Knowing the delay, it can be cancelled out in the output signal by trimming off an appropriate number of samples from the start (some extra zero samples can be added to the end of the output to keep the length the same as the input signal). Delay-free filtering therefore involves three steps: (i) design a symmetric FIR filter, (ii) apply it to the signal, and (iii) trim the correct number of samples from the start of the signal. These steps are all performed by fir_nodelay()
. Other tools that use filtering (e.g., decdc()
, smooth()
, comp_filt()
, odba()
) either call fir_nodelay()
, or perform the same steps internally.
This is not the only way to make a delay-free filter. Any filter can be made delay-free by running it twice over the signal: first in the forward direction, and then again in the backward direction. In effect, the delays introduced by the first pass of the filter are exactly matched by negative delays in the second pass. This technique is used by the Matlab function filtfilt()
. However, by running the filter twice, you get twice as much filtering so, for example, the -3 dB frequency becomes the -6 dB frequency meaning that more of the passband (the frequency range that you don’t want to be affected by the filter) gets attenuated. You can compensate for that by adjusting the cut-off frequency and steepness of the filter, but using fir_nodelay()
gives you better control over the filter characteristics.
What is it good for?
A delay-free filter is essential if you want to preserve the temporal relationships between different sensors. For example, to get the pitch, roll, and heading of an animal you need to combine data from a triaxial accelerometer and magnetometer. But the accelerometer is often sampled at a higher rate than the magnetometer. To get data streams at the same sampling-rate, you need to decimate the accelerometer signal and this involves filtering. If the filter adds delay to the accelerometer, the time alignment with the magnetometer will be wrong and artefacts will show up in the heading. These may be particularly apparent when you compute the dead-reckoned track (Shiomi et al., 2008 and 2010).
Another example is in computing the pitch and roll of an animal when it strikes at prey. In this case we use jerk to detect the prey strikes but need to smooth (i.e., low-pass filter) the accelerometer data to get the slower-changing orientation of the animal without the transient motions. If the filter used for smoothing adds delay, then the prey strikes will align with the incorrect posture.
Technical description
Usage of tool
In this vignette you will learn to use fir_nodelay()
to gain insight about how to apply a delay-free filter by using a linear-phase (symmetric) FIR filter followed by a group delay correction.
Inputs:
x
: is the signal to be filtered. It can be multi-channel with a signal in each column, e.g., an acceleration matrix. The number of samples (i.e., the number of rows inx
) must be larger than the filter length,n
.n
: is the length of symmetric FIR filter to use in units of input samples (i.e., samples of x). The length should be at least4/fc
. A longer filter gives a steeper cut-off.fc
: is the filter cut-off frequency relative to 1=Nyquist frequency. If a single number is given, the filter is a low-pass or high-pass. Iffc
is a vector with two numbers, the filter is a bandpass filter with lower and upper cut-off frequencies given byfc(1)
andfc(2)
. For a bandpass filter, n should be at least4/fc(1)
or4/diff(fc)
, whichever is larger.qual
: is an optional qualifier determining if the filter is: % ‘low’ low-pass (the default value iffc
has a single number) ‘high’ high-pass. Default is “low”.
Outputs:
y
: is the filtered signal with the same size asx
.h
: is the vector of filter coefficients used byfir_nodelay()
(a vector). This can be used to plot the filter characteristic usingfreqz(h,1,1024,fs)
.
References
Shiomi K, Sato K, Mitamura H, Arai N, Naito Y, and Ponganis PJ. (2008). Effect of ocean current on the dead-reckoning estimation of 3-D dive paths of emperor penguins. Aquatic Biology, 3:265–270 https://doi.org/10.3354/ab00087
Shiomi K, Narazaki T, Sato K, Shimatani K, Arai N, Ponganis PJ, and Miyazaki N. (2010). Data-processing artefacts in three-dimensional dive path reconstruction from geomagnetic and acceleration data. Aquatic Biology, 8:299-304. https://doi.org/10.3354/ab00239