Filter data with a sample-and-decay filter.
Filter Data with a non-linear sample-and-decay filter. This filter is similar to Sample-And-Hold, except that the held value slowly decays until a bigger is found. The filter features streaming support via State variable. Data can be real or complex. The decay parameter defines how much will the value decay from sample to sample. The State variable has to be initialized with zeros before it is passed to the routine for the first time.
Example 1
Sample and delay filter example on a sine.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit,OptimalFir;
procedure TForm1.Button1Click(Sender: TObject);
var b,c: Vector;
n,i: integer;
State: TSampleAndHoldState;
begin
Tone(b,300,5.0/300,0,1);
c.Copy(b);
n := 10;
for i := 0 to b.Length div n-1 do
begin
b.SetSubRange(i*n,n);
SampleAndDecayFilter(b,State,0.95);
end;
b.SetFullRange;
DrawIt([c,b],['Unfiltered','Filtered']);
end;
#include "MtxVecCPP.h" //MtxVecCPP.cpp must be included in the project
#include "MtxVecEdit.hpp"
#include "MtxVecTee.hpp"
#include "SignalUtils.hpp"
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Vector num,den,b,c,Response;
int n,i;
TSampleAndHoldState State;
Tone(b,300,5.0/300,0,1); //generate sine with 5 periods in 300 samples
c = b;
n = 10;
for (i = 0; i < (b->Length/n); i++) //Streaming test 1
{
SampleAndDecayFilter(b(i*n,i*n+n-1),State,0.95);
}
DrawIt(OPENARRAY(TVec*,(c,b)),OPENARRAY(AnsiString,("Unfiltered","Filtered")));
}
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);
int n = 10;
int i;
TSampleAndHoldState State = new TSampleAndHoldState();
SignalUtils.Tone(b,300,5.0/300,0,1,false);
c.Copy(b);
int bLength = b.Length;
for (i = 0; i < (bLength/n); i++)
{
b.SetSubRange(i*n,n);
SignalUtils.SampleAndDecayFilter(b,ref State,0.95);
}
b.SetFullRange();
TeeChart.DrawIt(new TVec[2] {c,b}, new string[2] {"Unfiltered","Filtered"},"SampleAndDecay",false);
}