Documentation Center 
On this page… 

Overview of the Digital Filter Block Implement a Lowpass Filter in Simulink Implement a Highpass Filter in Simulink 
You can use the Digital Filter block to implement digital FIR and IIR filters in your models. Use this block if you have already performed the design and analysis and know your desired filter coefficients. You can use this block to filter singlechannel and multichannel signals, and to simulate floatingpoint and fixedpoint filters. Then, you can use the Simulink^{®} Coder™ product to generate highly optimized C code from your filter block.
To implement a filter with the Digital Filter block, you must provide the following basic information about the filter:
Whether the filter transfer function is FIR with all zeros, IIR with all poles, or IIR with poles and zeros
The desired filter structure
The filter coefficients
You can use the Digital Filter block to implement a digital FIR or IIR filter. In this topic, you use it to implement an FIR lowpass filter:
Define the lowpass filter coefficients in the MATLAB^{®} workspace by typing
lopassNum = [0.0021 0.0108 0.0274 0.0409 0.0266 0.0374 0.1435 0.2465 0.2896 0.2465 0.1435 0.0374 0.0266 0.0409 0.0274 0.0108 0.0021];
From the DSP System Toolbox™ Filtering>Filter Implementations library, clickanddrag a Digital Filter block into your model.
Doubleclick the Digital Filter block. Set the block parameters as follows, and then click OK:
Coefficient source = Dialog parameters
Transfer function type = FIR (all zeros)
Filter structure = Direct form transposed
Numerator coefficients = lopassNum
Input processing = Columns as channels (frame based)
Initial conditions = 0
Note that you can provide the filter coefficients in several ways:
Type in a variable name from the MATLAB workspace, such as lopassNum.
Type in filter design commands from Signal Processing Toolbox™ software or DSP System Toolbox software, such as fir1(5, 0.2, 'low').
Type in a vector of the filter coefficient values.
The Digital Filter block in your model now represents a lowpass filter. In the next topic, Implement a Highpass Filter in Simulink, you use a Digital Filter block to implement a highpass filter. For more information about the Digital Filter block, see the Digital Filter block reference page. For more information about designing and implementing a new filter, see Digital Filter Design Block.
In this topic, you implement an FIR highpass filter using the Digital Filter block:
If the model you created in Implement a Lowpass Filter in Simulink is not open on your desktop, you can open an equivalent model by typing
ex_filter_ex1
at the MATLAB command prompt.
Define the highpass filter coefficients in the MATLAB workspace by typing
hipassNum = [0.0051 0.0181 0.0069 0.0283 0.0061 ... 0.0549 0.0579 0.0826 0.2992 0.5946 0.2992 0.0826 ... 0.0579 0.0549 0.0061 0.0283 0.0069 0.0181 0.0051];
From the DSP System Toolbox Filtering library, and then from the Filter Implementations library, clickanddrag a Digital Filter block into your model.
Doubleclick the Digital Filter block. Set the block parameters as follows, and then click OK:
Coefficient source = Dialog parameters
Transfer function type = FIR (all zeros)
Filter structure = Direct form transposed
Numerator coefficients = hipassNum
Input processing = Columns as channels (frame based)
Initial conditions = 0
You can provide the filter coefficients in several ways:
Type in a variable name from the MATLAB workspace, such as hipassNum.
Type in filter design commands from Signal Processing Toolbox software or DSP System Toolbox software, such as fir1(5, 0.2, 'low').
Type in a vector of the filter coefficient values.
You have now successfully implemented a highpass filter. In the next topic, Filter HighFrequency Noise in Simulink, you use these Digital Filter blocks to create a model capable of removing high frequency noise from a signal. For more information about designing and implementing a new filter, see Digital Filter Design Block.
In the previous topics, you used Digital Filter blocks to implement FIR lowpass and highpass filters. In this topic, you use these blocks to build a model that removes high frequency noise from a signal. In this model, you use the highpass filter, which is excited using a uniform random signal, to create highfrequency noise. After you add this noise to a sine wave, you use the lowpass filter to filter out the highfrequency noise:
If the model you created in Implement a Highpass Filter in Simulink is not open on your desktop, you can open an equivalent model by typing
ex_filter_ex2
at the MATLAB command prompt.
If you have not already done so, define the lowpass and highpass filter coefficients in the MATLAB workspace by typing
lopassNum = [0.0021 0.0108 0.0274 0.0409 0.0266 ... 0.0374 0.1435 0.2465 0.2896 0.2465 0.1435 0.0374 ... 0.0266 0.0409 0.0274 0.0108 0.0021]; hipassNum = [0.0051 0.0181 0.0069 0.0283 0.0061 ... 0.0549 0.0579 0.0826 0.2992 0.5946 0.2992 0.0826 ... 0.0579 0.0549 0.0061 0.0283 0.0069 0.0181 0.0051];
Clickanddrag the following blocks into your model file.
Block  Library  Quantity 

Add  Simulink / Math Operations library  1 
Random Source  Sources  1 
Sine Wave  Sources  1 
Time Scope  Sinks  1 
Set the parameters for the rest of the blocks as indicated in the following table. For any parameters not listed in the table, leave them at their default settings.
Block  Parameter Setting 

Add 

Random Source 

Sine Wave 

