This is as days pass by, by Stuart Langridge

And this is Line smoothing in Python, written , and concerning LazyWeb, Software

I'm looking for a line smoothing algorithm in Python; specifically, something which will take the list of data points plotted in the top line in this image and convert it to the bottom line. I've got a list of data points to smooth; I'm not looking to smooth the image of the line. Anyone got any direct suggestions? I'm not much cop at maths things like this.

Comments

Davyd

Spline interpolation might give you the results you're after. Google has lots of resources.

Rich Wareham

The simplest would be a moving average smooth. Suppose your N-points are x(1) , x(2), x(3), ..., x(n), ..., x(N). Form the set of points x'(n) such that

x'(n) = (ax(n-1) + bx(n) + cx(n+1))/(a+b+c)

Which will be your smothed line. Usually a = b = c = 1 but you might get nicer results with a = c = 1 and b = 2.

--

Rich

chris procter

As Rich said a moving average is probably the way to go, averge the data point with the ones on either side and use that as the point to plot on the graph. If the result of that isn't smooth enough average the data point with the two (or three or whatever) on either side until it looks right to you, the more points you average the nearer the entire graph will be to a straight line.

You can also weight the surrounding points to be worth less then the data point which will make the graph less smooth again.

Basically unless you have a definite result you're trying to get (getting the graph to fit a model for example) just tinker with the window size (the number of points you are averaging) and the weighting until you get something that 'looks right'

Tom Edwards

Cubic spline interpolation is consider the best for point fitting if I recall my numerical methods correctly. It should also ensure that the first and second derivatives are continuous. (Polynomical fitting is another technique, though this can produce wild results near the end points).

Tom Edwards

By the way I have no idea what polynomical actually means, i think I mean polynomial

sparkes

As loads of people have suggested the average algorthim is quick to chuck together and allows you to get on with some more fun code without struggling with the maths. It's smooth enough to 'look' right even if it's not as mathimatically realistic as the more technical transformations. You could do what games programmers do and that would be to tweak the average code until it provides a slope similar to the more cpu intensive transformations. Or even go all out and pregenerate a whole massive lookup table using some nasty fortran downloaded from somebodies phd and then use that ;-)

If you want to do the maths I pretty sure numarray in numeric does this. I am pretty sure I used it for 3d transformations.

or alternatively just use http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/457658 which I don't understand at all :-)

Tom West

How did you make out on this? I have a more or less similar need to smooth a polygon enclosing objects. The polygon is created by mouse clicks to include and exclude the objects of interest.

Tnx.

Tom

sil

Tom West: blimey, I don't remember. I'm not even sure what I needed this for now. Sorry!

This website belongs to Stuart Langridge. Contact details are available. Don't eat yellow snow. Valid HTML5, at least in theory, except for the bits that aren't because I'm that futuristic that I'm ahead of the spec, oh yes. HTML5 help from Bruce Lawson, among others. Fonts from the superb FontSquirrel. End.