Filter Design Toolbox    

gremez Examples

Each of these examples uses one or more features provided in the function gremez. Review each example to get an overview of the capabilities of the function.

Examples--Using gremez to Design FIR Filters

gremez provides a wide range of new capabilities for FIR filter design. Because of the comprehensive nature of the generalized Remez algorithm, the best way to learn what you can do with the new function is by example. This section presents a series of examples that investigate the filters you can design through gremez. You can view these examples as a demonstration program in MATLAB by opening the MATLAB demos and selecting Filter Design from Toolboxes. Listed there you see a number of demonstration programs. Select Minimax FIR Filter Design to see function gremez used to create many filters, from a lowpass filter to a constrained stopband design to a minimum phase, lowpass filter with a constrained stopband.

To open the FIR filter design demo.   

Follow these steps to open the FIR filter design demo in MATLAB.

  1. Start MATLAB.
  2. At the MATLAB prompt, enter demos.
  1. The MATLAB Demo Window dialog opens.

  1. On the left-hand list, double-click Toolboxes to expand the directory tree.
  1. You see a list of the toolbox demonstration programs available in MATLAB.

  1. Select Filter Design.
  2. From the right-hand list, select Minimax FIR Filter Design.

A few examples include comparisons to other filter designs and some include analysis notes. For details about using function gremez, refer to Function Reference. While this set of examples covers some of the options for gremez, many options exist that do not appear in these examples. Examples cover common or interesting gremez options to demonstrate some of the capabilities.

In each of the examples in this section, we use the output argument res to return the structure res that contains information about the filter.

Structure res Element
Contents
res.order

Filter order.

res.fgrid

Vector containing the frequency grid used in the filter design optimization.

res.H
Actual frequency response on the grid in fgrid.
res.error
Error at each point on the frequency grid (desired response- actual response).
res.des
Desired response at each point on fgrid.
res.wt
Weights at each point on fgrid.
res.iextr
Vector of indices into fgrid of extremal frequencies.
res.fextr
Vector of extremal frequencies.
res.iterations
Number of Remez iterations for the optimization.
res.evals
Number of function evaluations for the optimization.
res.edgeCheck

Results of the transition-region anomaly check. Computed when the 'check' option is specified. One element returned per band edge. Returned values can be:

  • 1 = OK
  • 0 = Probable transition-region anomaly
  • -1 = Edge not checked. In the normalized frequency domain, the edges at f=0 and f=1 cannot have anomalies and are not checked.

Example--Designing a Minimax Filter

To use gremez to design an equiripple or minimax filter, we use the following statement.

If you use the same statement, replacing gremez with remez, you get the same filter. You can reproduce any filter that remez generates by replacing remez with gremez in the statement. gremez retains full compatibility with remez.

Here's a plot of the magnitude response of the minimax filter as created by gremez. The following code creates this figure.

Our filter ends up as a 22nd-order filter with magnitude response that has ripples about 1 in the passband and ripples about 0 in the stopband. Using the weight vector, we chose to emphasize meeting the stopband performance five times as much as meeting the passband performance. Hence the reduced ripple in the stopband relative to the passband. In the next figure, we switch the weighting to emphasize the passband, and see that the passband ripple is much smaller than the stopband ripple.

Example--Designing a Minimax Filter, Odd-Order, Antisymmetric

In this example, gremez designs a filter that remez cannot. When you evaluate the following code in MATLAB, the result is a minimax FIR filter, this time having odd-order and antisymmetric structure, known as type 4. You can see from the figure that the magnitude response now represents a high pass filter. In this example, we specify the filter as type 4 ('4' in the statement) to get the odd-order, antisymmetric design we want.

We have weighted the stopband more heavily than the passband ([2 1]) in the function syntax. The 2 and 1 tell gremez that we care about meeting the stopband specification twice as much as the passband specification. Notice that the weighting is relative, not absolute. Our weights say that the stopband is twice as important as the passband. They do not specify the weighting in absolute terms.

Example--Designing a "Least Squares-Like" Filter

