filtering by the second derivative?

조회 수: 9 (최근 30일)
Steve Miller
Steve Miller 2020년 1월 6일
댓글: Walter Roberson 2020년 1월 6일
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.

채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 6일
gradient(gradient(arr)) < 2
Watch out for < 2 compared to <= 2
  댓글 수: 2
Steve Miller
Steve Miller 2020년 1월 6일
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 2020년 1월 6일
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()

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by