comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » derivative function in IDL similar as DIFF in matlab
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
derivative function in IDL similar as DIFF in matlab [message #86461] Tue, 12 November 2013 02:08 Go to next message
Jie Zhou is currently offline  Jie Zhou
Messages: 27
Registered: February 2014
Junior Member
Is there a derivative function in IDL similar as DIFF in matlab? I think the DERIV is different from DIFF.
Re: derivative function in IDL similar as DIFF in matlab [message #86462 is a reply to message #86461] Tue, 12 November 2013 02:27 Go to previous messageGo to next message
lecacheux.alain is currently offline  lecacheux.alain
Messages: 325
Registered: January 2008
Senior Member
Le mardi 12 novembre 2013 11:08:11 UTC+1, Jie Zhou a écrit :
> 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.
Re: derivative function in IDL similar as DIFF in matlab [message #86464 is a reply to message #86461] Tue, 12 November 2013 04:01 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
On Tuesday, November 12, 2013 5:08:11 AM UTC-5, Jie Zhou wrote:
> 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.
Re: derivative function in IDL similar as DIFF in matlab [message #86465 is a reply to message #86464] Tue, 12 November 2013 04:07 Go to previous messageGo to next message
lecacheux.alain is currently offline  lecacheux.alain
Messages: 325
Registered: January 2008
Senior Member
Le mardi 12 novembre 2013 13:01:15 UTC+1, Matthew Argall a écrit :
> On Tuesday, November 12, 2013 5:08:11 AM UTC-5, Jie Zhou wrote:
>
>> 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.
Re: derivative function in IDL similar as DIFF in matlab [message #86466 is a reply to message #86464] Tue, 12 November 2013 04:08 Go to previous messageGo to next message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
On Tuesday, November 12, 2013 7:01:15 AM UTC-5, Matthew Argall wrote:
> On Tuesday, November 12, 2013 5:08:11 AM UTC-5, Jie Zhou wrote:
>
>> 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]
Re: derivative function in IDL similar as DIFF in matlab [message #86467 is a reply to message #86466] Tue, 12 November 2013 04:18 Go to previous messageGo to next message
Jie Zhou is currently offline  Jie Zhou
Messages: 27
Registered: February 2014
Junior Member
On Tuesday, November 12, 2013 1:08:10 PM UTC+1, Matthew Argall wrote:
> On Tuesday, November 12, 2013 7:01:15 AM UTC-5, Matthew Argall wrote:
>
>> On Tuesday, November 12, 2013 5:08:11 AM UTC-5, Jie Zhou wrote:
>
>>
>
>>> 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
Re: derivative function in IDL similar as DIFF in matlab [message #86468 is a reply to message #86467] Tue, 12 November 2013 06:07 Go to previous message
Matthew Argall is currently offline  Matthew Argall
Messages: 286
Registered: October 2011
Senior Member
> 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:*]
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: writing fixed-length string arrays to netcdf-4
Next Topic: negative array index in IDL8

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:45:57 PDT 2025

Total time taken to generate the page: 0.00512 seconds