Engee documentation

firpm

Designing an optimal FIR filter by the Parks-McClellan method.

Library

EngeeDSP

Syntax

Function call

  • b, err, res = firpm(n, f, a) — returns a vector string b, containing n+1 coefficients of the FIR filter of the order n, the maximum deviation of the amplitude characteristic from the desired one err and the frequency response in the form of a structure res. The frequency and amplitude characteristics of the resulting filter correspond to the vectors f and a.

  • b, err, res = firpm(n, f, a, w) — uses weighting factors w for frequency intervals.

  • b, err, res = firpm(n, f, a, lgrid) — uses an integer value lgrid to control the density of the frequency grid.

  • b, err, res = firpm(n, f, fresp, w) — designs an FIR filter, the amplitude characteristics of which correspond to the characteristic specified by the function fresp.

  • b, err, res = firpm(n, f, fresp, w, ftype) — designs antisymmetric (odd) filters, where ftype defines the filter type. If ftype not specified, then fresp uses the default symmetry properties.

Arguments

Input arguments

# n — filter order

+ positive integer

Details

The filter order is set as a positive integer.

Типы данных

Int64

# f — normalized frequency values

+ vector | the matrix

Details

Normalized frequency values specified as a vector or matrix of dimension on 1. The values must be in the range [0, 1], where 1 corresponds to the Nyquist frequency. The number of elements of a vector is always a multiple 2. The frequencies should be arranged in ascending order.

Типы данных

ComplexF64

# a is the desired amplitude response

+ vector | the matrix

Details

The desired amplitude at the appropriate points f, defined as a vector or matrix of dimension on 1. Dimensions a and f they must match. The dimension must be an even number.

  • On segments with an odd index the interpolation is linear.

  • On segments with an even index the behavior is undefined (transition zones).

Типы данных

ComplexF64

# w — weight coefficients

+ vector | the matrix

Details

The weighting coefficients used to adjust the approximation in each frequency band are set as a vector or matrix of dimension on 1. Vector length w It is half the length f and a therefore, there is exactly one weighting factor value for each band.

Типы данных

ComplexF64

# ftype — filter type

+ "hilbert" | "differentiator" | "multiband" | "lowpass" | "highpass" | "bandpass" | "bandstop" | "invsinc"

Details

Filter type for calculating linear-phase filters with odd symmetry (type III or IV):

  • "hilbert" — the Hilbert filter: ;

    An example of calculating an approximate FIR of a Hilbert transformer. 31:

    h = firpm(30,[0.1; 0.9],[1; 1],"hilbert")
  • "differentiator" — a derivative filter with weights inversely proportional to the frequency;

  • "multiband" — multiband filter;

  • "lowpass" — low-pass filter (low-pass filter);

  • "highpass" — high-pass filter (high-frequency filter);

  • "bandpass" — bandpass filter;

  • "bandstop" — notch (blocking) filter;

  • "invsinc" — inverse sinc filter.

Типы данных

String

# lgrid — frequency grid density

+ 16 (by default) | scalar

Details

The density of the frequency grid, which contains approximately (lgrid*n)/(2*bw) points where bw — this is a fraction of the total frequency range interval [0, 1], covered by f. Increase lgrid This often results in filters that more accurately match filters with a characteristic with a uniform ripple level, but require more time to calculate. Default value 16 — this is the minimum value that should be specified for lgrid.

Типы данных

IntF64

# fresp — frequency response

+ function

Details

It is used to set the desired amplitude as a function, rather than as a set of values.

Function firpm causes fresp as follows:

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

Input arguments

  • n — filter order;

  • f — vector of band boundaries, values from 0 before 1 (where 1 — this is the Nyquist frequency);

  • gf — the frequency grid on which it is necessary to calculate the desired characteristic;

  • w — a vector of weighting coefficients, one for each lane (if not specified, then w = ones(…​)).

Output arguments

  • dh — the desired frequency response calculated on the frequency grid gf;

  • dw — a vector of weighting coefficients corresponding to gf, usually repeating w at the relevant sites.

Function example fresp:

function fresp(
    n::Int,
    f::AbstractArray{<:Real},
    gf::AbstractArray{<:Real},
    w::AbstractArray{<:Real}
)
    dh = Float64.((gf .>= f[1]) .& (gf .<= f[2]))	# The frequency response is equal to 1 in the band [f[1], f[2]]
    dw = ones(length(gf)) .* w[1] 					# The weight is the same across the band
    return dh, dw
end

Output arguments

