[Subtraction] [Division] [Interpolation] [SPECTRUM for Mac OS8] [Spreadsheets] [Matlab] [Octave]
[Getting data into Matlab/Octave]

The most basic signal processing functions are those that involve simple signal arithmetic: point-by-point addition, subtraction, multiplication, or division of two signals or of one signal and a constant. Despite their mathematical simplicity, these functions can be very useful. For example, in the left part of Figure 1 (Window 1) the top curve is the optical absorption spectrum of an extract of a sample of oil shale, a kind of rock that is is a source of petroleum.

*Figure 1. A simple point-by-point subtraction of two
signals allows the background (bottom curve on the left) to be
subtracted from a complex sample (top curve on the left),
resulting in a clearer picture of what is really in the sample
(right). (X-axis = wavelength in nm; Y-axis = absorbance).*

This optical spectrum exhibits
two absorption bands, at about 515 nm and 550 nm, that are due
to a class of molecular fossils of chlorophyll called *porphyrins.*
(Porphyrins are used as geomarkers in oil exploration). These
bands are superimposed on a background absorption caused by the
extracting solvents and by non-porphyrin compounds extracted
from the shale. The bottom curve is the spectrum of an extract
of a non-porphyrin-bearing shale, showing only the background
absorption. To obtain the spectrum of the shale extract without
the background, the background (bottom curve) is simply
subtracted from the sample spectrum (top curve). The difference
is shown in the right in Window 2 (note the change in Y-axis
scale). In this case the removal of the background is not
perfect, because the background spectrum is measured on a
separate shale sample. However, it works well enough that the
two bands are now seen more clearly and it is easier to measure
precisely their absorbances and wavelengths. (Thanks to Prof.
David Freeman for the spectra of oil shale extracts).

In this example and the one
below, I am making the assumption that the two signals in Window
1 have the * same x-axis values* - in other words, that
both spectra are digitized at the same set of wavelengths.
Subtracting or dividing two spectra would not be valid if two
spectra were digitized over different wavelength ranges or with
different intervals between adjacent points. The x-axis values
must match up point for point. In practice, this is very often
the case with data sets acquired within one experiment on one
instrument, but the experimenter must be careful if the
instruments settings are changed or if data from two experiments
or two instrument are combined. It is possible to use the
mathematical technique of interpolation
to change the number of points or the x-axis intervals of
signals; the results are only approximate but often close enough
in practice. (Interpolation is one of the functions of my *iSignal
*function described later).

Sometimes one needs to know whether two signals have the same shape, for example in comparing the signal of an unknown to a stored reference signal. Most likely the amplitudes of the two signals, will be different. Therefore a direct overlay or subtraction of the two signals will not be useful. One possibility is to compute the point-by-point ratio of the two signals; if they have the same shape, the ratio will be a constant. For example, examine Figure 2.

*Figure 2. Do the two **signals on the left have
the same shape? They certainly do not look the same, but that
may simply be due to the fact that one is much weaker than the
other. The ratio of the two **signals, shown in the
right part (Window 2), is relatively constant from 300 to 440
nm, with a value of 10 +/- 0.2. This means that the shape of
these two signals is very nearly identical over this x-axis
range.*

The left part (Window 1) shows two superimposed signals,
one of which is much weaker than the other. But do they have the
same shape? The ratio of the two signals, shown in the right
part (Window 2), is relatively constant from x=300 to 440, with
a value of 10 +/- 0.2. This means that the shape of these two
signals is the same, within about +/-2 %, over this x-axis
range, and that top curve is very nearly 10 times more intense
than the bottom one. Above x=440 the ratio is not even
approximately constant; this is caused by *noise*, which
is the subject of the next
section.

A **division by zero error** will be caused by *even
a single zero* in the denominator vector, but that can
usually be avoided by applying a small amount of
smoothing of the denominator, by
adding a small positive number to the vector, or by using the
custom Matlab/Octave function rmz.m (**r**e**m**ove
**z**eros), which replaces zeros with the nearest non-zero
numbers, or the nlt.m (**n**o **l**ower
**t**han) function that limits the lowest number in a vector
to a specified value.

