ASN Filter Designer
DSP ANSI C SDK user’s guide
Overview
This document gives an overview of how to use the ASN Filter Designer’s DSP ANSI C SDK.
The content is as follows:
- Project folder structure generated from the ASN Filter Designer
- Modifying the makefile for your file system
- Filter cascade and non-linear functions
- API description
Project folder structure generated from the ASN Filter Designer
The ASN Filter Designer’s automatic code generator produces the following files and folders in the project folder:
ASN_DSP – This folder contains all the framework dependencies required to compile the filter code. The ASN Filter Designer’s code generation wizard will automatically select the correct datatype, and produce the necessary framework C files.
ASNFDConfig.c – This file contains API and filter structures of the designed filter(s).
ASNFDConfig.h – Associated header design file.
main.c – Contains example code to demonstrate the use of the SDK.
ASNFD.cbp –Code Blocks project file.
makefile – Using this makefile, you can build and execute your filter code directly from the terminal. Steps involved in using the makefile are discussed below.
To use the filter code in your project, include the following folders and files in your project folder ASN_DSP, ASNFDConfig.c, ASNFDConfig.c
Install codeblocks-20.03mingw-setup.exe to quickly try the project.
NB: – the makefile provided will only work under Windows.
Modifying the makefile for your file system
The following steps are required to modify the makefile for your computer:
a. Open the makefile in notepad and update the compiler’s path in the PATH_TO_COMPILER variable in the makefile.
b. To build the project, open the terminal in the project folder type make then, you should see the project’s build process.
c. To see the output of the filter with the example code, type following command in the terminal
“.\bin\Debug\ASNFD.exe”
Filter cascade and non-linear functions
In order to implement a complete application, the ASN Filter Designer uses a combination of filters and non-linear functions, as described below in the architecture diagram. All blocks can be enabled or disabled from within the ASN Filter Designer depending on the user’s requirements.
Depending on the application, we can enable or disable Input and Output math functions. These functions are helpful for pre- or post-processing operations on the signal. The ASN Filter Designer supports the following math functions. You can select one of them according to your application’s needs.
Function () | Math operation | Description |
None | – | Disable the function block. |
Abs | \(\displaystyle |x|=\sqrt{a^2+b^2}\) |
Absolute. |
Ln | \(\displaystyle log_e x\) |
Natural logarithm. |
Angle | \(\displaystyle \tan^{-1}(\frac{b}{a})\) |
Compute the arctangent (phase in radians). |
RMS | \(\displaystyle \frac{\sqrt{a^2+b^2}}{\sqrt{2}}\) |
Root mean square. |
Sqr | \(\displaystyle x^2\) |
Square. |
Sqrt | \(\displaystyle \sqrt{x}\) |
Square root. |
TKEO | \(\displaystyle y(n)=x^2 (n-1)-x(n)(x-2)\) |
TKEO (Teager-Kaiser energy operator) algorithm. |
The H1 (primary) filter(s) are designed via standard prototype methods, such as Butterworth, Chebyshev for IIR filters, and Parks-McClellan for FIR filters using the UI within the tool.
The H2 filter block implements a single section IIR/FIR floating point filter. This filter is available for performing experiments with the P-Z editor or the ASN FilterScript scripting language. The FilterScript language is primarily intended as a sandbox concept, allowing for the design and experimentation of transfer functions with symbolic mathematical expressions. Both H1 and H2 filters may be fully programmed using FilterScript.
Unlike the H1 and H2 filters, the H3 filter is always lowpass and is preceded by an optional mathematical function operation (i.e., Abs, Angle, Ln, RMS, Sqr or Sqrt, and TKEO).
The following four filters are supported:
Type | Transfer function | Gain at DC | Order |
IIR | \(\displaystyle H_3 (z)= \frac{(1+2z^{-1}+z^{-2})}{(1+2αz^{-1}+α^2 z^{-2} )} \) |
\(\displaystyle \frac{(1+2α+α^2)}{4} \) |
2 |
Moving Average | \(\displaystyle H_3 (z)=1+z^{-1}+z^{-2}… +z^{-M}\) |
\(\displaystyle \frac{1}{(M+1)}\) |
1-200 |
Feed through | \(\displaystyle H_3 (z)=1\) |
\(\displaystyle 1\) |
– |
Median | data window | – | 3-195 |
Understanding main.c
ASN Filter Designer’s code generator will automatically generate a C project for CodeBlocks. The C code is ANSI C compliant and is not processor specific, so it can be used agnostically on a variety of hardware platforms. Careful design has been undertaken to ensure that the complexities of the filtering operations are hidden from the developer.
The main.c script is shown below:
As seen, the automatically generated code initialises the filter cascade via the ASNFD object and the initFilter() function. A test sinusoid is then defined (50Hz in this case) and assigned to the InputValues array. Filterdata() is then called on this test data in order to perform the filtering operation. In order to achieve high implementation efficiency on Arm Cortex-M processors, 4 samples are computed in parallel. Therefore, TEST_LENGTH_SAMPLES must be a multiple of 4 samples, where a good default value is 128 or 256.
An optional reset command ResetCascade(&ASNFD) may be called to reset the filter cascade by setting the contents of the delaylines of all filters to zero. NB. Calling this command will not affect the filter coefficients.
API description
A detailed overview of the ASN-DSP library’s API is now given. A complete list can be found ASNFDFilter.h/.c files in the ASN-DSP directory.
The content is as follows:
- Initialise filter structure to default values
- Release or deallocate the memory blocks
- Initialise the filters and non-linear functions
- filter using the filter cascade
- Reset the filter cascade
- Set filter coefficients of H1 filter
- Set filter coefficients of H2 filter
- Set filter coefficients of Heq filter
- Set coefficients of H3 filter
Initialise filter structure to default values
Description
This API Initialises the filter structure to its default values.
Argument
filterptr: pointer to filter structure.
Example
InitASNFDDefinition(&ASNFD);
Release or deallocate the memory blocks
Data type | API prototype |
---|---|
float |
void DeinitASNFDDefinition(ASNFDDefinition_t* filterptr); |
double |
void DeinitASNFDDefinition(ASNFDDefinition_t* filterptr); |
complex float |
void CMPLX_DeinitASNFDDefinition(CMPLX_ASNFDDefinition_t* filterptr); |
complex double |
void CMPLX_DeinitASNFDDefinition(CMPLX_ASNFDDefinition_t* filterptr); |
Description
Release or deallocate the memory blocks utilised by filter structure.
Argument:
filterptr:
Pointer to filter structure.
Example
DeinitASNFDDefinition(&ASNFD);
Initialise the filters and non-linear functions
Data type | API prototype |
---|---|
float | void InitialiseCascade(ASNFDDefinition_t* filterptr); |
double | void InitialiseCascade(ASNFDDefinition_t* filterptr); |
complex float | void CMPLX_InitialiseCascade(CMPLX_ASNFDDefinition_t* filterptr); |
complex double | void CMPLX_InitialiseCascade(CMPLX_ASNFDDefinition_t* filterptr); |
Description
Initialises the filters and non-linear functions according to the design specifications.
Argument:
filterptr: Pointer to filter structure.
Example
InitialiseCascade(&ASNFD);
Filter using the filter cascade
Data type | API prototype |
---|---|
float | void FilterCascadeData(ASNFDDefinition_t* filterptr, float* Output, const float* Input, const uint32_t FrameSize); |
double | void FilterCascadeData(ASNFDDefinition_t* filterptr, double* Output, const double* Input, const uint32_t FrameSize); |
complex float | void CMPLX_FilterCascadeData(ASNFDDefinition_t* filterptr, float* Output, const float* Input, const uint32_t FrameSize); |
complex double | void CMPLX_FilterCascadeData(ASNFDDefinition_t* filterptr, double* Output, const double* Input, const uint32_t FrameSize,); |
Description
Performs the filtering operation using the filter cascade.
Argument:
filterptr: Pointer to filter structure.
FrameSize: Size of input buffer data.
Output: Pointer to an output buffer.
Input: Pointer to the input buffer.
Example
FilterCascadeData(&ASNFD,1024,Output,Input);
Reset the filter cascade
Data type | API prototype |
---|---|
float | void ResetCascade(ASNFDDefinition_t* filterptr); |
double | void ResetCascade(ASNFDDefinition_t* filterptr); |
complex float | void CMPLX_ResetCascade(CMPLX_ASNFDDefinition_t* filterptr); |
complex double | void CMPLX_ResetCascade(CMPLX_ASNFDDefinition_t* filterptr); |
Description
Resets the filter cascade.
Argument
filterptr: Pointer to filter structure.
Example
ResetCascade(&ASNFD);
Set filter coefficients of H1 filter
Data type | API prototype |
---|---|
float | void setH1Numerator(ASNFDDefinition_t* filterptr,float* ptr,int len); |
void setH1Denominator(ASNFDDefinition_t* filterptr,float* ptr,int len); | |
void setH1SOS(ASNFDDefinition_t* filterptr, float* h1sos); | |
double | void setH1Numerator(ASNFDDefinition_t* filterptr, float* ptr, int len); |
void setH1Denominator(ASNFDDefinition_t* filterptr, float* ptr, int len); | |
void setH1SOS(ASNFDDefinition_t* filterptr, float* h1sos); | |
complex float | void CMPLX_setH1Numerator(CMPLX_ASNFDDefinition_t* filterptr, complex float* ptr,int len); |
void CMPLX_setH1Denominator(CMPLX_ASNFDDefinition_t* filterptr, complex float* ptr,int len); | |
void CMPLX_setH1SOS(CMPLX_ASNFDDefinition_t* filterptr,complex float* h1sos); | |
complex double | void CMPLX_setH1Numerator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr, int len); |
void CMPLX_setH1Denominator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr, int len); | |
void CMPLX_setH1SOS(CMPLX_ASNFDDefinition_t* filterptr,complex double* h1sos); |
Description
These APIs are used to set filter coefficients of the H1 filter.
Argument:
filterptr: pointer to filter structure.
ptr, h1sos: Pointer to the array.
len: length of the array.
Example
float H1SOS[] = { 0.15919464484408, 0.15919464484408, 0.00000000000000, 0.68165454497010, 0.00000000000000},{ 0.04362563518631, 0.01333388307825, 0.04362563518631, 1.38973983422963,-0.49030168693325};
setH1SOS(&ASNFD, H1SOS);
float H2Numerator[] = { 0.98453370859690, -0.98453370859690, 0.98453370859690, -0.98453370859690};
setH2Numerator(&ASNFD,(float*) H2Numerator,4);
float H2Denominator[] = {-1.00000000000000, 0.96906741719379, 0.92300230934793, 0.89445146398370};
setH2Denominator(&ASNFD,(float*) H2Denominator,4);;
Set filter coefficients of H2 filter
Data type | API prototype |
---|---|
float | void setH2Numerator(ASNFDDefinition_t* filterptr, float* ptr,int len); |
void setH2Denominator(ASNFDDefinition_t* filterptr, float* ptr,int len); | |
double | void setH2Numerator(ASNFDDefinition_t* filterptr, double* ptr,int len); |
void setH2Denominator(ASNFDDefinition_t* filterptr, double* ptr,int len); | |
complex float | void CMPLX_setH2Numerator(CMPLX_ASNFDDefinition_t* filterptr, complex float* ptr,int len); |
void CMPLX_setH2Denominator(CMPLX_ASNFDDefinition_t* filterptr, complex float* ptr,int len); | |
complex double | void CMPLX_setH2Numerator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr,int len); |
void CMPLX_setH2Denominator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr,int len); |
Description
These APIs are used to set filter coefficients of the H2 filter.
Argument:
filterptr: Pointer to filter structure.
ptr: Pointer to the array.
len: Length of the array.
Example
float H2Numerator [] = {0.98453370859690, -0.98453370859690, 0.98453370859690, -0.98453370859690};
setH2Numerator(&ASNFD,(float*) H2Numerator,4);
float H2Denominator [] = {-1.00000000000000, 0.96906741719379, -0.92300230934793, 0.89445146398370};
setH2Denominator(&ASNFD,(float*) H2Denominator,4);
Set filter coefficients of Heq filter
Data type | API prototype |
---|---|
float | void setHeqSOS(ASNFDDefinition_t* filterptr,float* heq); |
double | void setHeqSOS(ASNFDDefinition_t* filterptr,double* heq); |
complex float | void CMPLX_setHeqSOS(CMPLX_ASNFDDefinition_t* filterptr, complex float* heq); |
complex double | void CMPLX_setHeqSOS(CMPLX_ASNFDDefinition_t* filterptr, complex double* heq); |
Description
These APIs are used to set filter coefficients of the Heq (all-pass equalisation) filter if enabled.
Argument:
filterptr: Pointer to filter structure.
ptr: Pointer to the array.
len: Length of the array.
Example
float HeqSOS[]= {0.15919464484408, 0.15919464484408, 0.00000000000000, 0.68165454497010, 0.00000000000000},{ 0.04362563518631, 0.01333388307825, 0.04362563518631, 1.38973983422963,-0.49030168693325};
setHeqSOS(&ASNFD, HeqSOS);
Set coefficients of H3 filter
Data type | API prototype |
---|---|
float | void setH3Numerator(ASNFDDefinition_t* filterptr, float* ptr, int len); |
void setH3Denominator(ASNFDDefinition_t* filterptr, float* ptr, int len); | |
double | void setH3Numerator(ASNFDDefinition_t* filterptr, double* ptr,int len); |
void setH3Denominator(ASNFDDefinition_t* filterptr, double* ptr, int len); | |
complex float | void CMPLX_setH3Numerator(ASNFDDefinition_t* filterptr, complex float* ptr, int len); |
void CMPLX_setH3Denominator(ASNFDDefinition_t* filterptr, complex float* ptr, int len); | |
complex double | void CMPLX_setH3Numerator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr, int len); |
void CMPLX_setH3Denominator(CMPLX_ASNFDDefinition_t* filterptr, complex double* ptr, int len); |
Description
These APIs are used to set filter coefficients of the H3 filter (if enabled).
Argument:
filterptr: Pointer to filter structure.
ptr: Pointer to the array.
len: Length of the array.
Example
float H3Numerator[] = { 0.98453370859690, -0.98453370859690, 0.98453370859690, -0.98453370859690};
setH3Numerator(&ASNFD,(float*) H3Numerator,4);
float H3Denominator[] = {-1.00000000000000, 0.96906741719379, -0.92300230934793, 0.89445146398370};
setH3Denominator(&ASNFD,(float*) H3Denominator,4);