The python code is as follows. Obviously,this algorithm is able to work when the waveform/data is certain. I wonder how to call the data collected by the arduino and apply it to this algorithm in real time.
import numpy as np
def AMPD(data):
p_data = np.zeros_like(data, dtype=np.int32)
count = data.shape[0]
arr_rowsum = []
for k in range(1, count // 2 + 1):
row_sum = 0
for i in range(k, count - k):
if data[i] > data[i - k] and data[i] > data[i + k]:
row_sum -= 1
arr_rowsum.append(row_sum)
min_index = np.argmin(arr_rowsum)
max_window_length = min_index
for k in range(1, max_window_length + 1):
for i in range(k, count - k):
if data[i] > data[i - k] and data[i] > data[i + k]:
p_data[i] += 1
return np.where(p_data == max_window_length)[0]
import matplotlib.pyplot as plt
def sim_data():
N = 1000
x = np.linspace(0, 200, N)
y = 2 * np.cos(2 * np.pi * 300 * x)
return y
def vis():
y = sim_data()
plt.plot(range(len(y)), y)
px = AMPD(y)
plt.scatter(px, y[px], color="red")
plt.show()
vis()
I couldn't find a suitable solution for this question.