gremez lets you design filters that resemble least squares design. In this example, we design a 53rd-order filter and use the user-supplied file taperedresp.m to specify a frequency response weighting function to perform the error weighting for the design. So you can reproduce this example, the file taperedresp.m is in the matlabroot\toolbox\filterdesign\filtdesdemos folder. taperedresp.m contains the following code to specify the weighting.

To generate the least-squares-like filter, use the following code.

When you issue these statements at the MATLAB prompt, you get the following plot for the filter magnitude response.

Example--Designing a Constrained Lowpass Filter

With gremez, you can both apply weighting to the passband and apply a limit or constraint to the error in the stopband, called constraining. Limiting the stopband error can be useful in circumstances where your filter must meet a specified stopband requirement. To create a lowpass filter with a constrained stopband and weighted passband response, we use gremez with the 'w' optional input argument to weight the passband. The optional input argument 'c' constrains the filter stopband error not to exceed 0.2. Note that to use the constraining and weighting options, your filter must have at least one unconstrained band. That is, cell array c must contain at least one 'w' entry. In our example, c is {'w' 'c'}.

The next figure shows the lowpass filter with the constraints applied.

When you use constraining values in your gremez filter design, check to see that your filter actually touches the constraining value in the stopband. If it does not, increase the error weighting ('w') for your unconstrained bands. This change causes the constrained errors to approach the constraint value more quickly. Notice that the plot shows our filter just touches the desired constraint of 0.2.

Example--Designing a Constrained Bandstop Filter

Continuing with the concept of using weighting in gremez, we design a bandstop filter whose passband ripple we constrain not to exceed 0.05 and 0.1. In this instance, cell array c is {'c' 'w' 'c'} to constrain the passbands and we use the optional input vector W=[0.05 1 0.1] to constrain the passband ripple not to exceed 0.05 and 0.1.

As expected the magnitude response shows different peak ripple values in the passbands -- 0.05 for the low frequency band and 0.1 for the high frequency band.

Example--Designing a Single-Point Band Filter

The following statements

generate an interesting filter that you cannot design when you use functions in Signal Processing Toolbox: a multiple stopband filter where the stop bands are defined by single points. In the gremez command in this example, the syntax is b=gremez(N,F,A,S). The input vectors F, A, and S, each containing eight values, define the response curve for the filter.

Input Vector
Use
F=[0 0.2 0.25 0.3 0.5 0.55 0.6 1]
Defines the points of interest in the frequency response. In this case, you are working with frequencies normalized between 0 and 1.
A=[1 1 0 1 1 0 1 1]
Set the gain at each frequency point.
S={'n' 'n' 's' 'n' 'n' 's' 'n' 'n'}
Specifies whether the frequency points represent normal or single-point bands. By comparing the frequency and type vector entries, we see that F=0.25 and F=0.55 are single point bands (marked by s), and the gain at those points is 0. The other bands are normal bands (marked with n) with
gain =1.

From the next figure, you see that the filter has just the response we defined, with zeros at F = 0.25 and F = 0.55.

Example--Designing a Filter with a Specified In-band Value

In some filter design tasks, you want a filter whose inband value you determine exactly. For example, you might want a 60 Hz noise rejection filter to have zero gain at F = 0.06 (F = 60 Hz in real frequency). For this example, the sampling frequency is 2 KHz, so 60 Hz is F = 0.06 when we normalize the frequency. We use the following code example to design such a filter.

At F = 0.06, we require the gain of the filter response to be exactly 0.0. So we force the gain at F = 0.06 to zero by adding the 'f' input option to the S vector. As shown in the plot, the filter response is zero at F = 0.06, and the resulting filter rejects 60 Hz noise quite effectively.

You might have noticed in the gremez statement that the S vector includes an 'i' option. Entries in the S vector have any of the following values.

Vector Symbol
Meaning
n
Represents a normal frequency point
s
Represents a single-point band frequency
f
Forces the gain at this frequency to a fixed value, as specified in the weighting vector W
i
Represents an indeterminate frequency point. Usually used when the band should abut the next band

For our noise rejecting filter, the sampling frequency is 2 KHz, so 60 Hz is f=0.06 in normalized frequency.

