Home > data-analysis > manip > cont > cont_sampled_intervals.m

cont_sampled_intervals

PURPOSE ^

cont_sampled_intervals finds all intervals when data exists in given var.

SYNOPSIS ^

function event_sampled = cont_sampled_intervals(cont, max_gap, no_data_value, sample_duration)

DESCRIPTION ^

cont_sampled_intervals finds all intervals when data exists in given var.
 USAGE:
 cont_sampled_intervals(CONT_VAR)
   Finds all time periods (no matter how small) during which data exists
   in the given CONT_VAR.

 cont_sampled_intervals(CONT_VAR, MAX_GAP)
   Same as cont_sampled_intervals(CONT_VAR), but will overlook MAX_GAP *
   the sample interval worth of missing data without calling it a
   discontinuity.  The default MAX_GAP is 1 sample.

 cont_sampled_intervals(CONT_VAR, MAX_GAP, NO_DATA_VALUE)
   Finds all time periods during which data exists in the given CONT_VAR,
   treating data equal to NO_DATA_VALUE as missing.

 cont_sampled_intervals(CONT_VAR, MAX_GAP, NO_DATA_VALUE, SAMPLE_DURATION)
   Same as cont_sampled_intervals(CONT_VAR, NO_DATA_VALUE), but treats
   SAMPLE_DURATION as the interval between samples that are contiguous.
   Otherwise, the mode(diff(timestamps)) is treated as the sample
   interval.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function event_sampled = cont_sampled_intervals(cont, max_gap, no_data_value, sample_duration)
0002 %cont_sampled_intervals finds all intervals when data exists in given var.
0003 % USAGE:
0004 % cont_sampled_intervals(CONT_VAR)
0005 %   Finds all time periods (no matter how small) during which data exists
0006 %   in the given CONT_VAR.
0007 %
0008 % cont_sampled_intervals(CONT_VAR, MAX_GAP)
0009 %   Same as cont_sampled_intervals(CONT_VAR), but will overlook MAX_GAP *
0010 %   the sample interval worth of missing data without calling it a
0011 %   discontinuity.  The default MAX_GAP is 1 sample.
0012 %
0013 % cont_sampled_intervals(CONT_VAR, MAX_GAP, NO_DATA_VALUE)
0014 %   Finds all time periods during which data exists in the given CONT_VAR,
0015 %   treating data equal to NO_DATA_VALUE as missing.
0016 %
0017 % cont_sampled_intervals(CONT_VAR, MAX_GAP, NO_DATA_VALUE, SAMPLE_DURATION)
0018 %   Same as cont_sampled_intervals(CONT_VAR, NO_DATA_VALUE), but treats
0019 %   SAMPLE_DURATION as the interval between samples that are contiguous.
0020 %   Otherwise, the mode(diff(timestamps)) is treated as the sample
0021 %   interval.
0022 %
0023 
0024 if nargin > 1
0025     gap = max_gap;
0026 else
0027     gap = 1;
0028 end
0029 
0030 if nargin > 2
0031     missing = cont(:, 2:end) == no_data_value;
0032     data = cont(~ missing, :);
0033 else
0034     data = cont;
0035 end
0036 
0037 if nargin > 3
0038     interval = sample_duration;
0039 else
0040     interval = mode(diff(cont(:, 1)));
0041 end
0042 
0043 tol = interval * 0.0001;
0044 
0045 times = data(:, 1);
0046 
0047 % breaks contains the linear indices of the differences that are greater
0048 % than interval+tol.  Since diff(x) has one fewer element than x, this is
0049 % the linear index of the last contiguous value in the times array.
0050 breaks = find(diff(times) > ((gap * interval) + tol));
0051 
0052 % finds the *indices* of the time stamps of the event.
0053 indices = zeros(length(breaks) + 1, 2);
0054 last_bound = 0;
0055 for I = 1:length(breaks)
0056     indices(I, 1) = last_bound + 1;
0057     indices(I, 2) = breaks(I);
0058     
0059     last_bound = breaks(I);
0060 end
0061 % Wrap up the loop: add the last interval
0062 indices(end, 1) = last_bound + 1;
0063 indices(end, 2) = length(times);
0064 
0065 
0066 % Event intervals are half-open intervals
0067 out(:, 1) = times(indices(:, 1));
0068 out(:, 2) = times(indices(:, 2)) + interval - tol;
0069 
0070 
0071 event_sampled = out;

Generated on Wed 24-May-2017 17:00:59 by m2html © 2005