I have a transect with peaks and trough, and want to determine the peak values of both. The dataset has quite some noise so currently, the peaks do not return as a single value. I tried to smooth the data with a rolling mean, and even though the outcome is better than without smoothing, there are still multiple 'peaks'. [CSV file here]
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.signal import argrelextrema
from pandas import read_csv
from numpy import mean
from matplotlib import pyplot
import csv
df = pd.read_csv('transect2.csv', delimiter=',', header=None, names=['x', 'y'])
plt.plot(df['x'], df['y'], label='Original Height')
rolling = df.rolling(window=100)
rolling_mean = rolling.mean()
plt.xlabel('Distance')
plt.ylabel('Height')
plt.plot(rolling_mean['x'], rolling_mean['y'], label='Mean Height 100')
plt.legend(loc='upper left')
plt.show()
n=1000
ilocs_min = argrelextrema(rolling_mean.y.values, np.less_equal, order=n)[0]
ilocs_max = argrelextrema(rolling_mean.y.values, np.greater_equal, order=n)[0]
df.y.plot (color='gray')
df.iloc[ilocs_max].y.plot(style='.', lw=10, color='red', marker="v");
df.iloc[ilocs_min].y.plot(style='.', lw=10, color='green', marker="^");
Smoothing the data further will not represent the reality so either I can improve this smoothing or use a different smoothing function.