Popular spreadsheets, such as Excel or Open Office Calc, have built-in functions for all common math operations, named variables, x,y plotting, text formatting, matrix math, etc. Cells can contain numerical values, text, mathematical expression, or references to other cells. A vector of values such as a spectrum can be represented as a row or column of cells; a rectangular array of values such as a set of spectra can be represented as a rectangular block of cells. User-created names can be assigned to individual cells or to ranges of cells, then referred to in mathematical expression by name. Mathematical expressions can be easily copied across a range of cells, with the cell references changing or not as desired. Plots of various types (including the all-important

If you are working on a tablet or smartphone, you could use the Excel mobile app, Numbers for iPad, or several other mobile spreadsheets. These can do basic tasks but do not have the fancier capabilities of the desktop computer versions. By saving their data in the "cloud" (e.g. iCloud or SkyDrive), these apps automatically sync changes in both directions between mobile devices and desktop computers, making them useful for field data entry.

To create

For

The function

A Matlab variable can also be a

The subtraction of two signals

creates a matrix

Yes, that's right, it makes that much difference, at least in this simple example. It will help if you pre-allocate memory space for the

In Matlab/Octave, "/" is not the same as "\". Typing "b\a" will compute the "matrix right divide", in effect the weighted average ratio of the amplitudes of the two vectors (a type of least-squares best-fit solution), which in the example in Figure 2 will be a number close to 10. The point here is that

Probably the most common errors you'll make in Matlab/Octave are punctuation errors, such as mixing up periods, commas, colons, and semicolons, or parentheses, square brackets, and curly brackets; type "help punct" at the Matlab prompt and

Matlab Compiler lets you share programs as standalone applications, and Matlab Compiler SDK lets you build C/C++ shared libraries, Microsoft .NET assemblies, Java classes, and Python packages from Matlab programs.

Getting data into Matlab/Octave.

It is also possible to import data from graphical line plots or

Matlab R2013a or newer can even read the sensors on your iPhone or Android phone via Wi-Fi. To read the outputs of older analog instruments, you need an analog-to-digital converter, an Arduino microcontroller board, or a USB voltmeter.

Octave Version 5.1.0 has been released and is now available for download. Installation of Octave is somewhat more laborious than installing a commercial package like Matlab. More seriously, Octave is also slower than Matlab computationally - about 2 to 20 times slower, depending on the task (for some specific examples, see TimeTrial.txt). Also, at least in the earlier versions, the error handling is less graceful and it's less stable and tends to crash more often (in my experience). I am working to make all my Matlab scripts and functions compatible with Octave; make sure you have the latest versions of my functions (a number of functions were made more Octave-compatible in March 2015). Bottom line: Matlab is better, but if you can't afford Matlab, Octave provides almost all of the functionality for 0% of the cost.

Using the analogy of electronic circuits, spreadsheets are like discrete component electronics, where every resistor, capacitor, and transistor is a discrete, macroscopic entity that you can see and manipulate directly. A function-based programming language like Matlab/Octave is more like micro-electronics, where the functions (the "m-files" that begin with "function...") are the integrated circuit "chips", which condense complex operations into one package with documented input and output pins (the function's input and output arguments) that you can connect to other functions, but which hide the internal details (unless you care to look at the code, which you always can do). A good example is the ubiquitous "555 timer", an 8-pin timer, pulse generator, and oscillator chip introduced in 1972, which has since become the most popular integrated circuit ever manufactured. These days, almost all electronics is done with chips of one type or another, because it's easier to understand the relatively small number of inputs and outputs of a chip than to deal with the greater number of internal components.

Much of Matlab/Octave is actually written in the Matlab/Octave language, using more basic functions to build more complex ones. You can write new functions of your own that essentially extend the language in whatever direction you need. Just remember to include your custom functions whenever you share you code with others.

Both spreadsheets and Matlab/Octave programs have a huge advantage over commercial end-user programs and compiled freeware programs such as SPECTRUM; they can be

Hint: This site contains many links to code and data examples, Matlab/Octave scripts, enlarged graphics, screen images, and spreadsheet templates; to view these alongside the text, right-click and select "Open link in new window".

This page is part of "