probably a stupid question: MatLab equivalent of "diff" ?
![](https://secure.gravatar.com/avatar/0e31f673b2f054ea61da64e35bd52cc0.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/64d7caa0e3bf58428afc9c729765988f.jpg?s=120&d=mm&r=g)
I may be misunderstanding you but isn't what you want just: arr[1:]-arr[:-1] If you only want positive differences it's: (arr[1:]-arr[:-1]) > 0 or negative: (arr[1:]-arr[:-1]) < 0 These should be very fast. --Tom On 12/29/06, Stef Mientki <s.mientki@ru.nl> 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.
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 _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/64d7caa0e3bf58428afc9c729765988f.jpg?s=120&d=mm&r=g)
Ignore, mine. Otto's answer is better. I didn't see it. On 12/29/06, Tom Denniston <tom.denniston@alum.dartmouth.org> wrote:
I may be misunderstanding you but isn't what you want just:
arr[1:]-arr[:-1]
If you only want positive differences it's:
(arr[1:]-arr[:-1]) > 0
or negative:
(arr[1:]-arr[:-1]) < 0
These should be very fast.
--Tom
On 12/29/06, Stef Mientki <s.mientki@ru.nl> 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.
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 _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/0e31f673b2f054ea61da64e35bd52cc0.jpg?s=120&d=mm&r=g)
thanks Tom, Otto,Lev, for the fast respons, but the problem still exists (btw I'm fairly newbie, for those didn't discovered that ;-). - I import SciPy - I do some calculations to find specific events, and the last calculation is BP_detect = ( dBP_dt > 0 ) & ( IOO > 40000 ) - now this is really great (compared to MatLab), because the resulting array is a real boolean array, so it only consists of True and False and presumably only takes 8 bits per value ??? - now if I run some standard diff formule, I'll detect both the edges, I expect that is due to the True/False elements of the array, probably they are internally translated to +1 and -1 ??? - even this gives both edges BP_peaks = numpy.diff ( BP_detect) > 0 - in the meanwhile I found a algorithm that works, but I find it very ugly ( I process the data in chunks, therefor I use and save the last sample "BP_peak_prev") BP_peak = logical_and( concatenate(( ([BP_peak_prev]), BP_detect[:-1])), logical_not(BP_detect) ) BP_peak_prev = BP_detect[-1] Any Ideas ? thanks, Stef Mientki Tom Denniston wrote:
Ignore, mine. Otto's answer is better. I didn't see it.
On 12/29/06, Tom Denniston <tom.denniston@alum.dartmouth.org> wrote:
I may be misunderstanding you but isn't what you want just:
arr[1:]-arr[:-1]
If you only want positive differences it's:
(arr[1:]-arr[:-1]) > 0
or negative:
(arr[1:]-arr[:-1]) < 0
These should be very fast.
--Tom
On 12/29/06, Stef Mientki <s.mientki@ru.nl> 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.
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 _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/64d7caa0e3bf58428afc9c729765988f.jpg?s=120&d=mm&r=g)
It would help if you could give a concrete example with a small set of data as below to demonstrate the problem: In [4]: numpy.diff(numpy.array([1,2,3,4,7])) Out[4]: array([1, 1, 1, 3]) In [5]: numpy.diff(numpy.array([1,2,1,3,2,4,7])) Out[5]: array([ 1, -1, 2, -1, 2, 3]) In [6]: numpy.diff(numpy.array([1,2,1,3,2,4,7]))>0 Out[6]: array([True, False, True, False, True, True], dtype=bool) On 12/29/06, Stef Mientki <s.mientki@ru.nl> wrote:
thanks Tom, Otto,Lev, for the fast respons, but the problem still exists (btw I'm fairly newbie, for those didn't discovered that ;-).
- I import SciPy
- I do some calculations to find specific events, and the last calculation is BP_detect = ( dBP_dt > 0 ) & ( IOO > 40000 )
- now this is really great (compared to MatLab), because the resulting array is a real boolean array, so it only consists of True and False and presumably only takes 8 bits per value ???
- now if I run some standard diff formule, I'll detect both the edges, I expect that is due to the True/False elements of the array, probably they are internally translated to +1 and -1 ???
- even this gives both edges BP_peaks = numpy.diff ( BP_detect) > 0
- in the meanwhile I found a algorithm that works, but I find it very ugly ( I process the data in chunks, therefor I use and save the last sample "BP_peak_prev")
BP_peak = logical_and( concatenate(( ([BP_peak_prev]), BP_detect[:-1])), logical_not(BP_detect) ) BP_peak_prev = BP_detect[-1]
Any Ideas ? thanks, Stef Mientki
Tom Denniston wrote:
Ignore, mine. Otto's answer is better. I didn't see it.
On 12/29/06, Tom Denniston <tom.denniston@alum.dartmouth.org> wrote:
I may be misunderstanding you but isn't what you want just:
arr[1:]-arr[:-1]
If you only want positive differences it's:
(arr[1:]-arr[:-1]) > 0
or negative:
(arr[1:]-arr[:-1]) < 0
These should be very fast.
--Tom
On 12/29/06, Stef Mientki <s.mientki@ru.nl> 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.
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 _______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/0e31f673b2f054ea61da64e35bd52cc0.jpg?s=120&d=mm&r=g)
hi Tom, Tom Denniston wrote:
It would help if you could give a concrete example with a small set of data as below to demonstrate the problem:
# the orginal array a = array([True,True,False,False,True,True])
B = numpy.diff(a) B array([False, True, False, True, False], dtype=bool) # now elements B[1] and B [3] are true, so both edges
# that doesn't seem to be unlogical, if we try to subtract booleans True - False 1 False - True -1
And now I guess that both -1 and +1 are translated into True, and can't be distinguished anymore :-(
# and now also this doesn't work: again 2 edges a[1:] - a[:-1] array([False, True, False, True, False], dtype=bool) a[1:] - a[:-1] > 0 array([False, True, False, True, False], dtype=bool)
# So the first thing that worked: a[1:] & ~(a[:-1]) array([False, False, False, True, False], dtype=bool)
And that was (besides the history element) the solution I posted in the previous mail. Due to some typing error, I thought my editor didn't except the "&" and "~ ", and therefore I used the longer logical_and, logical_not. Now I can live quiet well with this, but as Newbie I want to know that's the correct way, or are their better ways ? thanks, Stef
![](https://secure.gravatar.com/avatar/64d7caa0e3bf58428afc9c729765988f.jpg?s=120&d=mm&r=g)
Does this help? In [5]: numpy.diff(numpy.array([True,True,False,False,True,True], dtype=int)) Out[5]: array([ 0, -1, 0, 1, 0]) or if a is already defined: In [7]: numpy.diff(numpy.array([True,True,False,False,True,True], dtype=int)) Out[7]: array([ 0, -1, 0, 1, 0]) In [8]: a=numpy.array([True,True,False,False,True,True]) In [9]: a Out[9]: array([True, True, False, False, True, True], dtype=bool) In [10]: a.astype(int) Out[10]: array([1, 1, 0, 0, 1, 1]) In [11]: numpy.diff(a.astype(int)) Out[11]: array([ 0, -1, 0, 1, 0]) On 12/29/06, Stef Mientki <s.mientki@ru.nl> wrote:
hi Tom,
Tom Denniston wrote:
It would help if you could give a concrete example with a small set of data as below to demonstrate the problem:
# the orginal array a = array([True,True,False,False,True,True])
B = numpy.diff(a) B array([False, True, False, True, False], dtype=bool) # now elements B[1] and B [3] are true, so both edges
# that doesn't seem to be unlogical, if we try to subtract booleans True - False 1 False - True -1
And now I guess that both -1 and +1 are translated into True, and can't be distinguished anymore :-(
# and now also this doesn't work: again 2 edges a[1:] - a[:-1] array([False, True, False, True, False], dtype=bool) a[1:] - a[:-1] > 0 array([False, True, False, True, False], dtype=bool)
# So the first thing that worked: a[1:] & ~(a[:-1]) array([False, False, False, True, False], dtype=bool)
And that was (besides the history element) the solution I posted in the previous mail. Due to some typing error, I thought my editor didn't except the "&" and "~ ", and therefore I used the longer logical_and, logical_not.
Now I can live quiet well with this, but as Newbie I want to know that's the correct way, or are their better ways ?
thanks, Stef
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
![](https://secure.gravatar.com/avatar/375db2b141a23559e312dfd2b983d1c5.jpg?s=120&d=mm&r=g)
Quoting Stef Mientki <s.mientki@ru.nl>:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array.
Then it is the numpy.diff function you'r looking for. In [19]:import numpy In [20]:numpy.diff([1,2,4,1]) Out[20]:array([ 1, 2, -3]) Otto
![](https://secure.gravatar.com/avatar/208ce48ff62531986b1602bfe054c94e.jpg?s=120&d=mm&r=g)
Received from Otto Tronarp on Fri, Dec 29, 2006 at 11:26:06AM EST:
Quoting Stef Mientki <s.mientki@ru.nl>:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array.
Then it is the numpy.diff function you'r looking for.
In [19]:import numpy In [20]:numpy.diff([1,2,4,1]) Out[20]:array([ 1, 2, -3])
Otto
Moreover, if you want to ignore the rising edges, you can use something like this: a = <array of some integers> da = diff(a) where(da>=0,zeros(shape(da),dtype=int),-ones(shape(da),dtype=int)) L.G.
participants (4)
-
Lev Givon
-
Otto Tronarp
-
Stef Mientki
-
Tom Denniston