Cython: Fourier Transform

This project demonstrates wrapping C/C++ functions in Cython. It also shows how to pass numpy arrays to C/C++ arrays and vice versa.

There are potential execution speed advantages to this mixed language approach.
The sample source code uses this approach to calculate a Fourier transform from a time history signal.
The scripts on this page require the utility module

The C/C++ source code and its header file are: fourier_ccode.cpp & fourier_ccode.h

The Cython file and its setup script are: fourier_trans.pyx &

The Cython & C/C++ files are compiled in command-line via: python build_ext –inplace

The main Python file is:
This project was developed using the reference material at: Wrapping Cython

Tom Irvine


Cython: Runge-Kutta

The follow set of scripts calculate the response of a specific single-degree-of-freedom system to an arbitrary base input time history, via the Runge-Kutta fourth order method.

The scripts also demonstrate the passing of numpy arrays to the function, which processes the the data and then returns the resulting arrays to the main Python script.

The scripts on this page require the utility module

The function is defined in: sdof_rk4.pyx

The setup file is:

The function is complied in command-line mode via: python build_ext –inplace
The main Python script which imports the function is:

Tom Irvine



Cython is a compiler which compiles Python-like code files to C code. The resulting functions can then be imported into other Python scripts.

This method can be used to increase the execution speed of a Python script, particularly if the script uses for-loops.

The Python-like *.pyx script which provides the function must have have static C data types in order for the main Python script to gain full speed performance.


Cython can be installed on a Ubuntu system using the “Ubuntu Software Center.”

It is also bundled with the Enthought Python Distribution [EPD] and with Sage Notebook.


Using Cython consists of these steps:

1. Write a .pyx source file for the function

2. Write a file using the disutils function

3. Compile the source code in command-line mode using: python build_ext –inplace

4. Import the function into the main Python script and then run it
There are a few alternative compiler methods, but disutils is the most reliable from the author’s experience.


Cython: Runge-Kutta

Cython: Fourier transform

Tom Irvine