Example Code

Eliminating Digital Filter Artifact Using Initialization Method

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • Data Acquisition (DAQ)

    Software

  • LabVIEW

    Driver

  • NI DAQmx

Code and Documents

Attachment

Overview:
Demonstrates on a correct method to initialize digital filter to remove artifact or start-up error.
 
Description:
Internally, they are various states in digital filtering.  You may think of them as shift registers.  As they comb over the data, the output for the n-th element depends on the last x elements.  
The actual number varies based on the implementation. One common issue when using digital filters is seeing artifacts when they are uninitialized.

Take a look at the following example: The data in red is the unfiltered data, while the data in blue is filtered.  
The one on the left was filtered by a VI where the internal states were initialized to zero (the default for many VIs). The one on the right, however, is what most of us would expect to see.
There is an artifact on the start of the left dataset as the internal zeros get filled up and the filter adjusts for the signal's offset.  On the other hand, the signal on the right seems more intuitive.

To eliminate the artifacts or startup error from a digital filter, a subVI that initializes the internal storage elements of the filter to the first data element has to be created.
It was done by duplicating a large number of copies of the first element and append them to the beginning of the dataset.  

Then only both of duplicated and original data will be fed into the filter.  Afterward, the duplicated elements together with the startup artifacts are removed.
As a result, when the filter gets to the actual data, it will be fully initialized to the correct offset.

The number of extra elements to add may vary depending on your signal or on which filter you use on the lower level.

Additionally, if you are filtering signals continuously, it would not be necessary to do this because many of the filter VIs keep their states between calls.
That means if you are continuously filtering sets of continuous data, you will only see these artifacts at the very beginning and not at the beginning of each set.

Requirements:

  1. LabVIEW 2012 or compatible.


Instructions:

  1. Drop this VI into the VI in which digital filtering is needed.
  2. Wire the waveform to be filtered to the Input Waveform terminal.
  3. Specify the number of extra elements to add, if needed. The default value is 10 000.
  4. Verify that there is minimum startup effect in the filtered waveform.


Block Diagram Steps:

  1. Extracts the Y values from the waveform.
  2. Extracts the first element from the 1D array of Y values.
  3. Creates extra copies (duplicates) the first element by the amount specified at the Number of duplicated elements control.
  4. Concatenates the duplicated elements to the beginning of the original elements and wire it to the input terminal of the digital filter.
  5. Generates a digital Butterworth filter.
  6. Once filtering is done, removes the duplicated elements from the filtered data.
  7. Builds an analog waveform from the filtered data.

2017-06-05_14-45-23.jpg

 

2017-06-05_14-42-22.jpg

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors