Compute a FIR impulse response filter with user defined Window type.
Compute a FIR impulse response filter with user defined Window type applied and place the result in H. Gain defines the filter gain. WindowParam holds the parameter (if required) for the window function. In case of a Kaiser window WindowParam should define the Beta parameter.
Compute a FIR impulse response filter (no window applied) and place the result in H. The transition regions are defined with the W array. There must be at least one (lowpass, highpass) and at most two (bandpass, bandstop) transition regions (2 or 4 elements). Filter type is defined with TFilterType. The length of the filter H.Length must be preset. Filters of even length (odd order), must have a stop band next to the nyquist (FS/2) frequency. 20*Log10(Ripple) is also the required attenuation of the stop band in decibel. FS is the sampling frequency.
Example 1
Impulse responses of FIR filters.
uses MtxExpr, Math387, MtxVec, SignalUtils, MtxVecTee, MtxVecEdit;
procedure TForm1.Button1Click(Sender: TObject);
var FS: TSample;
n: integer;
H, Response, X: Vector;
begin
FS := 2; //sampling frequency
n := 41; // filter length
//lowpass design with cutoff frequency
FIRImpulse(H.Size(n), [0.5], ftlowpass,FS);
FrequencyResponse(H,nil,Response,8);
X := Ramp(Response.Length, 0, 1/Response.Length);
DrawIt(X,Response,'Lowpass with cutoff at 0.5');
//lowpass design with transition band
FIRImpulse(H.Size(n), [0.3,0.5], ftlowpass,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response,'Lowpass with transition band: 0.3-0.5');
//Highpass design with cutoff frequency
FIRImpulse(H.Size(n), [0.5], ftHighpass,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response,'Highpass with cutoff at 0.5');
//Highpass design with transition band
FIRImpulse(H.Size(n), [0.3,0.5], ftHighpass,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response,'Highpass with transition band: 0.3-0.5');
//Bandpass design with transition bands
FIRImpulse(H.Size(n), [0.3,0.4,0.5,0.6], ftBandpass,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response,'Bandpass with transitoin band: 0.3-0.4, 0.5-0.6');
//Bandstop design with transition bands
FIRImpulse(H.Size(n), [0.3,0.4,0.5,0.6], ftBandstop,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response,'Bandstop with transition bands: 0.3-0.4, 0.5-0.6');
//Hilbert III
FIRImpulse(H.Size(n), [0], ftHilbertIII,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response);
//Hilbert IV
Inc(n); //n must be even
FIRImpulse(H.Size(n), [0], ftHilbertIV,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response);
//Differentiator III
Dec(n); // n must be odd
FIRImpulse(H.Size(n), [0], ftDifferentiatorIII,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response);
//Differentiator IV
Inc(n); //n must be even
FIRImpulse(H.Size(n), [0], ftDifferentiatorIV,FS);
FrequencyResponse(H,nil,Response,8);
DrawIt(X,Response);
end;
#include "MtxVecCPP.h" //MtxVecCPP.cpp must be included in the project
#include "MtxVecEdit.hpp"
#include "MtxVecTee.hpp"
#include "SignalUtils.hpp"
void __fastcall TForm41::BitBtn1Click(TObject *Sender)
{
TSample FS;
int n;
Vector H, Response, X;
FS = 2; //sampling frequency
n = 41; // filter length
//lowpass design with cutoff frequency
FirImpulse(H->Size(n), OPENARRAY(double,(0.5)), ftLowPass,FS);
FrequencyResponse(H,NULL,Response,8);
X = Ramp(Response->Length, 0, 1.0/Response->Length);
DrawIt(X,Response,"Lowpass with cutoff at 0.5",false);
//lowpass design with transition band
FirImpulse(H->Size(n), OPENARRAY(double,(0.3,0.5)), ftLowPass,FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"Lowpass with transition band: 0.3-0.5",false);
//Highpass design with cutoff frequency
FirImpulse(H->Size(n), OPENARRAY(double,(0.5)), ftHighPass,FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"Highpass with cutoff at 0.5",false);
//Highpass design with transition band
FirImpulse(H->Size(n), OPENARRAY(double,(0.3,0.5)), ftHighPass,FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"Highpass with transition band: 0.3-0.5",false);
//Bandpass design with transition bands
FirImpulse(H->Size(n), OPENARRAY(double,(0.3,0.4,0.5,0.6)), ftBandPass,FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"Bandpass with transitoin band: 0.3-0.4, 0.5-0.6",false);
//Bandstop design with transition bands
FirImpulse(H->Size(n), OPENARRAY(double,(0.3,0.4,0.5,0.6)), ftBandStop,FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"Bandstop with transition bands: 0.3-0.4, 0.5-0.6",false);
//Hilbert III
FirImpulse(H->Size(n), OPENARRAY(double,(0)), ftHilbertIII, FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"",false);
//Hilbert IV
n++; //n must be even
FirImpulse(H->Size(n), OPENARRAY(double,(0)), ftHilbertIV, FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"",false);
//Differentiator III
n--; // n must be odd
FirImpulse(H->Size(n), OPENARRAY(double,(0)), ftDifferentiatorIII, FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"",false);
//Differentiator IV
n++; //n must be even
FirImpulse(H->Size(n), OPENARRAY(double,(0)), ftDifferentiatorIV, FS);
FrequencyResponse(H,NULL,Response,8);
DrawIt(X,Response,"",false);
}
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 h = new Vector(0);
Vector response = new Vector(0);
Vector x = new Vector(0);
double FS = 2;
int n = 41;
//lowpass design with cutoff frequency
SignalUtils.FirImpulse(h.Size(n), new double[1] {0.5}, TFilterType.ftLowPass,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
x = MtxExpr.Ramp(response.Length, 0, 1.0/response.Length);
TeeChart.DrawIt(x,response,"Lowpass with cutoff at 0.5",false);
//lowpass design with transition band
SignalUtils.FirImpulse(h.Size(n), new double[2] {0.3,0.5}, TFilterType.ftLowPass,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Lowpass with transition band: 0.3-0.5",false);
//Highpass design with cutoff frequency
SignalUtils.FirImpulse(h.Size(n), new double[1] {0.5}, TFilterType.ftHighPass,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Highpass with cutoff at 0.5",false);
//Highpass design with transition band
SignalUtils.FirImpulse(h.Size(n), new double[2] {0.3,0.5}, TFilterType.ftHighPass,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Highpass with transition band: 0.3-0.5",false);
//Bandpass design with transition bands
SignalUtils.FirImpulse(h.Size(n), new double[4] {0.3,0.4,0.5,0.6}, TFilterType.ftBandPass,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Bandpass with transitoin band: 0.3-0.4, 0.5-0.6",false);
//Bandstop design with transition bands
SignalUtils.FirImpulse(h.Size(n), new double[4] {0.3,0.4,0.5,0.6}, TFilterType.ftBandStop,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Bandstop with transition bands: 0.3-0.4, 0.5-0.6",false);
//Hilbert III
SignalUtils.FirImpulse(h.Size(n), new double[1] {0}, TFilterType.ftHilbertIII,FS);
SignalUtils.FrequencyResponse(h,null,response,8,false,TSignalWindowType.wtRectangular,0);
TeeChart.DrawIt(x,response,"Hilbert III",false);
//Hilbert IV
n++; //n must be even
SignalUtils.FirImpulse(h.Size(n), new double[1] {0}, TFilterType.ftHilbertIV,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response,"Hilbert IV", false);
//Differentiator III
n--; // n must be odd
SignalUtils.FirImpulse(h.Size(n), new double[1] {0}, TFilterType.ftDifferentiatorIII,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response, "Differentiator III",false);
//Differentiator IV
n++; //n must be even
SignalUtils.FirImpulse(h.Size(n), new double[1] {0}, TFilterType.ftDifferentiatorIV,FS);
SignalUtils.FrequencyResponse(h, null, response, 8, false, TSignalWindowType.wtRectangular, 0);
TeeChart.DrawIt(x,response, "Differentiator IV", false);
}