Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find peaks in sine curve

Posted on 2011-04-19
2
Medium Priority
?
368 Views
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.

sinedata.txt
0
Comment
Question by:allelopath
  • 2
2 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35428730

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.
 

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35429008

This is the code:

import java.io.DataInputStream;
import java.io.FileInputStream;
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()));

            }

            in.close();
              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());
            ex.printStackTrace();

    }
System.out.println(peaksx.toString());
        System.out.println("");
           System.out.println("");

        System.out.println(peaksy.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

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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