filtering by the second derivative?

8 views (last 30 days)
I have an array, and I'm trying to figure out how to mark the values where the second derivative is above a threshold. To get the second derivative I'm doing
diff(diff(array)), the problem is that creates a result of length(array)-2, and I want to created a logical array for the original array.
For example, with
arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34];
and a filter of x < 2, I'd like to produce:
[1,1,1,1,1,1,1,0,0,0]
since the second derivative is
[-1,1,0,1,1,2,3,5]
Hopefully that makes sense.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jan 2020
gradient(gradient(arr)) < 2
Watch out for < 2 compared to <= 2
  2 Comments
Steve Miller
Steve Miller on 6 Jan 2020
That looks like it will work in this simple case (thank you!) but I'm not sure it will work in what I'm trying to do. I need to do some calculations on the diff() and filter based on values less than 1, and gradient() returns values that differ from diff() by more than what I'm filtering based on -
diff(diff(arr) = -1 1 0 1 1 2 3 5
gradient(gradient(arr)) = -0.5 -0.25 0.25 0.5 0.75 1.25 2 3.25 3.25 2.5
Walter Roberson
Walter Roberson on 6 Jan 2020
Well, then, you are stuck. diff() will never return an array of full size equal to its input (except in the degenerate case diff([]) in which case the result is [] which is equal in size to the input.) Therefore if you define taking the derivative as using diff(), then you have boxed yourself into a corner.
Mathematically, diff() is only one way among several to numerically estimate derivative. It is not considered to be the best numeric derivative because at any one point, it is only using two adjacent points to do the estimate, instead of using points before and after to do the estimation. https://en.wikipedia.org/wiki/Finite_difference
gradient() uses central difference except at the two edges. Central difference is often considered a closer numeric approximation to derivative than what is used by diff()

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!