Code covered by the BSD License  

Highlights from
Field Spectroscopy Facility Post Processing Toolbox

from Field Spectroscopy Facility Post Processing Toolbox by Iain Robinson
A toolbox for importing and processing optical spectra acquired with portable spectroradiometers.

absolutereflectance(input, panel)
%ABSOLUTEREFLECTANCE Calculates absolute reflectance spectra from relative
%reflectance spectra.
%   R_ABS = ABSOLUTEREFLECTANCE(R_REL, PANEL)
%   Multiplies all spectra in the structure R_REL by a panel calibraiton
%   specturm PANEL.
%
%   P must be a structure with two fields: a column vector of wavelengths
%   (P.wavelength) and a column vector of corresponding panel reflectances
%   (P.data) in the range 0 to 1.
%
%   The panel calibration PANEL can be imported from a comma-seaprated
%   values (CSV) file or an Excel spreadsheet using the
%   importpanelcalibrationspectrum function.
%
%   Example:
%
%      panel = importpanelcalibration('FSF panels.xls', 'SRT #005')
%      R_abs = absolutereflectance(R_rel, panel)
%
%   See also: importpanelcalibration
%
%   FSF Post Processing Toolbox
%   Field Spectroscopy Facility, Natural Environment Research Council
%
%   Author: Iain Robinson
%   Contact: fsf@nerc.ac.uk
%   Requirements:
%      MATLAB R2009b or later
%      importpanelcalibration from FSF Post Processing Toolbox
%   Revision: 1.2.9
%   Date: 2011-03-28

function [output] = absolutereflectance(input, panel)
    % Check that the wavelength scale is the same for all spectra in the
    % array.
    if ~isequal(input.wavelength)
        error('The wavelength scales of the input spectra are not all identical. To calculate the absoluterefelectance of an array of spectra all wavelength values must be equal.');
    end    
    % Check that the wavelength scale is sorted in increasing order.
    if ~issorted(input(1).wavelength)
        warning('The wavlelength values of the input spectrum (or spectra) are not sorted in increasing order. This may indicate that there are overlapped data in the spectrum; for example at the joins between different detectors. This may cause unpredicatble results in the overlapped regions during post processing.');
    end

    % Get the wavelength scale of the input spectrum.
    inW = input(1).wavelength;
        
    % The panel calibration data may cover a different range of wavelengths
    % to the input spectra. The output wavelength scale should therefore be
    % the wavelength range which is covered by _both_ panel and spectra.
    minW = max(inW(1), panel.wavelength(1)); %...the smallest wavelength in _both_ panel and spectra (i.e. the maximum of the minimums).
    maxW = min(inW(end), panel.wavelength(end)); %...the largest wavelength in _both_ panel and spectra (i.e. the minimum of the maximums).
    
    % Set the output wavelength scale.
    outW = inW(inW >= minW & inW <= maxW);
    
    % Copy the input spectra to the output.
    output = input;
    
    % Overwrite wavelength and data fields with new values.
    for i=1:length(input)
        % Crop the output spectrum to the wavelength range: minW to maxW.
        output(i).wavelength = outW;
        output(i).data = input(i).data(inW >= minW & inW <= maxW);
        
        % Interpolate the panel spectrum to the cropped wavelength scale.
        %if panel.wavelength == outW
        %    P = panel.data;
        %else
        P = spline(panel.wavelength, panel.data, outW);
        %    warning('Interpolated panel data.');
        %end
        
        % Multiply the output spectrum by the interpolated panel spectrum.
        output(i).data = P .* output(i).data;
    end
end

Contact us