# Discrete Filter

Model Infinite Impulse Response (IIR) filters

Discrete

## Description

The Discrete Filter block independently filters each channel of the input signal with the specified digital IIR filter. You can specify the filter structure as one of | ```Direct form I``` | `Direct form I transposed` | ```Direct form II``` | `Direct form II transposed`. The block implements static filters with fixed coefficients. You can tune the coefficients of these static filters.

This block filters each channel of the input signal independently over time. The Input processing parameter allows you to specify how the block treats each element of the input. You can specify treating input elements as an independent channel (sample-based processing), or treating each column of the input as an independent channel (frame-based processing). To perform frame-based processing, you must have a DSP System Toolbox™ license.

The output dimensions equal those of the input, except when you specify a matrix of filter taps for the Numerator coefficients parameter. When you do so, the output dimensions depend on the number of different sets of filter taps you specify.

Use the Numerator coefficients parameter to specify the coefficients of the discrete filter numerator polynomial. Use the Denominator coefficients parameter to specify the coefficients of the denominator polynomial of the function. The Denominator coefficients parameter must be a vector of coefficients.

Specify the coefficients of the numerator and denominator polynomials in ascending powers of z-1. The Discrete Filter block lets you use polynomials in z-1 (the delay operator) to represent a discrete system. This method is the one that signal processing engineers typically use. Conversely, the Discrete Transfer Fcn block lets you use polynomials in z to represent a discrete system. This method is the one that control engineers typically use. When the numerator and denominator polynomials have the same length, the two methods are identical.

### Specifying Initial States

In Dialog parameters and Input port(s) modes, the block initializes the internal filter states to zero by default, which is equivalent to assuming past inputs and outputs are zero. You can optionally use the Initial states parameter to specify nonzero initial states for the filter delays.

To determine the number of initial state values you must specify, and how to specify them, see the following table on Valid Initial States and Number of Delay Elements (Filter States). The Initial states parameter can take one of four forms as described in the following table.

Valid Initial States

Initial stateExamplesDescription

Scalar

`5`

Each delay element for each channel is set to `5`.

The block initializes all delay elements in the filter to the scalar value.

Vector
(for applying the same delay elements to each channel)

For a filter with two delay elements: [d1 d2]

The delay elements for all channels are d1 and d2.

Each vector element specifies a unique initial condition for a corresponding delay element. The block applies the same vector of initial conditions to each channel of the input signal. The vector length must equal the number of delay elements in the filter (specified in the table Number of Delay Elements (Filter States)).

Vector or matrix
(for applying different delay elements to each channel)

For a 3-channel input signal and a filter with two delay elements:

[d1 d2 D1 D2 d1 d2] or

$\left[\begin{array}{lll}{d}_{1}\hfill & {D}_{1}\hfill & {d}_{1}\hfill \\ {d}_{2}\hfill & {D}_{2}\hfill & {d}_{2}\hfill \end{array}\right]$

• The delay elements for channel 1 are d1 and d2.

• The delay elements for channel 2 are D1 and D2.

• The delay elements for channel 3 are d1and d2.

Each vector or matrix element specifies a unique initial condition for a corresponding delay element in a corresponding channel:

• The vector length must be equal to the product of the number of input channels and the number of delay elements in the filter (specified in the table Number of Delay Elements (Filter States)).

• The matrix must have the same number of rows as the number of delay elements in the filter (specified in the table Number of Delay Elements (Filter States)), and must have one column for each channel of the input signal.

Empty matrix

`[ ]`
Each delay element for each channel is set to `0`.

The empty matrix, `[]`, is equivalent to setting the Initial conditions parameter to the scalar value `0`.

The number of delay elements (filter states) per input channel depends on the filter structure, as indicated in the following table.

Number of Delay Elements (Filter States)

Filter StructureNumber of Delay Elements per Channel

```Direct form I Direct form I transposed```

• `number of zeros - 1`

• `number of poles - 1`

```Direct form II Direct form II transposed```

`max(number of zeros, number of poles)-1`

The following tables describe the valid initial states for different sizes of input and different number of channels. These tables provide this information according to whether you set the Input processing parameter to frame based or sample based.

Frame-Based Processing

Input Number of ChannelsValid Initial States (Dialog Box)Valid Initial States (Input Port)
• Column vector (K-by-1)

• Unoriented vector (K)

1
• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Scalar

• Column vector (M-by-1)

• Row vector (1-by-N)

• Matrix (K-by-N)

N
• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Matrix (M-by-N)

• Scalar

• Matrix (M-by-N)

Sample-Based Processing

InputNumber of ChannelsValid Initial States (Dialog Box)Valid Initial States (Input Port)
• Scalar

