some options in R would be
t=seq(0,10,0.01)
y=sin(t)+rnorm(length(t))
plot(t,y,cex=0.1)
# puts a loess smoother through the points
lines(loess.smooth(t,y),col=2)
library(mgcv)
# puts a spline through the points - read Simon Woods work to learn more about this
g<-gam(y~s(t),data=data.frame(t=t,y=y))
lines(t,g$fitted.values,col=3)
and for your recently added data
t=1:400
y=rep(NA,length(t))
y[10]<-30
y[111]<-100
y[171]<-128
y[181]<-86
y[201]<-42
y[211]<-44
y[281]<-39
y[321]<-59
y[341]<-20
y[351]<-4
library(mgcv)
g<-gam(y~s(t),data=data.frame(t=t,y=y))
lm<-predict.gam(g,data.frame(t=t,y=y))
plot(t,y,cex=0.5,pch=16,ylim=c(min(lm),max(lm)))
lines(t,lm,col='red')
The first think you need to decide on is what is a spike and what is not.
Consider the following graphs, surely there are spikes on left-most one, but what about the right one? If these are spikes, then how would you smooth that graph without increasing the sampling frequency? If these are not spikes, then what about the graphs in the middle, where do you put the threshold?
![spikes graph](https://i.stack.imgur.com/xSooO.png)
If your data is not too big, then as a first approach I would recommend using Fourier transform - for example by cutting the high frequencies you make your graph less jagged and you should be able to keep the significant spikes. Of course, some especially thin spikes might get removed, but this is, among others, what "less jagged" means.
I hope this helps $\ddot\smile$
Best Answer
In Wolfram Mathematica 13.0, after defining the following function:
just write:
to obtain:
which is "the best function" (at least according to Mathematica algorithms) that approximates the points of the approximate starting graph (in black the start graph, almost completely superimposed by the red end graph).
The power of
FindFormula
is that it almost instantly manages to identify the type of function that best approximates the experimental data among a multitude (obtainable by sum, product, composition, inversion of basic functions, i.e. polynomials, trigonometric, exponential functions). In fact, having decided to approximate the data with a ninth degree polynomial, it's sufficient to apply the least squares method:by which the coefficients are quickly calculated:
which are essentially the same as those obtained automatically.
Of course, no one forbids to settle for a polynomial with a lesser degree, essentially this depends on the circumstances in which one operates. Anyway, by adding this other simple code:
you can visually realize how "wrong" you are: