function ButterOrder(BEdges: TDoubleArray; PassRipple: Double; StopRipple: Double; FilterType: TFilterType; var CutoffFreq: TDoubleArray; Analog: Boolean): Integer;
Estimate the minimum Butterworth filter order that meets a transition-band specification, and fill CutoffFreq with the natural (3 dB) cutoff(s). BEdges holds the band edges in ascending order (passband edge then stopband edge per band); PassRipple (R_p dB) and StopRipple (R_s dB) bound the passband ripple and stopband attenuation. The returned order is n=\lceil(log_(10)(10^(R_s/10)-1)/(10^(R_p/10)-1))/(2 log_(10)(omega_s/omega_p))\rceil, capped at MaxIirOrder (=50). CutoffFreq length must be half BEdges length and match FilterType (1 value for lp/hp, 2 for bp/bs). Domain: digital edges lie in with sampling frequency 2 (Analog=False); analog edges are in rad/s (Analog=True). NOTE: the natural cutoff placement differs slightly from scipy buttord (same integer order, the 3 dB frequency may differ by under 1 percent).
| # | Name | Type | Description |
|---|---|---|---|
| 1 | BEdges | TDoubleArray | |
| 2 | PassRipple | Double | scalar |
| 3 | StopRipple | Double | scalar |
| 4 | FilterType | TFilterType | |
| 5 | CutoffFreq | TDoubleArray | |
| 6 | Analog | Boolean |
Returns: Int32
Returns the order of a butterworth type IIR filter. Bedg array must contain the band edges of the transition region(s) sorted in ascending order. PassRipple defines the ripple of the passband and StopRipple defines the ripple of the stopband. The length of the CutoffFreq array must be equal to one half of the length of the BEdg array and must match the specified FilterType. The routine returns the estimated order as a result and fill's the CutoffFreq array. This array can then be passed to the ButterFilter routine.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit, IirFilters,
LinearSystems;
procedure TForm1.Button1Click(Sender: TObject);
var z,p, num,den, FreqFr,Response: Vector;
Order: integer;
k,BW: double;
WcArray: array of double; //modified 3dB frequency
begin
SetLength(WcArray,1);
Order := ButterOrder([2,6],0.2,40,ftLowpass,WcArray,True);
ButterAnalog(Order,z,p,k); //design analog protype
LowpassToLowpass(z,p,k,WcArray[0]); //frequency transformation in s-domain
ZeroPoleToTransferFun(num,den,z,p,k);
FreqFr.Length := 1000; //Define the frequency grid (logarithmic)
LogRamp(FreqFr,-1,1); //between 0.1 (=10^(-1)) and 10 (=10^1) rad/sec
FrequencyResponseS(num,den,FreqFr,Response); //Laplace
DrawIt(Response); //Y axis linear, X axis logarithmic;
end;