Example--Designing Extra-Ripple and Maximal-Ripple Filters

Extra-ripple and maximal-ripple filters have some interesting properties:

gremez lets you use multiple independent approximation errors to directly design extra- and maximal ripple filters. In this example, we use independent errors to design two filters, then we revisit our 60 Hz noise rejection filter to compare these two different approaches to designing the same filter.

Example of an Extra-Ripple Lowpass Filter

The code to design our extra-ripple filter is

The last entries in the command, [1 1] and {'e1' 'e2'}, are the vectors W and E that determine the weights and independent approximation errors for filters with special properties. 'e1' is applied to the passband and 'e2' applied to the stopband. Where the gremez algorithm usually results in equiripple filters, using the approximations lets gremez adjust the ripple in each band separately, as we have done in this design.

Example of an Extra-Ripple Bandstop Filter With Two Independent Approximation Errors

Now we extend the extra-ripple concept by using two independent error approximations. The two passbands share the first approximation error 'e1'. The stopband uses 'e2'. So you can see the effectiveness of this design approach, also create and plot a single approximation error filter for comparison.

In the figure, the responses are similar for the two designs, but the extra-ripple design shows less ripple in the passbands and slightly more in the stopband. If you evaluate the example code in MATLAB to create the plot, you can select Zoom in from the Tools menu in the figure window to examine the curves more closely.

For this design, we let gremez use the same error approximation for the passbands and a different one in the stopband. The result is a filter that has minimum total error in the passbands, and minimum error in the stopband.

Example--Comparing Two 60 Hz Noise Rejection Filters

With the extra-ripple filter design technique available in gremez, we can use two different design techniques to redo our 60 Hz noise rejection filter. We use three independent error approximations in this design, one for each band, as shown in the following code.

We have included the second gremez statement in this example to reproduce the earlier noise rejection filter for comparison. We plot them on the same figure for easy reference. In the stopband, the original design has lower ripple; the new, independent error design has less ripple in the passband. Also, the new filter has slightly steeper transition region performance.

Using independent approximation errors, as we did in this filter when we specified 'e1', 'e2', and 'e3', can result in better filter performance. The strings 'e1', 'e2', and so on direct gremez to consider the associated band alone, or with other bands that use the same error approximation. By assigning independent errors to each band, we let the generalized Remez algorithm used by gremez minimize the error in each band without considering the error in the other bands. If we do not use independent errors, the algorithm minimizes the total error in all bands at once.

At times, you need to use independent approximation errors to get designs that use forced inband values to converge. Error approximations are needed where the polynomial used to approximate the filter becomes undetermined when you try to force the inband values to converge.

Example--Checking for Transition-Region Anomalies

To allow you to check your filter designs for anomalies, gremez provides an input option called 'check'. With the check option included in the command, gremez reports anomalies in the response curve for the filter. An anomaly in gremez is defined as out-of-the-ordinary response behavior in a transition, or "don't care," region of the filter response.

To demonstrate anomaly checking, we use gremez to design a filter with an anomaly, and include the 'check' optional input argument.

With the 'check' option, gremez returns the results vector res.edgeCheck in the structure res. Each zero-valued entry in this vector represents the location of a probable anomaly in the filter response. Entries that are not checked, such as the edges at f=1 and f=0, have -1 entries in res.edgeCheck.

To check for anomalies, the following command returns the vector containing the edge check results.

There are anomalies between the f=0.6 and f=0.8 edges, as shown clearly in the figure. This represents a transition region for our filter. Notice that the edges at f=0 and f=1 were not checked.

In our example, the anomalous behavior happened because of the width of the transition region. When we define a narrower transition band, the anomaly disappears. Generally, reducing the transition region width eliminates anomalies in the filter response.

Example--Using Automatic Minimum Filter Order Determination

Rather than entering the filter order N in the gremez command, you can let the generalized Remez algorithm determine the minimum order for your filter. You set the specifications for the filter and the generalized Remez algorithm repeatedly designs the filter until the design just meets your specifications.

You have three options for setting the minimum order option for the filter:

For this example, we let the Remez algorithm find a minimum order filter that implements a lowpass filter with a transition band between f=0.4 and f=0.5.

