Discussion:
derivative function in IDL similar as DIFF in matlab
(too old to reply)
Jie Zhou
2013-11-12 10:08:11 UTC
Permalink
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
alx
2013-11-12 10:27:24 UTC
Permalink
Post by Jie Zhou
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
The DERIV function in IDL uses a second order formula for the discrete differenciation (known as 3-point Lagrange interpolation). Afaik, Matlab DIFF computes a simple difference given, in IDL, by x - shift(x,1), where x is your data series.
alx.
Matthew Argall
2013-11-12 12:01:15 UTC
Permalink
Post by Jie Zhou
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
TS_DIFF(data, 1) would be equivalent.
alx
2013-11-12 12:07:56 UTC
Permalink
Post by Matthew Argall
Post by Jie Zhou
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
TS_DIFF(data, 1) would be equivalent.
TS_DIFF(data, 1) is simply doing a shift by one (i.e. data - shift(data, 1)), but by using a for loop!
alx.
Matthew Argall
2013-11-12 12:08:10 UTC
Permalink
Post by Matthew Argall
Post by Jie Zhou
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
TS_DIFF(data, 1) would be equivalent.
Actually, maybe not. TS_DIFF calculates the forward difference. I think you are looking for the backward difference.

In that case, I tend to use

result = data[1:*] - data[0:n_elements(data)-1]

or

result = shift(data, 1) - data
result = result[0:n_elements(result)-1]
Jie Zhou
2013-11-12 12:18:01 UTC
Permalink
Post by Matthew Argall
Post by Matthew Argall
Post by Jie Zhou
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
TS_DIFF(data, 1) would be equivalent.
Actually, maybe not. TS_DIFF calculates the forward difference. I think you are looking for the backward difference.
In that case, I tend to use
result = data[1:*] - data[0:n_elements(data)-1]
or
result = shift(data, 1) - data
result = result[0:n_elements(result)-1]
In fact what I tried to do is using diff function to calculate the n-th derivative of an 2-d matrix. for example, for a matrx:
A=
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1

in matlab, the DIFF(A,2) gives:
1 -2 1 0 0 0 0 0 0 0
0 1 -2 1 0 0 0 0 0 0
0 0 1 -2 1 0 0 0 0 0
0 0 0 1 -2 1 0 0 0 0
0 0 0 0 1 -2 1 0 0 0
0 0 0 0 0 1 -2 1 0 0
0 0 0 0 0 0 1 -2 1 0
0 0 0 0 0 0 0 1 -2 1

Until now, I don't find a equivalent function in IDL.
Thanks to alx, I use
D=(shift(shift(A,0,1)-A,0,1)-(shift(A,0,1)-A))[*,2:*]
to finish the task.

jie
Matthew Argall
2013-11-12 14:07:29 UTC
Permalink
Post by Jie Zhou
D=(shift(shift(A,0,1)-A,0,1)-(shift(A,0,1)-A))[*,2:*]
print, (Shift_Diff(Shift_Diff(A, DIRECTION=1, /EDGE_WRAP), DIRECTION=1, /EDGE_WRAP))[*,2:*]
Loading...