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

cont_pad_with_nans

PURPOSE ^

Fill in gaps in cont or cstream data using NaN

SYNOPSIS ^

function extended = cont_pad_with_nans(realdata)

DESCRIPTION ^

 Fill in gaps in cont or cstream data using NaN

 Conservatively evaluates a cont or cstream variable.  If it contains gaps
 that are longer than a tolerance times the sampling period of the
 variable, it fills in that gap with NaNs.


 >> cont_test = [1 10; 2 20; 3 30; 4 40; 5 50; 6 60; 7 70; 8 80];
 >> cont_pad_with_nans(cont_test)
 ans =
   1 10
   2 20
   3 30
   4 40
   5 50
   6 60
   7 70
   8 80
 >> cont_pad_with_nans(cont_test([1 2 3 5 6], :))
 ans =
   1 10
   2 20
   3 30
   4 NaN
   5 50
   6 60


 >> cont_pad_with_nans([cont_test; 9.5 95])
 Warning: Strange gap length ***

 >> cont_pad_with_nans(cont_test([1 3 4 6 7 8], :))
 ans =
   1 10
   2 NaN
   3 30
   4 40
   5 NaN
   6 60
   7 70
   8 80

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function extended = cont_pad_with_nans(realdata)
0002 % Fill in gaps in cont or cstream data using NaN
0003 %
0004 % Conservatively evaluates a cont or cstream variable.  If it contains gaps
0005 % that are longer than a tolerance times the sampling period of the
0006 % variable, it fills in that gap with NaNs.
0007 %
0008 %
0009 % >> cont_test = [1 10; 2 20; 3 30; 4 40; 5 50; 6 60; 7 70; 8 80];
0010 % >> cont_pad_with_nans(cont_test)
0011 % ans =
0012 %   1 10
0013 %   2 20
0014 %   3 30
0015 %   4 40
0016 %   5 50
0017 %   6 60
0018 %   7 70
0019 %   8 80
0020 % >> cont_pad_with_nans(cont_test([1 2 3 5 6], :))
0021 % ans =
0022 %   1 10
0023 %   2 20
0024 %   3 30
0025 %   4 NaN
0026 %   5 50
0027 %   6 60
0028 %
0029 %
0030 % >> cont_pad_with_nans([cont_test; 9.5 95])
0031 % Warning: Strange gap length ***
0032 %
0033 % >> cont_pad_with_nans(cont_test([1 3 4 6 7 8], :))
0034 % ans =
0035 %   1 10
0036 %   2 NaN
0037 %   3 30
0038 %   4 40
0039 %   5 NaN
0040 %   6 60
0041 %   7 70
0042 %   8 80
0043 %
0044 %
0045 
0046 %
0047 tolerance = 0.1;
0048 
0049 period = median(diff(realdata(:, 1)));
0050 
0051 gap_starts = find(diff(realdata(:, 1)) > (1+tolerance) * period);
0052 
0053 extended = realdata;
0054 
0055 amount_filled = 0;
0056 
0057 for G = 1:length(gap_starts)
0058     gap_start = gap_starts(G);
0059     output_gap_start = gap_start + amount_filled;
0060     gap_length = diff(realdata(gap_start:gap_start+1, 1));
0061     gap_num_periods = gap_length / period;
0062     gap_num_whole_periods = round(gap_num_periods);
0063     if abs(gap_num_whole_periods - gap_num_periods) > tolerance
0064         warning('cont_pad_with_nans:irregular_sampling', ...
0065             'Strange gap length of %f: %f times period (%f)', ...
0066             gap_length, gap_num_periods, period);
0067     end
0068     
0069     % imagine you count from 1 to 3 skipping 2.  Then period is 1, you miss
0070     % two periods, but you only need to add one new entry
0071     num_fillers = gap_num_whole_periods - 1; 
0072     
0073     filler = realdata(gap_start, 1) + (1:num_fillers)' * period;
0074     filler(:, 2) = NaN;
0075     extended = [extended(1:output_gap_start, :); ...
0076         filler; ...
0077         extended(output_gap_start+1:end, :)];
0078     amount_filled = amount_filled + num_fillers;
0079 end
0080

Generated on Tue 23-May-2017 02:00:59 by m2html © 2005