ASN FilterScript – Reference guide digital filters
ASN FilterScript scripting language supports over 65 scientific commands for DSP filter design. It provides designers with a familiar and powerful programming language for designing digital filters with the most demanding technical specifications, IIR filters and FIR filters. Convert a Analog filter to a digital filter design with the Laplace transforms
ClearH1; // clear primary filter from cascade
interface fc = {45,55,.1,50}; // interface variable definition
interface radius ={0.5,.999,0.01,0.97};
Main()
Rts={newpz(1,fc),conj(newpz(1,fc)),newpz(1,2*fc),conj(newpz(1,2*fc)),
newpz(1,3*fc),conj(newpz(1,3*fc))};
Num = {poly(Rts)}; // define numerator coefficients
Den = {poly(Rts*radius)}; // define denominator coefficients
Gain = sum(Den)/sum(Num); // define gain
real
y = real(x)
Get the real component of x
round
y = round(x)
Round x
sqr
y = sqr(x)
Square of x
sqrt
y = sqrt(x)
Square root of x
Math operators
Math Operators
Operator
Example syntax
Description
+
a+b
Addition.
-
a-b
Subtraction.
*
A*B
Multiplication.
/
A/B
Division.
.*
A.*B
Element-by-element vector multiplication.
./
A./B
Element-by-element vector division.
.^
a.^N
Element-by-element vector to the power.
^
a^N
Vector to the power.
!
N!
Factorial.
IIR filter design methods
A summary of all classic IIR filter design methods that are supported is given in this section.
ellip
Syntax
Hd = ellip (Order, Frequencies, Rp, Rs, Type, DFormat)
Description
Classic IIR Elliptic filter design.
Equiripple in both the passband and stopband.
Fastest roll-off.
Lowest order filter of all supported prototypes.
Hd = ellip (Order, Frequencies, Rp, Rs, Type, DFormat)
Order: may be specified up to 20 (professional) and up to 10 (educational) edition. Setting the Order to 0, enables the automatic order determination algorithm.
Frequencies: lowpass and highpass filters have one transition band, and in as such require two frequencies (i.e. lower and upper cut-off frequencies of the transition band). For bandpass and bandstop filters, four frequencies are required (i.e. two transition bands). All frequencies must be ascending in order and < Nyquist (see the example below). Rp: Passband ripple in dB.
Rs: Stopband attenuation in dB.
Type: The elliptic method facilitates the design of lowpass, highpass, bandpass and bandstop filters respectively.
Hd: the elliptic method designs an IIR Elliptic filter based on the entered specifications and places the transfer function (i.e. numerator, denominator, gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object.
void
Create a filter object, but do not display output.
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
Main()
Rp=1;
Rs=80;
F={50,120};
Hd=ellip(0,F,Rp,Rs,"lowpass","symbolic");
F={50,80,100,120};
Hd=ellip(0,F,Rp,Rs,"bandpass","symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
butter
Syntax Hd = butter (Order, Frequencies, Rp, Rs, Type, DFormat)
Description
Classic IIR Butterworth filter design
Smooth monotonic response (no ripple)
Slowest roll-off for equivalent order
Highest order of all supported prototypes
Order: may be specified up to 20 (professional) and up to 10 (educational) edition. Setting the Order to 0, enables the automatic order determination algorithm.
Frequencies: lowpass and highpass filters have one transition band, and in as such require two frequencies (i.e. lower and upper cut-off frequencies of the transition band). For bandpass and bandstop filters, four frequencies are required (i.e. two transition bands). All frequencies must be ascending in order and < Nyquist (see the example below). Rp: Passband ripple in dB. This is somewhat of a misnomer, as the Butterworth filter has a maximally flat passband. A good default value is 0.001dB, but increasing this value will affect the position of the filter’s lower cut-off frequency.
Rs: Stopband attenuation in dB. This is somewhat of a misnomer, as the Butterworth filter has a maximally flat stopband, which means that the stopband attenuation (assuming the correct filter order is specified) will be ≥ stopband specification.
Type: The Butterworth method facilitates the design of lowpass, highpass, bandpass and bandstop filters respectively.
Hd: the Butterworth method designs an IIR Butterworth filter based on the entered specifications and places the transfer function (i.e. numerator, denominator, gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
Main()
Rp=0.001;
Rs=80;
F={50,120};
Hd=butter(0,F,Rp,Rs,"lowpass","symbolic");
F={50,80,100,120};
Hd=butter(0,F,Rp,Rs,"bandpass","symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
cheby1
Syntax Hd = cheby1 (Order, Frequencies, Rp, Rs, Type, DFormat)
Description
Classic IIR Chebyshev Type I filter design
Maximally flat stopband
Faster roll off (passband to stopband transition) than Butterworth
Hd = cheby1 (Order, Frequencies, Rp, Rs, Type, DFormat)
Order: may be specified up to 20 (professional) and up to 10 (educational) edition. Setting the Order to 0, enables the automatic order determination algorithm.
Frequencies: lowpass and highpass filters have one transition band, and in as such require two frequencies (i.e. lower and upper cut-off frequencies of the transition band). For bandpass and bandstop filters, four frequencies are required (i.e. two transition bands). All frequencies must be ascending in order and < Nyquist (see the example below). Rp: Passband ripple in dB. This is somewhat of a misnomer, as the Butterworth filter has a maximally flat passband. A good default value is 0.001dB, but increasing this value will affect the position of the filter’s lower cut-off frequency.
Rs: Stopband attenuation in dB. This is somewhat of a misnomer, as the Butterworth filter has a maximally flat stopband, which means that the stopband attenuation (assuming the correct filter order is specified) will be ≥ stopband specification.
Type: The Butterworth method facilitates the design of lowpass, highpass, bandpass and bandstop filters respectively.
Hd: the Butterworth method designs an IIR Butterworth filter based on the entered specifications and places the transfer function (i.e. numerator, denominator, gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart</code>
Main()
Rp=1.4;
Rs=80;
F={50,120};
Hd=cheby1(0,F,Rp,Rs,"lowpass","symbolic");
F={50,80,100,120};
Hd=cheby1(0,F,Rp,Rs,"bandpass","symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
cheby2
Syntax Hd = cheby2 (Order, Frequencies, Rp, Rs, Type, DFormat)
Description
Classic IIR Chebyshev Type II filter design
Maximally flat passband
Slower roll off (passband to stopband transition) than Chebyshev Type I
Hd = cheby2 (Order, Frequencies, Rp, Rs, Type, DFormat)
Order: may be specified up to 20 (professional) and up to 10 (educational) edition. Setting the Order to 0, enables the automatic order determination algorithm.
Frequencies: lowpass and highpass filters have one transition band, and in as such require two frequencies (i.e. lower and upper cut-off frequencies of the transition band). For bandpass and bandstop filters, four frequencies are required (i.e. two transition bands). All frequencies must be ascending in order and < Nyquist (see the example below). Rp: Passband ripple in dB. This is somewhat of a misnomer, as the Chebyshev Type II filter has a maximally flat passband. A good default value is 0.001dB, but increasing this value will affect the position of the filter’s lower cut-off frequency.
Rs: Stopband attenuation in dB.
Type: The Chebyshev Type II method facilitates the design of lowpass, highpass, bandpass and bandstop filters respectively.
Hd: the cheby2 method designs an IIR Chebyshev Type II filter based on the entered specifications and places the transfer function (i.e. numerator, denominator, gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
Main()
Rp=1;
Rs=80;
F={50,120};
Hd=cheby2(0,F,Rp,Rs,"lowpass","symbolic");
F={50,80,100,120};
Hd=cheby2(0,F,Rp,Rs,"bandpass","symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
bessel
Syntax Hd = bessel (Order, Frequencies, Type, DFormat)
Description
IIR Bessel filter design. Analog Bessel filters have a constant group delay in the passband, which is very desirable for a variety of measurement applications. The method implemented within FilterScript uses the Bilinear transform which modifies the standard analog Bessel characteristic, and as a consequence it does not preserve the constant group delay characteristic in the passband. You may equalise the group delay by using an all-pass filter, either in FilterScript or in the main tool with the all-pass filter designer.
Near constant group delay in the passband.
Slower roll-off than other prototypes
Cascading a single all-pass filter (designed with the all-pass filter designer) with the Bessel filter, linearizes the group delay in the passband – see below.
Hd = bessel (Order, Frequencies, Type, DFormat)
Order: may be specified up to 20 (professional) and up to 10 (educational) edition.
Frequencies: lowpass and highpass are specified via one cut-off frequency, whereas bandpass and bandstop filters require two frequencies (i.e. lower and upper cut-off). All frequencies must be ascending in order and < Nyquist (see the example below). Type: The Bessel method facilitates the design of lowpass, highpass, bandpass and bandstop filters respectively.
Hd: the Bessel method designs an IIR Bessel filter based on the entered specifications and places the transfer function (i.e. numerator, denominator, gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
Main()
F={75};
Order=5;
Hd=bessel(Order,F,"lowpass","symbolic");
F={50,100};
Hd=bessel(Order,F,"bandpass","symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
where, \(w_o=\frac{2\pi f_o}{f_s}\) controls the centre frequency, \(f_o\) of the notch, and \( r=1-\frac{\pi BW}{f_s} \)controls the bandwidth (-3dB point) of the notch.
Fo: centre frequency of the notch
BW: Bandwidth (-3dB point) of the notch. Where, \(BW ≤ frac{F_o}{4}\)
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
interface BW={5,40,2,10};
Main()
F=75;
Hd=notch(F,BW,"symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
dcremover
Syntax Hd = dcremover (Fc, DFormat)
Description Implements a first order IIR highpass filter (DC component remover), defined as:
Fc: -3dB cut-off frequency, given by: \(w = tan (\frac{pi F _c}{f s}) \)
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers; // show DM on chart
Main()
F=5;
Hd=dcremover(Fc,"symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
cplxfreqshift
Syntax Hds=cplxfreqshift(Hd,Fo,DFormat)
Description Apply a Complex frequency shift transformation to the digital transfer function object (Hd) centred at frequency point, Fo. Where, Fo is specified in same base unit as the sampling frequency, Fs, i.e. Hz, kHz etc. The function returns a new (frequency shifted) digital transfer object in Hds.
This transform may be used for designing a complex bandpass filter, whereby a real lowpass filter’s frequency response is shifted up or down the spectrum in order to produce a complex bandpass filter. Complex bandpass filters are useful for communication applications and signal property analysis, as they provide a simple way of obtaining the instantaneous frequency, phase and amplitude of sinusoid.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
interface f = {1,200,1,2}; // define cut-off frequency
interface fo = {0,200,10,10}; // define centre frequency of bandpass
interface Rs = {10,100,5,60}; // define stopband attenuation
interface BW = {1,100,1,5}; // define bandwidth
Main()
fc={f,f+BW}; // define a transition band
Rp=0.001; // define passband ripple in dB
Hd=butter(5,fc,Rp,Rs,"lowpass","void"); // 5th order Butterworth lowpass
Hd=cplxfreqshift(Hd,fo,"symbolic"); // shift lowpass filter poles and zeros,
// and make a bandpass
Num=getnum(Hd); // get numerator coefficients
Den=getden(Hd); // get denominator
Gain=getgain(Hd); // get gain
Miscellaneous digital filter functions
FUNCTION
SYNTAX
DESCRIPTION
augment
Hd=augment(Hd1,Hd2, DFormat)
Augment or merge two digital filter objects, and return the merged objects as a single object in Hd
computegain
G=computegain(Hd,Fo)
Get the gain, G of the analog filter (Hd) at frequency point, Fo. Where, Fo is specified in same base unit as the sampling frequency, Fs, i.e. Hz, kHz etc
getnum
Num = getnum(Hd)
Get the numerator coefficients of analog filter object,Hd
getden
Den = getden(Hd)
Get the denominator coefficients of analog filter object, Hd
getgain
G = getgain(Hd)
Get the gain of analog filter object, Hd
FIR filter design methods
A summary of all FIR filter design methods that are supported is given in this section.
movaver
Syntax Hd = movaver(Order,DFormat)
Description Moving average FIR filter design. The moving average (MA) filter is probably one of the most widely used FIR filters due to its conceptual simplicity and ease of implementation. However, despite its simplicity, the moving average filter is optimal for reducing random noise while retaining a sharp step response. Where a simple rule of thumb states that the amount of noise reduction is equal to the square-root of the number of points in the average. For example, an MA of length 9 will result in a factor 3 noise reduction.
Order: may be specified up to 499 (professional) and up to 128 (educational) edition.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Example
ClearH1; // clear primary filter from cascade
Main()
Hd=movaver(8,"symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = {1}; // define denominator coefficients
Gain = getgain(Hd); // define gain
Further Reading
A more detailed disucussion about the moving average filter can be found in the blog.
firwin
Syntax Hd = firwin(Order,Frequencies,Window,Type,DFormat)
Description FIR filter design based on the Window method.
Order: may be specified up to 499 (professional) and up to 128 (educational) edition.
Frequencies: lowpass and highpass are specified via one cut-off frequency, whereas bandpass, bandstop and hilbert filters require two frequencies (i.e. lower and upper cut-off). All frequencies must be ascending in order and < Nyquist (see the example below). Window: The firwin method supports the following window functions: rectangular, blackman, blackmanharris, hamming, hanning, flattop and Chebyshev
Type: The firwin method facilitates the design of lowpass, highpass, bandpass, bandstop and hilbert filters respectively.
Hd: the firwin method designs an FIR window filter based on the entered specifications and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd) and getgain(Hd) will extract the numerator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers;
interface L = {10,400,10,50};
Main()
F={40,90};
Hd=firwin(L,F,"hamming","bandstop","numeric"); // Bandpass with a Hamming window
Hd=firwin(L,F,"hanning","hilbert","numeric"); // Hilbert with a Hanning Window
Num=getnum(Hd);
Den={1};
Gain=getgain(Hd);
firarb
Syntax Hd = firarb(Order,Amplitude,Frequencies,Window,DFormat)
Description Designs an FIR window based filter with an arbitrary magnitude response.
Order: may be specified up to 499 (professional) and up to 128 (educational) edition.
Amplitude: a vector of the magnitude specification in dB.
Frequencies: a vector of the frequency specification. The first element must be 0 and the last element equal to Nyquist – see below.
Window: The firarb method supports the following window functions: rectangular, blackman, blackmanharris, hamming, hanning, flattop and Chebyshev.
Hd: the firarb method designs an FIR filter based on the entered specifications and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd) and getgain(Hd) will extract the numerator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
Running the script shown above, we obtain the following plots (with and without the design markers). Where, it can be seen that the design specifications have been met.
firkaiser
Syntax Hd = firkaiser(Frequencies,Rs,Type,DFormat)
Description Designs an FIR filter based on the Kaiser window method.
The method automatically determines the required filter order (499 (professional) and up to 128 (educational) edition) and returns coefficients in Hd.
Frequencies: lowpass and highpass are specified via one cut-off frequency, whereas bandpass, bandstop and hilbert filters require two frequencies (i.e. lower and upper cut-off). All frequencies must be ascending in order and < Nyquist (see the example below). Type: The firkaiser method facilitates the design of lowpass, highpass, bandpass, bandstop, hilbert3 (Type 3 Hilbert), hilbert4 (Type 4 Hilbert), integrator3, integrator4, differentiator3 and differentiator4 filters respectively.
Hd: the firkaiser method designs an FIR window filter based on the entered specifications and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd) and getgain(Hd) will extract the numerator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
ShowH2DesignMarkers;
interface F = {10,80,2,40}; // frequency spec (2 bands for bandstop)
interface TW ={10,40,2,20}; // Band transition width
Main()
Freq={F,F+TW,100+TW,125+TW}; // frequency specification
Hd=firkaiser(Freq,60,"bandstop","numeric");
Num=getnum(Hd);
Den={1};
Gain=getgain(Hd);
firgauss
Syntax Hd = firgauss(L,Gain,Alpha,DFormat)
Description Designs an FIR Gaussian lowpass filter. Gaussian filters are good pulse shaping filters, and as such are typically used in communication systems, as they have no overshoot and fast transitions. The function returns a Gaussian window of length L with a standard deviation, \(\sigma\)
\( \sigma = \frac {L-1}{2\alpha} \)
As seen, the width of the window (standard deviation) is inversely related to alpha(\(\alpha\)), i.e. a smaller value of \(\alpha\) produces a tighter transition frequency band in the frequency domain and vice versa. A good default value is 2.5.
Hd: the firgauss method designs an FIR Gaussian lowpass filter based on the entered specifications and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd) and getgain(Hd) will extract the numerator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
interface L = {2,100,1,14}; // filter length
interface alpha = {0.5,10,0.1,2.5}; // standard deviation = (L – 1)/(2*alpha)
Main()
Hd=firgauss(L,1,alpha,"symbolic");
Num=getnum(Hd);
Den={1};
Gain=getgain(Hd);
savgolay
Syntax Hd = savgolay(Order, Polyfit, DFormat)
Description Design an FIR Savitzky-Golay lowpass smoothing filter. Savitzky-Golay (polynomial) smoothing filters or least-squares smoothing filters are generalizations of the FIR average filter that can better preserve the high-frequency content of the desired signal, at the expense of not removing as much noise as an FIR average (see movaver for more information). The particular formulation of Savitzky-Golay filters preserves various moment orders better than other smoothing methods, which tend to preserve peak widths and heights better than Savitzky-Golay.
Order: may be specified up to 499 (professional) and up to 128 (educational) edition.
Polyfit: Polynomial fit, which must be < Order+1
Hd: the savgolay method designs an FIR Savitzky-Golay lowpass smoothing filter based on the entered specifications and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd) and getgain(Hd) will extract the numerator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
interface L = {2, 50,2,24};
interface P = {2, 10,1,4};
Main()
Hd=savgolay(L,P,"numeric"); // Design Savitzky-Golay lowpass filter
Num=getnum(Hd);
Den={1};
Gain=getgain(Hd);
Miscellaneous digital filter functions
FUNCTION
SYNTAX
DESCRIPTION
augment
Hd=augment(Hd1,Hd2, DFormat)
Augment or merge two digital filter objects, and return the merged objects as a single object in Hd
computegain
G=computegain(Hd,Fo)
Get the gain, G of the analog filter (Hd) at frequency point, Fo. Where, Fo is specified in same base unit as the sampling frequency, Fs, i.e. Hz, kHz etc
getnum
Num = getnum(Hd)
Get the numerator coefficients of analog filter object,Hd
getgain
G = getgain(Hd)
Get the gain of analog filter object, Hd
Analog -> digital filter design: Laplace transforms
Laplace analog transfer functions may be entered and converted into their digital equivalents via the following commands:
analogtf
Syntax Ha=analogtf(ANum, ADen, AGain, DFormat)
Description Define an analog filter object. Where, the ANum and ADen vectors are the Laplace transfer function coefficients in descending order.
Examples A first order analog lowpass filter may be designed by an RC network:
Choosing R=100k\(Omega\) and C=100nF, gives cut-off at 100rad/s or ≈ 15.9Hz. The analog transfer function may be simply implemented in FilterScript (output shown below) as:
This analog filter object may be now transformed into a digital filter via the Bilinear or Match Z-transform methods. See the example script AnalogRCfilter.afs for more information.
bilinear
Syntax Hd=bilinear(Ha,Fp,DFormat)
Description Convert an analog filter object to its digital equivalent using the Bilinear transform (S→Z transformation).
Pre-warping and scaling: In order to better match the analog transfer function, you may specify a single pre-warping frequency (matching frequency) via the Fp parameter. A further internal gain scaling operation is automatically performed in order to match the analog gain specification.
Hd: the bilinear method designs an IIR filter based on the analog filter object, Ha and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
interface wc={20,200,10,100}; // wc=1/(R*C);
Main()
// define analog RC filter
ANum={0,1};
ADen={1,wc};
AGain=wc;
Ha=analogtf(ANum,ADen,AGain,"symbolic");
Hd=bilinear(Ha,wc/Twopi,"symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
Description Convert an analog filter object to its digital equivalent using the Match Z-transform (S→Z transformation).
Hd: the bilinear method designs an IIR filter based on the analog filter object, Ha and places the transfer function (i.e. numerator and gain) into a digital filter object, Hd. The digital filter object can then be combined with other methods if so required. For a digital filter object, Hd, calling getnum(Hd), getden(Hd) and getgain(Hd) will extract the numerator, denominator and gain coefficients respectively – see below.
DFormat: allows you to specify the display format of resulting digital filter object.
symbolic
Display a symbolic representation of the filter object. If the order > 10, the symbolic display option will be overridden and set to numeric.
numeric
Display a matrix representation of the filter object
void
Create a filter object, but do not display output
Examples
ClearH1; // clear primary filter from cascade
interface wc={20,200,10,100}; // wc=1/(R*C);
Main()
// define analog RC filter
ANum={0,1};
ADen={1,wc};
AGain=wc;
Ha=analogtf(ANum,ADen,AGain,"symbolic");
Hd=mztrans(Ha,"symbolic");
Num = getnum(Hd); // define numerator coefficients
Den = getden(Hd); // define denominator coefficients
Gain = getgain(Hd); // define gain
miscellaneous analog filter design functions
FUNCTION
SYNTAX
DESCRIPTION
augment
Ha=augment(Ha1,Ha2, DFormat)
Augment or merge two analog filter objects, and return the merged objects as a single object in Ha
computegain
G=computegain(Ha,Fo)
Get the gain, G of the analog filter (Ha) at frequency point, Fo. Where, Fo is specified in same base unit as the sampling frequency, Fs, i.e. Hz, kHz etc
getnum
Num = getnum(Ha)
Get the numerator coefficients of analog filter object,Ha
getden
Den = getden(Ha)
Get the denominator coefficients of analog filter object, Ha
getgain
G = getgain(Ha)
Get the gain of analog filter object, Ha
Loading an external coefficient datafile
Loading an external coefficient datafile
You may import a vector of data/coefficients into FilterScript via the importdata function:
h=importdata(Filename)
Where, Filename must be the full pathname and filename entered in quotes, e.g.
h=importdata("c:Tempsg.txt");
All data must be single lined and followed by a comma. Complex data may be imported by using the i or j keyword.
The vector definition must be preceded with a { brace and closed with a };
A maximum of 200 values may be imported.
Comments may be placed anywhere, and must be preceded with the // keyword.
General syntax and data manupilation
General syntax and data manipulation
Function
Description
General
All variables may contain upper and lower case characters, and numbers. e.g. Num1, myGain, alpha15
Interface Variables
The interface keyword must be used to define all interface variables.
Matrices
A generalised matrix is defined as A(rows,columns). Although matrix assignment is not supported, certain vector operations may result in a matrix result, e.g. the vector multiplication: A=a*transpose(a). All data indexes run from 0...N, you may access a matrix element at row R and column M as: y=A(R,M). However, you may also access a range of values using the : keyword, e.g. Y=A(3:5,1:2) which produces a new matrix Y. For modifying a value of a matrix/vector, use the eldef function, e.g. a(2,1)=eldef(5)
Vector assignment
By default, a vector is defined as an array with multiple rows and one column. It may contain expressions, variables and constants and must be enclosed in braces { } with comma delimitation.
Example: b = {1,0,3.4,0,1};
Example: A = {1,-2*cos(TwoPi*fc/fs),1};
Vector manipulation
In order to accommodate transposed vectors, all vectors are defined as a generalised matrix, i.e. A(rows,columns). By default, a vector of length N is defined as A(N,1), whereas a transposed vector is defined as A(1,N). As all data indexes run from 0...N, you may access vector element M as: y=A(M,0). However, you may also access a range of values using the : keyword, e.g. y=A(3:5,0). For modifying a value of a vector, use the eldef function.
Example
a = {1,0,3.4,0,1}; // assign five elements to vector a
a(2,0)=eldef(5); // set element three to 5
y=a(0,0); // get element zero and assign it to y
Data series
A real valued data series can be created with the following syntax:
y = series(min,step,max)
where, step represents the step size between min (minimum) and max (maximum).
Example
a=series(-12,1,1.2);
User comments
All user comments must be preceded with the// keyword. Where, the /* */ syntax is not supported.
System variables and reserved constants
There are several system variables and constants which can be used in every script and expression.
System variables and reserved constants
Variable
Description
fs
The fs variable specifies the system sampling frequency in Hz, i.e. 50MHz is given as 50e6
fsunits
Returns the sampling frequency units, e.g. 500kHz would return 1e3 for kHz
Ts
The Ts variable specifies the system sampling period Ts=1/fs
pi
3.14159265358979
Twopi
6.28318530717959
i
Complex number token, \( sqrt{-1}\)
Mandatory & action keywords
Mandatory keywords
Variable
Description
Main()
Main() is used to seperate the initialisation code from the “main” code – see the getting started guide for more information.
Den
Den specifies the denominator filter coefficients. This must be a vector or scalar.
Num
Num specifies the denominator filter coefficients. This must be a vector or scalar.
Gain
Gain specifies the filter gain. This must be real.
Action keywords
Variable
Description
ClearH1
The ClearH1 keyword will delete the H1 filter from the cascade.
ShowH2DM
The ShowH2DM keyword will show the H2 design marker specification used with IIR and FIR design functions.