0

I have Acceleration data I need to detect the positive acceleration peaks where valley is in between 0 and 5 and peak should be greater than 20. so far I used this code

import numpy as np
from scipy.signal import find_peaks, find_peaks_cwt
import matplotlib.pyplot as plt
import pandas as pd
import sys
np.set_printoptions(threshold=sys.maxsize)
op_col = []
for i in df['Speed']:
 op_col.append(i)
x = np.array([1, 9, 18, 24, 26, 5, 26, 25, 26, 16, 20, 16, 23, 5, 1, 27, 
22, 26, 27, 26, 25, 24, 25, 26, 3, 25, 26, 24, 23, 12, 22, 11, 15, 24, 11, 
26, 26, 26, 24, 25, 24, 24, 22, 22, 22, 23, 24])
peak, _ = find_peaks(x,height=24)
fig= plt.figure(figsize=(10,4))
plt.plot(x)
plt.plot(peak, x[peak], "x", color = 'r')

with the above code I got this output 1: edited response in this image the numbers mentioned peaks needs to be detected how can I do that using scipy

1 Answers1

1

Using itertools

import itertools
peaks=[max(it) for (sep,it) in  itertools.groupby(enumerate(x), lambda e: e[1]>5) if sep]

Then you could filter out the values under 20 (there are none here)

[p for p in peaks if p[1]>=20]

(You could combine both comprehension. But tho I like the fun of one-liner, over a line size it becomes obfuscation)

chrslg
  • 9,023
  • 5
  • 17
  • 31
  • I will try this definitely – aparna podili Dec 13 '22 at 02:52
  • with the above code got peaks values as tuples, when i tried second comprihension got this error "TypeError: '>=' not supported between instances of 'tuple' and 'int' " – aparna podili Dec 13 '22 at 04:24
  • Yeah. Since your question did not say whether you wanted x or y position of the peaks (if you wanted, for point 1, x=4 or y=26), I gave you both. But indeed, then my second comprehension should filter on y, aka `p[1]`. Corrected. – chrslg Dec 13 '22 at 04:30
  • sir i edited my expected output of the curve check the image, the peaks should be,where lower end of the peak is in between 0 and 5 and upper end in between 18 and 23 it has to be detect peaks – aparna podili Dec 13 '22 at 05:22
  • can you give me reply – aparna podili Dec 13 '22 at 06:14