By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,739 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,739 IT Pros & Developers. It's quick & easy.

probably a stupid question: MatLab equivalent of "diff" ?

P: n/a
Does anyone know the equivalent of the MatLab "diff" function.
The "diff" functions calculates the difference between 2 succeeding
elements of an array.
I need to detect (fast) the falling edge of a binary signal.

There's derivate function in Python diff,
but when you use an binary (true/false) input,
it also detects rising edges.

Probably a stupid question,
but I still have troubles,
digging to huge amount of information about Python.

thanks,
Stef Mientki
Dec 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Stef Mientki wrote:
Does anyone know the equivalent of the MatLab "diff" function.
The "diff" functions calculates the difference between 2 succeeding
elements of an array.
I need to detect (fast) the falling edge of a binary signal.
Using numpy (or predecessors), you can do this easily with slicing:

a[1:] - a[:-1]

Slicing shares data, so the slices above don't create new arrays, but
new views into the old array, so it avoids that overhead.

For Python lists you'd have to use a list comprehension or some such
thing like that. For example:

[ x-y for (x,y) in zip(a[1:],a[:-1]) ]

Or, to avoid creating two new arrays, use iterators:

import itertools
[ x-y for (x,y) in
itertools.izip(itertools.islice(a,0,-1),itertools.islice(a,1)) ]
Carl

Dec 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.