Time Scope 

Connect the blocks as shown in the following figure. You may need to resize some of your blocks to accomplish this task.
From the Simulation menu, select Model Configuration Parameters.
The Configuration Parameters dialog box opens.
In the Solver pane, set the parameters as follows, and then click OK:
Start time = 0
Stop time = 5
Type = Fixedstep
Solver = discrete (no continuous states)
In the model window, from the Simulation menu, choose Run.
The model simulation begins and the Scope displays the three input signals.
After simulation is complete, select View > Legend from the Time Scope menu. The legend appears in the Time Scope window. You can clickanddrag it anywhere on the scope display. To change the channel names, doubleclick inside the legend and replace the current numbered channel names with the following:
Add = Noisy Sine Wave
Digital Filter – Lowpass = Filtered Noisy Sine Wave
Sine Wave = Original Sine Wave
In the next step, you will set the color, style, and marker of each channel.
In the Time Scope window, select View > Line Properties, and set the following:
Line  Style  Marker  Color 

Noisy Sine Wave    None  Black 
Filtered Noisy Sine Wave    diamond  Red 
Original Sine Wave  None  *  Blue 
The Time Scope display should now appear as follows:
You can see that the lowpass filter filters out the highfrequency noise in the noisy sine wave.
You have now used Digital Filter blocks to build a model that removes high frequency noise from a signal. For more information about designing and implementing a new filter, see Digital Filter Design Block.
You can use the Digital Filter block to specify a static filter by setting the Coefficient source parameter to Specify via dialog. Depending on the filter structure, you need to enter your filter coefficients into one or more of the following parameters. The block disables all the irrelevant parameters. To see which of these parameters correspond to each filter structure, see Supported Filter Structures in DSP System Toolbox Reference:
Numerator coefficients — Column or row vector of numerator coefficients, [b0, b1, b2, ..., bn].
Denominator coefficients — Column or row vector of denominator coefficients, [a0, a1, a2, ..., am].
Reflection coefficients — Column or row vector of reflection coefficients, [k1, k2, ..., kn].
SOS matrix (Mx6) — Mby6 SOS matrix. You can also use the Biquad Filter block to create a static biquadratic IIR filter.
Scale values — Scalar or vector of M+1 scale values to be used between SOS stages.
To change the static filter coefficients during simulation, doubleclick the block, type in the new vector(s) of filter coefficients, and click OK. You cannot change the filter order, so you cannot change the number of elements in the vector(s) of filter coefficients.
Timevarying filters are filters whose coefficients change with time. You can specify a timevarying filter that changes once per frame or once per sample and you can filter multiple channels with each filter. However, you cannot apply different filters to each channel; all channels must be filtered with the same filter.
To specify a timevarying filter:
Set the Coefficient source parameter to Input port(s), which enables extra block input ports for the timevarying filter coefficients.
Set the Coefficient update rate parameter to One filter per frame or One filter per sample depending on how often you want to update the filter coefficients.
Provide vectors of numerator, denominator, or reflection coefficients to the block input ports for filter coefficients. The series of vectors must arrive at their ports at a specific rate, and must be of certain lengths.
Select or clear the First denominator coefficient = 1, remove a0 term in the structure parameter depending on whether your first denominator coefficient is always 1. To learn more, see Removing the a0 Term in the Filter Structure.
When you know that the first denominator filter coefficient (a_{0}) is always 1 for your timevarying filter, select the First denominator coefficient = 1, remove a0 term in the structure parameter. Selecting this parameter reduces the number of computations the block must make to produce the output (the block omits the 1 / a_{0} term in the filter structure, as illustrated in the following figure). The block output is invalid if you select this parameter when the first denominator filter coefficient is not always 1 for your timevarying filter. Note that the block ignores the First denominator coefficient = 1, remove a0 term in the structure parameter for fixedpoint inputs, since this block does not support nonunity a_{0} coefficients for fixedpoint inputs.
The Digital Filter block does not support timevarying biquadratic filters. To specify a static biquadratic filter (also known as a secondorder section or SOS filter) using the Digital Filter Block, you need to set the following parameters as indicated:
Transfer function type — IIR (poles & zeros)
Filter structure — Biquad direct form I (SOS), or Biquad direct form I transposed (SOS), or , or Biquad direct form II transposed (SOS)
SOS matrix (Mx6) Mby6 SOS matrix
The SOS matrix is an Mby6 matrix, where M is the number of sections in the secondorder section filter. Each row of the SOS matrix contains the numerator and denominator coefficients (b_{ik} and a_{ik}) of the corresponding section in the filter.
Scale values Scalar or vector of M+1 scale values to be used between SOS stages
If you enter a scalar, the value is used as the gain value before the first section of the secondorder filter. The rest of the gain values are set to 1.
If you enter a vector of M+1 values, each value is used for a separate section of the filter. For example, the first element is the first gain value, the second element is the second gain value, and so on.
You can use the ss2sos and tf2sos functions from Signal Processing Toolbox software to convert a statespace or transfer function description of your filter into the secondorder section description used by this block.
The block normalizes each row by a_{1i} to ensure a value of 1 for the zerodelay denominator coefficients.
Note: You can also use the Biquad Filter block to implement a static biquadratic IIR filter. 