I’ve found a great code which is built for finding peaks in arrays, which print the result in the console (Finding peaks and troughs in time series data in a 1D array - Javascript) . I would like to apply it on each pixel of an 8-day NDVI image collection. The result should be an image with the peak number in each pixel.
I tried to apply the .toArray() function to the image collection to obtain an array. Then I created a function “findPeaks” that should add a peak number Band to the image. Then I tried to apply this function to the array image using .map(findPeaks), but I get an error “array.map is not a function” Here you find the code https://code.earthengine.google.com/32e78cc57f87c05a76665ed0e8b6c720.
var aoi =
ee.Geometry.Polygon(
[[[11.111455811313702, 46.3205838600638],
[11.111455811313702, 46.31527834569152],
[11.11800040131004, 46.31527834569152],
[11.11800040131004, 46.3205838600638]]], null, false);
var ndvi_IC=ee.ImageCollection("LANDSAT/LC08/C01/T1_8DAY_NDVI")
.filterDate('2020-04-01','2020-10-01')
.filter(ee.Filter.bounds(aoi))
var array=ndvi_IC.toArray()
print(array)
//code from https://stackoverflow.com/questions/43567335/finding-peaks-and-troughs-in-time-series-data-in-a-1d-array-javascript
//I would like to obtain a new image presenting the number of peaks found for each pixel.
var findPeaks=function(array) {
var start = 1; // Starting index to search
var end = array.length - 2; // Last index to search
var obj = { peaks: [], troughs: [] };// Object to store the indexs of peaks/thoughs
for(var i = start; i<=end; i++)
{
var current = array[i];
var last = array[i-1];
var next = array[i+1];
if(current > next && current > last)
obj.peaks.push(i);
else if(current < next && current < last)
obj.troughs.push(i);
}
var peaksNum=obj.peaks.size()
return array.addBands(peaksNum);
}
var arraywithpeaksBand=array.map(findPeaks)
print(arraywithpeaksBand)
Thank you for your help, I’m new to GEE and coding.
Davide