OS/2 eZine - http://www.os2ezine.com
Spacer
July 16, 2002
 
Isaac Leung got a degree in Engineering Physics and then Electrical Engineering after which he promptly got a job as a product engineer at a company which makes high speed datacom chips. He is old enough to have cut his computer teeth on Commodore 64's and first played with OS/2 1.3 EE while at a summer job with IBM. The first PC he ever owned came with Windows 95, but he soon slapped on OS/2 Warp 3 and has been Warping ever since. In his spare time, he plots to take over the world.

If you have a comment about the content of this article, please feel free to vent in the OS/2 eZine discussion forums.

There is also a Printer Friendly version of this page.



Spacer
Previous Article
Home
Next Article


Do you have an OS/2 product or service you'd like to advertise?


Octave for OS/2

What's Octave?

Nope, contrary to what the name might suggest, it has almost nothing to do with music. Octave grew out of a project, believe it or not, by a professor in chemical engineering, who named ths program after one of his former professors. Which might perhaps make it even more surprising, because Octave is, to put it simply, a Matlab clone. (Well, maybe not that surprising. Chemical Engineers do use a lot of Matlab.) Matlab is a very expensive commercial package (but very good) which is possibly the standard for dealing with discrete mathematics and data. (There are other good packages, such as IDL or Igor, but Matlab is the most widely used, by far.) Discrete numbers, of course, means almost all real-life applications.


Octave comes with general purpose functions for matrix and vector manipulations. As well, there have been many other libraries created for it for statistics, control theory, signal processing, image and audio processing and much more. Those of you familiar with Matlab will know exactly what I mean, as Matlab comes with very similar (though much more extensive) sets of libraries.

The core portion of Octave is what I called the "engine", in it there are a bunch of simple, built-in functions. Nearly everything else, all the other more complex functions that you might use, are simply script files which execute a series of Octave commands. (Very UNIX-like, where even a simple "ls" - the equivalent to "dir" - is an executable.) For the user, there is no real disadvantage to this setup. There are plenty of advantages, as you can modify or add functions as you wish. In fact, since it is mostly Matlab compatible, you can even move a lot of the Matlab ".m" files over with some modifications. (Octave also uses the ".m" extension.)

Getting and Installing Octave

Octave is a free download, and you can get the pre-compiled OS/2 program and source code from or download the sources directly from the Octave home page. The latest stable version is 2.0.17 however, only 2.0.15 is available for OS/2, though there will likely be only minor bug fixes between the two versions. A development version of Octave is at 2.1.36, the OS/2 version is further behind at 2.1.23, but keep in mind these are not stable releases and can have strange and wonderful effects.

Everything comes in a .ZIP file. Included is an OS/2 command file inst-octave.cmd that will create a desktop folder and program object for you. This is pretty much just a convenience feature, and you can skip running the install script if you wish and just unzip the package into the folder of your choice. (But, be sure to unzip the extra SCRIPTS.ZIP file if you do that.) I recommend you just run the install script.

Octave Program Folder

