Examples

The C code generator can be used to create standalone executables that link to the Wolfram runtime library.

Lowpass Filter

This example creates a standalone C executable for a lowpass filter.

Building the Executable

First, you need to create a target directory for the output.

In[1]:=
Click for copyable input
Out[1]=

Now a compiled function that implements a discrete lowpass filter is defined.

In[2]:=
Click for copyable input

This uses the code generator to save the C source file and header file in the target directory.

In[3]:=
Click for copyable input
Out[5]=

To run the code you need to embed it in a main program. The following is a suitable C function that calls the generated code.

In[21]:=
Click for copyable input

Here, the main program is written into the target directory.

In[7]:=
Click for copyable input
Out[7]=

Now you can build the standalone executable as in the following. Notice how it links in the static minimal library: this avoids a need to set up library paths. Since it uses a platform-independent name, this should work on all platforms. Note how the option is set to : this makes sure that MathLink libraries are not included in the link stage.

In[9]:=
Click for copyable input
Out[10]=

This standalone executable can be used by itself, distributed to others, and incorporated into other systems.

Running the Standalone Program

Now you can run the standalone program. In a real application you would not run it from Mathematica as is done here. The following is really for the purpose of demonstration.

First, you need to make an input signal with several frequency components.

In[11]:=
Click for copyable input
Out[14]=

The standalone program reads from its input in the following format.

nelems
elem #1
...
elem #n
dt
RC

Now you can create an input file of the appropriate format from the signal. This is shown below.

In[15]:=
Click for copyable input
Out[16]=

This runs the standalone executable and loads back the result into Mathematica.

In[19]:=
Click for copyable input

The result can be converted into a Mathematica expression and verified. Finally, you can extract the actual numbers.

In[21]:=
Click for copyable input

Here the filtered signal is plotted in red along with the original input signal in blue.

In[23]:=
Click for copyable input
Out[23]=

The standalone program does not have to be run from Mathematica; you can run it any way you wish. But you can test it from Mathematica and illustrate its output conveniently this way.

Finally, you can remove the example directory, if desired.

In[24]:=
Click for copyable input

Standalone Lifting Wavelet Transforms

Examples of using standalone code generation are demonstrated in the documentation for LiftingFilterData. You can find these in "Applications".

New to Mathematica? Find your learning path »
Have a question? Ask support »