-2

I have two numpy arrays x and y, I have plotted a curve with these values. Now I want to find all the values of x where local maxima exists on that curve. How it will be done?

Please give a method to fit the best possible curve with these x and y values and the values of x (or the indices of the value of x in array x) where local maxima exists.

prtkp
  • 537
  • 1
  • 6
  • 8
  • Can you provide an example data set? What have you tried so far? – Cleb Jun 25 '15 at 07:28
  • 1
    Unless you define what a `best possible curve` is, your question is meaningless. – cel Jun 25 '15 at 09:07
  • @cel : best possible curve means, The curve plotted by taking those x and y values. I want those x where local maximum occurs. – prtkp Jun 25 '15 at 09:49
  • @Cleb : can u give ur email id so that i can mail u the data set. the data set is huge (2287 x and y values). – prtkp Jun 25 '15 at 09:51
  • Then just post a subset of your data or a minimal example – Cleb Jun 25 '15 at 09:59
  • 25.04420302,6.4726 24.9237955,6.3794 24.80454022,6.2407 24.68642074,5.9546 24.5694209,6.02 24.45358465,6.3752 24.3387763,6.4521 24.22504094,6.5293 24.11236361,6.874 24.00072962,7.2593 23.89012454,7.4043 23.78053421,7.6499 23.67194472,8.1933 23.56434244,8.5998 23.45771395,8.9446 23.35204611,9.3696 23.24732598,9.8728 23.14354087,10.2693 23.04067832,10.5884 22.93872607,10.8491 22.83767211,11.0273 22.7375046,11.2046 22.63821194,11.351 22.53978272,11.373 22.44220571,11.4511 22.3454699,11.5783 22.24956446,11.5554 22.15447875,11.5808 22.06020229,11.6865 21.966773,11.7422 21.874084,11.7846 21.78,11.81 – prtkp Jun 26 '15 at 04:40
  • values are in (x,y) ordered pair. like x = 25.04420302,24.9237955,24.68642074 y = 6.4726,6.3794,6.2407 etc – prtkp Jun 26 '15 at 04:40
  • I added a simple example below which should be quite efficient. Please let me know whether it has to be revised; if not, feel free to accept the answer :) – Cleb Jun 26 '15 at 18:37

2 Answers2

0

I added code for a simple example below which finds all local minima and all local maxima of an array and stores them in minVal and maxVal, respectively. Please note: When you apply this to large datasets, make sure to smooth the signals first; otherwise you will end up with tons of extrema.

The data looks as follows; the calculated minima/maxima are at [2, 5, 7] and [1, 3, 6], respectively:

enter image description here

Here is the code:

import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

X=np.arange(9)
Y=np.array([ 3,  5,  1,  9,  3,  2, 10,  7,  8])
plt.plot(X,Y)
plt.show()
maxVal = argrelextrema(Y, np.greater) #(array([1, 3, 6]),)
minVal = argrelextrema(Y, np.less) #(array([2, 5, 7]),)
Cleb
  • 25,102
  • 20
  • 116
  • 151
0
x=np.array([6,3,5,2,1,4,9,7,8])
y=np.array([2,1,3,5,7,9,8,10,7])

sort_idx = np.argsort(x)
y=y[sort_idx]
x=x[sort_idx]
minm=np.array([],dtype=int)
maxm=np.array([],dtype=int)
length = y.size
i=0

while i < length-1:
    if i < length - 1:
        while i < length-1 and y[i+1] >= y[i]:
            i+=1

        if i != 0 and i < length-1:
            maxm = np.append(maxm,i)

        i+=1

    if i < length - 1:
        while i < length-1 and y[i+1] <= y[i]:
            i+=1

        if i < length-1:
            minm = np.append(minm,i)
        i+=1


print minm,maxm

array minm and maxm contain indices of minimas and maximas respectively...

prtkp
  • 537
  • 1
  • 6
  • 8