Thread Subject: Linear interpolation of values in a time series

Subject: Linear interpolation of values in a time series

From: Ryan Utz

Date: 25 Jul, 2009 20:40:18

Message: 1 of 3

I have a time series that consists of a continuous variable that occasionally has some missing data. The format of the data appears below:

1 0.5
2 0.6
3 0.6
4 0.7
5 -1
6 -1
7 -1
8 0.9
9 0.9
10 0.9
11 1.0

The first column is simply an observation number tally; the second is the data of interest. Where a -1 occurs is missing data. I am trying to do a simple linear interpolation based on the points before and after the data go missing. For instance, for observation 5, the formula I wish to use is 0.7+(0.9-0.7)/(8-4). Thus observation 5 is 0.75. Observation 6 would be 0.75+(0.9-0.7)/(8-4), and so on until the data are no longer missing. Thus the second part of the expression (0.9-0.7)/(8-4) stays consistent for each instance of missing data (there are many, the one above is simply some example data).

I'm a novice and am having some trouble getting my head around how to do this in a for-loop. Does anyone have any ideas or functions that may help with this endeavor?

Many thanks ahead of time,
Ryan

Subject: Linear interpolation of values in a time series

From: us

Date: 25 Jul, 2009 20:59:03

Message: 2 of 3

"Ryan Utz" <rutz@al.umces.edu> wrote in message <h4fqji$qu7$1@fred.mathworks.com>...
> I have a time series that consists of a continuous variable that occasionally has some missing data. The format of the data appears below:
>
> 1 0.5
> 2 0.6
> 3 0.6
> 4 0.7
> 5 -1
> 6 -1
> 7 -1
> 8 0.9
> 9 0.9
> 10 0.9
> 11 1.0
>
> The first column is simply an observation number tally; the second is the data of interest. Where a -1 occurs is missing data. I am trying to do a simple linear interpolation based on the points before and after the data go missing. For instance, for observation 5, the formula I wish to use is 0.7+(0.9-0.7)/(8-4). Thus observation 5 is 0.75. Observation 6 would be 0.75+(0.9-0.7)/(8-4), and so on until the data are no longer missing. Thus the second part of the expression (0.9-0.7)/(8-4) stays consistent for each instance of missing data (there are many, the one above is simply some example data).
>
> I'm a novice and am having some trouble getting my head around how to do this in a for-loop. Does anyone have any ideas or functions that may help with this endeavor?
>
> Many thanks ahead of time,
> Ryan

one of the many solutions

% the data
     m=[
            1 0.5
            2 0.6
            3 0.6
            4 0.7
            5 -1
            6 -1
            7 -1
            8 0.9
            9 0.9
           10 0.9
           11 1
     ];
% the engine
     t=m(m(:,2)~=-1,:);
     xi=m(:,1);
     r=interp1(t(:,1),t(:,2),xi);
% the result
     line([xi,xi],[m(:,2),r],'marker','s');
     legend({'raw','interpolated'},'location','southeast');

us

Subject: Linear interpolation of values in a time series

From: dpb

Date: 25 Jul, 2009 21:02:06

Message: 3 of 3

Ryan Utz wrote:
> I have a time series that consists of a continuous variable that
occasionally has some missing data. The format of the data appears below:
>
> 1 0.5
> 2 0.6
> 3 0.6
> 4 0.7
> 5 -1
> 6 -1
> 7 -1
> 8 0.9
> 9 0.9
> 10 0.9
> 11 1.0
>
> The first column is simply an observation number tally; the second is
> the data of interest. Where a -1 occurs is missing data. I am trying to
do a simple linear interpolation based on the points before and after
the data go missing. ...

 >> interp1(v(v(:,2)~=-1,1),v(v(:,2)~=-1,2),v(v(:,2)==-1,1))
ans =
     0.7500
     0.8000
     0.8500
 >>

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
legend us 25 Jul, 2009 17:04:03
interp1 us 25 Jul, 2009 17:04:03
code us 25 Jul, 2009 17:04:03
logical indexing us 25 Jul, 2009 17:04:03
interpolation Ryan Utz 25 Jul, 2009 16:44:02
time series Ryan Utz 25 Jul, 2009 16:44:02
rssFeed for this Thread

Contact us at files@mathworks.com