if I have a picture like Result of the autocorrelation function: and I need to find my first maximum as well as my first minimum
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 6, 1000)
sig = np.sin(2 * np.pi * x)
ACF = np.correlate(sig, sig, mode='full')
res_ACF = ACF[len(sig)-1:]
res_ACF /= np.max(res_ACF)
plt.subplot(211)
plt.plot(x, sig, lw=0.5, label='signal originel')
plt.legend()
plt.subplot(212)
plt.plot(x, res_ACF, lw=3, label='ACF_with_np_correlate_in_full_mode')
plt.legend()
plt.show()
CodePudding user response:
2 simple for loops can do that
for i in range(1,len(res_ACF)-1):
if res_ACF[i-1] > res_ACF[i] < res_ACF[i 1]:
print('first minima: ', i, res_ACF[i])
break
for i in range(1,len(res_ACF)-1):
if res_ACF[i-1] < res_ACF[i] > res_ACF[i 1]:
print('first maxima: ', i, res_ACF[i])
break
yes, you can optimize it to use single for-loop.
CodePudding user response:
argmin
/argmax
return the index of the min/max value:
plt.axvline(x[res_ACF.argmax()], c='r')
plt.axvline(x[res_ACF.argmin()], c='r')