I am working on a project for school wherein I have to observe the movement of a certain object. I’ve received this graph that describes its motion after the first steps of analysis. I am however not sure as to how I can take the average of all the peaks and troughs I see here and get an Amplitude value that describes this particular set of conditions. I can’t simply take a mean of the values as equilibrium position, as there is a “drifting” of the baseline, which means I instead have to try and find “delta amplitude” for each spot, and average all these out for the amplitude of this set of conditions.
Image of the graph in question
I’ve begun trying to solve this problem in Python, and I run into one consistent issue. Even after smoothing the graph, the program I’ve written to calculate the average amplitude calculates the average of the inaccuracies of the graph ( values of a few pixel on the micro scale), instead of the actual movement of the object on the macro scale.
I ran my program on a perfect sinus curve, and it works as expected there, providing me the average amplitude as a value of displacement from position of equilibrium, as there are no miniscule movements that cause peaks and troughs that get recognized as local minima / maxima by my code.
The following code is the function I currently am using to get the amplitude. Selected Amplitude is just a subset of the data, and is arbitrary for the question.
# Amplitude calculation function for smoothed data
def calculate_smoothed_amplitude(start, end):
selected_time = time[start:end+1]
selected_amplitude = smoothed_amplitude[start:end+1]
if len(selected_amplitude) < 3:
print("Selected range is too small to calculate amplitude.")
return 0
peaks = []
troughs = []
for i in range(1, len(selected_amplitude) - 1):
if selected_amplitude[i] > selected_amplitude[i-1] and selected_amplitude[i] > selected_amplitude[i+1]:
peaks.append((selected_time[i], selected_amplitude[i]))
if selected_amplitude[i] < selected_amplitude[i-1] and selected_amplitude[i] < selected_amplitude[i+1]:
troughs.append((selected_time[i], selected_amplitude[i]))
amplitudes = []
for i in range(1, len(troughs) - 1):
prev_peak = max([p[1] for p in peaks if p[0] < troughs[i][0]], default=None)
next_peak = min([p[1] for p in peaks if p[0] > troughs[i][0]], default=None)
if prev_peak is not None and next_peak is not None:
delta_amp = (prev_peak - troughs[i][1] + next_peak - troughs[i][1]) / 2
amplitudes.append(delta_amp)
if amplitudes:
average_amplitude = np.mean(amplitudes)
else:
average_amplitude = 0
return average_amplitude
Thank you guys for the help !!
Aswath Krishna is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.