# Documentation

## Filter Realization Wizard

### Overview of the Filter Realization Wizard

The Filter Realization Wizard is another DSP System Toolbox™ block that can be used to design and implement digital filters. You can use this tool to filter single-channel floating-point or fixed-point signals. Like the Digital Filter Design block, double-clicking a Filter Realization Wizard block opens FDATool. Unlike the Digital Filter Design block, the Filter Realization Wizard starts FDATool with the Realize Model panel selected. This panel is optimized for use with DSP System Toolbox software.

For more information, see the Filter Realization Wizard block reference page. For information on choosing between the Digital Filter Design block and the Filter Realization Wizard, see Select a Filter Design Block.

### Design and Implement a Fixed-Point Filter in Simulink

In this section, a tutorial guides you through creating a fixed-point filter with the Filter Realization Wizard. You will use the Filter Realization Wizard to remove noise from a signal. This tutorial has the following parts:

#### Part 1 — Create a Signal with Added Noise

In this section of the tutorial, you will create a signal with added noise. Later in the tutorial, you will filter this signal with a fixed-point filter that you design with the Filter Realization Wizard.

1. Type

```load mtlb soundsc(mtlb,Fs)```

at the MATLAB® command line. You should hear a voice say "MATLAB." This is the signal to which you will add noise.

2. Create a noise signal by typing

`noise = cos(2*pi*3*Fs/8*(0:length(mtlb)-1)/Fs)';`

at the command line. You can hear the noise signal by typing

`soundsc(noise,Fs)`
3. Add the noise to the original signal by typing

`u = mtlb + noise;`

at the command line.

4. Scale the signal with noise by typing

`u = u/max(abs(u));`

at the command line. You scale the signal to try to avoid overflows later on. You can hear the scaled signal with noise by typing

`soundsc(u,Fs)`
5. View the scaled signal with noise by typing

`spectrogram(u,256,[],[],Fs);colorbar`

at the command line.

The spectrogram appears as follows.

In the spectrogram, you can see the noise signal as a line at about 2800 Hz, which is equal to `3*Fs/8`.

#### Part 2 — Create a Fixed-Point Filter with the Filter Realization Wizard

Next you will create a fixed-point filter using the Filter Realization Wizard. You will create a filter that reduces the effects of the noise on the signal.

1. Open a new Simulink® model, and drag-and-drop a Filter Realization Wizard block from the Filtering / Filter Implementations library into the model.

 Note:   You do not have to place a Filter Realization Wizard block in a model in order to use it. You can open the GUI from within a library. However, for purposes of this tutorial, we will keep the Filter Realization Wizard block in the model.
2. Double-click the Filter Realization Wizard block in your model. The Realize Model panel of the Filter Design and Analysis Tool (FDATool) appears.

3. Click the Design Filter button ( ) on the bottom left of FDATool. This brings forward the Design filter panel of the tool.

4. Set the following fields in the Design filter panel:

• Set Design Method to ```IIR -- Constrained Least Pth-norm```

• Set Fs to `Fs`

• Set Fpass to `0.2*Fs`

• Set Fstop to `0.25*Fs`

• Set Max pole radius to `0.8`

• Click the Design Filter button

The Design filter panel should now appear as follows.

5. Click the Set quantization parameters button on the bottom left of FDATool ( ). This brings forward the Set quantization parameters panel of the tool.

6. Set the following fields in the Set quantization parameters panel:

• Select `Fixed-point` for the Filter arithmetic parameter.

• Make sure the Best precision fraction lengths check box is selected on the Coefficients pane.

The Set quantization parameters panel should appear as follows.

7. Click the Realize Model button on the left side of FDATool ( ). This brings forward the Realize Model panel of the tool.

8. Select the Build model using basic elements check box, then click the Realize Model button on the bottom of FDATool. A subsystem block for the new filter appears in your model.

 Note:   You do not have to keep the Filter Realization Wizard block in the same model as the generated Filter block. However, for this tutorial, we will keep the blocks in the same model.
9. Double-click the `Filter` subsystem block in your model to view the filter implementation.

#### Part 3 — Build a Model to Filter a Signal

In this section of the tutorial, you will filter noise from a signal in your Simulink model.

1. Connect a Signal From Workspace block from the Sources library to the input port of your filter block.

2. Connect a To Workspace block from the Sinks library to the output port of your filter block. Your blocks should now be connected as follows.

3. Open the Signal From Workspace block dialog box and set the Signal parameter to `u`. Click OK to save your changes and close the dialog box.

4. Open the Model Configuration Parameters dialog box from the Simulation menu of the model. In the Solver pane of the dialog, set the following fields:

• Stop time = `length(u)-1`

• Type = `Fixed-step`

Click OK to save your changes and close the dialog box.

5. Run the model.

6. From the Display menu of the model, select Signals & Ports > Port Data Types. You can now see that the input to the Filter block is a signal of type `double` and the output of the Filter block has a data type of `sfix16_En11`.

#### Part 4 — Examine Filtering Results

Now you can listen to and look at the results of the fixed-point filter you designed and implemented.

1. Type

`soundsc(yout,Fs)`

at the command line to hear the output of the filter. You should hear a voice say "MATLAB." The noise portion of the signal should be close to inaudible.

2. Type

```figure spectrogram(yout,256,[],[],Fs);colorbar```

at the command line.

From the colorbars at the side of the input and output spectrograms, you can see that the noise has been reduced by about 40 dB.

### Set the Filter Structure and Number of Filter Sections

The Current Filter Information region of FDATool shows the structure and the number of second-order sections in your filter.

Change the filter structure and number of filter sections of your filter as follows:

• Select Convert Structure from the Edit menu to open the Convert Structure dialog box. For details, see "Converting to a New Structure" in the Signal Processing Toolbox™ documentation.

• Select Convert to Second-order Sections from the Edit menu to open the Convert to SOS dialog box. For details, see "Converting to Second-Order Sections" in the Signal Processing Toolbox documentation.

 Note:   You might not be able to directly access some of the supported structures through the Convert Structure dialog of FDATool. However, you can access all of the structures by creating a `dfilt` filter object with the desired structure, and then importing the filter into FDATool. To learn more about the Import Filter panel, see "Importing a Filter Design" in the Signal Processing Toolbox documentation.

### Optimize the Filter Structure

The Filter Realization Wizard can implement a digital filter using either digital filter blocks from the DSP System Toolbox library or by creating a subsystem block that implements the filter using Sum, Gain, and Delay blocks. The following procedure shows you how to optimize the filter implementation:

1. Open the Realize Model pane of FDATool by clicking the Realize Model button in the lower-left corner of FDATool.

2. Select the desired optimizations in the Optimization region of the Realize Model pane. See the following descriptions and illustrations of each optimization option.

• Optimize for zero gains — Remove zero-gain paths.

• Optimize for unity gains — Substitute gains equal to one with a wire (short circuit).

• Optimize for negative gains — Substitute gains equal to `-1` with a wire (short circuit), and change the corresponding sums to subtractions.

• Optimize delay chains — Substitute any delay chain made up of n unit delays with a single delay by n.

• Optimize for unity scale values — Remove all scale value multiplications by 1 from the filter structure.

The following diagram illustrates the results of each of these optimizations.