Unfortunately, this is not quite it. There is a minor little change that most users will want to fix immediately. It is standard in Matlab that it will search in your current directory for any commands that you execute (if it isn't a built in command.) For example, if you have a script called "Generate_Waveform.m", you want to be able to enter the command "Generate_Waveform" on the Octave command line and have that run. However, by default, the current directory is not included in the search path, and the documentation on what to modify is weak. I did manage to figure out how to do it, so I'll save you the trouble of figuring out how.

In your Octave program directory, you need to find the file called "octaverc". This is usually located in \scripts\startup under your Octave directory. In this file, edit the line that starts with LOADPATH = sprintf... and add the current directory into the search path. For example, I have mine modified to read: LOADPATH = sprintf ("%s/scripts//;%s/dlfcn/examples;./", oct_home, oct_home);

No, that's not a typo. oct_home is repeated twice for some reason. If not, it will not be able to search all the script sub-directories. Save the file and re-start Octave. This will make the change take effect and your current working directory will now be searched first for any script files.

That's enough to setup Octave itself, and it's ready to be used. However, it is highly recommended that you also install gnuplot, as Octave does not have any plotting capabilities by itself and relies on gnuplot for this purpose. gnuplot is also freely available (you can download it from here, or see the previous OS/2 eZine article on gnuplot) and installation consists of unzipping it into the directory of your choice and adding that path to your PATH= statement in the CONFIG.SYS file.

Using Octave

Octave's interface is basically a command-line window. This is still the most efficient way for entering this sort of data, and this is how all similar programs still operate (e.g. Matlab, IDL, Igor, etc.)

Octave Window

That's it. Very plain and spartan. If you have never used such a program before, do not under-estimate its power! Matlab users will feel right at home and know what to expect.

If you don't know what to do, try typing "help". This will generate a very lengthy list of available commands. If you see a command that you want further information on, (e.g. polyfit.m), you can explicitly ask for help on that function by typing "help polyfit". Yes, this is primitive, and there is only a very basic search capability ... so yes, this is just like Matlab. (Actually, the very latest version includes a help search facility now, but this is very recent.)

The command prompt can also be customized, but by default, it numbers your commands for you so you can keep track of it, if you wish. As with the OS/2 command prompt, pressing the up and down arrow keys will scroll through your command history. In an improvement, it actually has command-line completion using the TAB key. So, if you had a function or a variable called "frequency", all you have to do is type "freq" and hit the TAB key and it will attempt to complete the command for you. It will work, unless you have a similar command like "freq_range", in which case, it would stop at the point where the commands differed.

A Simple Example

Octave deals with discrete numbers, so before you do anything, you need to generate some numbers. I'll go through a very simple example to give you an idea of how it works.     t = 1:100;

This command will generate a vector called "t" (keep in mind Octave is case sensitive, "t" is not the same as "T") starting from 1 and ending at 100 in steps of 1. So, you'll get a sequence of integers from 1 to 100. The last semi-colon at the end is not strictly necessary, but this instructs Octave to not print out the results of the command. If you had left this out, no harm done, but you'll scroll through a couple of pages listing all 100 elements of "t". If you have a 10,000 element vector, this will be painful. You need to press Ctrl-Break to stop the listing (but Ctrl-C does not work.)

You can also get a bit tricky with this command.     f = 1:(N-1)/100:N/2;

This will generate a sequence of numbers called "f" which starts from 1, ends at N/2 (assuming you have defined a value for "N") in steps of size equal to (N-1)/100.

Now that you have something to work with, you can generate more complicated data.     sig1 = sin(2*pi*0.1*t);

The above command will generate another sequence of numbers called "sig1" which is a sinusoidal function of "t". Note that "pi" is a constant known to Octave, and is what you expect it to be, 3.14159265....., but, be warned, Octave will let you assign just about anything, and if you're not careful, you can make pi = 10; if you wish!

Well, all that number work is very nice, but not worth a lot unless you can see what's going on!     plot(t,sig1);

will generate a plot an x-y line plot of your function "sig1" vs. "t". It is possible to just plot "sig1", this would generate exactly the same plot, except the x-axis is labelled with the point number, and not a time value.

As with nearly all commands, plot will accept more than just the default arguments.     plot(t,sig1,"3+");

This will plot the same function as above, but use colour 3 (which happens to be green) and points which look like "+" instead of a connected line.

If you want to get more tricky, try:     SIG1_real = real(fftshift(fft(sig1)));

This command will return the real component of the Fourier transform of "sig1", which has been shifted so that zero frequency is in the center. (If you have an audio signal, this is how you would extract the frequency spectrum.) There are many other more complicated functions available (e.g. Bessel functions, polynomial fitting, image codecs, audio processing, etc), far too many for me to go into detail.

Plotting and images

As mentioned above, Octave is capable of plotting by sending to gnuplot, so whatever you can do with plots in gnuplot, you can do with Octave. It has also the capability of loading other image files such as .BMP and .PNM. This is done with the command:

    [pic, cmap] = loadimage("/images/flag.bmp","bmp");

This will load up a .BMP file and separate the image and colour map into the two variables "pic" and "cmap", respectively. Note that the full path seems to be required to load the image, even if it is in the current directory. (Also, you'll need to check what format .BMP it is. I believe it is OS/2 format, since the codec is appears to be provided by Klaus Gebhardt, the fellow who did the OS/2 port.) The argument "bmp" tells it which type of image file it is, and it is case-sensitive and requires the quotes enclosing it. But, one major weakness is that it depends on having an X-Window system and xloadimage in order to do this, which probably rules out OS/2 for now (unless you are running XFree86/2 or some other X server.) However, all functions still "work", and you can save the image to several formats. This brings up the possibility of doing batch image processing with Octave. Just write a script to load up the images, do some manipulation on them, and write them back out.

Programming

Octave (like Matlab) is more than just an interactive tool. You can generate a script file to automate your commands simply by editing a text file and adding a ".m" extension to it. (It doesn't have to be ".m", but in that case, you'll have to manually specify to Octave that file name.) And, if you give your script file as an argument to Octave on the command-line, it will automatically run the script and exit. This can be quite handy for batch processing jobs.

If you wanted to make a command for linear regression, you could simply create a file called "regress.m" and every time you typed "regress" in Octave, it would execute that script. Actually, it doesn't have to just be a series of commands, you could make it a function, a custom command that required arguments so that you would have to enter "regress(x,Y);" for example. This is, in fact, how all the functions for Octave are implemented. And this is also why the help system is so... let's just say under-developed.

When you ask for help on a command, for example "help plot", Octave looks in all its known paths for the file "plot.m". It then extracts the comments at the beginning of that file to deliver the help. So, if you create your own custom function and don't add those comments, there will be no help presented!

If for some reason you really need to have a compiled C program instead of running in Octave, you're free to make use of the source code and compile your own. There is a help file detailing the available functions and how to call them. You'll need to grab the source code packages if you want to do this. The default binaries-only package just comes with some image codecs in C++. I would generally recommend against this (unless you are using the routines for some other purpose), as the speed of Octave is going to be fairly competitive with your C program that most users can generate, and you will spend far less time debugging your Octave script than your C program. As one professor put it to me (he used FORTRAN), "I used to spend days programming and debugging, now I do this in hours!". He, as have many others, realized that his job wasn't to program, but to do something else (in his case, astronomy), so it was far better to expend his effort and brain power on his field than programming, which was simply a vehicle to get him where he's going. Programming was not a competitive advantage, but that's a rant for another day.

Matlab Compatibility

Octave seems to actively strive to be as compatible to Matlab as it can. In fact, there is a mailing list to send details about incompatibities. Most of the differences appear to be in the plotting functions, as Matlab has very powerful plotting routines, but Octave has to rely on gnuplot.

If one really needs Matlab compatibility, the included documentation (in traditional OS/2 .INF format) includes a section detailing how to setup Octave for maximum compatibility.

Alternative Packages

Believe it or not, there are actually other similar products for OS/2. One such product is Scilab's XACT product. This is mainly for scientific plotting, but you can do quite a bit of processing of the data as well. However, English user's are going to receive quite a sticker shock, as it goes for $499US for the basic version and $799US for the pro version. If you're a starving student, you still have to come up with $399US. German reading users are luckier, as it is significantly cheaper. Frankly, Scilab's pricing scheme mystifies me for a product that doesn't command market share. As a student, I could by a cheap computer and a copy of Matlab or Igor and still come in under the cost of just XACT.

If, like me, you're on a tight budget, you can also take a look at Euler for OS/2, the latest package which some kind soul has compiled and uploaded as of April 2002. The packages are freely available from hobbes, and you can find out more at Euler's Home Page. Like Octave, Euler is released under GPL, so it is open-source. The author specifically states that Euler is not a Matlab clone, however, the command syntax and environment is very similar. It also includes its own plotting routines, so it might be worth checking out if similarity to Matlab is not a concern.

Current Status of Octave/2

The pre-compiled binaries for OS/2 are pretty much at the latest release, though the development releases seemed to have lagged a little bit. The maintainer of the OS/2 packages and his web site seem to have disappeared. Fortunately, relatively up-to-date source code packages are available from Hobbes, and if anyone is so inclined, it should be easy to incorporate the latest development builds of Octave and generate the latest OS/2 version. (This is one of the great benefits of open source software, it can never be truly abandoned.) Any of you developers with extra time, here's something to work with. And any of you thinking about trying some OS/2 programming, well, here's a package with everything ready to compile.

Conclusion

For a free package, it's pretty hard to beat what you get with Octave. I suspect that (like all software packages) most users only need a few of the major functions, so uses of Matlab will find that the functions they actually use are available on Octave as well. Octave needs a little polish on the user-friendliness end, but then again, so does Matlab.

I hope that this mini-review has given you an idea of what Octave can do, but more generally, what you can do with OS/2!


Where to get Octave

Octave Home Page
Hobbes

Related Links

gnuplot ftp site
gnuplot OS/2 developer site
Euler for OS/2
Euler Home Page

Previous Article
Home
Next Article

Copyright (C) 2002. All Rights Reserved.