Tutorial Contents

Filter

Principles

Frequency domain

FIR

Time domain

RC-type

Smoothing

Narrow-band (notch)

De-buzz

Contents

Filter Data

Principles

First I acknowledge my debt to the excellent (and free!) book “The Scientists and Engineers Guide to Digital Signal Processing” by S. W. Smith. Much of the following is a digest of what I have learned from this source – although I offer no guarantees that I have completely understood the many issues involved!

All digital filters have an impulse response, a step response, and a frequency response. The impulse response is the output when presented with an input that consists entirely of zeros except for a single non-zero sample. The step response is the output when presented with an input which steps suddenly from a constant signal of one value (usually zero) to a constant signal of some other value (usually 1). The frequency response is a graph of the filter gain (output-to-input ratio) plotted against the frequency of the signal. Each of these three characteristics completely describes the filter – if you know one, you can, in theory at least, calculate either of the other two by various mathematical techniques.  The file impulse contains an input signal consisting of an impulse followed by a step, while the file sin-100-20-5 contains a mixed sine wave signal with frequencies of 100, 20 and 5 Hz. These two files can be used to investigate the responses of various filters.

Nyquist frequency

A crucial point about digital filters is that they can only perform operations at frequencies up to the Nyquist frequency of the data (half the sample frequency). Furthermore, the original analogue data should have been filtered to remove any components with a frequency higher than this prior to being digitized, or the dreaded aliasing will occur, in which the high-frequency signal fraudulently appears within the low-frequency data.

There is a separate tutorial on the Nyquist frequency and acquisition sample rates.

Filter domain

Before filtering any signal it is important to consider how information is contained within that signal. In particular, one should know whether it is the amplitude at particular times that contains the information (time domain signals), or the frequency at particular times that contains the information (frequency domain signals). The reason that this is important is that different types of filter are appropriate for different types of signal, and a filter that is optimized for performance in one domain will inevitably perform badly in the other. This seems to be one aspect of that general rule of the universe with which we are all horribly familiar, namely, there is no such thing as a free lunch.

A typical frequency domain signal is an audio signal. A single digitized data point containing the amplitude of the sound pressure at a particular moment in time is of very little interest, what matters is the relationship of this data point to the others around it, which tells us about its frequency (pitch) and the amplitude of the sound as a whole. In contrast, a typical time domain signal (in a neurophysiological context) would be an intracellular recording. In this case, the amplitude of the signal at any particular moment represents the membrane potential of the neuron at that moment, and that may be of very great interest. However, some neurophysiological signals do contain information in the frequency rather than the time domain, such as an EEG recording, where the various rhythms (alpha, gamma etc), contain the information.

Filters that are optimized for signals in the frequency domain are best characterized by their frequency response. The purpose of these filters is to allow some frequencies of signal to pass, and to block other frequencies. It is possible to design filters that have extremely sharp cut-off frequencies, so that they can separate out very similar frequencies within the signal. The penalty for this is considerable overshoot or “ringing” in the step response. So long as the data have been pre-filtered to the Nyquist frequency this does not matter, since the digital filter would then never see a real step response. However, not many data acquisition systems perform such pre-filtering automatically, and thus step inputs (such as a current pulse, or voltage clamp step) are frequently recorded unfiltered. If this step is then passed through a digital filter, the output gets very distorted. In contrast, filters optimized for signals in the time domain usually have a very smooth step response, so that they produce little amplitude distortion, but have a relatively poor ability to discriminate between different frequencies within the signal.

Edge effects

Most digital filters produce distorted output at the beginning and end of the section of filtered data. The length of distorted data depends on the filter settings, but you should treat these edges with caution (preferably by not using them in any analysis).

Frequency domain

FIR

The filtered data file should now look like this:

frequency filter
Frequency domain filtering (FIR). The upper trace shows the original signal, the middle trace shows the signal after passing through a 50-500 Hz band-pass filter, the lower trace shows the signal after passing through a 50-500 Hz band-stop filter.

To check FIR performance in the time domain:

Time domain

RC-type

The simplest filter appropriate for the time domain is an RC-type. This is a recursiveA recursive filter re-uses part of its output as its input. filter and is the digital equivalent to an analogue single-pole resistor-capacitor filter. This is ideal for tasks like removing DC or long-term voltage trends from records, or smoothing sharp steps. DataView supplies both a low-pass and high-pass version of the RC-type filter.

The output from the various options applied to a square pulse is shown below:

RC filter
An RC filter with a time constant of 50 ms applied to a square waveform (top trace). The trace labels (lower traces) show the various filter options applied.

Smoothing

Smoothing filters all work by replacing each data point by a value that is representative of the general values in a time window around that point. In DataView, these filters are all acausal, meaning that the window includes data points ahead of the point of interest, as well as behind it. All smoothing filters are low-pass filters.

Dataview provides several different types of smoothing filters, and they are all available through a single dialog interface with optional preview. This means that you can switch between different types and immediately see their performance. This is useful because, beyond some broad generalizations, it is often difficult to predict from first principles which filter type or parameter set will provide optimum performance for a particular recording.

Moving average

The simplest smoothing is the moving average filter, which replaces each data point by the average of itself and a user-determined number of data points on either side of the central point. The moving average filter is (or so I have read) the theoretical best filter for solving a common problem; the reduction of random white noise while keeping the sharpest possible step response.

