Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of peaks in graph ? -graph analysis-

I have this curve that contains certain peaks - I want to know how to get the number of these peaks.

Sample Data:

0.10    76792
0.15    35578
0.20    44675
0.25    52723
0.30    27099
0.35    113931
0.40    111043
0.45    34312
0.50    101947
0.55    100824
0.60    20546
0.65    114430
0.70    113764
0.75    15713
0.80    83133
0.85    79754
0.90    17420
0.95    121094
1.00    117346
1.05    22841
1.10    95095
1.15    94999
1.20    18986
1.25    111226
1.30    106640
1.35    34781
1.40    66356
1.45    68706
1.50    21247
1.55    117604
1.60    114268
1.65    26292
1.70    88486
1.75    89841
1.80    49863
1.85    111938

The 1st column is the X values, the 2nd column is the y values.

I want to write a macro or formula that tell me how many peaks in this graph.

Note: this graph is actualy ploted and exported from matlab, so if there is a way i can tell my code to do it for me from matlab it would be also great!

like image 286
Zalaboza Avatar asked Jan 23 '12 22:01

Zalaboza


1 Answers

if your data was in A1:B36 then this formula =SUMPRODUCT(--(B2:B35>B1:B34),--(B2:B35>B3:B36))
returns 11 peaks

It checks if

  • B2 is higher than B1 and B3, if so counts it as a peak
  • then if B3 is higher than B2 and B4, if so counts it as a peak and so on

enter image description here

[Updated: VBA request added]

Sub GetMax()
    Dim chr As ChartObject
    Dim chrSeries As Series
    Dim lngrow As Long
    On Error Resume Next
    Set chr = ActiveSheet.ChartObjects(1)
    Set chrSeries = chr.Chart.SeriesCollection(1)
    On Error GoTo 0

    If chrSeries Is Nothing Then Exit Sub

    For lngrow = 2 To UBound(chrSeries.Values) - 1
        If chrSeries.Values(lngrow) > chrSeries.Values(lngrow - 1) Then
            If chrSeries.Values(lngrow) > chrSeries.Values(lngrow + 1) Then
                chrSeries.Points(lngrow).ApplyDataLabels
                With chrSeries.Points(lngrow).DataLabel
                    .Position = xlLabelPositionCenter
                    .Border.Color = 1
                End With
            End If
        End If
    Next
End Sub
like image 77
brettdj Avatar answered Nov 26 '22 12:11

brettdj