4 missing } at end of extPoints edited Apr 23 '13 at 17:02 bill s 56.2k33 gold badges8080 silver badges161161 bronze badges Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)}} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). It is however not advisable to set fitOrder to very high numbers, so this method will work reaonably well for relatively smooth data. If your data oscillates rapidly, it may make sense to pick a different set of basis functions for regression, such as trigonometric functions (Sin, Cos). Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). It is however not advisable to set fitOrder to very high numbers, so this method will work reaonably well for relatively smooth data. If your data oscillates rapidly, it may make sense to pick a different set of basis functions for regression, such as trigonometric functions (Sin, Cos). Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)}} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). It is however not advisable to set fitOrder to very high numbers, so this method will work reaonably well for relatively smooth data. If your data oscillates rapidly, it may make sense to pick a different set of basis functions for regression, such as trigonometric functions (Sin, Cos). 3 deleted 2 characters in body edited Apr 23 '13 at 17:01 Leonid Shifrin 99.4k1313 gold badges289289 silver badges383383 bronze badges Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). It is however not advisable to set fitOrder to very high numbers, so this method will work reaonably well for relatively smooth data. If your data oscillates rapidly, it may make sense to pick a different set of basis functions for regression, such as trigonometric functions (Sin, Cos). Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). It is however not advisable to set fitOrder to very high numbers, so this method will work reaonably well for relatively smooth data. If your data oscillates rapidly, it may make sense to pick a different set of basis functions for regression, such as trigonometric functions (Sin, Cos). 2 deleted 2 characters in body edited Apr 23 '13 at 16:55 Leonid Shifrin 99.4k1313 gold badges289289 silver badges383383 bronze badges Here is an alternative based on LinearModelFit and polynomial data fittingregression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). Here is an alternative based on LinearModelFit and polynomial data fitting. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). Here is an alternative based on LinearModelFit and polynomial regression. The idea is to fit your data to a polynomial of some degree, to find approximate extrema points of the smoothed data, and then locate nearest extremal points. Here is the code: ClearAll[findExtrema]; findExtrema[points_List, fitOrder_Integer, around_Integer: 5] := Module[{fit, fn, extrema, x, signs, extPoints}, fit = LinearModelFit[points, x^Range[0, fitOrder], x]; fn = fit["Function"]; extrema = NSolve[fn'[x] == 0, x, Reals]; signs = Sign[fn''[x] /. extrema]; extPoints = MapThread[ First@SortBy[Nearest[points, #, around], #2] &, {{x, fn[x]} /. extrema, signs /.{1 -> Last, -1 -> (-Last[#] &)} ]; Map[Pick[extPoints , signs, #] &, {1, -1}] ]  What happens here is that we fit the data with a polynomial of degree fitOrder, then find extremal points as zeros of the derivative, and determine the types of extrema by the sign of the second derivative. Having found extrema for the polynomial, we then find the nearest extremal points in the dataset. Here is a display function then: ClearAll[displayPoints] displayPoints[pts_, extr_] := ListPlot[pts, Epilog -> {PointSize[Medium], Red, Point[First@extr], Green,Point[Last@extr]}, ImageSize -> 700 ]  which we can use as displayPoints[#, findExtrema[#, 10, 10]] &[Transpose[{temptimelist, tempvaluelist}]]  You can play with the parameters fitOrder and around. Basically, fitOrder needs to be equal or greater than the number of "features" in your data (number of intervals where it is roughly monotonous). 1 answered Apr 23 '13 at 16:39 Leonid Shifrin 99.4k1313 gold badges289289 silver badges383383 bronze badges