0

How to do linear interpolation for a vector corresponding to a constant section? For example we have a matrix of two column as follow.

Matrix =
  125.2985    5.7057
  125.2991    5.7098
  125.2997    5.6880
  125.3004    5.6739
  125.3010    5.7140
  125.3016    6.0141
  125.3022    6.3620
  125.3029    6.4793
  125.3041    6.4665
  125.3047    6.4646
  125.3053    6.4844
  125.3060    6.4743
  125.3066    6.4865
  125.3072    6.4878
  125.3078    6.4975
  125.3085    6.4952
  125.3091    6.4958
  125.3128    6.5867
  125.3134    7.0733
  125.3141    7.3427
  125.3147    7.3238
  125.3153    7.3093
  125.3159    7.3188
  125.3166    7.3436

A second matrix is as 'C'.

C =
125.2985    2.0000
125.3004    3.0000
125.3053    5.0000
125.3085    4.0000
125.3147    6.0000
125.3166    7.0000 

Now I need to do interpolation of 'C(:,2)' values for 'Matrix(:,1)'. The out put result of linear interpolation is as follow

Cinter=interp1(C(:,1),C(:,2),Matrix(:,1),'linear')


Cinter =

2.0000
2.3158
2.6316
3.0000
3.2449
3.4898
3.7347
4.0204
4.5102
4.7551
5.0000
4.7813
4.5937
4.4063
4.2187
4.0000
4.1935
5.3871
5.5806
5.8065
6.0000
6.3158
6.6316
7.0000

But I want to do interpolation only for those data points where 'Matrix(:,2)' is fairly stable. The rest should be as NaN in the interpolated vector. The required output should be as follow instead of 'Cinter'. How to achieve this?

output1=
2.0000
2.3158
2.6316
3.0000
   NaN
   NaN
   NaN
   NaN
   NaN
   NaN
5.0000
4.7813
4.5937
4.4063
4.2187
4.0000
   NaN
   NaN
   NaN
   NaN
6.0000
6.3158
6.6316
7.0000

This also can be taken a step further to achieve a second desired output. Based on first 'output1' data points (NaN) at constant 'Matrix(:,2)' can be replaced by nearby 'C(:,2) value'. The second output would be as follow and how to get this?

output2=
2.0000
2.3158
2.6316
3.0000
3.0000
   NaN
   NaN
5.0000
5.0000
5.0000
5.0000
4.7813
4.5937
4.4063
4.2187
4.0000
4.0000
4.0000
   NaN
6.0000
6.0000
6.3158
6.6316
7.0000

Thank you very much. Best regards

Umar
  • 89
  • 3
  • 10
  • Define "stable". Once you have that definition, simply interpolate and set all values not adhering to the definition to `NaN`. – Adriaan Jan 10 '17 at 17:01
  • @ Adriaan: For changing period it can be defined as diff(Matrix(:,2))<0.1 but the problem comes for the points which are in the stable period but the interpolation for that point is impacted by the other level. So this should be replaced to NaN or consider nearby 'C(:,2)' without interpolation. – Umar Jan 10 '17 at 17:15

1 Answers1

1

To find stable regions, you could estimate derivatives (presumably first and/or second depending on what you mean by "stable") and then select only those within some range; e.g. using gradient for the first derivative:

stable = abs(gradient(Matrix(:,2),Matrix(:,1))) < 50;
Cinter = NaN(size(Matrix,1),1);
Cinter(stable) = interp1(C(:,1),C(:,2),Matrix(stable,1),'linear')

If you want to keep every point that has low slope on the left or on the right, you could try something like:

grad = diff(Matrix(:,2)) ./ diff(Matrix(:,1));
leftgrad = [0; grad];
rightgrad = [grad; 0];
stable2 = abs(leftgrad) < 50 | abs(rightgrad) < 50;

You could then use stable2 instead of stable above; if you insist on picking the value at the closest point (rather than the interpolated points), why not do:

boundary = stable2 & not(stable);
Cinter(boundary) = interp1(C(:,1),C(:,2),Matrix(boundary,1),'nearest')
Thales
  • 585
  • 3
  • 9
  • Thank you. This is pretty much I am looking for. How do I can get the 'output2'? The stable means that the point to point change in 'Matrix(:,2)' is less than defined threshold. If you plot 'Cinter(stable)' as follow.' plotyy(Matrix(:,1),Cinter,Matrix(:,1),Matrix(:,2))'. You will recognize that in 'Cinter(stable)' atleast one 'NaN' is on the sort of stable level. How to replace this 'NaN' value with the nearby value of 'C(:,2)' (as result is described in output2). – Umar Jan 10 '17 at 17:54
  • @Umar, I do not understand what exactly you want in `output2`. Could you edit your question to clarify the request? What should be replaced by what according to what rule? – Thales Jan 10 '17 at 17:57
  • Please plot 'plotyy(Matrix(:,1),Cinter (stable),Matrix(:,1),Matrix(:,2))'. You will recognize that after/before the cutoff of interpolation there are still some data points are 'NaN' on the same stable level of 'Matrix(:,2)'. How to replace these with the nearby original value of 'C(:,2)'.Thanks – Umar Jan 10 '17 at 18:05
  • 'output2' is given in question. Maybe this is more helpful to understand the problem. 'plotyy(Matrix(:,1),Cinter (stable),Matrix(:,1),Matrix(:,2))' hold on plot(Matrix(:,1),output2,'ro') – Umar Jan 10 '17 at 18:07
  • @Umar, I extended my answer to accommodate your request. It is not very clear from your question what you want; if you think that we should look at graphs to understand, please put them into you question. Also, it would be helpful to have data in a form that is easy to copy-and-paste into Matlab. – Thales Jan 10 '17 at 18:26