Filter your data in software
Digital filters remove unwanted frequency components from digitized signals. Learn how digital filters work, and download one to process your data.
Werner Haussmann, Agilent Technologies, Loveland, CO -- Test & Measurement World, 4/1/2002
When you capture waveform data, you often want to process the data to extract information about the signal or to extract a particular wave shape. Filters—analog or digital—often provide the signal processing you need. Analog filters process signals before you digitize them. Digital filters, which apply software algorithms to digitized waveforms, can work in digital signal processors for "real-time" filtering. You can also use digital filters that run on a PC to process data you've already captured.
Filters act on specific frequency components. With a filter, you can isolate wanted frequencies (pass band) from unwanted frequencies (stop band). Digital filters isolate frequencies by applying mathematical formulas called transfer functions to data points. The characteristics of a transfer function define a filter's frequency response.
While working on a recent project, I needed to capture a signal, remove unwanted high-frequency noise, and store the "clean" signal in a file. Then, I needed to load the clean signal data into a waveform generator.
![]() |
| Figure 1. A digital filter removes unwanted frequency components such as noise from the upper trace to produce the filtered lower trace. |
You can download a free copy of the filter, which resides in an ActiveX control, and use it in your own programs or in an application program I wrote. ("Using the filter," below.) You also can download the complete source code for the filter control and the accompanying application.
How digital filters work
![]() |
| Figure 2. A moving average algorithm takes samples, sums them, and divides them by the number of samples. This example shows a seven-point moving average. |
Figure 2 introduces notation you can use to develop a moving average into a digital filter. Each box in the left column represents a point in the data array. The boxes in the right column represent a seven-point moving average. Each of the seven boxes becomes a "term" of the filter, in which each term has a coefficient. For the moving average, each coefficient equals 1/7 (you could also make each coefficient equal 1, and then divide the sum by 7), which gives each term equal weight. When you apply equally weighted coefficients, you apply what is commonly called a "rectangular window" to the data.
Figure 3 shows an example of how a moving average changes a signal. A 25-point data set contains a pulse that is 10 points wide (green trace). The blue trace shows the result of applying a seven-point moving average to the pulse. The waveform transitions occur more smoothly in the blue trace than in the green trace.
![]() |
| Figure 3. The waveforms show a 10-point pulse (green trace) after applying a seven-point moving-average filter (blue trace). Adjusting the relative weights of the filter’s coefficients removes sharp corners (red trace). |
Note the change in the value of the moving average starting at point 3 of the blue trace. While that change is smoother than at point 6 of the green trace, it's still abrupt. By changing the relative weight of the coefficients in the moving average, you can further smooth the transition into the filter (red trace). For example, you can reduce the relative weight of coefficient C3 to half that of the other coefficients. Doing so produces a weighted moving average. But the sum of the coefficients must equal 1, so you can't simply set the two occurrences of C3 to 1/14 and leave the others at 1/7. To retain the sum, you must set coefficients C0, C1, and C2 (five coefficients because C1 and C2 appear twice) equal to 1/6 and set C3 equal to 1/12. The sum of the coefficients is (5 x 1/6) + (2 x 1/12) = 1. Tapering the windows gives less emphasis to points farther from point N in Figure 2.
![]() |
| Figure 4. In a seven-point moving average, applying equal weights to each coefficient produces the frequency response in the blue trace, but changing the weights can reduce ripple at the expense of a wider pass band (red trace). |
By modifying the coefficients, you can improve a filter's response. For example, increasing the number of points in a filter window will improve ripple (provide greater attenuation) without a loss in transition bandwidth.
An ideal filter, if one existed, would multiply all frequencies in its pass band by 1 and all frequencies in its stop band by 0 with zero bandwidth between the two regions. This theoretical filter's transfer function requires an infinite number of terms. Unfortunately, calculating the coefficients for an ideal filter takes forever. Therefore, you must limit the number of coefficients in a filter's transfer function to make it practical to use. In my digital filter, I employ a Kaiser filter to process the data because it's easy to program. (You don't need to know how to calculate the coefficients in a Kaiser filter to use my program, because my code does that for you, but you can learn how the calculations work by reading "How to calculate Kaiser coefficients," below.)
![]() |
| Figure 5. To use a Kaiser filter, you must specify the cutoff frequency, transition frequency, and percent of ripple. |

