'finding peaks and peak interval for non-uniform waves

Big Update I found an inefficient, but working solution for finding peaks and peak interval, although it doesn't work for multiple groups(in the code below I used a subset of 1 of the groups).

for(i in 2:nrow(data)){
  if(data$Testnr[i] != data$Testnr[i-1]){
    data$index[i] = 1
  }else{
    data$index[i] = data$index[i-1]+1
  }
}
subdata=subset(data,data$Testnr=="T1_1S")
peaks0= findPeaks(subdata$Sensor0,thresh=300)
peakdata=data.frame(Sensor0=subdata$Sensor0[peaks0],
Testnr=subdata$Testnr[peaks0],index=maxdata$index[peaks0])

I'm working with a dataset looking at wave patterns where multiple experiments have been combined. in the experiments we looked at wave characteristics at multiple point in a tank. Because of that the peak of the wave is not registered on all sensors at the same time.

I'm quite new to programming, so I'm having a hard time propperly formulating my question.

The data looks like this:

head(data)
      Sensor0     Sensor1     Sensor2 Treatment Testnr RPM
1  0.01330145  0.02220079  0.03166394      T1_S  T1_1S 510
2  0.13280891  0.22166476  0.30203344      T1_S  T1_1S 510
3  0.65641299  1.09558630  1.41693014      T1_S  T1_1S 510
4  2.17661658  3.63057638  4.44955241      T1_S  T1_1S 510
5  5.52784774  9.20709684 10.74679125      T1_S  T1_1S 510
6 11.59723321 19.28116975 21.65056654      T1_S  T1_1S 510

#This continues for about 200k rows where the value of Treatment and Testnr differ

I am interested in the peaks and peak interval of the waves at the different sensors for each unique Testnr. Since the sensors picked up the same waves, al be it delayed from eachother, I only need 1 peak interval per Testnr. Unfortunately there are consistant small peaks (of very low value) in between the larger peaks.

Before I had heard of the findPeaks function I was looking at the top 10% of the waves for the peaks, but this is not a accurate depiction of the data of interest.

maxsensor0=data%>%
   group_by(Testnr)%>%
   filter(Sensor0>quantile(Sensor0,probs=0.9))


maxsensor1=data%>%
  group_by(Testnr)%>%
  filter(Sensor1>quantile(Sensor1,probs=0.9))
  
maxsensor2=data%>%
  group_by(Testnr)%>%
  filter(Sensor2>quantile(Sensor2,probs=0.9))

maxdata=data.frame(Sensor0=maxsensor0$Sensor0,Sensor1=maxsensor1$Sensor1,Sensor2=maxsensor2$Sensor2,Treatment=maxsensor0$Treatment,Testnr=maxsensor0$Testnr,RPM=maxsensor0$RPM)

In this code the original index of the selected data is lost, therefor it can't be used for the peak interval. To circumvent this I tried to add an index that would start at 0 at a new Testnr as a column:

for(i in unique(data$Testnr)){
  data$index<-seq(1,length(data$Sensor0),1)
}

But the sequence does not restart at 0 if the Testnr changes.

For the peaks

To avoid the small, low value waves I used the top 10% of the waves to find the peaks.

#I tried:
maxsensor0=data%>%
   group_by(Testnr)%>%
   filter(Sensor0>quantile(Sensor0,probs=0.9))%>%
   findPeaks(Sensor0,thresh = 0)

#and
maxsensor0=data%>%
   group_by(Testnr)%>%
   filter(Sensor0>quantile(Sensor0,probs=0.9))

maxdata0=maxsensor0%>%
  group_by(Testnr)%>%
  findPeaks(maxdata0$Sensor0,thresh = 0)

But none of them worked. The ideal outcome would look like this,(values are random in the example below):

     Sensor0     Sensor1     Sensor2    Treatment Testnr  RPM  index
1   450.013301  460.220079  455.166394      T1_S   T1_1S  510   200
20  455.132808  452.166476  453.203344      T1_S   T1_1S  510   324
124 520.656412  519.558630  511.693014      T2_S   T2_1S  810   240
435 482.661658  493.057638  489.955241      T2_S   T2_4S  660   10024
522 800.578477  799.709684  800.679125      T3_S   T3_2S  780   6960
900 900.597233  919.281169  911.056654      T4_S   T4_1S  780   820


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source