Four types of moving average filters are available:

  1. The standard flat (boxcar or top-hat) filter gives equal weighting to all data points within the averaging window. The interface allows you to specify multiple passes of the filter. Two passes approximates to filtering with a triangular kernel, while 4 or more passes approximates to Gaussian filtering (see below).
  2. The moving median filter is the same as the moving average, except that it uses the median rather than the average (mean). Computing the moving median is more expensive than the moving average, and is not so efficient at removing random noise unless the noise is precisely normally distributed, but it is very much less sensitive to large but brief outliers (data “glitches”), which can distort the average and pull it in their direction.
  3. The moving percentile filter is like the moving median filter, except that it allows the user to specify any percentile (0-100) of the data within the moving window as the replacement value. The moving median filter is actually a sub-set of the moving percentile filter, with the percentile specified as 50.
    The code for the moving percentile calculation is adapted from that supplied to me by Leemon Baird, who I gratefully acknowledge.
  4. The Gaussian filter option applies Gaussian weighting directly – data points distant from the central point have a reduced weighting in the average, with the reduction following a bell-shaped curve on either side of the centre. A Gaussian moving average filter has similar properties to an RC low-pass filter. Note that specifying 4 iterations of a boxcar filter produces output similar to Gaussian filtering, and is usually considerably faster.
spikes plus noise
The top trace is an intracellular recording showing some spikes and PSPs. The intrinsic noise level is very low. The second trace has random Gaussian noise added to it. The third trace has some additional random glitches. The aim of filtering is to recover the first trace from the other two, preferrably with even less membrane noise!
Moving average option

These parameters produces a reasonably smooth membrane potential trace without significantly reducing the peaks of the spikes. It seems to be close to an optimal setting for these data.

Moving median option

It is apparent that the moving median filter does a much better job of retrieving the original recording than the moving average when there are outlier spikes in the data.

In the absence of the outlier spikes, the moving average is definitely superior to the moving median.

Gaussian

Robust LOWESS

The robust LOWESSLOWESS = LOcally WEighted Scatterplot Smoothing. smoothing technique is more sophisticated than the moving median because it attempts to identify outliers and distinguishes between these and genuine data peaks. The algorithm for LOWESS smoothing is described in the Robust fitting and smoothing section.

There are 4 parameters that control the process. The polynomial degree is set depending on how ‘bendy’ the expected best fit will be. It is rarely useful to set the degree higher than cubic. A degree of 0 means that a weighted moving average is used. The half-window width largely determines the degree of smoothing. Larger windows produce smoother output, but at the expense of loss of detail in following small but real trends in the data. The iteration number determines how many iterations are carried out in the outlier reduction process. A value of 0 means that no outlier reduction is carried out. The outlier threshold determines how far the data have to deviate from the median to be regarded as outliers.

The LOWESS procedure does a good job of both reducing standard membrane noise and removing outlier glitches, while at the same time having little effect on the spike peak amplitude.

Savitzky-Golay

Savitzky-Golay (S-G) smoothing can be tuned to follow narrow peaks and troughs in the data more closely than moving average smoothing, but at the expense of less effective smoothing of slowly-changing data. It's also a lot slower. It works by taking a window of data on either side of a central point, and fitting a polynomial function to these data. The central point is then replaced by the value of the function at that location. The window then slides one sample forward in the recording, and the process is repeated. The user specifies both the size of the window, and the degree of the polynomial. Degree values of 2, 4 or 6 are normal, and the higher the order, the more closely the smoothed data follow peaks and troughs. The size of the window determines the overall amount of smoothing (just as in moving average smoothing).

An important potential advantage of the Savitzky-Golay filter is that it directly gives the derivative of the smoothed waveform, since this is available analytically from the polynomial function. Derivatives are notoriously susceptible to noise, and if you want to calculate the derivative of a membrane potential change it is common to have to smooth the data first (see e.g. the Spike shape tutorial). The Savitzky-Golay filter does both jobs in one go - although here we are only considering the smoothing aspect.

The “best” parameter settings for S-G smoothing will depend on the features of the data that you wish to preserve. As can be seen above, the parameters can be interactively hand-tuned using the transform preview facility.

So far we have looked at data where artificial noise has been added. What about the real recording?

At the normal gain, the raw and filtered traces look virtually identical. However, if we zoom in we can see a difference.

The trace was very "clean" to begin with, but there was still membrane noise that has been reduced by the S-G filter. If we were calculating a derivative, particularly a higher-order derivative, even a small amount of noise gets massively amplified, and the S-G filter can be very useful in reducing the problem. This is explored further in the tutorial on Spike shape analysis.

Narrow-band (notch)

Narrow-band filters either stop or pass a very narrow range of frequency. In the Dataview implementation, the bandwidth is about 0.0003 of the sampling frequency.

Narrow band stop filters are sometimes used to remove mains interference from a signal when they are configured in band-stop mode (a notch filter). However, this is usually not as efficient as the de-buzz technique described elsewhere because mains interference frequently contains harmonics at multiples of the base, and these are not touched by a narrow-band filter.

The lower trace shows a low-amplitude sine wave which has been constructed using a formula which generates a period of 60 ms, and hence a frequency of 16.66+ Hz. The upper trace shows the same sine wave embedded in noise with a standard Gaussian distribution. You probably wouldn’t be certain just by looking at it that there was any sine wave component in the upper trace because the signal-to-noise ratio is rather low.

The Preview window shows an edge effect at the start of the transformed trace (lower, red), but then the trace stabilizes to show the sine wave embedded in the original noisy trace (upper, black).

If you selected Band stop as the filter option, the transformed trace would appear virtually identical to the untransformed trace. However, the sine wave would be removed. This could be confirmed by activating the filter (by clicking OK), and then using the Analyse: Power spectrum facility (there is a tutorial on that here).

De-buzz

The specialized de-buzz filter to remove mains interference is described here.