I have a noisy sinewave in varying frequencies. I save all the samples (in mV) of this sinewave in variable data_mV
I can find the peaks of my sinewave with the following function using find_peaks
from the scipy
python library
def find_peaks_in_signal(data_mV,sampling_rate,signal_frequency,plot):
if(debug_prints):
print("Sampling rate = ",sampling_rate)
print("Expected signal frequency = ",signal_frequency)
distance = (sampling_rate / signal_frequency) - 100
print("Distance = ",distance)
peaks, _ = find_peaks(data_mV, distance=distance)
if plot:
plt.plot(data_mV)
plt.plot(peaks, data_mV[peaks], "x")
plt.plot(np.zeros_like(data_mV), "--", color="gray")
plt.show()
return peaks
The result is:
As you can see I am able to detect the peaks quite nicely. My absolute goal is to be able to calculate Average Peak to Peak voltage of the sinewave. Perhaps someone could point me in the right direction on what is the most reliable way to do that?
Can this be done using scipy functions or scipy does not have this kind of thing implemented?
I too am trying to do this, and the best solution I have come up with is to take all the peaks, then invert the signal, and find the troughs. Then you can take the average of all the trough values and peaks and get your average pk-pk.
e.g.
import numpy as np
peaks, _ = find_peaks(data_mV, distance=distance)
troughs, _ = find_peaks([x*-1 for x in data_mV], distance=distance)
pk_list = []
tr_list = []
for peak in peaks:
pk_list.append(peaks[peak])
for trough in troughs:
tr_list.append(troughs[trough])
pkpk = np.average(pk_list)-np.average(tr_list)
however, it looks like numpy also has a pk-pk function, but it only provides the value of the maximum-min for the list, not the average peak-peak value, so its not quite as useful as the above.
Avery Louie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.