How to get intervals according to conditions of other parameters?

4 views (last 30 days)
Hi,
I am trying to analyze some data and I need to identify the intervals of certain parameters using the conditions of other parameters. For example, I need the intervals marked on the picture(vertical lines):
Conditions are: red increasing, dashdot is const, blue > ref value, black > ref value. Yellow represents someone else's attempt of doing this. Especially, I want to know how can I check for increasing/decreasing condition with cases where it sometimes const in between: small intervals in between where obj.red(i) = obj.red(i+1), which need to be included, but I want to avoid intervals like shown in the circle.
This is how I do it now:
%obj - instance of a class
i = 0;
eachInterval = [];
while (i < length (obj.dashdot))
i = i + 1;
if ( obj.blue(i) > ref...
% also sometimes there are small intervals in between
% where obj.red(i) = obj.red(i+1), which need to be included,
% but I want to avoid intervals like shown in the circle
&& obj.red(i) < obj.red(i+1)...
&& obj.dahsdot(i) == obj.dahsdot(i+1))
eachInterval(end+1) = i;
% make sure here (i+1)
if ( ~( obj.blue(i) > ref...
&& obj.red(i) < obj.red(i+1)...
&& obj.dahsdot(i) == obj.dahsdot(i+1)))
%
if (length(eachInterval) > 0)
obj.interStarts(end+1) = eachInterval(1);
obj.interEnds(end+1) = eachInterval(end);
eachInterval = [];
else
eachInterval = [];
end
end %if
end % if
end % while
Thanks, Nurlan.

Answers (1)

SK
SK on 2 Oct 2014
Edited: SK on 2 Oct 2014
Its a little tricky, unless you have a little more information. For example, do you know the number of increasing runs - call it N. Suppose you knew that N = 3, then use the
fit()
function with the 'linearinterp' fittype. You'll need to create a function that returns the y-coordinate of a piecewise linear function, given the parameters and x-coordinate. In your case the parameters will be the starting and ending points of the increasing segments. The fit() function will call your function with different trial parameter values and x-coordinates. See the matlab documentation for fit(). There is an example there.
This will work only for small values of N. For larger values, it may take too much time to run. However it is very robust and covers all those niggling issues of small constant segments and so on.
One you have the fit (purely based on the condition of increasing red), you can apply the other conditions to eliminate segments.
  2 Comments
Nurlan Mukanov
Nurlan Mukanov on 3 Oct 2014
hi SK,
Thanks for your advice. Actually, it's a good point to use fitting. I also looked at the smoothing option as well. The best thing so far is the fit() with 'smoothingspline'. However, the problem there is that the endpoints are off. Especcially, where I need the constant intervals fit() or smooth() are not good.
Also, to add, I don't know any of x values. I need to identify the x points using those conditions on different parameters. And I don't know the number of runs. I might have some approximate information, like the distance between the intervals or approximate values of max and min y. But all these values are approximate.
Could you also advise how can I make my code more robust?
Thanks for your help, Nurlan
SK
SK on 4 Oct 2014
Edited: SK on 4 Oct 2014
As you probably realized, the problem is not so easy. I've concluded that curve fitting is the most robust, but if you don't want to spend too much time on it, you could use some quick and dirty way. For this you will need to define some cutoff values. Some, possibly stupid, ideas:
idea 1:
1. Cutoff run length (r0). If run length < r0, then discard.
2. Cutoff average slope over length r0 (s0). If average slope over r0 is < s0, then discard.
3. Then move along the curve computing these two quantities at every point. If average slope > s0 for a run of at least r0, then you have an increasing interval.
---------
idea 2:
Take second differences. The take off point for an increasing interval will have a positive peak in the second derivative and landing point will have negative peak. Try to identify pairs of positive and negative peaks with condition that:
peak_height * distance_between_peaks > some_cutoff_value
which is supposed to ensure that it is a significant ramp. There may be pairs between pairs (just like opening and closing brackets) that you need to skip that are not significant. Also some take offs and /or landings may be slow and so may not have a very high peak - but perhaps such segments are best excluded anyway.
You may want to combine both these methods with some sort of prior smoothing on the curve.
In any case, the if's and but's for the quick and dirty methods are never-ending, so you cant be discouraged by that. Just test and see if it works for your particular type of data.
By the way, take these ideas with a pinch of salt. You may be able to come up with better ones.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!