title: Constant sample based background subtraction and interpolation
body: <p>I need your expertise for a problem. I have the following matrix with different columns.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>It contains two different type of raw data (column 3). Each type of raw data has same no. of sub sample (column 2). The 'filt_BG' and 'filter' seprate the type of data. I try to explain the problem and required solution with the following diagram. 
  <a href="https://i.stack.imgur.com/7XHFG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7XHFG.png" alt="enter image description here"> </a>
  In the above diagram (leftside), raw data is plotted as a function of time. The colors are representing different windows(subsample) 'sig' and 'BG'based on 'filter' and 'filt_BG', repectively. I want to do interpolation from the window 'BG1' to the window 'BG2'(subsample to subsample), so that interpolated data can be subtracted (subsample to subsample) from 'sig1' and 'sig2'. Similarly from 'BG2' to 'BG3' interpolation and subtraction from 'sig3' and 'sig4'. This gives me 'solution1' (upper right). Now, if 'BG3' do not exsist then I want to use only 'BG2' for subtraction from 'sig3' and 'sig4'(solution2, lower right). The solution matrix would be something like below.!!! Thank you for your ideas/solution.!!! Please keep in mind that I shall have millions of data points not as simple data as described here. Also, I do not have any toolbox but only the matlab available. Any solution based on toolbox is also fine. </p>
  
  <pre><code>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 
  </code></pre>
  
  <p>Best Regards</p>