1
• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Row vector (1-by-N)

• Column vector (N-by–1)

• Unoriented vector (N)

N
• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Matrix (M-by-N)

• Scalar

• Matrix (K-by-N)

K×N
• Scalar

• Column vector (M-by-1)

• Row vector (1-by-M)

• Matrix (M-by-(K×N))

• Scalar

When the Initial states is a scalar, the block initializes all filter states to the same scalar value. Enter `0` to initialize all states to zero. When the Initial states is a vector or a matrix, each vector or matrix element specifies a unique initial state. This unique state corresponds to a delay element in a corresponding channel:

• The vector length must equal the number of delay elements in the filter, `M = max(number of zeros, number of poles)`.

• The matrix must have the same number of rows as the number of delay elements in the filter, ```M = max(number of zeros, number of poles)```. The matrix must also have one column for each channel of the input signal.

The following example shows the relationship between the initial filter output and the initial input and state. Given an initial input u1, the first output y1 is related to the initial state [x1, x2] and initial input by:

${y}_{1}={b}_{1}\left[\frac{\left({u}_{1}-{a}_{2}{x}_{1}-{a}_{3}{x}_{2}\right)}{{a}_{1}}\right]+{b}_{2}{x}_{1}+{b}_{3}{x}_{2}$

To see an example of how to set initial conditions as a vector:

• Click on the model `ex_discretefilter_nonzero_icex_discretefilter_nonzero_ic`, or type it at the MATLAB® command prompt.

• Double-click on the Discrete Filter block, and set the parameters. The following shows how to set the initial conditions of the Discrete Filter block to [1 2].

• Simulate the model, by left-clicking the green simulation icon.

• Double-click the scope. You can see that the difference between the signal filtered by the Discrete Filter block, and the signal from the filter's building blocks, is zero.

This demonstrates that you can enter the initial conditions of the Discrete Filter block as a vector of [1 2]. You can also set the initial condition of the first Unit Delay to 1 and the second Unit Delay to 2. The resulting outputs are the same.

## Data Type Support

The Discrete Filter block accepts and outputs real and complex signals of any signed numeric data type that Simulink® supports. The block supports the same types for the numerator and denominator coefficients.

Numerator and denominator coefficients must have the same complexity. They can have different word lengths and fraction lengths.

The following diagrams show the filter structure and the data types used within the Discrete Filter block for fixed-point signals.

The block omits the dashed divide when you select the Optimize by skipping divide by leading denominator coefficient (a0) parameter.

## Parameters and Dialog Box

The Main pane of the Discrete Filter block dialog box appears as follows.

Numerator

Numerator coefficients of the discrete filter. To specify the coefficients, set the Source to `Dialog`. Then, enter the coefficients in Value as descending powers of z. Use a row vector to specify the coefficients for a single numerator polynomial.

Denominator

Denominator coefficients of the discrete filter. To specify the coefficients, set the Source to `Dialog`. Then, enter the coefficients in Value as descending powers of z. Use a row vector to specify the coefficients for a single denominator polynomial.

Initial states

If the Source is `Dialog`, then, in Value, specify the initial states of the filter states. To learn how to specify initial states, see Specifying Initial States.

If the Source is `Input port`, then you do not need to specify Value.

External reset

Specify the trigger event to use to reset the states to the initial conditions.

Reset ModeBehavior
`None`No reset.
`Rising`Reset on a rising edge.
`Falling`Reset on a falling edge.
`Either`Reset on either a rising or falling edge.
`Level`Reset in either of these cases:
• when there is a nonzero at the current time step

• when the time step value changes from nonzero at the previous time step to zero at the current time step

`Level hold`Reset when nonzero at the current time step.

The reset signal must be a scalar of type `single`, `double`, `boolean`, or `integer`. Fixed point data types, except for `ufix1`, are not supported.

Input processing

Specify whether the block performs sample- or frame-based processing.

• `Elements as channels (sample based)` — Process each element of the input as an independent channel.

• `Columns as channels (frame based)` — Process each column of the input as an independent channel.

 Note:   Frame-based processing requires a DSP System Toolbox license.For more information, see Sample- and Frame-Based Concepts in the DSP System Toolbox documentation.
Optimize by skipping divide by leading denominator coefficient (a0)

Select when the leading denominator coefficient, a0, equals 1. This parameter optimizes your code.

When you select this check box, the block does not perform a divide-by-a0 either in simulation or in the generated code. An error occurs if a0 is not equal to one.

When you clear this check box, the block is fully tunable during simulation. It performs a divide-by-a0 in both simulation and code generation.

Sample time

