Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

# filter

Filter data with filter object

## Syntax

```y = filter(hd,x) y = filter(hd,x,dim) ```

## Description

### Fixed-Point Filter Syntaxes

`y = filter(hd,x)` filters a vector of real or complex input data `x` through a fixed-point filter `hd`, producing filtered output data `y`. The vectors `x` and `y` have the same length. filter stores the final conditions for the filter in the `States` property of `hd``hd.states`.

When you set the property `PersistentMemory` to `false` (the default setting), the initial conditions for the filter are set to zero before filtering starts. To use nonzero initial conditions for `hd`, set `PersistentMemory` to `true`. Then set `hd.states` to a vector of `nstates``(hd)` elements, one element for each state to set. If you specify a scalar for `hd.states`, `filter` expands the scalar to a vector of the proper length for the states. All elements of the expanded vector have the value of the scalar.

If `x` is a matrix, `y = filter(hd,x)` filters along each column of `x` to produce a matrix `y` of independent channels. If `x` is a multidimensional array, `y = filter(hd,x)` filters `x` along the first nonsingleton dimension of `x`.

To use nonzero initial conditions when you are filtering a matrix `x`, set the filter states to a matrix of initial condition values. Set the initial conditions by setting the `States` property for the filter (`hd.states`) to a matrix of `nstates(hd)` rows and `size``(x,2)` columns.

`y = filter(hd,x,dim)` applies the filter `hd` to the input data located along the specific dimension of `x` specified by `dim`.

When you are filtering multichannel data, `dim` lets you specify which dimension of the input matrix to filter along — whether a row represents a channel or a column represents a channel. When you provide the `dim` input argument, the filter operates along the dimension specified by `dim`. When your input data `x` is a vector or matrix and `dim` is 1, each column of `x` is treated as a one input channel. When `dim` is 2, the filter treats each row of the input `x` as a channel.

To filter multichannel data in a loop environment, you must use the `dim` input argument to set the proper processing dimension.

You specify the initial conditions for each channel individually, when needed, by setting `hm.states` to a matrix of `nstates``(hd)` rows (one row containing the states for one channel of input data) and `size(x,2)` columns (one column containing the filter states for each channel).

## Examples

collapse all

Filter a signal using a filter with various initial conditions (IC) or no initial conditions.

```x = randn(100,1); % Original signal. b = fir1(50,.4); % 50th-order linear-phase FIR filter. hd = dfilt.dffir(b); % Direct-form FIR implementation.```

Do not set specific initial conditions.

```y1 = filter(hd,x); % 'PersistentMemory'='false'(default). zf = hd.states; % Final conditions.```

Now use nonzero initial conditions by setting ICs before you filter.

```hd.persistentmemory = true; hd.states = 1; % Uses scalar expansion. y2 = filter(hd,x); stem([y1 y2]) % Different sequences at beginning. legend('y1','y2');``` Looking at the stem plot shows that the sequences are different at the beginning of the filter process.

Here is one way to use filter with streaming data.

```reset(hd); % Clear filter history. y3 = filter(hd,x); % Filter entire signal in one block.```

As an experiment, repeat the process, filtering the data as sections, rather than in streaming form.

```reset(hd); % Clear filter history. yloop = zeros(20,5); % Preallocate output array. xblock = reshape(x,[20 5]); for i=1:5 yloop(:,i) = filter(hd,xblock(:,i)); end```

Use a stem plot to see the comparison between streaming and block-by-block filtering.

```stem([y3 yloop(:)]); legend('y3','yloop')``` Filtering the signal section-by-section is equivalent to filtering the entire signal at once.

## Algorithms

collapse all

### Quantized Filters

The `filter` command implements fixed- or floating-point arithmetic on the quantized filter structure you specify.

The algorithm applied by `filter` when you use a discrete-time filter object on an input signal depends on the response you chose for the filter, such as lowpass or Nyquist or bandstop. To learn more about each filter algorithm, refer to the literature reference provided on the appropriate discrete-time filter reference page.

### Note

`dfilt/filter` does not normalize the filter coefficients automatically. Function `filter` supplied by MATLAB does normalize the coefficients.

The algorithm used by `filter` when you apply an adaptive filter object to a signal depends on the algorithm you chose for your adaptive filter. To learn more about each adaptive filter algorithm, refer to the literature reference provided on the appropriate adaptive filter object reference page.