## OCTAVE: AN ALTERNATIVE TO THE HIGH COST OF MATLAB

**Introduction**

Octave is a free (both free as in beer and free as in speech) MATLAB compatible numerical programming tool available under the GNU General Public License. MATLAB, an abbreviation for “Matrix Laboratory”, is currently the most widely used commercial, proprietary numerical programming tool. Even a single license for the MATLAB software is a substantial cost. MATLAB is essentially a scripting language similar to Perl, Python, or PHP with a comprehensive, highly integrated set of numerical, statistical, mathematical, and graphical functions including, for example, the Fourier transform, matrix inversion, and histograms. In part because MATLAB has become the de facto industry standard for numerical programming, Octave is of particular interest to individuals, companies, and organizations engaged in numerical and mathematical programming and research and development.

**A Little Bit About MATLAB**

MATLAB was originally developed in the 1970′s as an easy-to-use interactive front-end for recently developed Fortran libraries of matrix algebra and matrix manipulation functions. For this reason, MATLAB was originally designed around two-dimensional matrices of real numbers. In the 1980′s, MATLAB was rewritten in the then popular C programming language and converted into a commercial software product from MathWorks. Over the years, MATLAB has been extended to support multi-dimensional numerical arrays, character strings, many kinds of mathematics beyond matrix and linear algebra, object-oriented programming, and many other features. MATLAB has a number of good features, but it is also a hodge-podge of features added by many different programmers at MathWorks over the last 30 years.

**Octave**

Octave was developed by John W. Eaton and others as a free clone of MATLAB. Octave shares most of the syntax and features of MATLAB. For a wide range of mathematical and numerical programming tasks, Octave is excellent and can do everything MATLAB can do. It is available as both source code and pre-compiled binaries for the Microsoft Windows, Mac OS X, and Linux/Unix platforms. Octave can be easily downloaded from the Octave web site:http://www.gnu.org/software/octave/

Octave and MATLAB have almost identical syntax. There is extensive information on the Web about MATLAB and Octave as well as many books about MATLAB. This article will focus on certain specific issues and features deemed especially important in adopting or using Octave for numerical and mathematically oriented projects. For detailed information on the syntax, readers are referred to the extensive and excellent on-line and published documentation.

**Example 1: Adding Two Vectors in Octave**

octave-3.2.4.exe:10> a = [1 2 3]a = 1 2 3

octave-3.2.4.exe:11> b = [4 5 6]b = 4 5 6

octave-3.2.4.exe:12> a + bans = 5 7 9

**Example 2: A Numerical Range in Octave**

octave-3.2.4.exe:13> 5:10ans = 5 6 7 8 9 10

**Example 3: A Simple Plot**

octave-3.2.4.exe:15> a = 1:100;octave-3.2.4.exe:16> pi4.exe:17> daans = 3.1416 octave-3.2 .ta = sin(2*pi*a/20);lot(data)octave-3.2.4.exe:18> p

Octave includes built-in functions to read and write human readable tab-delimited and comma separated values (CSV) data files. In this day and age, most computer programs ranging from databases to spreadsheets to word processors can easily read and write data files in tab-delimited or comma separated value formats. Almost all computer programming languages can easily read and write tab-delimited data files and, with somewhat more work, comma separated value data files. The Octave built-in functions for this are:

*dlmread*,*dlmwrite*,*csvread*, and*csvwrite*. In real world projects, it is actually rare to use Octave or similar tools fully standalone. One is usually importing data from other programs and exporting data to other programs. With huge disk drives and powerful modern computer networks, it is nearly always possible to use human readable tab-delimited or comma separated value data files to import, export, and share data. This is often the most convenient way to integrate Octave with other tools. In addition, Octave can read and write binary data, for example through C style built-in*fopen*,*fread*,*fwrite*, and*fclose*functions.
Octave’s support for common multimedia and binary file formats in somewhat limited but adequate. Octave can read and write a number of binary image file formats using its

