Design Butterworth IIR filter.
Design Butterworth filter of Order with CutoffFreq frequencies and of FilterType type. Set Analog to True, to request and analog filter design in s-plane or set it to false to obtain a digital filter design in z-plane. The CutoffFreq must be in range between 0 and 1 (Sampling frequency = 2) in case of a digital filter design. IIrFrequencyTransform specifies when and how will the frequency band transformation be applied. The resulting transfer function is returned in the zero-pole form, with z,p,k variables.
The resulting transfer function is returned in the state-space form with A,B,C,D variables.
Example 1
Design a digital bandstop filter with transition band between 0.2..0.3 and 0.6..0.7 Hz and with at least 50dB attenuation in the stopband. The passband should not have more then 0.2dB ripple. The sampling frequency is 2Hz.
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,Wc: TSample;
WcArray: TSampleArray; //modified 3dB frequency
begin
SetLength(WcArray,2);
Order := ButterOrder([0.2,0.3,0.6,0.7],0.2,50,ftBandstop,WcArray);
ButterFilter(Order,WcArray,ftBandstop,false,num,den);
// Alternative 1. Specify the order and the 3 dB frequencies explicitely:
//
// ButterFilter(5,[0.2,0.7],ftBandstop,false,num,den);
// Alternative 2. Specifying the 3 dB frequencies explicitely
// will result in 3 dB ripple (and not 0.2 as requested) in the passband,
// but one coulde always move the 3 dB frequencies a little:
//
// ButterFilter(5,[0.22,0.68],ftBandstop,false,num,den);
// Alternative 3. Specifying the order explicitely
// will not ensure 50 dB attenuation in the edges of the stopband,
// but one can increase filter order:
//
// ButterFilter(10,[0.22,0.68],ftBandstop,false,num,den);
FrequencyResponse(num,den,Response,64);
DrawIt(Response);
end;
#include "MtxVecCPP.h" //MtxVecCPP.cpp must be included in the project
#include "MtxVecEdit.hpp"
#include "MtxVecTee.hpp"
#include "SignalUtils.hpp"
#include "IirFilters.hpp"
#include "LinearSystems.hpp"
void __fastcall TForm41::BitBtn1Click(TObject *Sender)
{
Vector z,p, num,den, FreqFr, Response, WcArray;
int Order;
double k,Wc,Bw;
WcArray->Size(2);
Order = ButterOrder(OPENARRAY(double,(0.2,0.3,0.6,0.7)),0.2,50,ftBandStop,WcArray->PValues1D(0),WcArray->Length-1);
ButterFilter(Order,WcArray->PValues1D(0),WcArray->Length-1,ftBandStop,false,num,den);
// Alternative 1. Specify the order and the 3 dB frequencies explicitely:
//
// ButterFilter(5,OPENARRAY(double,(0.2,0.7)),ftBandStop,false,num,den);
// Alternative 2. Specifying the 3 dB frequencies explicitely
// will result in 3 dB ripple (and not 0.2 as requested) in the passband,
// but one coulde always move the 3 dB frequencies a little:
//
// ButterFilter(5,OPENARRAY(double,(0.22,0.68)),ftBandStop,false,num,den);
// Alternative 3. Specifying the order explicitely
// will not ensure 50 dB attenuation in the edges of the stopband,
// but one can increase filter order:
//
// ButterFilter(10,OPENARRAY(double,(0.22,0.68)),ftBandStop,false,num,den);
FrequencyResponse(num,den,Response,64);
DrawIt(Response);
}
using Dew.Math;
using Dew.Math.Editors;
using Dew.Math.Units;
using Dew.Signal;
using Dew.Signal.Units;
using Dew.Math.Tee;
using Dew.Signal.Tee;
private void button1_Click(object sender, EventArgs e)
{
Vector z = new Vector(0);
Vector p = new Vector(0);
Vector num = new Vector(0);
Vector den = new Vector(0);
Vector Response = new Vector(0);
Vector FreqFr = new Vector(0);
double[] WcArray = new double[2];
int Order; //design a fifth order filter.
Order = IIRFilters.ButterOrder(new double[4] { 0.2, 0.3, 0.6, 0.7 }, 0.2, 50, TFilterType.ftBandStop, ref WcArray, true); //design analog protype
IIRFilters.ButterFilter(Order, WcArray, TFilterType.ftBandStop, false, num, den, TIirFrequencyTransform.ftStateSpaceAnalog);
// Alternative 1. Specify the order and the 3 dB frequencies explicitely:
//
// IIRFilters.ButterFilter(5, new double[2] {0.2,0.7}, TFilterType.ftBandStop,false,num,den,TIirFrequencyTransform.ftStateSpaceAnalog);
// Alternative 2. Specifying the 3 dB frequencies explicitely
// will result in 3 dB ripple (and not 0.2 as requested) in the passband,
// but one coulde always move the 3 dB frequencies a little:
//
// IIRFilters.ButterFilter(5, new double[2] { 0.22, 0.68 }, TFilterType.ftBandStop, false, num, den, TIirFrequencyTransform.ftStateSpaceAnalog);
// Alternative 3. Specifying the order explicitely
// will not ensure 50 dB attenuation in the edges of the stopband,
// but one can increase filter order:
//
// IIRFilters.ButterFilter(10, new double[2] { 0.22, 0.68 }, TFilterType.ftBandStop, false, num, den, TIirFrequencyTransform.ftStateSpaceAnalog);
SignalUtils.FrequencyResponse(num, den, Response, 32, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(Response, "Frequency response", false);
}
Declaration
Function ButterFilter(Order: integer; const CutoffFreq: array of TSample; FilterType: TFilterType; Analog: boolean; num, den: TVec; IirFrequencyTransform: TIirFrequencyTransform = ftStateSpaceAnalog): TSample;
Description
The resulting transfer function is returned in the numerator/denumerator form with num and den.