index previous next

Signal arithmetic

[Subtraction]   [Division]   [Interpolation]   [SPECTRUM]   [Spreadsheets]   [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 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 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, the assumption is being made that the two signals in Window 1 have the same x-axis values, that is, that both spectra are digitized at the same set of wavelengths.  Strictly speaking this operation 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 take care if the instruments settings are changed or if data from two experiments or two instrument are combined.  (Note: 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).  

Sometimes one needs to know whether two signals have the same shape, for example in comparing the spectrum of an unknown to a stored reference spectrum. Most likely the concentrations of the unknown and reference, and therefore the amplitudes of the spectra, will be different. Therefore a direct overlay or subtraction of the two spectra 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 spectra on the left have the same shape? They certainly do not look the same, but that may simply be due to that fact that one is much weaker that the other. The ratio of the two spectra, 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 wavelength range.

The left part (Window 1) shows two superimposed spectra, one of which is much weaker than the other. But do they have the same shape? The ratio of the two spectra, 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 the same, within about +/-2 %, over this wavelength range, and that top curve is about 10 times more intense than the bottom one. Above 440 nm the ratio is not even approximately constant; this is caused by noise, which is the subject of the next section.


SPECTRUM, the freeware signal-processing application for Macintosh OS8, includes the following signal arithmetic functions: addition and multiplication with constant; addition, subtraction, multiplication, and division of two signals, normalization, and a large number of other basic functions (common and natural log and antilog, reciprocal, square root, absolute value, standard deviation, etc.) in the Math menu.

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 x-y or scatter graph) can be created by menu selection. See http://www.youtube.com/watch?v=nTlkkbQWpVk for a nice video demonstration. Both Excel and Calc offer a form design capability with full set of user interface objects such as buttons, menus, sliders, and text boxes; these can be user to create attractive graphical user interfaces for end-user applications, such as in http://terpconnect.umd.edu/~toh/models/. The latest versions of both Excel (Excel 2013) and OpenOffice Calc (3.4.1) can open and save either spreadsheet file format (.xls and .ods, respectively). Simple spreadsheets in either format are compatible with the other program. However, there are small differences in the way that certain functions are interpreted, and for that reason I supply most of my spreadsheets in .xls (for Excel) and in .ods (for Calc) formats.  See "Differences between the OpenDocument Spreadsheet (.ods) format and the Excel (.xlsx) format". Basically, Calc and do everything Excel can do, but Calc is free to download and is more Windows-standard in terms of look-and-feel.  Excel is more "Microsoft-y".
Matlab is a commercial numerical computing environment and programming language in which a single variable can represent either a single "scalar" value, a vector of values (such as a spectrum or a chromatogram), or a matrix (a rectangular array of values, such as a set of spectra). This greatly facilitates mathematical operations on signals. For example, if you have a spectrum in the variable a, you can plot it just by typing "plot(a)". And if you also have a vector w of the same length containing x-axis values (such as wavelengths), you can plot a vs w by typing "plot(w,a)".  Two signals a and b can be plotted on the same axis for comparison by typing "plot(w,a,w,b)". Matlab automatically assigns different colors to each line, but you can control the color and line style yourself by adding additional symbols; for example "plot(w,a,'r.'w,b,'b-')" will plot a with red dots and b with a blue line. Type "help plot" for more options.

The subtraction of two spectra a and b, as in Figure 1, can be performed simply by writing a-b. To plot the difference, you would write "plot(a-b)". Likewise, to plot the ratio of two spectra, as in Figure 2, you would write "plot(a./b)". So, "./" means divide point-by-point and ".*" means multiply point-by-point. 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 Matlab doesn't require you to deal with vectors and matrices as collections of numbers; it knows when you are dealing with matrices and adjusts your calculations accordingly. See http://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html.

You can easily import your own data into Matlab by using the load command or by using the convenient Matlab Import Wizard by clicking File > Import Data.  Data can be imported from text files and from spreadsheets.

GNU Octave is a free alternative to Matlab that is "mostly compatible". In fact, almost all of the Matlab functions, scripts, demos, and examples in this document will work in the latest version of Octave without change. (The exceptions are the keystroke-operated interactive iPeak, iSignal, and ipf.m). There is a FAQ that may help in porting Matlab programs to Octave. See Key Differences Between Octave & Matlab. There are Windows, Mac, and Unix versions of Octave; the Windows version can be downloaded from Octave Forge; be sure to install all the "packages". There is lots of help online: Google "GNU Octave" or see the YouTube videos for help. The current version of Octave as of December 2013 (Octave 3.8.0) does not use a graphical user interface by default as Matlab does, which makes operation less convenient. GNU has promised that the upcoming version 4 will have a GUI. Installation of Octave is somewhat more laborious than installing a commercial package like Matlab. More seriously, Octave is also slower that Matlab computationally - about 2 to 20 times slower, depending on the task (for some specific example, see TimeTrial.txt). I am working to make all my Matlab scripts and functions compatible with Octave. Bottom line: Matlab is better, but if you can't afford Matlab, Octave provides perhaps 95% of the functionality for 0% of the cost.

Spreadsheet or Matlab/Octave? For signal processing, Matlab/Octave is faster and more powerful than using a spreadsheet, but spreadsheets are easier for novices and they offer flexible presentation and user interface design. Spreadsheets are concrete and more low-level, showing every single value explicitly in a cell. Matlab/Octave is more high level and abstract, because a single variable, punctuation, or function can do so much. Also, user-defined functions can call other built-in or user-defined functions, which in turn can call other functions, and so on, allowing very complex high-level functions to be built up in layers. The bottom line is that spreadsheets are easier at first, but sooner or later the Matlab/Octave approach is more productive. This point is demonstrated the comparison of both approaches to multilinear regression in multicomponent spectroscopy (RegressionDemo.xls vs the Matlab/Octave CLS.m), and particularly by the dramatic difference between the spreadsheet and Matlab/Octave approaches to finding and measuring peaks in signals (i.e. a 250Kbyte spreadsheet vs a 7Kbyte script).

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 inspected and modified by the user to customize the routines for specific needs. Simple changes are easy to make with only modest knowledge of programming. For example, you could easily change the titles or colors or line style of graphs (in Matlab or Octave programs, search for "title(" or "plot(", respectively). My code contains comments that indicate places where specific changes can easily be made: just search for the word "change". 


index previous next


This page is maintained by Prof. Tom O'Haver , Department of Chemistry and Biochemistry, The University of Maryland at College Park. Comments, suggestions and questions should be directed to Prof. O'Haver at toh@umd.edu. Updated July 2014.
Unique visits since May 17, 2008: