DSP Master VCL
|
State parameter holds the filter state.
function DcFilter(NewValue: double; var State: TCplx; alpha: double = 0.99): double; overload;
NewValue is the next sample and alpha is typically between 0.99 and 0.9999 and must be < 1. Big alpha will cause longer filter delay and more ringing. State should be initialized to zero before the routine is called for the first time.
DC filtering.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit; procedure TForm1.Button1Click(Sender: TObject); var b,c,Response,num,den,X: Vector; n,i: integer; IirState: TIirState; DCState: TCplx; begin Tone(b,300,5/300,0,1); //generate sine with 5 periods in 300 samples // Alternative: // b.RandGauss; b := b + 2; c.Copy(b); n := 10; DcFilter(0.95,num,den); IirInit(num,den,IirState); for i := 0 to b.Length div n-1 do //only to test the streaming begin b.SetSubRange(i*n,n); c.SetSubRange(i*n,n); IirFilter(b,c,IirState); end; b.SetFullRange; c.SetFullRange; DrawIt([b,c],['Unfiltered','Filtered'],'DC filter'); FrequencyResponse(num,den,Response,64); DrawIt(Response,'Frequency response'); DcFilter(0.05,2,num,den); IirInit(num,den,IirState); for i := 0 to b.Length div n-1 do // the loop is only to test the streaming begin b.SetSubRange(i*n,n); c.SetSubRange(i*n,n); IirFilter(b,c,IirState); end; b.SetFullRange; c.SetFullRange; DrawIt([b,c],['Unfiltered','Filtered']); FrequencyResponse(num,den,Response,64); DrawIt(Response,'Frequency response'); DCState := C_ZERO; for i := 0 to b.Length -1 do c.Values[i] := DcFilter(b.Values[i],DcState,0.95); DrawIt([b,c],['Unfiltered','Filtered']); end;
#include "MtxExpr.hpp" #include "MtxVecEdit.hpp" #include "MtxVecTee.hpp" #include "SignalUtils.hpp" #include <string.h> void __fastcall TForm1::BitBtn1Click(TObject *Sender) { sVector num,den,b,c,Response; int n,i; TIirState IirState; double State = 0; TCplx DCState; Tone(b,300,5.0/300,0,1); //generate sine with 5 periods in 300 samples // Alternative: // b.RandGauss(); b += 2; //add 2 to vector c = b; //delayed deep copy n = 10; DcFilter(0.95,num,den); //specify alfa memset(&IirState,0,sizeof(IirState)); IirInit(num,den,IirState); for (i = 0; i < (b.Length/n); i++) //streaming test 1 IirFilter(b(i*n,i*n+n-1),c(i*n,i*n+n-1),IirState); DrawIt(OPENARRAY(TVec*,(b,c)),OPENARRAY(AnsiString,("Unfiltered","Filtered"))); FrequencyResponse(num,den,Response,64); DrawIt(Response,"Frequency response"); DcFilter(0.05,2,num,den); //specify transition bandwidth and FS IirInit(num,den,IirState); for (i = 0; i < (b.Length/n); i++) //streaming test 1 IirFilter(b(i*n,i*n+n-1),c(i*n,i*n+n-1),IirState); DrawIt(OPENARRAY(TVec*,(b,c)),OPENARRAY(AnsiString,("Unfiltered","Filtered"))); FrequencyResponse(num,den,Response,64); DrawIt(Response,"Frequency response"); DCState = C_ZERO; for (i = 0; i < b.Length; i++) //streaming test 1 c.Values(i) = DcFilter(b.Values(i),DCState,0.95); DrawIt(OPENARRAY(TVec*,(b,c)),OPENARRAY(AnsiString,("Unfiltered","Filtered"))); }
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
|
What do you think about this topic? Send feedback!
|