Understanding how to do an Empirical Mode Decomposition I am trying to create my own code for Empirical Mode Decomposition and extraction of IMFs. 
As far as I understand, the first part of the process must be done like this:


*

*find local maxima and minima. I understand that I have to find all peaks and valleys of the time series. It will be a peak if a value is bigger than the previous and bigger than the next point. It will be a valley if a value is smaller than the previous and smaller than the next.

*then I must create a cubic spline connecting these points.
Here is my first problem. I have created a time series for a sinusoidal wave and found 20 points as peaks and 19 as valleys. This time series has 1000 values and covers 15 seconds of data.
Now I have several questions:


*

*Suppose the first peak is at t=1.2s and the last peak is at t=13s. I create the first cubic spline using the peaks. So, now this spline can be used o find any value for any time between 1.2 and 13s. But the data goes from t=0 to t= 14. What about the times outside the spline interval?

*I have a different number of points for maxima and minima, 20 peaks and 19 valleys. So, the spline representing the minima will have 19 valleys and will represent a different interval. In my case, from t=0.5 to t=13.8. What about times outside this interval?

*It is time now to calculate the "average spline", but I have to do it for the full time covered by the data, that is 15 seconds. How do I do that if the splines cover smaller intervals?
Please explain it like I am five years old. My math sucks at this time.
thanks
 A: I think it's common to just take the function value at each endpoint and consider it as a maximum and minimum. Then your piecewise function defined by the splines will be defined on the entire interval $[0,15]$. It doesn't matter than there are an unequal number of maxima and minima, at the end of this process we have an upper envelope (from the maxima) and a lower envelope (from the minima) which are defined on the entire interval $[0,15]$; the number of maxima or minima only defines how many "pieces" we have in our piecewise defined envelope.
Starting around slide 5 ish you can see the envelopes calculated.
A: The accepted answer states that

it's common to just take the function value at each endpoint and consider it as a maximum and minimum

However, in the original paper which introduced the Empirical Mode Decomposition [1], the authors mention that if this approach is taken,

[...] serious problems of the spline fitting can occur near the ends, where the cubic spline fitting can have large swings. Left by themselves, the end swings can eventually propagate inward and corrupt the whole data span especially in the low-frequency components.

The authors suggest the following approach for dealing with the boundaries:

We have adopted a method of adding characteristic waves at the ends which are defined by the two consecutive extrema for both their and amplitude of the added waves.

Other approaches have been proposed, such as the one described by Zeng and He [2]. In any case, it should be noted that just taking the ends as maxima/minima is generally not a good idea, as pointed out by Huang et al.
If you are merely interested in implementation details, then [3] might be a nice paper to read.

[1]: HUANG, Norden E., et al. The empirical mode decomposition and the Hilbert spectrum for nonlinear and non-stationary time series analysis. Proceedings of the Royal Society of London. Series A: mathematical, physical and engineering sciences, 1998, 454.1971: 903-995. (http://www.ccpo.odu.edu/~klinck/Reprints/PDF/huangPRSLA1998.pdf)
[2]: ZENG, Kan; HE, Ming-Xia. A simple boundary process technique for empirical mode decomposition. En IGARSS 2004. 2004 IEEE International Geoscience and Remote Sensing Symposium. IEEE, 2004. p. 4258-4261. (https://ieeexplore.ieee.org/abstract/document/1370076)
[3]: KIM, Donghoh; OH, Hee-Seok. EMD: A Package for Empirical Mode Decomposition and Hilbert Spectrum. R J., 2009, vol. 1, no 1, p. 40. (https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.649.7157&rep=rep1&type=pdf)
