DewDSPMasterNET
FirFilter Routines
Summary
Filter data with a FIR filter.

Unit
SignalUtils

Declaration
Procedure FirFilter(Src, Dst: TVec; var FirState: TFirState);

Description
Filter data in Src and place the result in Dst. FirState must be initialized with a call to FirInit. This version of FirFilter routine can filter streaming blocks of data.
Declaration
Procedure FirFilter(const Src: TSample; out Dst: TSample; var FirState: TFirState);

Description
Filter real Src sample and place the filtered result in Dst sample.
Declaration
Procedure FirFilter(const Src: TCplx; out Dst: TCplx; var FirState: TFirState);

Description
Filter real TCplx sample and place the filtered result in Dst sample.
Declaration
Procedure FirFilter(Data, FirTaps: TVec; UpSample: integer = 1; DownSample: integer = 1);

Description
Filter Data with a FIR filter and place the result back in the Data. This version of FirFilter can not be used to filter streaming data. The routine compensates for group delay and returns filtered data delayed by 0 (odd FIR length) or 0.5 samples (even FIR length).
Categories
FIR filters
 See Also 
FirInit 
FirFilter 

Example 1

Lowpass filter a signal with a FIR filter. Sampling frequency is 2Hz, cutoff frequency is 0.6 Hz. Stopand passband ripple is 0.001.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit,OptimalFir; procedure TForm1.Button1Click(Sender: TObject); var b,c,Response,Response1,H: Vector; State: TFirState; n,i: integer; FS: TSample; begin FS := 2; Tone(b, 300,6/300,0,1); // Alternative: try gaussian noise // b := RandGauss(300); c.Size(b); RemezImpulse(H,[0.5,0.7],0.001,ftLowpass,1,FS); FillChar(State,SizeOf(State),0); FirInit(H,State); try //Alternative 1, FIR streaming n := 10; for i := 0 to (b.Length div n) - 1 do begin b.SetSubRange(i*n,n); c.SetSubRange(i*n,n); FirFilter(b,c,State); end; //Alternative 2 single block filter (does not require TIirState) // c.Copy(b); // FirFilter(c,H); //Alternative 3 single block // FirFilter(b,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 FirFree(State); end; end;
#include "MtxVecCPP.h" //MtxVecCPP.cpp must be included in the project #include "MtxVecEdit.hpp" #include "MtxVecTee.hpp" #include "SignalUtils.hpp" #include "OptimalFir.hpp" #include <string.h> void __fastcall TForm1::BitBtn1Click(TObject *Sender) { Vector b,c, Response1, Response, H; TFirState State; TToneState ToneState; int i,n; double FS = 2; Tone(b,300,6.0/300,0.0,1.0); // Alternative: try gaussian noise // b->RandGauss(); c->Size(b); RemezImpulse(H,OPENARRAY(double,(0.5,0.7)),0.001,ftLowPass,1,FS); memset(&State,0,sizeof(TFirState)); FirInit(H,State,1,0,1,0); try { //Alternative 1, FIR streaming n = 10; for (i = 0; i < (b->Length/n); i++) { FirFilter(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); // FirFilter(c,H); //Alternative 3 single block // FirFilter(b,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 { FirFree(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 h = new Vector(0); Vector Response1 = new Vector(0); Vector Response2 = new Vector(0); TFirState state = new TFirState(); int n; int i; double FS = 2; SignalUtils.Tone(b,300,6.0/300,0,1,false); // Alternative: try gaussian noise // b = MtxExpr.RandGauss(300); c.Size(b); OptimalFir.RemezImpulse(h, new double[2] { 0.5, 0.7 }, 0.001, TFilterType.ftLowPass, 1, FS,false); SignalUtils.FirInit(h,ref state,1,0,1,0); try { //Alternative 1, FIR 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.FirFilter(b,c,ref state); } } finally { c.SetFullRange(); b.SetFullRange(); SignalUtils.FirFree(ref state); } //Alternative 2 single block filter (does not require TFirState) // c.Copy(b); // SignalUtils.FirFilter(c,h,1,1); //Alternative 3 single block // c.Copy(b); // SignalUtils.FirFilter(b,c,state); TeeChart.DrawIt(new TVec[2] {b,c}, new string[2] {"Original signal","Filtered signal"},"Time signals", false); SignalUtils.FrequencyResponse(b, null, Response1, 1, true, TSignalWindowType.wtHanning, 0); SignalUtils.FrequencyResponse(c, 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); }

Copyright 2008 Dew Research
http://www.dewresearch.com