0
$\begingroup$

I have some result in list form and after I build the graph I have: enter image description here As you can see on the graph there is a spurious peak at the beginning. So when I tried to find maximum peak, I receive value of first point which isn't correct. Code to find maximum:

Position[list1, Max[list1]][[1, 1]]

Than I tried to use another code to find this maximum:

Peaks1=tbl1[[FindPeaks[tbl1[[All, 2]]][[All, 1]]]]

Sort[Peaks1, #1[[1]] > #2[[1]] &][[2]]

Here I have another problem. For example for some data it's correct: enter image description here and in the same time for anoter data it's not correct enter image description here So my question is How can I ignore peak at the begining in the first case and What can I do in the second case to avoid incorrect result. Here is link on google disk with list and tables with datas

$\endgroup$
  • $\begingroup$ There's no way to automatically select the "right peak" from data that can't possible know to ignore the first peak. There needs to be some additional definition (outside of the data) to define what a peak is. FindPeaks can certainly help with the task but if the "right answer" is "I'll know it when I see it", then I'd say that's not a consistent or convincing approach. So what is the extra information that you have that would get the peak(s) that you want? $\endgroup$ – JimB Apr 4 at 22:45
2
$\begingroup$

Use FindPeaks

data1 = Import["/Users/roberthanlon/Downloads/1 graph.txt"] //
   ToExpression;

peak1 = MaximalBy[Rest@FindPeaks@data1, Last][[1]];

ListLinePlot[data1, PlotRange -> {0, 0.14}, Epilog ->
  {Red, AbsolutePointSize[4], Point[peak1]}]

enter image description here

data2 = "{" <> Import["/Users/roberthanlon/Downloads/2 graph.txt"] <> "}" // 
   ToExpression;

peak2 = MaximalBy[
    Rest@
     data2[[FindPeaks[data2[[All, 2]]][[All, 1]]]],
    Last][[1]];

ListLinePlot[data2, PlotRange -> {0, 0.06},
 Epilog ->
  {Red, AbsolutePointSize[4], Point[peak2]}]

enter image description here

data3 = "{" <> Import["/Users/roberthanlon/Downloads/3 graph.txt"] <> "}" // 
   ToExpression;

peak3 = MaximalBy[
    Rest@
     data3[[FindPeaks[data3[[All, 2]]][[All, 1]]]],
    Last][[1]];

ListLinePlot[data3, PlotRange -> {0, 0.05},
 Epilog ->
  {Red, AbsolutePointSize[4], Point[peak3]}]

enter image description here

$\endgroup$
2
$\begingroup$

FindPeaks finds peaks that do not fit your definition of a peak. But you haven't given any rule to define what constitutes a candidate peak, only that the "wrong peak" is sometimes chosen.

If what disqualifies a peak found by FindPeaks is that there needs to be at least one point to the left and right of the peak, then the following code will do that (using your first dataset):

(* Find potential peaks *)
peaks = FindPeaks[data]

(* Ignore peaks that don't have at least one value to the left or right of it *)
peaks = Select[peaks, 1 < #[[1]] < Length[data] &]

(* Select peak(s) with the maximum value of all peaks *)
peaks = Select[peaks, #[[2]] == Max[peaks[[All, 2]]] &]

(* Plot results *)
ListPlot[{data, peaks}, PlotStyle -> {Blue, {Red, PointSize[0.02]}}]

Peaks

This rule would also exclude any peak determined by FindPeaks to be at the very end of the list (although that is easily modified). I don't know if that fits your definition. But the point is that the definition needs to be given explicitly and then the code is written as opposed to finding code that seems to give you what you want.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.