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

There are 1 answers

5
chrslg On

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)