# b — filter coefficients

+ vector | the matrix

Details

Filter coefficients returned as a vector or matrix of dimension n+1 on 1.

Типы данных

ComplexF64

# err — maximum deviation of the amplitude characteristic from the desired one
real scalar

Details

The maximum deviation of the amplitude characteristic from the desired one, returned as a scalar.

# res — frequency response

+ structure

Details

The frequency response returned as a structure. Structure res it has the following fields:

  • res.fgrid — the frequency grid vector used to optimize the filter design;

  • res.des — the desired frequency response for each point res.fgrid;

  • res.wt — weight coefficients at each point res.fgrid;

  • res.H — the obtained frequency response for each point res.fgrid;

  • res.error — error for each point res.fgrid (res.des-res.H);

  • res.iextr — the vector of extreme frequency indices in res.fgrid;

  • res.fextr — vector of extreme frequencies.

Recommendations

If the filter does not converge, it is possible that it is designed incorrectly. Verify the correctness of the calculation by checking the frequency response.

If the filter does not converge and the result is incorrect, try one or more of the following actions:

  • Increase the filter order.

  • Soften the filter calculation by reducing the attenuation in the delay bands and/or extending the transition regions.

Algorithms

Function firpm designs a FIR filter with a linear phase characteristic using the Parks-McClellan algorithm [2]. The Parks-McClellan algorithm uses the Remez substitution algorithm and Chebyshev approximation theory to design filters with optimal matching between desired and actual frequency characteristics. Filters are optimal in the sense that the maximum error between the desired and actual frequency characteristics is minimized. Filters designed in this way exhibit a frequency response with a uniform ripple level. Function firpm It exhibits discontinuities at the beginning and end of its pulse response due to a single level of pulsation.

These are filters with linear phase characteristics of type I (n odd) and type II (n even). Vectors f and a the frequency-amplitude characteristics of the filter are set:

  • f is a vector of pairs of frequency points set in the range from 0 before 1, where 1 corresponds to the Nyquist frequency. The frequencies should be arranged in ascending order.

  • a — a vector containing the desired amplitude at the points indicated in f.

    The desired amplitude function at frequencies between pairs of points for odd numbers — this is a straight line segment connecting the points and .

    The desired amplitude function at frequencies between pairs of points with an even number not defined. These are transitional or irrelevant areas.

  • f and a they have the same length. This length must be an even number.

Function firpm always uses an even filter order for configurations with even symmetry and non-zero bandwidth at the Nyquist frequency. The reason for the even filter order is that for pulse characteristics with even symmetry and odd orders, the frequency response at the Nyquist frequency is necessarily equal to 0. If an odd value is specified n Then firpm increases it by 1.

Function firpm designs filters with linear phase characteristics of types I, II, III and IV. Types I and II are used by default for even and odd n accordingly, while types III (n even) and IV (n odd) are set using the argument ftype using "hilbert" or "differentiator" accordingly. Different types of filters have different symmetries and certain limitations on their frequency characteristics. (For more information, see [3]).

Filter type with linear phase response Filter order Symmetry of coefficients (Nyquist)

Type I

Even-numbered

Even numbers:

There are no restrictions

There are no restrictions

Type II

Odd

Even numbers:

There are no restrictions

Function firpm increases the filter order by 1 if you are trying to build a type II filter with a non-zero bandwidth at the Nyquist frequency.

Type III

Even-numbered

Odd numbers:

Type IV

Odd

Odd numbers:

There are no restrictions

Literature

  1. Digital Signal Processing Committee of the IEEE Acoustics, Speech, and Signal Processing Society, eds. Selected Papers in Digital Signal Processing. Vol. II. New York: IEEE Press, 1976.

  2. Digital Signal Processing Committee of the IEEE Acoustics, Speech, and Signal Processing Society, eds. Programs for Digital Signal Processing. New York: IEEE Press, 1979, algorithm 5.1.

  3. Oppenheim, Alan V., Ronald W. Schafer, and John R. Buck. Discrete-Time Signal Processing. Upper Saddle River, NJ: Prentice Hall, 1999, p. 486.

  4. Parks, Thomas W., and C. Sidney Burrus. Digital Filter Design. New York: John Wiley & Sons, 1987, p. 83.

  5. Rabiner, Lawrence R., James H. McClellan, and Thomas W. Parks. «FIR Digital Filter Design Techniques Using Weighted Chebyshev Approximation.» Proceedings of the IEEE®. Vol. 63, Number 4, 1975, pp. 595–610.