resample: resample a colorSpec Object to new wavelengths

Description Usage Arguments Details Value References See Also Examples

View source: R/colorSpec.ops.R


interpolate or smooth to new wavelengths. Simple extrapolation and clamping is also performed.


## S3 method for class 'colorSpec'
resample( x, wavelength, method='auto', span=0.02, extrapolation='const', clamp='auto' )



a colorSpec object


vector of new wavelengths, in nanometers


interpolation methods available are 'sprague', 'spline', and 'linear'. Also available is 'auto' which means to use 'sprague' if x is regular, and 'spline' otherwise. An available smoothing method is 'loess'. See Details.


smoothing argument passed to loess() during interpolation, and not used by other methods. The default value span=0.02 is suitable for .scope spectra but may be too small in many other cases.


extrapolation methods available are 'const' and 'linear'. These can be abbreviated to the initial letter. Also available is a numeric value, which is used for simple padding. See Details.
Also available is a vector or list of length 2 that combines 2 of the above. The first item is used on the low side (shorter wavelengths), and the second item is used on the high side (longer wavelengths).


clamp methods available are 'auto', TRUE, and FALSE. Also available is a numeric vector of length 2, which defines the clamping interval. See Details.


If method is 'sprague', the quintic polynomial in De Kerf is used. Six weights are applied to nearby data values: 3 on each side. The 'sprague' method is only supported when x is regular.

If method is 'spline', the function stats::spline() is called with method='natural'. The 'spline' method is supported even when x is irregular.

If method is 'linear', the function stats::approx() is called. Two weights are applied to nearby data values: 1 on each side. The 'linear' method is supported even when x is irregular.

If method is 'loess', the function stats::loess() is called with the given span parameter. Smoothing is most useful for noisy data, e.g. raw data from a spectrometer. I have found that span=0.02 works well for Ocean Optics .scope files, but this may be too small in other cases, which triggers an error in stats::loess(). The 'loess' method is supported even when x is irregular.

If extrapolation is 'const', the extreme values at each end are simply extended. If extrapolation is 'linear', the line defined by the 2 extreme values at each end is used for extrapolation. If the ultimate and penultimate wavelengths are equal, then this line is undefined and the function reverts to 'const'.

If clamp is 'auto', output values are clamped to the physically realizable interval appropriate for x. This is the interval [0,1] when quantity(x) is 'reflectance' or 'transmittance', and the interval [0,) otherwise. Exception: If an input spectrum in x violates a limit, then clamping the output spectrum to this limit is NOT enforced. This happens most frequenty for theoretical (or matrixed) cameras, such as BT.709.RGB.

If clamp is TRUE, the result is the same as 'auto', but with no exceptions. If clamp is FALSE, then no clamping is done.

If clamp is a numerical interval, then clamping is done to that interval, with no exceptions. The two standard intervals mentioned above can be expressed in R as c(0,1) and c(0,Inf) respectively.


resample(x) returns a colorSpec object with the new wavelength. Other properties, e.g. organization, quantity, ..., are preserved.
In case of ERROR, the function returns NULL.


De Kerf, Joseph L. F. The interpolation method of Sprague-Karup. Journal of Computational and Applied Mathematics. volume I, no 2, 1975. equation (S).

See Also

organization(), quantity(), wavelength(), is.regular(), theoreticalRGB, spline(), approx(), loess


path = system.file( "extdata/sources/pos1-20x.scope", package='colorSpec' )
y = readSpectra( path )
# plot noisy data in gray
plot( y, col='gray' )
# plot smoothed plot in black on top of the noisy one to check quality
plot( resample( y, 200:880, meth='loess', span=0.02 ), col='black', add=TRUE )

Example output

This is colorSpec.  Version: 0.5-3.  Author: Glenn Davis [aut, cre].  Built: R 3.4.1; ; 2017-09-21 19:34:23 UTC; unix

colorSpec documentation built on Nov. 19, 2018, 5:06 p.m.