[SciPy-user] probably a stupid question: MatLab equivalent of "diff" ?

Tom Denniston tom.denniston at alum.dartmouth.org
Fri Dec 29 11:26:16 CST 2006


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 at 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 at 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 at 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 at scipy.org
> >>> http://projects.scipy.org/mailman/listinfo/scipy-user
> >>>
> >>>
> > _______________________________________________
> > SciPy-user mailing list
> > SciPy-user at scipy.org
> > http://projects.scipy.org/mailman/listinfo/scipy-user
> >
> >
> >
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-user mailing list