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

cont_best_overlap

PURPOSE ^

cont_best_overlap attempts to align one cont value to the other

SYNOPSIS ^

function [score, shift] = cont_best_overlap(cont1, cont2, window)

DESCRIPTION ^

cont_best_overlap attempts to align one cont value to the other
   USAGE:
   [score, shift] = cont_best_overlap(cont1, cont2, window_size)

 cont_best_overlap tries to find the
 best overall time shift to make these two stream overlap.  this function
 takes only two cont streams that have the same size and sampling rate. 

 The 'best' overlap is judged by subtracting one stream from the other, and
 the alignment with the minimal distance is chosen as the best one.


 Input: two cont  
        window specifies the max temporal shift since we don't want to get
        unrealistic warping in time
 Output: overall overlapped score, and the temporal shift for cont2 to
 best match cont1.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [score, shift] = cont_best_overlap(cont1, cont2, window)
0002 %cont_best_overlap attempts to align one cont value to the other
0003 %   USAGE:
0004 %   [score, shift] = cont_best_overlap(cont1, cont2, window_size)
0005 %
0006 % cont_best_overlap tries to find the
0007 % best overall time shift to make these two stream overlap.  this function
0008 % takes only two cont streams that have the same size and sampling rate.
0009 %
0010 % The 'best' overlap is judged by subtracting one stream from the other, and
0011 % the alignment with the minimal distance is chosen as the best one.
0012 %
0013 %
0014 % Input: two cont
0015 %        window specifies the max temporal shift since we don't want to get
0016 %        unrealistic warping in time
0017 % Output: overall overlapped score, and the temporal shift for cont2 to
0018 % best match cont1.
0019 %
0020 %
0021 
0022 % cont1 and cont2 have the same size?
0023 if (size(cont1) ~= size(cont2))
0024     fprintf(1,'two inputs should have the same size\n');
0025     score=0; shift = 0; 
0026     return;
0027 end;
0028 
0029 % first check whether cont1 and cont2 have the fixed and same
0030 % sampling rate
0031 % there is a numerical resolution problem in matlab, has to use a small
0032 % number to compare two numerical values are the same or not
0033 rate = cont1(2,1) - cont1(1,1); 
0034 if (isempty(find(diff(cont1(:,1))- rate >= 0.0005)) == 0)
0035     fprintf(1,'cont1 sampling timestamps are not fixed\n');
0036     score=0; shift = 0; 
0037     return;
0038 end;
0039 if (isempty(find(diff(cont2(:,1))- rate >= 0.0005)) == 0)
0040     fprintf(1,'cont2 sampling timestamps are not fixed\n');
0041     score=0; shift = 0; 
0042     return;
0043 end;
0044 
0045 % calculate the moving window size
0046 n = round(window/rate);
0047 window = rate * n; 
0048     
0049 n = 0; 
0050 for i = -window : rate: window 
0051     n  = n + 1;
0052     
0053     % shift cont2 by i
0054     cont3 = cont_time_shift(cont2, i);
0055     
0056     % pairwise comparison
0057     res(n,1) = sum(abs(cont3(:,2) - cont1(:,2)));
0058     res(n,2) = i; 
0059 end;
0060 res
0061 [score index] = min(res(:,1));
0062 shift = res(index,2); 
0063 
0064 
0065 
0066 
0067 
0068 
0069

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005