# Hyperspectral Python: HypPy
## 2 Design and Implementation
### Tools
#### 2.4.2 Spectrum Class and Spectral Math  
Spectral math is used to apply mathematical expressions to spectra of images or spectral libraries. Band math uses the band paradigm; spectral math uses the spectrum paradigm. In spectral math, the image is processed pixel by pixel. In band math, the image is processed band by band. Spectral math relies on the functionality as it is implemented in the spectrum class. The interface of the spectral math tool is similar to the band math tool (see Figure 6), but the expression must follow the syntax of the spectrum class. The spectrum class was created for the manipulation of spectra. The idea is that a spectrum object contains information about wavelengths as well as values, be it reflectance, radiance, or other data. The spectrum class offers access to the data, as well as functions to operate on the spectral data. The list below summarizes some of the properties of the spectrum container object:  
* The spectrum object contains wavelengths and values. * The spectrum object may contain a name and a description. Figure 6: Interface of the band math tool. The formula shows an example of a band ratio for a simple monazite index based on the shoulder and the absorption feature of Nd at 840 nm and 800 nm, respectively. In this case, there is only one input file \(i1\). * An index can be used to select values or make subsets of the spectrum. * An integer as index denotes a band number; a float denotes a wavelength. * Mathematical operators like +, -,
* and/can be used on spectra. * The spectrum object contains a wealth of operators and methods. Currently, some 150 functions and 20 operators are implemented (see HypPy's spectral math manual). * When working with spectra with different wavelengths, the spectra are automatically resampled on-the-fly. As shown previously, spectral indices can be implemented using band math. A spectral index is supposed to have a high value if a certain mineral or material is present and a low value if it is absent. An example of such an index was given in the previous section in which we presented the example of the NDVI. However, if the image contains noise, then the resulting NDVI may also contain noise. In such a case, it may be beneficial to work with average values in band ranges rather than single bands. In band math, it is not possible to use band ranges because bands of a range must be addressed one by one. However, in spectral math, a spectral range can be obtained using a slice on the spectrum. The spectral math expression for NDVI may now look like the following expression (3):  
\[(S1[700.0:1100.0].mean()-S1[400.0:700.0].mean())/\] \[(S1[700.0:1100.0].mean()+S1[400.0:700.0].mean()), \tag{3}\]  
in which \(S1\) denotes the spectra from the first image. Spectra of subsequent input images are numbered \(S2\), \(S3\), and so on. However, in this example, only one image is used. The square brackets contain what in Python are called slices, denoted by the use of a colon '\(\therefore\)'. Notice that floats are used as indices, which denote wavelengths. Integers as indices are interpreted as band numbers. The result of a slice on a spectrum is a new spectrum object that contains a subset of the original spectrum object. In this case, the subset will contain the spectral values roughly between 400 nm and 700 nm and between 700 nm and 1100 nm. The wavelengths are approximate because these wavelengths are matched to the closest band numbers, which may not have the exact same wavelengths as central wavelengths. HypPy does not generate errors or warnings for the conversion of wavelengths to bands to prioritize speed over quality checks, as the checks would consume significant CPU time. However, users and programmers can verify the requested and obtained wavelengths using the functions wavelength2 index() and index2 wavelength(). Subsequently, the averaging function mean() is used on these subsets. In other words, the result will be the NDVI calculated from the average spectrum between 400 nm and 700 nm and the spectrum between 700 nm and 1100 nm.