Overload List
| # | Signature | Description |
|---|---|---|
| 1 | Double DcFilter(Double NewValue, ref TCplx State, Double alpha) | State parameter holds the filter state. |
| 2 | void DcFilter(Double alpha, TVec Num, TVec Den) | Design a DC-blocking (high-pass) IIR filter from alpha. |
| 3 | void DcFilter(Double TransitionBandwidth, Double FS, TVec Num, TVec Den) | Design a DC filter. |
Overload 1: Double DcFilter(Double NewValue, ref TCplx State, Double alpha)
State parameter holds the filter state.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | NewValue | Double | scalar |
| 2 | State | TCplx (ref) | output |
| 3 | alpha | Double | scalar |
Returns: Double
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.
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 x = new Vector(0);
Vector den = new Vector(0);
Vector Response = new Vector(0);
int n;
int i;
TIirState IirState = new TIirState();
TCplx DCState;
SignalUtils.Tone(b,300,5.0/300,0,1,false); //generate sine with 5 periods in 300 samples
// Alternative:
// b.RandGauss;
b = b + 2;
c.Copy(b);
n = 10;
SignalUtils.DcFilter(0.95,num,den);
SignalUtils.IirInit(num,den,ref IirState,false);
int bLength = b.Length;
for (i = 0; i < (bLength/n); i++) //only to test the streaming
{
b.SetSubRange(i*n,n);
c.SetSubRange(i*n,n);
SignalUtils.IirFilter(b,c,ref IirState);
}
b.SetFullRange();
c.SetFullRange();
MtxVecTee.DrawIt(new TVec[2] { b, c }, new string[2] { "Unfiltered", "Filtered" }, "DC IirFilter", false);
SignalUtils.FrequencyResponse(num,den,Response,64,false,TSignalWindowType.wtRectangular,0);
MtxVecTee.DrawIt(Response,"Frequency response",false);
SignalUtils.DcFilter(0.05,2,num,den);
SignalUtils.IirInit(num,den,ref IirState,false);
bLength = b.Length;
for (i = 0; i < (bLength/n); i++) //only to test the streaming
{
b.SetSubRange(i*n,n);
c.SetSubRange(i*n,n);
SignalUtils.IirFilter(b,c,ref IirState);
}
b.SetFullRange();
c.SetFullRange();
MtxVecTee.DrawIt(new TVec[2] { b, c }, new string[2] { "Unfiltered", "Filtered" }, "DC IirFilter", false);
SignalUtils.FrequencyResponse(num,den,Response,64,false,TSignalWindowType.wtRectangular,0);
MtxVecTee.DrawIt(Response,"Frequency response",false);
DCState = Math387.C_ZERO;
for (i = 0; i < (b.Length); i++) //only to test the streaming
{
c.Values[i] = SignalUtils.DcFilter(b.Values[i],ref DCState,0.95);
}
MtxVecTee.DrawIt(new TVec[2] { b, c }, new string[2] { "Unfiltered", "Filtered" }, "DC IirFilter", false);
}
Overload 2: void DcFilter(Double alpha, TVec Num, TVec Den)
Design a DC-blocking (high-pass) IIR filter from alpha.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | alpha | Double | scalar |
| 2 | Num | TVec | source TVec |
| 3 | Den | TVec | source TVec |
Result: stored in self (calling object)
Builds the transfer function of a first-order DC blocker and stores it in num / den for use with IirInit / IirFilter; the filtered signal is centred around zero (its mean is removed). With unity-DC-gain correction g = alpha/big(alpha + 1/2(1-alpha)big) the filter is
H(z) = g (1 - z^(-1))/(1 - alpha z^(-1))
i.e. a differentiator/integrator pair with a zero at z=1 (DC) and a pole at z=alpha. Domain: 0 < alpha < 1 (typically 0.99-0.9999); alpha nearer 1 pushes the cut-off lower and lengthens the transient. A finite alpha in range yields finite coefficients.
Overload 3: void DcFilter(Double TransitionBandwidth, Double FS, TVec Num, TVec Den)
Design a DC filter.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | TransitionBandwidth | Double | scalar |
| 2 | FS | Double | scalar |
| 3 | Num | TVec | source TVec |
| 4 | Den | TVec | source TVec |
Result: stored in self (calling object)
Design a DC filter with TransitionBandwidth and place the transfer function in Num (numerator) and Den (denominator). You can then use this transfer function to initialize an IIR filter with a call to IirInit.
A DC filtered signal will be centered around zero. This DC filter is a simple differentiator/integrator pair. Transition bandwidth is the width of the frequency band where the amplitude is not yet completely attenuated. With DC filters, the transition band starts at 0 Hz. Narrow transition band (TransitionBandwidth/FS ratio is small) will result in filters with longer delays. FS is the sampling frequency. The filter implements the following difference equation:
y[i] = x[i] - x[i-1] + alpha y[i-1]
x.. input signal y.. output signal alpha.. parameter
Alpha paremeter can control the 3dB frequency of the transition bandwidth:
alpha := 1-(TransitionBandwidth/FS)*Pi;
FS.. sampling frequency
TransitionBandwidth.. frequency up to which will the filter have more
then 3dB attenuation. Must be less then FS/2.