# Find where the slope is greatest

I have an x-y data list. I now need to denoise the data and make it smooth. The next step is to find the position with the largest slope and determine the x coordinate of this point.

The data list is here [1]

– Alan
Commented Aug 9, 2023 at 13:37
• Commented Aug 9, 2023 at 14:23
• You'll probably want to run a low pass filter in your data, interpolate the results, and then use the interpolating function to calculate the derivative to find the highest slope. But derivatives of interpolating functions are often noisy, hence the methods in the links above. Commented Aug 9, 2023 at 14:24

(* Read data *)
data = Import["C:\\Users\\Desktop\\data.tsv", "Table"];
(* Work only with y data and smooth via SG method *)
smoothed = ListConvolve[SavitzkyGolayMatrix[{10}, 2], data[[All, 2]]];
(* Plot *)
ListLinePlot[{data[[All, 2]], smoothed}]


(* Use SG method to determine smoothed derivative SG method has unexpected inversion so add "-" *)
diffsmoothed = -ListConvolve[SavitzkyGolayMatrix[{10}, 2, 1], data[[All,2]]];
ListLinePlot[diffsmoothed, PlotRange -> All]
(* If you wanted largest slope regardless of sign use Abs[diffsmoothed] in the next line *)
data[[Ordering[diffsmoothed][[1]], 1]]


Answer is near -13.1125 but will vary with smoothing

• thank you very much! Commented Aug 10, 2023 at 11:36
• Hi, @OpticsMan. Can we use other smoothing methods? I want to get the location of this point of maximum slope, the more precise the better. Besides the SG method, is there another method? What do you recommend? Do we need to preprocess the raw data? Thanks Commented Aug 10, 2023 at 11:44
• @Mr.2023 Mathematica includes many other filter types Butterwortk, Chebyshev, etc. search "signalProcessing" in help. I use SG primarily due to my personal experience and its understandable polynomial nature and the easy ability to do derivatives with it. Often I am looking for slopes, peaks, troughs, etc. in "low frequency" spectral data and do not have specific concerns about the noise bandwidth etc as many filters can provide similar passbands regardless of optimization. Commented Aug 10, 2023 at 14:09
• Thanks for your warm and detailed help! Commented Aug 11, 2023 at 1:30
data = Import["C:/data.xlsx.tsv", "Table"][[4 ;;]];
filtered = MeanFilter[TimeSeries[data], 0.25];
ListLinePlot[{data, filtered}, PlotStyle -> {Green, Red}]


Now that denoising has been done:

f = Interpolation[filtered];
solmin = NMinimize[f'[t], {t, -15, 0}]


{-399.662, -2.70942}

solmax = NMaximize[f'[t], {t, -15, 0}]


{179.365, {t -> -13.0257}}

Plot[{f[t], f'[t]}
, {t, -15, 0}
, PlotStyle -> {Red, GrayLevel[0.6]}
, Epilog -> {
Blue, AbsolutePointSize[8],
Point@{ t /. Last@solmin, First@solmin}
, Point@{ t /. Last@solmax, First@solmax}
}
]