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

S

Stef Mientki

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
 
C

Carl Banks

Stef said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top