Filter data with an IIR filter.
Filter data in Src and place the result in Dst. IirState must be initialized with a call to IirInit. Use this routine for filtering of streaming data.
Filter Data and place the result back in Data. Use this routine for filtering of non-consecutive blocks of data. Set LinearPhase to True to double filter attenuation and ensure no phase distortion and no phase delay.
Filters sample Src and places complex output of the filter in Dst.
Filters sample Src and places real valued output of the filter in Dst.
Filter Data and place the result back in Data. The Iir filter is defined with a transfer function. Num is numerator and Den is denominator. Set LinearPhase to True to double filter attenuation and ensure no phase distortion and no phase delay (except for the initial transition regions).
Example 1
Lowpass filter a sine signal with Chebyshev type I filter. Sampling frequency is 2Hz, cutoff frequency is 0.6 Hz. Passband ripple is 0.2 dB and filter order is 6.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit, IIRFilters;
procedure TForm42.Button1Click(Sender: TObject);
var b,c,Response,Response1,num,den: Vector;
State: TIirState;
n,i: integer;
begin
Tone(b,300,6/300,0,1);
// Alternative: try gaussian noise
// b := RandGauss(300);
c.Size(b);
ChebyshevIFilter(6,0.2,[0.6],ftLowpass,false,num,den);
FillChar(State,SizeOf(State),0);
IirInit(num,den,State);
try
//Alternative 1, IIR streaming
n := 10;
for i := 0 to (b.Length div n) - 1 do
begin
b.SetSubRange(i*n,n);
c.SetSubRange(i*n,n);
IirFilter(b,c,State);
end;
//Alternative 2 single block filter (does not require TIirState)
// c.Copy(b);
// IirFilter(c,num,den,True);
//Alternative 3 single block
// c.Copy(b);
// IirFilter(c,State);
c.SetFullRange;
b.SetFullRange;
DrawIt([b,c],['Original signal','Filtered signal']);
FrequencyResponse(b,nil,Response,8,True,wtHanning);
FrequencyResponse(c,nil,Response1,8,True,wtHanning);
DrawIt([Response,Response1],['Orig. signal','Filtered']);
finally
IirFree(State);
end;
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 <string.h>
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Vector b,c, Response1, Response, num, den;
TIirState State;
TToneState ToneState;
int i,n;
b->Size(300);
ToneInit(6.0/300,0.0,1.0,ToneState,false);
Tone(b,ToneState);
// Alternative: try gaussian noise
// b->RandGauss();
c->Size(b);
ChebyshevIFilter(6,0.2,OPENARRAY(double,(0.6)),ftLowPass,false,num,den);
memset(&State,0,sizeof(State));
IirInit(num,den,State);
try
{
//Alternative 1, IIR streaming
n = 10;
for (i = 0; i < (b->Length/n); i++)
{
IirFilter(b(i*n,i*n+n-1),c(i*n,i*n+n-1),State);
}
//Alternative 2 single block filter (does not require TIirState)
// c->Copy(b);
// IirFilter(c,num,den,true);
//Alternative 3 single block
// c->Copy(b);
// IirFilter(c,State);
DrawIt(OPENARRAY(TVec*,(b,c)),OPENARRAY(AnsiString,("Original signal","Filtered Signal")));
FrequencyResponse(b,NULL,Response,8,True,wtHanning);
FrequencyResponse(c,NULL,Response1,8,True,wtHanning);
DrawIt(OPENARRAY(TVec*,(Response,Response1)),OPENARRAY(AnsiString,("Spectrum: original signal","Spectrum: filtered signal")));
}
__finally
{
IirFree(State);
}
}
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 b = new Vector(0);
Vector c = new Vector(0);
Vector num = new Vector(0);
Vector den = new Vector(0);
Vector Response1 = new Vector(0);
Vector Response2 = new Vector(0);
TIirState state = new TIirState();
int n;
int i;
SignalUtils.Tone(b,300,6.0/300,0,1,false);
// Alternative: try gaussian noise
// b = MtxExpr.RandGauss(300);
c.Size(b);
IIRFilters.ChebyshevIFilter(6,0.2,new double[1] {0.6}, TFilterType.ftLowPass,false,num,den,TIirFrequencyTransform.ftStateSpaceAnalog);
SignalUtils.IirInit(num,den,ref state,false);
try
{
//Alternative 1, IIR streaming
n = 10;
int bLength = b.Length; //to prevente reevaluaton inside "for"
for (i = 0; i < (bLength/n); i++)
{
b.SetSubRange(i*n,n);
c.SetSubRange(i*n,n);
SignalUtils.IirFilter(b,c,ref state);
}
}
finally
{
SignalUtils.IirFree(ref state);
}
//Alternative 2 single block filter (does not require TIirState)
// c.Copy(b);
// SignalUtils.IirFilter(c,num,den,true);
//Alternative 3 single block
// c.Copy(b);
// SignalUtils.IirFilter(c,state);
c.SetFullRange();
b.SetFullRange();
TeeChart.DrawIt(new TVec[2] {b,c}, new string[2] {"Original signal","Filtered signal"},"Time signals", false);
SignalUtils.FrequencyResponse(c, null, Response1, 1, true, TSignalWindowType.wtHanning, 0);
SignalUtils.FrequencyResponse(b, null, Response2, 1, true, TSignalWindowType.wtHanning, 0);
TeeChart.DrawIt(new TVec[2] {Response1,Response2}, new string[2] {"Spectrum: original signal","Spectrum: filtered signal"}, "Frequency spectrum", false);
}