# Finding and integrating peaks in a data array

7 views (last 30 days)
Max on 12 Apr 2024
Answered: Star Strider on 12 Apr 2024
I have a 50,000 x 101 data array called "spc_real". Each row contains a 2D slice of spectral data containing a flat line interrupted by spikes (signals).
For each 2D slice, "spc_real(:,i)", I would like a block of code which finds the (i) peak locations, (ii) integrates the peaks, and (iii) determines the gap between each pair of peaks.
I then want these data (i)-(iii) to be inserted into separate data variables shown in the Workspace, with a name that indicates which column the variable belonged to. For example, (i) spc_real_peaklocation_1, (ii) peak_integrals_1, (iii) differences_1.
To find the peaks I am using "findpeaks()", to find the differences I am using "diff()", but I don't know how to integrate the peaks, nor do I know how to sequentially insert the results into correspondingly named files.
At the moment, I'm stuck here:
for c = 1:101
[peaks.c, locs.c] = findpeaks(spc_real(:,c),Btime,'MinPeakProminence',10^4);
peakInterval.c = diff(locs.c);
end
I know this is wrong. Rather than giving me 101 variables, it seems to overwrite the values each time through the loop.
Any help would be grately appreciated!

Star Strider on 12 Apr 2024
The findpeaks function will accurately locate the peaks. Integrating them depends on what you want to do. In the past, I have used the initial findpeaks information to locate the peak centres, and then fitted an appropriate function to them and integrated it using integral or the value of the integral function using the Symbolic Math Toolbox to code it (providing that it has an analytic integral). One example using this approach is How can I calculate the area under each peak / display the AUC on the graph?
Another option is to find the beginning and end of the peaks (and their corresponding independent variable values) and use trapz to do the integration.
Use the ‘locs’ output of findpeaks to find the differences in their locations. I always use the index result (not supplying the independent variable vector to findpeaks) and then index into the independent variable vector to get the locations or distances in terms of the independent variable.
With respect to writing the files, first use a table array to collect the necessary information, then use writetable to write ot to a text file or an Excel spreadsheet, as you wish.
.