Your widths
parameter in find_peaks_cwt
is the problem.
from scipy.signal import find_peaks_cwt
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 15, 0.1)
y = np.sin(x)
fig0 = plt.figure()
ax0 = fig0.add_subplot(111)
ax0.plot(y)
peakinds = find_peaks_cwt(y, np.arange(1, 10)) # Here changed 5 to 10
ax0.plot(peakinds, y[peakinds], 'o')
plt.axis([0, 160, -1.1, 1.1])

From the documentation:
widths : sequence
1-D array of widths to use for calculating the CWT matrix. In general, this range should cover the expected width of peaks of interest.
Edit:
The default wavelet used is the Ricker wavelet. Basically, a convolution is performed between the signal and the wavelet at all specified widths
(by calling ricker(width[i])
. Therefore, the range you give has to go from small (to precisely localize the peak) to big enough (for detecting the peaks of interest) but not too big (to avoid aliasing - let's remember here that wavelet work in the frequency domain).
Excerpt of the documentation: The algorithm is as follows: 1 - Perform a continuous wavelet transform on vector, for the supplied widths. This is a convolution of vector with wavelet(width) for each width in widths.
If you change widths
by np.arange(10, 20)
, you will notice that the peaks are detected but their maximum is not well-localized (we are missing the fine scales). If you try again with np.arange(1, 20)
, the peaks are better localized.
Also, if you want to visualize the ricker wavelet:
from scipy.signal import ricker
vec = ricker(100, 10) # (nb_of_points, frequency)
fig0 = plt.figure()
ax0 = fig0.add_subplot(111)
ax0.plot(vec)
Edit 2:
As for the extra peak erroneously detected at the end of the signal, this is most probably due to the border effect. Basically, the window for the convolution goes beyond the last sample of the signal. Usually, a padding (zero-padding, signal wrapping,...) is done on the signal but depending on how it is done (or not done at all) this kind of error can occur. Discarding the first few and last points is generally appropriate when working with these types of methods.