How to interpolate over long section of bad data?

4 views (last 30 days)
Hello all,
I'm very new to MATLAB (and coding in general) so forgive me if I don't explain the problem well. Basically I have a large dataset from an experiment during which the analyzer equipment malfunctioned. I want to interpolate over a long section of data for 2 variables (VO2 and VCO2). Here's a sample of the data (VO2; starting with 3 good data points, 3 empty data points when the equipment malfunctioned, and then 3 data points following the empty cells that I don't believe are accurate):
Screen Shot 2019-10-16 at 4.16.07 PM.png
I've tried this code so far:
x = 1:4076;
yVO2 = VO2(1:4076);
yVCO2 = VCO2(1:4076);
xq = x;
splineVO2 = spline(x,yVO2);
splineVCO2 = spline(x,yVCO2);
interpVO2 = ppval(splineVO2,xq);
interpVCO2 = ppval(splineVCO2,xq);
And the returned interpolated values match the original values. I've tried this same code with only cells I want to interpolate over (2407:2822) and I get the same result. I've also tried this same code but with xq as an input argument in the spline function ( splineVO2 = spline(x,yVO2,xq); ) and have gotten the same result.
I'm guessing I'm doing something wrong with defining xq but I'm not sure.
Thanks in advance!

Answers (1)

John D'Errico
John D'Errico on 16 Oct 2019
I had to laugh. No, your problem is not how you defined xq, but more in how you defined x! At least you were thinking in sort of the right direction.
The point is, you don't want to define the set of all values as x and y for the spline. Only use the ones where you have valid data for x and y.
x = 1:4076;
yVO2 = VO2(1:4076);
gooddatalocs = (yVo2 ~= 0); % you don't really need the parens here.
baddatalocs = ~gooddatalocs;
% use only the good data to create an interpolant
splineVO2 = spline(x(gooddatalocs),yVO2(gooddatalocs));
% now interpolate, but at the bad points
yVO2(baddatalocs) = ppval(splineVO2,x(baddatalocs));
  3 Comments
John D'Errico
John D'Errico on 17 Oct 2019
So your bad data is comprised of the set of indices
baddatalocs = 2807:2822;
Then your good data locations are everything else. HINT: you could use setdiff to find the good stuff. Or you could just recognize the good data is [1:2406,2823:4076] and build it directly.
Katie Colfer
Katie Colfer on 18 Oct 2019
Got it. I figured it out. Thanks so much!

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!