Find peaks in sine curve

Posted on 2011-04-19
Last Modified: 2012-05-11
What is the best way to find the peak points on a sine or sine-like curve?
Attached is some example data.
I'm not looking for code that reads in data, assume the data is in an array.

Question by:allelopath
    LVL 47

    Expert Comment


    I think you need to have some condition - say you want to find certain number of highest maxima or you want
    to find all maxima higher than some value

    Then you can scan all the pioints and find all locations where
     (value[j-1]<value[j] && value[j+1]<value[j])
    then you storea all such values (both x and values for each point)  say in the ArrayList
    and after you collected all of them analyze all these points
    using the condition specified - and end up with the list of peaks you need.

    LVL 47

    Accepted Solution


    This is the code:

    import java.util.ArrayList;
    import java.util.StringTokenizer;
    public class SineData {
        ArrayList x;
        ArrayList y;
        ArrayList peaksx;
        ArrayList peaksy;
        public SineData() {
             x = new ArrayList();
                y  = new ArrayList();;
                peaksx  = new ArrayList();;
               peaksy  = new ArrayList();;
            try {
                DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\test\\sinedata.txt"));
                String buff = null;
                while((buff=in.readLine()) != null){
                    StringTokenizer t = new StringTokenizer(buff);
                    x.add(new Double(t.nextToken()));
                    y.add(new Double(t.nextToken()));
                  double a0 =  ((Double)x.get(0)).doubleValue();
                   double b0 =  ((Double)y.get(0)).doubleValue();
                     double b1 =  ((Double)y.get(1)).doubleValue();
                if(b0 > b1) {
                     peaksx.add(new Double(a0));
                        peaksy.add(new Double(b0));
                for(int j=1; j<x.size()-1; j++){
                    double a = ((Double)x.get(j)).doubleValue();
                                 double b = ((Double)y.get(j)).doubleValue();
                             double bm1 = ((Double)y.get(j-1)).doubleValue();
                             double bp1 = ((Double)y.get(j+1)).doubleValue();
                    if(b > bm1 && b > bp1){
                        peaksx.add(new Double(a));
                        peaksy.add(new Double(b));
                   double ae =  ((Double)x.get(x.size()-1)).doubleValue();
                   double be =  ((Double)y.get(x.size()-1)).doubleValue();
                     double be1 =  ((Double)y.get(x.size()-2)).doubleValue();
                if(be > be1) {
                     peaksx.add(new Double(ae));
                        peaksy.add(new Double(be));
            } catch(Exception ex){
                  System.out.println("error " + ex.toString());
        public static void main(String [] args){
            new SineData();

    Open in new window

    This is the result:

    [45000.0, 65000.0, 85000.0, 105000.0, 125000.0, 145000.0, 160000.0]
    [10.0, 10.01, 10.005, 9.99987612345675, 10.02, 10.0, 4.99999999999999]

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    zeroMAx challenge 20 66
    maxMirror challenge 10 68
    hasOne  challenge 59 64
    network + 7 57
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now