0
$\begingroup$

I'm trying to write code that imports experimental data, finds peaks in it and cuts data around specific peak for further analysis (Fourier transform & model fitting of spectra).

Currently, I'm still struggling with cutting of data around the peak (method taken from @BlacKow answer). While it finds the peaks all right, but:

  1. I can't figure out how to get only data around one peak instead of all of them (2nd one)
  2. Dimensions of resulting "cut peak data" show that it is somehow 3d array "peakSeries dimensions"

My code:

sidata = Import[sifile, {"Data", All, {1, 2}}];
ListPlot[sidata[[All, {1, 2}]], 
 PlotRange -> {{2.5*10^-11, 4*10^-11}, {-5000, 10000}}, 
 AxesOrigin -> {2.5*10^-11, -2000}]

peaks = FindPeaks[sidata[[All, 2]], 0, 0, 1000]
sidata[[#, {1, 2}]] & @@@ peaks
peakSeries = (Transpose[{sidata[[All, 1]], #}] &@
       sidata[[All, 2]])[[# - 2860 ;; # + 2860]] & /@ (#[[1]] & /@ 
     peaks);
Dimensions[peakSeries]


Out[Peaks]= {{12916, 22698.5}, {19253, 6545.5}, {25589, 2084.2}}
Out[Peaks with X locations]={{2.2577*10^-11, 22698.5}, {3.36549*10^-11, 6545.5}, {4.4731*10^-11, 
       2084.2}}
Out[peakSeries dimensions]={3, 5721, 2}

Data around 2nd peak. X-axis is time in seconds - i want to cut +/- 5ps around peak (+/-2860 data points).

TSV data file is available here - it's just long 3 column data file (1st col - time in seconds, second and third columns - amplitude).

$\endgroup$
2
  • $\begingroup$ Your data, as shown, has one big peak and then lots of smaller peaks some of which may be noise. How do you distinguish between a peak and noise? $\endgroup$
    – Hugh
    Jul 15 at 8:28
  • $\begingroup$ Code-wise it's just a simple setting of minimum height of 1000. Just by looking at the data we see three peaks separated by identical time distances at 22.6 ps, 33.7 ps and 44,7 ps (difference of 11-11.1 ps) $\endgroup$ Jul 15 at 8:32
1
$\begingroup$

I more or less follow your code. fn is the list of files of type tsv.

fn = FileNames["*.tsv"];
sidata = Import[fn[[1]], {"Data", All, {1, 2}}];
ListPlot[sidata[[All, {1, 2}]], 
 PlotRange -> {{2.5*10^-11, 4*10^-11}, {-5000, 10000}}, 
 AxesOrigin -> {2.5*10^-11, -2000}]

enter image description here

This is how I would find the peaks and get the intervals around the peaks

peaks = FindPeaks[sidata[[All, 2]], 0, 0, 1000];
peakPositions = sidata[[#, {1, 2}]] & @@@ peaks;
peakIntervals = 
  sidata[[All, {1, 2}]][[# - 2860 ;; # + 2860]] & /@ peaks[[All, 1]];

I now replot your data and put vertical lines on the peak locations and overplot the three intervals you have found.

ListPlot[{sidata[[All, {1, 2}]], Sequence @@ peakIntervals},
 PlotRange -> {{2.*10^-11, 5*10^-11}, {-5000, 10000}}, 
 AxesOrigin -> {2.5*10^-11, -2000},
 Epilog -> {Pink, 
   InfiniteLine[{#, 0}, {0, 1}] & /@ peakPositions[[All, 1]]}]

enter image description here

I have widened up your plot range so we can see all the peaks. You seem to want the second peak which could be found from peakIntervals[[2]]

ListPlot[peakIntervals[[2]], PlotRange -> All]

enter image description here

Not sure I have answered your question... Hope that helps

$\endgroup$
7
  • $\begingroup$ Hugh - thank you for helping out. Your approach is a slightly easier to grasp than the one I mentioned. But I still have a question: Dimensions[peakIntervals] gives {3, 5721, 2} - whats located in second data plane(?) denoted by third number in Dimensions result? $\endgroup$ Jul 15 at 9:58
  • 1
    $\begingroup$ {3, 5721, 2} indicates that we have a list of lists. Three peaks have been selected, each of the three sublists contains 5721 coordinate pairs. The final 2 indicates that we are looking at coordinate pairs. If we had selected four peaks with x and y values for 15 coordinates the dimensions would read {4, 15, 2} . Does that help? $\endgroup$
    – Hugh
    Jul 15 at 10:06
  • $\begingroup$ Yes - thanks a lot, Hugh! $\endgroup$ Jul 15 at 10:08
  • 1
    $\begingroup$ I have written some notes on how to do numerical Fourier transforms here. You only use the ordinates in Fourier. You will do Fourier[sidata[[All,2]]] to get the Fourier transform. $\endgroup$
    – Hugh
    Jul 15 at 10:37
  • $\begingroup$ Cannot thank you enough :) $\endgroup$ Jul 15 at 10:39

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.