Our filter, shown in the figure, demonstrates the desired ripple in the passbands and stopbands, 0.1 and 0.02; the transition region meets our specifications; and the filter order (found from res.order) is 22.

When you use the minimum order feature, you can specify the initial order (your best guess) in the gremez statement. When you estimate the order, gremez does not use remezord to make an estimate of the filter order. This is important when remezord does not support your desired filter type, such as differentiators and Hilbert transformers, as well as for filters that use frequency response functions that you supply. For the following filter example, we provide an initial estimate of 18 for the filter order, and we specify that we want our filter to have the minimum even order possible by adding the 'mineven' option.

Though we provided an initial estimate of 18 for the order, the final order for our filter is again 22. If we had specified 'minodd', the result would be a 23rd-order filter.

Example -- Designing an Interpolation Filter

Now let us design an interpolation filter. These are usually used to upsample a band-limited signal by an integer factor, for example after the signal has been decimated by downsampling. Upsampling is often used while designing multirate filters to reduce the computational load required to use a filter. In Signal Processing Toolbox, you can use the function intfilt to design an interpolation filter. While intfilt provides a way to design the filter, it does not provide the control that gremez offers. Input options for gremez let you define the filter response and errors in each passband and stopband, and the weighting of the band responses in the filter design.

We specify a 30th-order filter with edges at 0.1, 0.4, 0.6, and 0.9, and weight them as [1 100 100]. The resulting design has stopbands between f=0.4 and f=0.6, and f=0.9 and f=1.0.

The next figure shows a filter designed by gremez.

Example--Comparing Filters Designed by gremez and intfilt

Now, to see that gremez lets you develop a better interpolation filter than intfilt, we compare filters designed by both functions. We need three sets of code to display the filters for our comparison -- the first set generates the detail plot of the first stopband, the second set displays the second stopband in detail, and the third plot focuses on the stopband ripple. To keep the frequency response displays consistent, we use the MATLAB plot function to ensure that the axes and labels are the same for both filters. freqzplot does not provide enough control of the plotting functions.

Code to display the first stopband.   

Code set to display the second stopband.   

Code set to display the passband ripple.   

In the next figure, showing the first stopband in detail, you see that using the weighting function in gremez improved the minimum stopband attenuation by almost 20 dB over the intfilt design.

If we switch to a plot of the second stopband, shown in the next figure, you see that the equiripple attenuation throughout the band is about 6 dB larger for the gremez-generated filter than the minimum stopband attenuation of the filter designed by intfilt.

Finally, let's look at the passbands of the two filters, shown in the next figure. Here, the ripple in the gremez-designed filter is slightly larger than the passband ripple for the intfilt design. Still, both are very small, less than 0.014 dB peak-to-peak.

Example--Designing a Minimum Phase Lowpass Filter with a Constrained Stopband

With gremez you can determine whether the FIR filter you design is minimum phase, maximum phase, or linear phase. Through this example we show a minimum phase filter and look at the roots of the filter transfer function to see that no roots lie outside the unit circle in the z-plane. First, we create the minimum phase filter by using gremez with the 'minphase' optional input argument.

gremez generates a lowpass filter with constrained stopband magnitude equal to 0.1, and the filter is minimum phase as well. We could have specified a maximum phase design by replacing the 'minphase' option with 'maxphase'. In the gremez statement, you might have noticed the cell array {64} entry. The cell array entries define the grid density for points across the frequency spectrum.

Now, plot the filter to view the frequency response.

We have a lowpass filter with stopband ripple not exceeding 0.1, as desired.

In the next figure, viewing our filter roots on the z-plane plot shows us that the roots lie in or on the unit circle. The zeros of a minimum phase delay FIR filter lie on or inside the unit circle. Maximum phase delay filters have zeros that lie on or outside the unit circle.

Notice that the filter, with eight zeros on the unit circle, could be very sensitive to quantization. You could use FDATool to investigate the effects of quantizing this filter, and to convert the filter to second order sections or make other changes the reduce the sensitivity to quantization.


  Advanced FIR Filter Designs firlpnorm Examples