Specify the time interval between samples. To inherit the sample time, set this parameter to `-1`. See Specify Sample Time.

The Data Types pane of the Discrete Filter block dialog box appears as follows.

State

Specify the state data type. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Same as input```

• A built-in integer, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the State parameter.

Numerator coefficients

Specify the numerator coefficient data type. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in integer, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Numerator coefficients parameter.

Numerator coefficient minimum

Specify the minimum value that a numerator coefficient can have. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Numerator coefficient maximum

Specify the maximum value that a numerator coefficient can have. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Numerator product output

Specify the product output data type for the numerator coefficients. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in data type, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Numerator product output parameter.

Numerator accumulator

Specify the accumulator data type for the numerator coefficients. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in data type, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Numerator accumulator parameter.

Denominator coefficients

Specify the denominator coefficient data type. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in integer, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Denominator coefficients parameter.

Denominator coefficient minimum

Specify the minimum value that a denominator coefficient can have. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Denominator coefficient maximum

Specify the maximum value that a denominator coefficient can have. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Denominator product output

Specify the product output data type for the denominator coefficients. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in data type, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Denominator product output parameter.

Denominator accumulator

Specify the accumulator data type for the denominator coefficients. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in data type, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Denominator accumulator parameter.

Output

Specify the output data type. You can set this parameter to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via internal rule```

• A built-in data type, for example, `int8`

• A data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Output parameter.

Output minimum

Specify the minimum value that the block can output. The default value is `[]` (unspecified). Simulink software uses this value to perform:

• Simulation range checking (see Signal Ranges)

• Automatic scaling of fixed-point data types

Output maximum

Specify the maximum value that the block can output. The default value is `[]` (unspecified). Simulink software uses this value to perform:

• Simulation range checking (see Signal Ranges)

• Automatic scaling of fixed-point data types

Lock data type settings against changes by the fixed-point tools

Select to lock all data type settings of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, see Lock the Output Data Type Setting in the Fixed-Point Designer™ documentation.

Integer rounding mode

Specify the rounding mode for fixed-point operations. For more information, see Rounding. in the Fixed-Point Designer documentation.

Saturate on integer overflow
ActionReasons for Taking This ActionWhat Happens for OverflowsExample

Select this check box.

Your model has possible overflow, and you want explicit saturation protection in the generated code.

Overflows saturate to either the minimum or maximum value that the data type can represent.

