MATLAB is the language of choice by scientist and researchers around the world when developing simulations or processing raw data. The main reason for this choice comes from the huge inversion made by Mathworks at the beginning of the 90s at universities and the lack of opportunities available in that time. Nonetheless, the world has changed in recent years and open source tools on hand nowadays are diverse and promising.
Although MATLAB performance and the tools offered by Mathworks are useful and effective, there are some pitfalls to take into consideration:
- It was not designed for large software projects. Even though the concepts of classes and OOP support are inside the MATLAB engine, the syntax of the language makes it difficult to develop the software itself.
- Since it is proprietary software, you don’t have access to the algorithms and libraries on every version. This is a shame because the MATLAB library is huge and powerful. But, the software is quite expensive and if a new library is added to the latest version you are forced to buy it. In addition, you can’t improve or modify existing algorithms.
- Portability can be a mess from time to time. Despite the fact that you can run a MATLAB compiled code without having the framework installed, you need to install the MCR (MATLAB Compiler) in order to do so. Any mismatch between the binaries and the MCR that you install will prevent you from running a desired piece of code.
Python as an Alternative
We all know the power and simplicity behind Python’s design and its extensive set of libraries. In addition to these benefits, a group of libraries has been evolving in recent years to do calculations and matrix-based operations, and plot the results that were obtained. The following are the most important ones:
- Numpy:It is the fundamental package for scientific computing with Python. Numpy is a numerical mathematics that lets you create N-dimensional arrays that can be used to replace MATLAB matrix operations. Like MATLAB, it is internally optimized to do it so.
- Matplotlib: It is a 2D plotting library, highly coupled with Numpy. It is used to create graphics from data stored in Numpy data structures. From the beginning, the idea behind the library was always to work as a replacement for MATLAB plotting libraries.
- Scipy:It is a scientific library that provides extra functionality to the ones provided by Numpy and Matplotlib. It is also highly coupled with Numpy. MATLAB libraries are full of specific algorithms and routines that cannot be fulfilled by just one library. Scipy provide some of these missing tools.
The Question Is: Is Python enough?
Even though Python works like a charm and its libraries are extremely mature at this point, there are some shortcomings to take into account:
- There is no replacement in the open source world for SIMULINK yet, and there is no project even close to reproducing the functionality available on it. SIMULINK is a block-diagram environment that lets you create simulations of any kind of experiment you can dream of.
- The scientific world is full of MATLAB projects and it would be preferable to reuse or even merge them with Python. Migrating these projects to Python from scratch is not an advisable option.
For the previously mentioned reasons, it is mandatory to find a tool that allows us to execute MATLAB code inside Python if we want to unleash the benefits of this excellent programming language. The solution has been developed by Mathworks itself, and it is called Python Engine.
Since the 2014b version, Mathworks is able to run MATLAB code inside Python thanks to the Python Engine module. This module starts a full MATLAB session, which let us run commands inside Python. Keep in mind that the installation of this module can be a little tricky. First of all, Python Engine requires us to have a dynamically compiled version of Python. Also, you must compile Python with certain flags in order to run the module without problems. If you are interested, please look at the following links for more information: Installation Guide, System Requirements.
from __future__ import division
import numpy as np
# Start matlab
engine = matlab.engine.start_matlab()
# Generate a the step signal u(t – 1) – u(-t + 1)
samples = 1000
x = np.linspace(-10, 10, samples)
y = np.zeros(len(x))
i = 0
for j in range(samples):
if -1 < x[j] < 1:
y[j] = 1
i = i + 1
engine.workspace[‘y’] = y.tolist()
engine.workspace[‘x’] = x.tolist()
engine.eval(“title(‘Step signal u(t + 1) – u(-t + 1)’);”)
engine.eval(“xlabel(‘Sample points [t]’);”)
engine.eval(“title(‘FFT of Step signal u(t + 1) – u(-t + 1)’);”)
In the example below, we created a nonperiodical square function with the help of Numpy and then calculated the FFT inside MATLAB. The result we obtained is the typical sinc function, which is showed in Figure 1. Even though the FFT is an easy-to-find algorithm in any numerical or signal processing library, it was our intention to show how we can use this module in our favor to execute concrete MATLAB functions.
Python Engine is a marvelous module to use if we need to migrate MATLAB code to Python. With the opportunity to execute MATLAB code in a native way inside Python, we can be sure it will work no matter what. Nonetheless, as a drawback we should keep in mind that since the module uses its own structures, Python libraries cannot coexist with structures retrieved from MATLAB. So, it is up to us to make the correct adjustments in order to achieve the expected results.