Interp1 for non-monotonic timeseries

3 views (last 30 days)
Hi all,
Recently I have been trying to fix a interp1-related problem. However, I am not able to solve. I need you guys help. Let me give an easy example:
I have a timeseries, y=[-3 -2 -1 1 2 3 4 5 6 5 4 3 2 1 -1 -2 -3 -2 -1 -0.5], and time axis is simply from 1 to 20. I want to find out all the time when y equals 0. It is easy to find out when we do 'plot (x, y)'. But when I use " interp1 (y,x,0)", a lot of problems happens:
_ __Error using griddedInterpolant The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 183) F = griddedInterpolant(X,V,method);_ _
Looking forward to your answer.
Regards, Guojian

Accepted Answer

Star Strider
Star Strider on 13 Feb 2015
This is likely the easiest way, and is reasonably fast:
t = 1:20;
y=[-3 -2 -1 1 2 3 4 5 6 5 4 3 2 1 -1 -2 -3 -2 -1 -0.5];
ycsm = y .* circshift(y, [0 -1]); % Will Be <0 Near Zero Crossing
yzxidx = find(ycsm < 0); % Find Negatives
for k1 = 1:length(yzxidx)
idx = [yzxidx(k1) yzxidx(k1)+1]; % Index Range
b = [[1; 1] t(idx)']\y(idx)'; % Regression Parameters
zx(k1) = -b(1)/b(2); % Zero Crossings
end
produces these values of ‘t’ for the zero-crossings:
zx =
3.5 14.5
  2 Comments
Guojian
Guojian on 18 Feb 2015
Thank you! It is brilliant!
Star Strider
Star Strider on 18 Feb 2015
My pleasure! And I very much appreciate your compliment!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!