The maximum value that the `int8` (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128.

Do not select this check box.

You want to optimize efficiency of your generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.

Overflows wrap to the appropriate value that is representable by the data type.

The maximum value that the `int8` (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, the software interprets the overflow-causing value as `int8`, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as `int8`, is -126.

When you select this check box, saturation applies to every internal operation on the block, not just the output or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

The State Attributes pane of the Discrete Filter block dialog box appears as follows.

State name

Use this parameter to assign a unique name to the block state. The default is `' '`. When this field is blank, no name is assigned. When using this parameter, remember these considerations:

• A valid identifier starts with an alphabetic or underscore character, followed by alphanumeric or underscore characters.

• The state name applies only to the selected block.

This parameter enables State name must resolve to Simulink signal object when you click Apply.

State name must resolve to Simulink signal object

Select this check box to require that the state name resolve to a Simulink signal object. This check box is cleared by default.

State name enables this parameter.

Selecting this check box disables Code generation storage class.

Package

Select a package that defines the custom storage class you want to apply. If you have defined any packages of your own, click Refresh. This action adds all user-defined packages on your search path to the package list.

Code generation storage class

Select custom storage class for state.

Default: `Auto`

`Auto`

Auto is the appropriate storage class for states that you do not need to interface to external code.

`SimulinkGlobal`

`model_P` initializes the state to its corresponding value in the workspace.

`ExportedGlobal`

State is stored in a global variable

`ImportedExtern`

`model_private.h` declares the state as an extern variable.

`ImportedExternPointer`

`model_private.h` declares the state as an extern pointer.

`Default`

A non-editable placeholder storage class is created.

`BitField`

A `struct` declaration is created that embeds Boolean data.

`Volatile`

Volatile type qualifier is used in state declaration.

`ExportToFile`

Header `(.h)` file containing global variable declarations is generated with user-specified name.

`ImportFromFile`

Predefined header `(.h)` files containing global variable declarations are included.

`FileScope`

A static qualifier is generated in front of the state declaration to make the state visible only to the current file.

`Struct`

A `struct` declaration is created to encapsulate parameter or signal object data.

`StructVolatile`

Volatile type qualifier is used in `struct` declaration.

`GetSet`

Supports specialized function calls to read and write memory.

State name enables this parameter.

The list of valid storage classes differs based on the Package selection.

Setting this parameter to `ExportedGlobal`, `ImportedExtern`, or `ImportedExternPointer` enables Code generation storage type qualifier.

Code generation storage type qualifier

Specify a Simulink Coder storage type qualifier. The default is `' '`. When this field is blank, no qualifier is assigned.

The Simulink Coder product does not check this string for errors. Thus, whatever value you enter appears automatically in the variable declaration.

Setting Code generation storage class to `ExportedGlobal`, `ImportedExtern`, or `ImportedExternPointer` enables this parameter.

During simulation, the block uses the following values:

• The initial value of the signal object to which the state name resolves

• Minimum and maximum values of the signal object

## Characteristics

 Direct Feedthrough Only when the leading numerator coefficient does not equal zero Sample Time Specified in the Sample time parameter Scalar Expansion Yes, of initial states States Dimensionalized Yes Zero-Crossing Detection No

## Filter Structure Diagrams

The diagrams in the following sections show the filter structures supported by the Digital Filter block. They also show the data types used in the filter structures for fixed-point signals. You can set the coefficient, output, accumulator, product output, and state data types shown in these diagrams in the block dialog.

### IIR direct form I

The following constraints are applicable when processing a fixed-point signal with this filter structure:

• Inputs can be real or complex.

• Numerator and denominator coefficients can be real or complex.

• Numerator and denominator coefficients must have the same complexity characteristics.

• When the numerator and denominator coefficients are specified using input ports and have different complexities from each other, you get an error.

• When the numerator and denominator coefficients are specified in the dialog box and have different complexities from each other, the block does not error. Instead, it processes the filter as if two sets of complex coefficients are provided. The real-valued coefficient set is treated as if it is a complex vector with zero-valued imaginary parts.

• Numerator and denominator coefficients must have the same word length. They can have different fraction lengths.

• The State data type cannot be specified on the block mask for this structure. Doing so is not possible because the input and output states have the same data types as the input and output buffers.

### IIR direct form I transposed

The following constraints are applicable when processing a fixed-point signal with this filter structure:

• Inputs can be real or complex.

• Numerator and denominator coefficients can be real or complex.

• Numerator and denominator coefficients must have the same complexity characteristics.

• When the numerator and denominator coefficients are specified using input ports and have different complexities from each other, you get an error.

• When the numerator and denominator coefficients are specified in the dialog box and have different complexities from each other, the block does not error. Instead, it processes the filter as if two sets of complex coefficients are provided. The real-valued coefficient set is treated as if it is a complex vector with zero-valued imaginary parts.

• States are complex when either the input or the coefficients are complex.

• Numerator and denominator coefficients must have the same word length. They can have different fraction lengths.

### IIR direct form II

The following constraints are applicable when processing a fixed-point signal with this filter structure:

• Inputs can be real or complex.

• Numerator and denominator coefficients can be real or complex.

• Numerator and denominator coefficients must have the same complexity characteristics.

• When the numerator and denominator coefficients are specified using input ports and have different complexities from each other, you get an error.

• When the numerator and denominator coefficients are specified in the dialog box and have different complexities from each other, the block does not error. Instead, it processes the filter as if two sets of complex coefficients are provided. The real-valued coefficient set is treated as if it is a complex vector with zero-valued imaginary parts.

• States are complex when either the inputs or the coefficients are complex.

• Numerator and denominator coefficients must have the same word length. They can have different fraction lengths.

### IIR direct form II transposed

The following constraints are applicable when processing a fixed-point signal with this filter structure:

• Inputs can be real or complex.

• Numerator and denominator coefficients can be real or complex.

• Numerator and denominator coefficients must have the same complexity characteristics.

• When the numerator and denominator coefficients are specified using input ports and have different complexities from each other, you get an error.

• When the numerator and denominator coefficients are specified in the dialog box and have different complexities from each other, the block does not error. Instead, it processes the filter as if two sets of complex coefficients are provided. The real-valued coefficient set is treated as if it is a complex vector with zero-valued imaginary parts.

• States are complex when either the inputs or the coefficients are complex.

• Numerator and denominator coefficients must have the same word length. They can have different fraction lengths.

## Supported Data Types

• Double-precision floating point

• Single-precision floating point

• Fixed point (signed only)

• 8-, 16-, and 32-bit signed integers

 Allpole Filter DSP System Toolbox Digital Filter Design DSP System Toolbox Discrete FIR Filter Simulink Filter Realization Wizard DSP System Toolbox `dsp.IIRFilter` DSP System Toolbox `dsp.AllpoleFilter` DSP System Toolbox `fdatool` DSP System Toolbox `fvtool` Signal Processing Toolbox