How do I fit a sine curve to my data with pylab and numpy?

Here is a parameter-free fitting function fit_sin() that does not require manual guess of frequency: import numpy, scipy.optimize def fit_sin(tt, yy): ”’Fit sin to the input time sequence, and return fitting parameters “amp”, “omega”, “phase”, “offset”, “freq”, “period” and “fitfunc””’ tt = numpy.array(tt) yy = numpy.array(yy) ff = numpy.fft.fftfreq(len(tt), (tt[1]-tt[0])) # assume uniform spacing Fyy … Read more

Android How to draw a smooth line following your finger

An easy solution, as you mentioned, is to simply connect the points with a straight line. Here’s the code to do so: public void onDraw(Canvas canvas) { Path path = new Path(); boolean first = true; for(Point point : points){ if(first){ first = false; path.moveTo(point.x, point.y); } else{ path.lineTo(point.x, point.y); } } canvas.drawPath(path, paint); } … Read more

Fitting a density curve to a histogram in R

If I understand your question correctly, then you probably want a density estimate along with the histogram: X <- c(rep(65, times=5), rep(25, times=5), rep(35, times=10), rep(45, times=4)) hist(X, prob=TRUE) # prob=TRUE for probabilities not counts lines(density(X)) # add a density estimate with defaults lines(density(X, adjust=2), lty=”dotted”) # add another “smoother” density Edit a long while … Read more