# Technical Explanation – Complementary Filter

#### Theoretical description

###### What is it?

A complementary filter breaks a signal of interest into two or more frequency bands such that the sum of the signals in the separate bands is equal to the original signal. The simplest complementary filter involves the separation of two signals that have complementary spectral characteristics.

###### How is it measured?

If `T(s)` is chosen as a low-pass filter, then `1-T(s)` is a high-pass filter and both signals are attenuated. Since both high and low frequency data are utilized, the filter output will not suffer from any delay in dynamic response due to low-pass filtering.

###### What is it good for?

Locomotion in many animals involves the oscillatory movement of limbs, fins or wings. These oscillations tend to occur at the dominant stroke frequency (dsf) and are overlaid on the constantly changing postural orientation, which tend to occur at a lower rate, as the animal maneuvers (Johnson and Tyack, 2003).

Assuming that this oscillations during steady swimming, flying or walking only produces signals at and above the stroking rate, these can be separated from slower postural changes by high-pass filtering (Sato et al., 2003) each axis of the signals measured with different sensors, such as accelerometers, magnetometers and gyroscopes. In the same way, we could also separate the rapid muscle movements and or unsteady flow due to the tag device vibration.

#### Technical description

###### Usage of tool
MATLAB
```Xf=comp_filt(X, sampling_rate = NULL, fc)
```
R
```Xf <- comp_filt(X, fc = fc1,fc2)
```

All filters introduce a delay; this means that the output signal is shifted in time with respect to the input signal. A symmetric Finite Impulse Response (FIR) filter (for more information see the technical explanation of delay-free filtering) was chosen because this filter design delays all frequency components by the same amount, namely one half of the filter length in samples. Thus, in order to correct for the delay, the filtered data were shortened by removing samples equivalent to half the filter length from the start of each signal, effectively cancelling the group delay of the filter (Stein, 2000).

###### Deep-Dive on Functionality

For a better understanding of how this works, let us plot the power spectra of the accelerometer and magnetometer data in the animal frame to visualize the orientation postural changes (low-frequency), the dominant stroke frequency (mid-frequency) and the muscle movements and or unsteady flow (high-frequency). While the low and mid frequencies are always present in any animal deployment, the high frequencies may not be. This plot will also help us to set an appropriate cut off-frequency for the complementary filter `comp_filt()`, to appropriately separate the different signals present in our data that occur at different frequencies.

MATLAB
```% Section in progress ...
```
R
```
```
MATLAB Output

Coming soon …

R Output

Coming soon …

#### Verification

To verify that you have run `comp_filt()` correctly, i.e., that you have set the filter cut-off frequencies correctly, to separate orientation from gait from strikes you will need to plot each signal separately and check that they look as they should.

A cut-off frequency (fc) of approx. half of the stroking rate is a conservative value appropriate for swimming with slow overall postural changes that ensures that all the steady-swimming frequencies are included whilst removing the high-frequency components from the low frequency sensor signals. The suitability of this filter frequency has to be checked by inspecting the resulting low frequency acceleration signals during stroking intervals.

MATLAB
```% Section in progress ...
```
R
```
```
MATLAB Output

Coming soon …

R Output

Coming soon …

#### Caveats

As with any filter, if the cut-off frequency to separate orientation from gait locomotion is too low, the magnitude of the gait locomotion will be overestimated and the animal orientation will be mistakenly identified as strokes. On the other hand, if this cut-off frequency is too high, the magnitude of the gait locomotion will be underestimated and the stroking will be visible in the animal orientation, i.e., low-frequency signal.

The complementary filter is useful to separate signals that occur at a different rate. However, if two signals occur at the same rate, then you need an extra sensor to separate them. This can be done by analyzing a tag’s magnetometer data, which will be the topic of an additional technical explanation piece.

#### References

Johnson, M. P. and Tyack, P. L. (2003). A digital acoustic recording tag for measuring the response of wild marine mammals to sound. Journal of Oceanic Engineering, 28, 3–12. https://doi.org/10.1109/JOE.2002.808212