Use Spectral Math to apply mathematical expressions or IDL procedures to spectra and to selected multi-band images. The spectra can be either from a multi-band image (using a Z Profile), a spectral library, or an ASCII file. You can apply mathematical expressions to all of the bands of multi-band images as long as the number of bands and spectral channels match.

To apply Spectral Math, each spectrum you want to process must be open and displayed in a plot window.

The following figure depicts Spectral Math processing that adds three spectra. Each spectrum in the expression is mapped to an input spectrum and summed, and the resulting spectrum is output to a plot window. You can map one or more of the expression spectra to a file instead of mapping each input to a single spectrum. The resulting output is a new image file. For example, in the expression s1 + s2 + s3, if s1 is mapped to a file and s2 and s3 are mapped to single spectra, then the resulting image file contains the spectra of the s1 file summed with s2 and s3. ## Enter Mathematical Expressions

1. From the Toolbox, select Spectral > Spectral Math. The Spectral Math dialog appears.
2. In the Spectral Math dialog, enter the desired mathematical expression, including variable names, into the Enter an expression field. The dialog accepts any valid IDL mathematical expression, function, or procedure as long as the result can be expressed as a vector (a 1D array) if you are processing spectra, or a 2D array if you are processing images. The expression is assigned to the entire spectrum or image.
3. Variable names must begin with the character “s” or “S” followed by up to five numeric characters. For example, to calculate the average of six spectra, you can enter:

`(s1+s2+s3+s4+s5+s6)/6`

Where s1 is one spectrum, s2 is a second spectrum, and so forth.

The following table lists the functions and operators (shown in parentheses) used in Spectral Math.

 Series and Scalar Math Trigonometric Functions Other Spectral Math Options Addition (+) Sine (sin(x)) Relational Operators (EQ, NE, LE, LT, GE, GT) Subtraction (-) Cosine (cos(x)) Boolean Operators (AND, OR, XOR, NOT) Multiplication (*) Tangent (tan(x)) Type conversion functions (byte, fix, long, float, double, complex) Division (/) ArcSine (asin(x)) IDL functions that return array results Minimum operator (<) ArcCosine (acos(x)) IDL procedures that return array results Maximum operator (>) ArcTangent (atan(x)) Absolute Value (abs(x)) Hyperbolic Sine (sinh(x)) Square Root (sqrt(x)) Hyperbolic Cosine (cosh(x)) Exponent (^) Hyperbolic Tangent (tanh(x)) Natural Exponent (exp(x)) Natural Logarithm (alog(x))

The Spectral Math dialog also contains the following functionality:

• Previous Spectral Math Expressions: This list shows previously applied mathematical expressions. To apply an expression to a new set of spectra, select it from the list and enter it into the Enter an expression field. Click OK.
• Save: Save mathematical expressions to a file. The Save Expressions to a File dialog appears. Enter an output filename with an .exp extension. Click OK. You can save expressions to a file without having to first run them through the Spectral Math function.
• Restore: Restore previously saved mathematical expressions. The Enter Expressions Filename dialog appears. Select a filename and click OK.
• Clear: Clear all expressions from the Previous Spectral Math Expressions list.
• Delete: Delete a single expression from the Previous Spectral Math Expressions list.
• Add to List: To add an individual expression to the Previous Spectral Math Expressions list, enter it in the Enter an expression field and click Add to List.
4. After entering a mathematical expression in the Enter an expression field, click OK. The Variable to Spectra Pairings dialog appears.

## Assign Spectra to Variables

Use the Variable to Spectra Pairings dialog to assign previously displayed input spectra to variable names you entered in the Enter an expression field in the Spectral Math dialog.

Using the example mathematical expression (s1+s2+s3+s4+s5+s6)/6, follow these steps:

1. In the Variables used in expression field, select the variable S1 - [undefined].
2. In the Available Spectra List, select a spectrum. The spectrum is paired with the selected variable.
3. Assign spectra to the other variables using the same steps. To clear the pairings, click Clear.
4. After all variables are paired with spectra, choose an output result.
• To plot the results in the same plot window as the input spectra, toggle Output Result to to select Same Window.
• To plot the results into a new plot window, toggle Output Result to to select New Window.
5. If you use spectra from two plot windows and you select output to the Same Window, ENVI plots the result in the same window as the first assigned spectrum.

6. Click OK. When the processing is complete, the Spectral Math result spectrum is plotted in the chosen plot window. The plot is available for additional processing or saving to an output file using plot functions.

### Map Variables to Input Files

If an image file or image memory item is available, a Map Variable to Input File button appears in the Variables to Spectra Pairings dialog.

1. To select a single-band or multi-band image for use in Spectral Math, select a variable name and click Map Variable to Input File.
2. Select the input file and perform optional spatial and spectral subsetting, and/or masking, then click OK.
3. Select output to File or Memory.
4. Click OK. When you use an input image in Spectral Math, the output image has the same spatial and spectral dimensions.
5. Note: You can also use Map Variable to Input File to perform mathematical operations simultaneously on all bands of an image.