C # simple optimization of the peak searching algorithm (including edge peak, minimum peak, and peak distance ),

Source: Internet
Author: User

C # simple optimization of the peak searching algorithm (including edge peak, minimum peak, and peak distance ),
 

For the principle of the core peak searching algorithm, see Ronny. Link: Find the peak of the projection curve,

C # For the translation principle code, see sowhat4999. Link: C # translate the findpeaks method in Matlab

The predecessors planted trees, and future generations enjoyed the cold. Thank you for your explanation.

 

 

 

Paste the translation code here (slightly modifying several parameters in sowhat4999 code)

// Call the method List <double> data = new List <double> {25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7 }; list <int> index = getPeaksIndex (trendSign (oneDiff (Constant. data )));
// Algorithm private double [] oneDiff (List <double> data) {double [] result = new double [data. count-1]; for (int I = 0; I <result. length; I ++) {result [I] = data [I + 1]-data [I];} return result;} private int [] trendSign (double [] data) {int [] sign = new int [data. length]; for (int I = 0; I <sign. length; I ++) {if (data [I]> 0) sign [I] = 1; else if (data [I] = 0) sign [I] = 0; else sign [I] =-1;} For (int I = sign. length-1; I> = 0; I --) {if (sign [I] = 0 & I = sign. length-1) {sign [I] = 1;} else if (sign [I] = 0) {if (sign [I + 1]> = 0) {sign [I] = 1;} else {sign [I] =-1 ;}}return sign;} private List <int> getPeaksIndex (int [] diff) {List <int> data = new List <int> (); for (int I = 0; I! = Diff. length-1; I ++) {if (diff [I + 1]-diff [I] =-2) {data. add (I + 1) ;}} return data; // equivalent to the subscript of the original array}

 

The above method does not take the peak distance, edge front, and peak value into consideration, but it has given us a complete idea for future generations.

Analysis of the peak interval:

We can understand the above method as the peak-1 algorithm. When we need to complete the peak-2 seeking, we need to determine

Whether data [I] is greater than data [I + 1], data [I + 2], data [I-1], data [I-2]

Similarly, if the number of points is 100000 and the peak distance is 1000, A 100000 power operation is required, which obviously takes a lot of time for calculation.

During the optimization process, we cannot change the peak distance (that is, the power index is 1000), but we can change the number of points (that is, the base number is 100000. This reduces the computing workload.

 

The above method for finding the peak from 1 has completed the judgment.

Whether data [I] is greater than data [I + 1], data [I-1]

Return the index column corresponding to the peak value.

 

When the peak distance is 2, we only need to judge the content in the index column again (only the point that wins when the peak distance is 1, only when the peak distance is 2)

Whether data [I] is greater than data [I + 2], data [I-2]

At this point, you will find that the base number we need to traverse is not the number of the origin number 100000, but the number of the peak-seeking series returned last time.

 

// Call the method List <double> data = new List <double> {25, 8, 15, 5, 6, 10, 10, 3, 1, 20, 7 }; // The topic List <int> index = getPeaksIndex (trendSign (oneDiff (Yaxis) obtained when the peak value is 1 ))); // judgment int level = 1; // The algorithm for expanding the peak range while (DisPeak> level) {level ++; List <int> result = DoPeakInstance (Yaxis, index, level); index = null; index = result ;}
 

// Extend the peak searching range Algorithm
Private List <int> DoPeakInstance (List <double> data, List <int> index, int level)
{
// Equivalent to the subscript of the original array
List <int> result = new List <int> ();
For (int I = 0; I <index. Count; I ++)
{
// Determine whether the upper and lower bounds are exceeded
If (index [I]-level> = 0 & index [I] + level <data. Count)
{
Double aa = data [index [I] + level];
Double bb = data [index [I];
Double cc = data [index [I]-level];
If (data [index [I] + level] <= data [index [I] & data [index [I]-level] <= data [index [I])
{
Result. Add (index [I]);
}
}
}
Return result;
}

 

Bian Feng situation analysis:

After carefully reading the above two algorithms, you will find that the algorithm has an unavoidable problem, such:

The peak interval is 3. At this time, the first vertex order of the peak (point 0, point 1, and point 2) is not involved in the peak computing because of the forward comparison failure. The tail point is not involved in the peak value calculation because it cannot be compared backward.

In this case, we must first understand that, because the above situations are not involved in the comparison, the first has only one peak, and the tail has only one peak.

Then we can add it.

// Obtain the maximum peak values (0, DisPeak) on both sides of the data at the beginning and end, and (Date. countFJ-DisPeak, Data. count) int TopIndex = 0; int BottomIndex = Yaxis. count-1; for (int I = 0; I <DisPeak; I ++) {if (Yaxis [I]> = Yaxis [TopIndex]) {TopIndex = I ;} if (Yaxis [Yaxis. count-1-I]> = Yaxis [BottomIndex]) {BottomIndex = Yaxis. count-1-I ;}/// determine whether the condition search conditions are met (comparison is performed at the first point backward, comparison is performed at the end point forward, and comparison is performed at DisPeak points) int newTopIndex = TopIndex; int newBottomIndex = BottomIndex; for (int I = 0; I <= DisPeak; I ++) {if (Yaxis [TopIndex + I]> = Yaxis [TopIndex]) {newTopIndex = TopIndex + I;} if (Yaxis [BottomIndex-I]> = Yaxis [BottomIndex]) {newBottomIndex = BottomIndex-I ;}} TopIndex = newTopIndex; BottomIndex = Taobao; // Add to the result sequence if (TopIndex <= DisPeak) {index. insert (0, TopIndex);} if (BottomIndex> = Xaxis. count-DisPeak) {index. add (BottomIndex );}

 

 

Finally, it is the simplest peak value judgment. Just a bit better.

// Minimum peak value int minPeakValue = 10; List <int> finalresult = new List <int> (); for (int I = 0; I <index. count; I ++) {if (Yaxis [index [I]> = minPeakValue) {finalresult. add (index [I]) ;}} index = null; index = finalresult;
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.