*imread*and*imwrite*functions. The author works a great deal with digital audio, including audio special effects and speech recognition. Octave can read and write the widely used Microsoft WAV (Wave) audio file format using the*wavread*and*wavwrite*functions. Uncompressed wave audio files are quite simple and can be easily read and written at a low level by almost any programming language as well as many common audio processing tools on all of the major computer operating systems: Microsoft Windows goes without saying, both Mac OS and the iPhone iOS support wave files, and many variants of Linux and Unix. The popular free (as in beer, as in speech) Audacity audio processing program can both read and write uncompressed WAV audio files; Audacity is available as both source code and pre-compiled binaries for Windows, Mac, and Linux platforms.
Octave is quite fast. The basic data type of Octave is a “matrix,” which now means a multi-dimensional array of double precision floating point numbers. Today, there is rarely a substantial difference in speed between single and double precision floating point arithmetic on most platforms. The various matrix operations built into Octave are very close in speed to fast compiled code. Because its basic data type is basically a multi-dimensional array of native data types (double precision floating point) Octave (and MATLAB) can generally perform faster on numerical programming tasks than list-based symbolic manipulation programs such as Mathematica. Octave also supports a simple mechanism to add fast compiled C functions to Octave through its

*mkoctfile*function/command:`mkoctfile [-options] file ...`

The mkoctfile function compiles source code written in C, C++, or Fortran. Depending on the options used with mkoctfile, the compiled code can be called within Octave or can be used as a stand-alone application. mkoctfile can be called from the shell prompt or from the Octave prompt.

Because Octave is free and available for download on all major computer platforms, it is now easy to create mathematical or numerical scripts to share with anyone on these platforms (MS Windows, Mac OS X, or Linux/Unix) without the huge cost of the MATLAB program.

**Caveats**

While Octave has very extensive features — few users will need more — Octave does not support all of the advanced and recent features of MATLAB. While Octave is free, it is licensed under the GNU General Public License (GPL), rather than the Berkeley style licenses that allow users to incorporate source code into their proprietary commericial programs. The GPL is sometimes a problem for commercial users of Octave or other free software. Octave error messages can be cryptic.

Both Octave and MATLAB have little support for symbolic manipulation. In many areas such as signal and image processing, this is rarely a concern. The author has used the free Maxima (formerly MACSYMA) program successfully for symbolic manipulation tasks that Octave could not perform.

**Alternatives**

There are many free and other not-so-free numerical, mathematical, and symbolic programming tools including MATLAB, Mathematica, MAPLE, Sage, Python/NumPy, Axiom, Maxima, and many others. Interested readers are referred to many online sources and discussions for a comprehensive list of alternatives. The special advantages of Octave are that it is mature software, has a large user base, is free, is available in both source code and pre-compiled binaries for all three major computer platforms, and is mostly compatible with MATLAB, which has become the de facto industry standard for numerical programming in recent years.

**Conclusion**

For many uses and users, Octave is an excellent alternative to MATLAB or an excellent complement to MATLAB. Some users may find that they will want one licensed copy of MATLAB to make sure their scripts will run under standard MATLAB while using Octave on all of their computers to save costs and to ensure their scripts can be run by anyone, anywhere at no cost using the free Octave tool. Many users will be glad to entirely save the considerable cost of MATLAB, no small amount especially in the current difficult economy.

© 2011 John F. McGowan

**About the Author**

*John F. McGowan, Ph.D.*is a software developer, research scientist, and consultant. He works primarily in the area of complex algorithms that embody advanced mathematical and logical concepts, including speech recognition and video compression technologies. He has extensive experience developing software in C, C++, Visual Basic, Mathematica, MATLAB, and many other programming languages. He is probably best known for his AVI Overview, an Internet FAQ (Frequently Asked Questions) on the Microsoft AVI (Audio Video Interleave) file format. He has worked as a contractor at NASA Ames Research Center involved in the research and development of image and video processing algorithms and technology. He has published articles on the origin and evolution of life, the exploration of Mars (anticipating the discovery of methane on Mars), and cheap access to space. He has a Ph.D. in physics from the University of Illinois at Urbana-Champaign and a B.S. in physics from the California Institute of Technology (Caltech). He can be reached at jmcgowan11@earthlink.net.

## No comments:

## Post a Comment