Periodic splines to fit periodic data In a comment to this question, user @whuber cited the possibility of using a periodic version of splines to fit periodic data. I would like to know more about this method, in particular the equations defining the splines, and how to implement them in practice (I'm mostly an R user, but I can make do with MATLAB or Python, if the need arises). Also, but this is a "nice to have", it would be great to know about possible advantages/disadvantages with respect to  trigonometric polynomials fitting, which is how I usually deal with these kind of data (unless the response is not very smooth, in which case I switch to Gaussian Process with periodic kernel). 
 A: I was looking for an answer to this question recently and found the following solution, using the recent package splines2. There is a function to compute periodic m-splines (m-splines are normalized b-splines). Usage is very similar to the bs function. Let's say we have a 24-h noisy stationary signal, measured at fixed intervals over 2 days:
library(ggplot2)
library(splines2)

t <- seq(0, 48, length.out = 500)
y <- sin(time/2*pi/6) + rnorm(500, sd = 0.5)

df <- data.frame(t = t, y = y)

ggplot(df, aes(x = t, y = y)) + geom_point() + theme_minimal()


Now we can fit a periodic spline on this data and create predictions for our regular intervals:
# (boundary knots determine the period)
pspline_fit <- lm(y ~ mSpline(x = t, 
                              df = 4, 
                              periodic = TRUE, 
                              Boundary.knots = c(0, 24)), data = df)
df <- cbind(df, as.data.frame(predict(pspline_fit, interval = "prediction")))
pred_plot <- 
  ggplot(df, aes(x = t, y = y)) + 
  geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.4) + 
  geom_line(aes(y = fit), size = 1, colour = "blue") + 
  geom_point() + 
  theme_minimal()
pred_plot


And what's nice about the periodic spline is that there is no discontinuity at the 24h mark, which you can visualise using polar coordinates:
pred_plot + xlim(0, 24) + coord_polar()