Before you download the filter, you should understand its limitations. In theory, the filter should attenuate frequencies in the stop band by as much as 100 dB. In practice, though, you'll get attenuation between 40 dB and 50 dB, which should be sufficient for most applications.
Attenuation is limited because I used 64-bit (15-digit) precision for the variables that represent the coefficients. At that precision, some of the coefficients are so small that their values consume less than the weight of one digit. Visual Basic 6.0 lets you double the precision, but doing so doubles the time the program takes to calculate the coefficients. For my application, 40 dB of attenuation was sufficient. If your application requires greater precision, you can modify the filter's source code.
In my project, I filtered a captured waveform and loaded the filtered signal into an arbitrary waveform generator. If you use an arbitrary waveform generator to reproduce the filtered waveform, then calculating coefficients with 64-bit precision is more than sufficient. Most waveform generators resolve signals to just 12 bits or 16 bits, so a better-filtered data set won't produce a cleaner waveform.
Another limitation of using the filter comes from the time required to calculate the coefficients, which depends on the number of samples in your waveform, the width of ft, and the amount of ripple in your stop band. If your waveform has just 1000 points, even a slow computer will quickly calculate the coefficients. If your waveform contains tens of thousands of points, though, you may have to wait a while. For calculations that take longer than 10 s, the application program will estimate the calculation time so you'll know the computer didn't crash.
Using the filter
| Instructions for
downloading the software You can download a stand-alone application that lets you use the FIR filter, or you can download the filter's source code and use it in your own application. To download either, go to www.agilent.com/find/tmwarticle.Under "Software" click on "Test and Measurement World FIR Article, April 2002." You will find some instructions, the application, and source code. |
If you choose the first option, you can use the filter without writing any code. The program lets you load a data file, filter the data, and save the filtered data to a file. You can then reproduce the filtered waveform with an arbitrary waveform generator. I'll use the example that I've provided in the download to demonstrate how the filter works.
To begin, I saved the data from a waveform into an ASCII, tab-delimited file. The filter assumes a repetitive waveform where the first point is a continuation of the last point in the data.
Figure 6 shows my filter's display screen, from which you can select a filter and enter its parameters. The "Filter Type" tab lets you select from low-pass, high-pass, or moving-average filters. For the frequency scale in most data-acquisition applications, you should select "Show frequency as a fraction of sample frequency." If you select "Show frequency as a multiple of window frequency," you'll see a scale from 0 to one half the number of points in the waveform that you want to filter. Remember, to meet the Nyquist criterion, the "window" that you apply to your waveform must have fewer than half of the waveform's number of points (Ref. 1). 
Figure 6. A digital-filter application program lets you select filter type, frequency scale, and destination. 
Figure 7. Enter a fraction of your sampling frequency for values of cutoff frequency (fc/fs), transition frequency (ft/fs), and percent ripple. 
Figure 8. The filter software calculates the number of terms needed to meet your needs and then plots the filter’s frequency response.
Listing 1
Dim data As Variant
Dim dummy As Double
Dim fir As agtFirFilter.CWaveForm
Dim result As Long
Set fir = New agtFirFilter.CWaveForm
With fir
.getWaveform data, dummy
result = .WaveFormResult
End With
Next, use the Parameters tab (Figure 7) to select the cutoff frequency, transition bandwidth, and ripple. In the "Frequency Response" tab (Figure 8), the calculated frequency response shows 100 dB of attenuation, but as I explained, you won't get that much. When you click OK in any tab, the application will apply the filter to the data and then plot the filtered waveform. You can save the filtered waveform in a text file.
If you choose to download the ActiveX control, you can use it to filter data in your own program. Listing 1 shows an example in Visual Basic. In the sample code, the variable data (an array of double-precision variables contained in a variant) represents the waveform that you want to filter. The ActiveX control uses the function GetWaveForm to process the data and then return the filtered waveform into the variable data. You can use the waveform in your application or you can store it to a file. For a more detailed example of how to use the control, download the application program, which also comes with Visual Basic source code. T&MW
| References | ||
|
|



























