**Filtering EEG Data:**

### Page Contents:

- Introduction
- Filter types and Filter response
- Filtering in the time domain
- Difference equations and block diagrams
- FIR vs. IIR Filters

**Introduction**

The number one rule when filtering is the less filtering applied the better. Why? Well, when we apply a filter to a time series we inevitably change the series and we may risk introducing changes that are less than desirable such as introducing spurios or shifting the phase, which leads to a shifting of the time series in time. Thus, careless use of filtering may lead to time shifts that could change the latency of our ERP data.

That said, filtering is most often a necessary evil and, if correctly applied, can facilitate the analysis of our EEG data. But before describing the drawbacks and merits of filtering, we will presenting some basic concepts.

### Filter types and Filter response

Figure 1 presents 4 basic filter types:

**Low-pass filter**– all frequencies below a defined frequency are passed and all frequencies above this limit are rejected.**Band-pass filter**– all frequencies between defined lower and upper frequency limits are passed.**High-pass**– the inverse of the low-pass filter in which all frequencies above a defined frequency limit are passed and all below are rejected.**Band-stop**– often refered to as a “notch filter” is the inverse of the band-pass filter; all frequencies between a defined lower and upper frequency limit are rejected.

**Figure 1:** Four basic filter types (f=frequency , a = amplitude).

In filter design, the high-pass filter is derived from the low-pass filter and the band-stop from the band-pass.

Figure 1 effectively presents the **response **of each type of filter. To be precise, figure 1 presents the** frequency responses** of each filter type. The frequency response of a filter tells us which frequencies are rejected and the nature of the transition from the rejected frequency band to the passed frequency band (figure 2).

**Figure 2:** Frequency response of an “ideal filter”.

The rectangular shape of the frequency response presented in figure 2 is known as the “boxcar” filter, the frequency response of an** ideal filter, **frequency response of all filters in an ideal world. But, unfortunately, the world is less than ideal and case presented by the boxcar filter, where all frequencies above/below a defined frequency limit are completely removed, does not occur in practice. In digital signal processing, sharp transitions, such as we can observe in the frequency response of an ideal filter leads to ** artifacts** – spurious activity due to an important fact in signal processing:

Any abrupt transition (discontinuity) in one domain (frequency or temporal) requires an infinite number of components in the other domain.

So, if, as is desirable, our filter applies a sharp transition in the frequency domain, to avoid all artifacts our time-domain specification of the filter would need to be of infinite duration.

Figure 3 presents the frequency response of a low pass filter with a sharp transition, the spurious frequency components occuring at the transition can be observed. In figure 3, the response is described as the **magnitude response** as it shows how the filter changes the magnitude of the frequency components. Here, the magnitude is presented in decibels (dB). For the same reason, the frequency response is also often referred to as the filter **gain**. A gain of 0 implies a complete attenuation of frequencies while a gain of 1 implies that the frequencies are retained.

** Figure 3:** Magnitude response of a low-pass filter with a sharp transition.

To view the effect of the filter in the time domain, we can visualise the filter’s **impulse response**. This does exactly what it says, it shows the response of the filter to a single impulse. Figure 4 presents the impulse response of the low-pass filter with the sharp transition.

** Figure 4:** Impulse response of low-pass filter with sharp transition.

From figure 3 and 4 we can see that while the frequency response presents a sharp discontinuity between the pass and stop band, the time-domain specification of the filter, its impulse response, is not infinite. In fact, this is an example of one, very popular, type of filter, the **Finite Impulse Response (FIR) **filter. In fact, we can think of the impulse response here as being truncated, which leads to the ringing type artifacts that we see in both figure 3 and 4, known as the **Gibb’s effect**.

### Filtering in the Time Domain

To get a fuller understanding of how filters work in general and of the difference between Finite Impulse Response (FIR) and Infinite Impulse Response (IIR) filters, it is useful to look at a simple example of a low-pass filter in the time domain.

So when a low-pass filter acts on the current time sample, *x(n)*, what is it actually doing?

To filter x(n) it takes into account a certain number, j, of time samples preceding and following x(n). The value of j is defined by the user and it determines the filter length. So if j=1, samples *x(n-1), x(n), x(n+1)* , are taking into account, that is 3 samples (N) are used. So the filter length here is 3.

A filter is most defined in terms of its **filter order**. The filter order is defined as the N -1 (1 less than the filter length). So for a filter of length 3, its filter order is 2.

But, do all samples contribute equally to the filtering of the current sample,* x(n)?
*Well, if we want them to. To decide the extent to which each sample contributes the filtering of x(n) we define a weighting function. If our filter has a filter length of N (N-1th order filter) and we want all N samples to contribute equally, the contribution of each is 1/N. Figure 6 shows the simple 2nd order filter of length 3; the weight applied to each sample is 1/3…these weights are the

**filter coefficients**. Figure 7 shows a weighting function for a 10th order filter.

** Figure 6:** Simple weighting function for 1st order filter.

** ****Figure 7:** Weighting function for a 10th order filter.

You have, no doubt, realised that the weighting function and the filter impulse response are one and the same thing…

#### Difference Equations and Block Diagrams

A very informative way of representing filters is by **difference equation**s and block diagrams. Figure 8a and figure 8b present a block diagram and its corresponding difference equation of a simple filter. They tell us:

- how many samples before the current sample, x(n), contributes to the filtering of x(n), other known as the number of
**delays**or taps, - the coefficients comprising the weighting function.

In the block diagram the delays are represented as * z^{-1}* and the weighting coefficients as

**.**

*b*_{N}**Figure 8a:** Block diagram of simple FIR filter

**Figure 8b:** Difference equation of simple FIR filter

The filters that we have shown far are also referred to as being **non-recursive**. **Recursive filters** include both the samples preceding the x(n) and the filter output, y(n), in their calculations. The filter output is fed back into the filter as shown in the block diagram presented in figure 9a and the difference equation in figure 9b. You can see clearly that this type of filter has both a non-recursive and recursive element.

Why complicate things and add the recursive element to the filter? We mentioned above that artifacts were generated by the fact of having a finite impule response (FIR); this is the design of an **Infinite Impulse Response (IIR)** filter that attempts to deal with the problem of having a truncated (finite) impulse response.

** Figure 9a:**

*Block diagram of a recursive filter. The loop to the left is the non-recursive element and the loop to the right the recursive part.*

** Figure 9b: **

*Difference equation of recursive*

*filter. We can see clearly the non-recursive and recursive parts of the filter.*

### FIR vs. IIR Filters

more to come…

Line Noise (50Hz interference) reduction using Multi-taper spectral decomposition