0

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()
1
  • ndarray.argmax and ndarray.argmin Commented Jun 3, 2022 at 10:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

In numpy, vectorization is much faster than a for loop, so you should try to avoid using for loop.
thank you for your answer, but you can see with this picture that if I use the loop for it gives me false results.
@yassinedhifaoui can't see the picture :|
Sorry, but I don’t know how to put the picture here please you can launch this program and see the result to chat. plt.axvline(res_max,color='red') plt.axvline(res_min,color='red')
i.sstatic.net/EZsWj.png You can see here this picture
|
0

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')

enter image description here

1 Comment

I have the data,I work for this program to watch this first minimal and the first maximum.But with double dip autocorrelation it does not work.But in simple dip he give me this result.Can you do something for me? Can't see the picture: i.stack.imgur.com/FVC3J.png AND THIS FOR SIMPLE DIP : i.stack.imgur.com/NrCWA.png

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.