OptimalFir.remez Method

function remez(h: TVec; bands: TDoubleArray; gains: TDoubleArray; Weights: TDoubleArray; FilterType: TRemezType; out err: Double; FS: Double; ConstantRipple: Boolean): Integer;

Design an optimal equiripple FIR filter with Parks-McClellan algorithm.

#NameDescription
1hAn array of length h.Length on entry and contains the filter impulse response on exit.
2bandsDefines the frequency bands.
3gainsDefines, if the band is a stopband or a passband.
4WeightsArray contains the ratios between required ripples for different bands.
5FSSpecifies the sampling frequency.
6errContains the maximum ripple error upon return.
7FilterTypeChoose between bandpass, hilbert, differentator and integrator.
8ConstantRippleIf True, the ripple will be constant and not weighted to give constant percentage error in case of the following filter types: rmtDifferentiator, rmtIntegrator, rmtDoubleDifferentiator, rmtDoubleIntegrator

Returns: Int32

Remarks:

Designs an equiripple (optimal) FIR filter. The routine will not always converge. Parameters have to be specified, for which the filter exists. Most common causes for trouble are:

  • too wide transition bands
  • transition bands not of equal width.
  • too strong attenuation (of the stopband) or to small ripple (of the passband) specified.
  • wrong filter length (odd/even).

The length of the filter can be estimated with the RemezLength routine. RemezLength routine will also properly adjust the error weights. An extensive explanation of the algorithm can be found in [1] Ch. 7.6, p. 462.

  • The length of the filter is defined as: n = Order-1. (Order is the order of the polynomial and n is the number of coefficients.)
  • highpass or a bandstop filter or any filter with the passband at FS/2 has to have odd length (even order). RemezLength routine automatically adjusts filter length.
  • a filter with negative symmetry also shifts the phase by 90 degrees. The routine automatically assumes negative symmetry, if the FilterType is different from rmtBandpass.
  • Required stopband attenuation is usually specified in dB. To obtain the required ripple, the following formula can be used: Ripple = Exp10(Att/-20);
  • Passband ripple can also be specified in dB. To obtain the required linear ripple, the following formula can be used: Ripple = (1-Exp10(PassRippldB/-20))/2
  • The passband ripple "rp" specified for the passband is a +/-rp specification. Total ripple is 2*rp.
  • the amount of ripple is a function of filter length. (longer filters give less ripple).
  • if different bands have different required ripple, this can be addressed by adjusting the error weights array.

The Fortran source code can be found in [2] p. 198.

References:

[[1] Discrete-time signal processing, Oppenheim and Schafer, Prentice-Hall, 1989.]([1] Discrete-time signal processing, Oppenheim and Schafer, Prentice-Hall, 1989.)

[[2] Theory and application of digital signal processing, Lawrence R. Rabiner and Bernard Gold. Prentice-Hall, 1975.]([2] Theory and application of digital signal processing, Lawrence R. Rabiner and Bernard Gold. Prentice-Hall, 1975.)

Examples
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit, OptimalFIR;

procedure TForm1.Button1Click(Sender: TObject);
var z,Response,Weights: Vector;
n: integer;
err: Double;
begin
    n := RemezLength([0, 1500, 2000, 4000], [1, 0], [0.001, 0.001],Weights, 8000);
    z.Size(n);
    Remez(z,[0, 1500, 2000, 4000], [1, 0], TDoubleArray(Weights), rmtBandpass,err,8000);

    //Design a highpass
    //      n := RemezLength([0, 1500, 2000, 4000], [0, 1], [0.001, 0.001],Weights, 8000);
    //      z.Size(n);
    //      Remez(z,[0, 1500, 2000, 4000], [0, 1], TDoubleArray(Weights), rmtBandpass,err,8000);

    //Design a bandpass (Sampling frequency = 2)
    //      n := RemezLength([0, 0.15, 0.25, 0.45, 0.55, 1], [0, 1, 0], [0.001, 0.001, 0.001],Weights);
    //      z.Size(n);
    //      Remez(z,[0, 0.15, 0.25, 0.45, 0.55, 1], [0, 1, 0], TDoubleArray(Weights), rmtBandpass, err);

    //Design a bandstop (Sampling frequency = 2)
    //      n := RemezLength([0, 0.15, 0.25, 0.45, 0.55, 1], [1, 0, 1], [0.001, 0.001, 0.001],Weights);
    //      z.Size(n);
    //      Remez(z,[0, 0.15, 0.25, 0.45, 0.55, 1], [1, 0, 1], TDoubleArray(Weights), rmtBandpass, err);

    //Design a multiband (Sampling frequency = 2)
    //      n := RemezLength([0.00, 0.10, 0.20, 0.30, 0.40,
    //                        0.45, 0.55, 0.60, 0.70, 1.00], [1, 0, 1, 0, 1], [0.001, 0.001, 0.001, 0.001, 0.001],Weights);
    //      z.Size(n);
    //      Remez(z,[0.00, 0.10, 0.20, 0.30, 0.40,
    //                      0.45, 0.55, 0.60, 0.70, 1.00], [1, 0, 1, 0, 1], TDoubleArray(Weights), rmtBandpass, err);

    //Design a hilbert (type III) transformer  (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.1, 0.9, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if not Odd(n) then Inc(n); //odd length type III filter
    //        z.Size(n);
    //        Remez(z,[0.1, 0.9], [1], [1], rmtHilbert,err);

    //Design a hilbert (type IV) transformer  (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.1, 0.9, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if Odd(n) then Inc(n); //even length type IV filter
    //        z.Size(n);
    //        Remez(z,[0.1, 1], [1], [1], rmtHilbert,err);

    //Design a differentiator (type III) (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.1, 0.9, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if not Odd(n) then Inc(n); //odd length type III filter
    //        z.Size(n);
    //        Remez(z,[0.1, 0.9], [1], [1], rmtDifferentiator,err);

    h//Design a differentiator (type IV) (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.1, 0.9, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if Odd(n) then Inc(n); //even length type IV filter
    //        z.Size(n);
    //        Remez(z,[0.1, 1], [1], [1], rmtDifferentiator,err);

    //Design a double differentiator (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.05, 0.95, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if Odd(n) then Inc(n); //even length type IV filter
    //        z.Size(n);
    //        Remez(z,[0.05, 1], [1], [1], rmtDoubleDifferentiator,err);

    //Design an integrator (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.05, 0.95, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if Odd(n) then Inc(n); //even length type IV filter
    //        z.Size(n);
    //        Remez(z,[0.05, 1], [1], [1], rmtIntegrator,err);

    //Design a double integrator  (Sampling frequency = 2)
    //        n := RemezLength([0, 0, 0.05, 0.95, 1, 1], [0,1,0], [0.01,0.01,0.01],Weights);
    //        if Odd(n) then Inc(n); //even length type IV filter
    //        z.Size(n);
    //        Remez(z,[0.05, 1], [1], [1], rmtDoubleIntegrator,err);

    FrequencyResponse(z,nil,Response,8);
    DrawIt(Response);
end;
See Also: SignalUtils.KaiserImpulse, SignalUtils.SavGolayImpulse, OptimalFir.RemezImpulse, OptimalFir.RemezLength