# How to interpolate over long section of bad data?

조회 수: 6(최근 30일)
Katie Colfer 16 Oct 2019
댓글: Katie Colfer 18 Oct 2019
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):
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.

댓글을 달려면 로그인하십시오.

### 답변(1개)

John D'Errico 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.
% use only the good data to create an interpolant
splineVO2 = spline(x(gooddatalocs),yVO2(gooddatalocs));
% now interpolate, but at the bad points

#### 댓글 수: 3

Katie Colfer 17 Oct 2019
Thank you for this! This is super helpful and makes a lot of sense. However, my 'bad' data points over which I want to interpolate aren't just the 0 points, it's a string of data beginning at my 0 points. Basically, I want to interpolate 2407:2822. I've been trying to modify your code to define gooddatalocs as basically yVO2(1:2406,1) and yVO2(2823:4076) with no luck. Any thoughts?
John D'Errico 17 Oct 2019