firgr - Parks-McClellan FIR filter - MATLAB (original) (raw)

Parks-McClellan FIR filter

Syntax

Description

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[w](#mw%5F70271df6-35d2-462b-9a82-a69c220fa3b2)) uses a minimax filter design algorithm to design a real linear phase FIR filterb of length n+1. The filter has the best approximation to the desired frequency response described by f anda in the minimax sense. w is a vector of weights, one per band. When you omit w, all bands are weighted equally. For more information on the input arguments, see firpm.

example

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[ftype](#mw%5Fd1b58ea4-f1ea-4cd3-b6c4-47c2f2bc7905)) design a filter of type specified by ftype

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([m](#mw%5F17cce870-3e9d-433e-a810-26f5a5425b52),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[r](#mw%5F01630ed2-f18c-43aa-b6a1-e734fc3b164e)), designs filters repeatedly until the minimum order filter as specified inm and that meets the specificationsf,a is found. m can be one of 'minorder', 'mineven' or 'minodd'. r is a vector containing the peak ripple per frequency band. You must specify r. When you specify 'mineven' or 'minodd', the minimum even or odd order filter is found.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr({[m](#mw%5F17cce870-3e9d-433e-a810-26f5a5425b52),[ni](#mw%5F75716a0a-28f4-4416-a996-0a594c647b40)},[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[r](#mw%5F01630ed2-f18c-43aa-b6a1-e734fc3b164e)) where m is one of 'minorder', 'mineven' or 'minodd', uses ni as the initial estimate of the filter order. ni is optional for common filter designs, but it must be specified for designs in which firpmord cannot be used, such as while designing differentiators or Hilbert transformers.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[w](#mw%5F70271df6-35d2-462b-9a82-a69c220fa3b2),[e](#mw%5F09f5179c-d5f2-4f6e-a3e9-6a62e9d1c58a)) specifies independent approximation errors for different bands. Use this syntax to design extra ripple or maximal ripple filters. These filters have interesting properties such as having the minimum transition width. e is a cell array of character vectors specifying the approximation errors to use. Its length must equal the number of bands.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[s](#mw%5F72b67128-39b2-4ff8-8e3c-208d87f6a4aa)) is used to design filters with special properties s at certain frequency points.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[a](#mw%5F43f6bf50-1688-4f88-b32c-9a05945d2b72),[s](#mw%5F72b67128-39b2-4ff8-8e3c-208d87f6a4aa),[w](#mw%5F70271df6-35d2-462b-9a82-a69c220fa3b2),[e](#mw%5F09f5179c-d5f2-4f6e-a3e9-6a62e9d1c58a)) specifies weights and independent approximation errors for filters with special properties. The weights and properties are included in vectors w ande. Sometimes, you may need to use independent approximation errors to get designs with forced values to converge. For example,

b = firgr(82,[0 0.055 0.06 0.1 0.15 1], [0 0 0 0 1 1],... {'n' 'i' 'f' 'n' 'n' 'n'}, [10 1 1] ,{'e1' 'e2' 'e3'});

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr(___,'1') designs a type 1 filter (even-order symmetric). You can specify type 2 (odd-order symmetric), type 3 (even-order antisymmetric), and type 4 (odd-order antisymmetric) filters as well. Note that restrictions apply to a at f =0 or f = 1 for FIR filter types 2, 3, and 4.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr(___,'minphase') designs a minimum-phase FIR filter. You can use the argument 'maxphase' to design a maximum phase FIR filter.

b = firgr(___,'check') returns a warning when there are potential transition-region anomalies.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr(___,{[lgrid](#mw%5F075e0f91-4a6f-4a78-9d27-fdfd3af72013)}), where {lgrid} is a scalar cell array. The value of the scalar controls the density of the frequency grid by setting the number of samples used along the frequency axis.

[[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd),[err](#mw%5F545fac5f-59c9-4493-ad0a-5e3ebb04ccc1)] = firgr(___) returns the unweighted approximation error magnitudes. err contains one element for each independent approximation error returned by the function.

[[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd),[err](#mw%5F545fac5f-59c9-4493-ad0a-5e3ebb04ccc1),[res](#mw%5F53069236-ca2d-407a-9194-8ae63380f5ad)] = firgr(___) returns the structure res comprising optional results computed byfirgr.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),[fresp](#mw%5Fade7cd57-2c2f-4e93-9f06-61a13e33c773),[w](#mw%5F70271df6-35d2-462b-9a82-a69c220fa3b2)) returns a length n + 1 FIR filter which has the best approximation to the desired frequency response as returned by the user-defined function_fresp_.

firgr includes a predefined frequency response function named'firpmfrf2'. You can write your own based on the simpler'firpmfrf'. See the help for private/firpmfrf for more information.

[b](#mw%5F730cae97-6ab4-43f1-95ee-19538d9f75bd) = firgr([n](#d126e474743),[f](#mw%5Fc70112bb-6f11-408a-8d4a-586484698000),{[fresp](#mw%5Fade7cd57-2c2f-4e93-9f06-61a13e33c773),p1,p2,...},[w](#mw%5F70271df6-35d2-462b-9a82-a69c220fa3b2)) specifies optional arguments p1, p2,…,pn to be passed to the response function_fresp_.

b = firgr(n,f,a,w) is a synonym for b = firgr(n,f,{'firpmfrf2',a},w), where a is a vector containing your specified response amplitudes at each band edge in f. By default, firgr designs symmetric (even) FIR filters. 'firpmfrf2' is the predefined frequency response function. If you do not specify your own frequency response function (the fresp variable), firgr uses 'firpmfrf2'.

b = firgr(___,'h') and b = firgr(__,'d') design antisymmetric (odd) filters. When you omit the'h' or 'd' arguments from thefirgr command syntax, each frequency response function_fresp_ can tell firgr to design either an even or odd filter. Use the command syntax sym = fresp('defaults',{n,f,[],w,p1,p2,...}).

firgr expects fresp to return sym = 'even' or sym = 'odd'. If fresp does not support this call, firgr assumes even symmetry.

For more information about the input arguments to firgr, refer tofirpm.

Examples

collapse all

Design an FIR filter with two single-band notches at 0.25 and 0.55.

b1 = firgr(42,[0 0.2 0.25 0.3 0.5 0.55 0.6 1],[1 1 0 1 1 0 1 1],... {'n' 'n' 's' 'n' 'n' 's' 'n' 'n'});

Design a highpass filter whose gain at 0.06 is forced to be zero. The gain at 0.055 is indeterminate since it should abut the band.

b2 = firgr(82,[0 0.055 0.06 0.1 0.15 1],[0 0 0 0 1 1],... {'n' 'i' 'f' 'n' 'n' 'n'});

Design a second highpass filter with forced values and independent approximation errors.

b3 = firgr(82,[0 0.055 0.06 0.1 0.15 1], [0 0 0 0 1 1], ... {'n' 'i' 'f' 'n' 'n' 'n'}, [10 1 1] ,{'e1' 'e2' 'e3'});

View the frequency response of the filters.

filterAnalyzer(b1,1,b2,1,b3,1,FilterNames=["Filter_b1","Filter_b2","Filter_b3"]);

Input Arguments

collapse all

Filter order, specified as an integer greater than or equal to 3.

Normalized frequency points, specified as a real-valued vector. The argument must be in the range [0, 1] , where 1 corresponds to the Nyquist frequency. The length off must be even. The frequencies must be in increasing order.

Desired amplitudes at the points specified in f, specified as a vector. f and a must be of the same length. The length of a must be even.

Weights used to adjust the fit in each frequency band, specified as a real-valued vector. The length of w is half the length off and a, so there is exactly one weight per band.

Filter type for linear-phase filters with odd symmetry (type III and type IV), specified as either 'hilbert' or'differentiator':

Type of minimum order filter, specified as 'minorder','mineven', or 'minodd'. When you specify 'mineven' or 'minodd', the function designs a minimum even order or minimum odd order filter.

Peak ripple per frequency band, specified as a real-valued vector. The length ofr is half the length of f anda, so there is exactly one weight per band.

Initial estimate of the filter order, specified as an integer greater than or equal to 3. The function accepts this value in the b = firgr({m,ni},f,a,r} syntax. ni is optional for common filter designs, but it must be specified for designs in whichfirpmord cannot be used, such as while designing differentiators or Hilbert transformers.

Independent approximation errors to use for different bands, specified as a cell array of character vectors or an array of strings. The length of e must equal the number of bands. Entries of e must be in the form'e#' or "e#" where # indicates which approximation error to use for the corresponding band. For example, whene = ["e1","e2","e1"], the first and third bands use the same approximation error "e1" and the second band uses a different one"e2". When all bands use the same approximation error, such as["e1","e1","e1",...], it is equivalent to omittinge, as in b = firgr(n,f,a,w).

Data Types: char | string

Special properties at certain frequency points, specified as a cell array of character vectors or an array of strings. s must have the same length as f and a. Entries ofs must be one of:

For example, the following command designs a bandstop filter with zero-valued single-point stopbands (notches) at 0.25 and 0.55.

b = firgr(42,[0 0.2 0.25 0.3 0.5 0.55 0.6 1],... [1 1 0 1 1 0 1 1],["n" "n" "s" "n" "n" "s" "n" "n"])

b = firgr(82,[0 0.055 0.06 0.1 0.15 1],[0 0 0 0 1 1],["n" "i" "f" "n" "n" "n"]) designs a highpass filter with the gain at 0.06 forced to be zero. The band edge at 0.055 is indeterminate since the first two bands actually touch. The other band edges are normal.

Data Types: char | string

Control the density of the frequency grid, which has roughly(lgrid*n)/(2*bw) frequency points, where bw is the fraction of the total frequency band interval [0,1] covered by f. Increasing lgrid often results in filters that more exactly match an equiripple filter, but that take longer to compute. The default value of16 is the minimum value that should be specified forlgrid.

Frequency response, specified as a function handle. The function is called from within firgr with this syntax:

[dh,dw] = fresp(n,f,gf,w)

where:

Output Arguments

collapse all

Filter coefficients, returned as a row vector of length n + 1.

The firgr uses the minimax filter design algorithm to design the following types of real FIR filters:

Unweighted approximation error magnitudes, returned as a scalar or a vector.err contains one element for each independent approximation error returned by the function.

Frequency response characteristics, returned as a structure. The structureres has the following fields:

Structure Field Contents
res.fgrid Vector containing the frequency grid used in the filter design optimization
res.des Desired response on fgrid
res.wt Weights on fgrid
res.h Actual frequency response on the frequency grid
res.error Error at each point (desired response - actual response) on the frequency grid
res.iextr Vector of indices into fgrid of external frequencies
res.fextr Vector of external frequencies
res.order Filter order
res.edgecheck Transition-region anomaly check. One element per band edge. Element values have the following meanings: 1 = OK, 0 = probable transition-region anomaly, -1 = edge not checked. Computed when you specify the'check' input option in the function syntax.
res.iterations Number of s iterations for the optimization
res.evals Number of function evaluations for the optimization

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced in R2011a

expand all

Starting in R2022b, you can specify the approximation errors (e) and the special properties (s) as an array of strings.

b = firgr(82,[0 0.055 0.06 0.1 0.15 1], [0 0 0 0 1 1],... ["n" "i" "f" "n" "n" "n"],[10 1 1],["e1" "e2" "e3"]);