Code covered by the BSD License  

Highlights from
Avantes to Matlab

image thumbnail

Avantes to Matlab

by

 

11 Jun 2012 (Updated )

Read spectral data from Avantes USB2 spectrometer files

getScansByTime(scans, time1, time2, maxscans)
function res = getScansByTime(scans, time1, time2, maxscans)
% Returns scans with timeStart >= TIME1 and timeEnd <= TIME2. 
% If TIME2 is not specified, returns the first scan with timeStart >= TIME1.
%
% MAXSCANS is an optional parameter. If specified, limits (approximately)
% the number of scans to return (at equal step).

% Copyright (cc-by) K. Karapetyan, kotya.karapetyan@gmail.com, 2011

%% Convert, if needed, time1 and time2 to serial date number
if ischar(time1) % time1 is a string
    if numel(time1) < 16 % no date specified, assume that the time is 
        % specified on the same date as the beginning of the first scan
        time1 = datenum([datestr(scans(1).timeStart, 29) ' ' time1]);
    else % date specified
        time1 = datenum(time1);
    end
end

if exist('time2', 'var')
    if ischar(time2)
        if numel(time1) < 16        
            time2 = datenum([datestr(scans(1).timeStart, 29) ' ' time2]);
        else % date specified
            time2 = datenum(time2);
        end
    end    
end

%% Find first scan
res = [];
i = 1;
while i < numel(scans)
    if scans(i).timeStart >= time1
        break; % first needed scan found
    end
    i = i + 1;
end

if i > numel(scans) % no needed scan found
    return
else
    if exist('time2', 'var')
        if scans(i).timeStart > time2
            return
        end
    end
    res = scans(i);
    fprintf('%s: First scan at %s\n', mfilename, datestr(scans(i).timeStart, 31));
    i = i + 1;
end

i1 = i;
if exist('time2', 'var')
    while i <= numel(scans) && scans(i).timeEnd <= time2
        i = i + 1; 
    end
    i2 = i - 1;
    res = [res scans(i1:i2)];
    fprintf('%s: Last scan at %s\n', mfilename, datestr(res(end).timeStart, 31));
end

%% If MAXSCANS given, remove redundant scans
if exist('maxscans', 'var')
    if numel(res) > maxscans
        step = round(numel(res) / maxscans);
        res = res(1:step:end);
    end
end

Contact us