基于常量样本的背景减法和插值

Constant sample based background subtraction and interpolation

我需要你的专业知识来解决一个问题。我有以下具有不同列的矩阵。

Time            subsamp raw     filt_BG filter
230.5382060     1       1       1       0
230.5382176     2       1       1       0
230.5382292     1       4       0       1
230.5382407     2       4       0       1
230.5382523     1       3       0       1
230.5382639     2       3       0       1
230.5382755     1       2       1       0
230.5382870     2       2       1       0
230.5382986     1       2       0       1
230.5383102     2       2       0       1
230.5383218     1       1       0       1
230.5383333     2       1       0       1
230.5383449     1       3       1       0
230.5383565     2       3       1       0

它包含两种不同类型的原始数据(第 3 列)。每种类型的原始数据都有相同的编号。子样本(第 2 列)。 'filt_BG' 和 'filter' 分隔数据类型。我尝试用下图解释问题和所需的解决方案。 在上图中(左侧),原始数据绘制为时间的函数。颜色分别代表不同的 windows(子样本)'sig' 和基于 'filter' 和 'filt_BG' 的 'BG'。我想从 window 'BG1' 到 window 'BG2'(子样本到子样本)进行插值,以便可以从 [= 中减去插值数据(子样本到子样本) 26=] 和 'sig2'。类似地,从 'BG2' 到 'BG3' 的插值和从 'sig3' 和 'sig4' 的减法。这给了我 'solution1'(右上角)。现在,如果 'BG3' 不存在,那么我只想使用 'BG2' 从 'sig3' 和 'sig4' 中减去(解决方案 2,右下角)。解决方案矩阵如下所示。!!!感谢您的 ideas/solution.!!!请记住,我将拥有数百万个数据点,而不是此处描述的简单数据。另外,我没有任何工具箱,只有 matlab 可用。任何基于工具箱的解决方案也可以。

Time            subsamp solution1   solution2   
230.5382060     1       NaN         NaN
230.5382176     2       NaN         NaN
230.5382292     1       2.5         2.5
230.5382407     2       2.5         2.5
230.5382523     1       1.5         1.5
230.5382639     2       1.5         1.5
230.5382755     1       NaN         NaN
230.5382870     2       NaN         NaN
230.5382986     1       -0.5        0
230.5383102     2       -0.5        0
230.5383218     1       -1.5        -1
230.5383333     2       -1.5        -1
230.5383449     1       NaN         NaN
230.5383565     2       NaN         NaN 

此致

您可以使用 cumsum to compute sample and query points for the interpolation and the data can be interpolated using interp1。在缺少 BG 数据(原始数据包含 NaN)的情况下,需要将第一个和最后一个有效原始数据添加到其开头和结尾,以便 interp1 可以生成所需的外推。

idxsig = data(:,5)==1;                         % logical index to sig
idxBG  = (data(:,4)==1) & (~isnan(data(:,3))); % logical index to BG   

f1 = find(idxBG,1,'first');
f2 = find(idxBG,1,'last');
                                               % Add the first and the last-
                                               % valid data to the beginning-
                                               % and the end of raw data to-
                                               % get valid extrapolation                           
sig = data(idxsig,3);
BG = [data(f1,3);data(idxBG,3);data(f2,3)];
idxBG =  [true ;idxBG; true];
idxsig = [false; idxsig; false];
                                               % preparing sample and query-
                                               % points for interpolation
idx_sum = cumsum(idxBG);                          
idx_sig = idx_sum(idxsig)+0.5;                 % query points
idx_BG = idx_sum(idxBG);                       % sample points
intr = sig - interp1(idx_BG, BG, idx_sig);
solution = NaN(size(idxBG));
solution(idxsig) = intr                        % reformat to the original size
solution = solution(2:end-1);

solution =

       NaN
       NaN
   2.50000
   2.50000
   1.50000
   1.50000
       NaN
       NaN
  -0.50000
  -0.50000
  -1.50000
  -1.50000
       NaN
       NaN