pan-tompkins algorithm

The difference equation of the differentiator used in pan-tompkins algorithm is as follows : ( a five-point derivative)

$$y \left( n T \right) = \frac{1}{8} T \left[-x \left(nT - 2 T \right) - 2x \left(nT - T \right)+ 2x \left(nT + T \right) + x \left(nT + 2T \right) \right]$$

Can someone explain to me from where they got it.

This is a "generic" Finite Difference (FD) five-point approximation to the first order derivative. You can find more information about the generic formulation of the FD derivative approximations in this link (see the attached article)

The general approach is to expand function in its Taylor series around the $$nT$$ point. You expand as many times as you want to get the sought amount of points. The points of evaluation will be your $$\left( nT - iT \right)$$ points, where $$i$$ is an index that shows "how far" from the point of expansion you are evaluating (for example $$i = 2$$ gives the last point in your formula, $$i = -2$$ gives the first and $$i = -1$$ the second. I hope you see the pattern).

As a last step you have to find a linear combination of all the truncated Taylor series corresponding to each evaluation point. This ends up being a system of equations that has to be solved which can be done with linear algebra tools, or you could of course try to solve the equations in any other way.

UPDATE

I have found some more time to provide more information. Please note that the presented derivation is a mix of information found on many sources, presented at the end of this answer.

I will change the argument of the function from $$\left( nT + iT \right)$$ to $$\left(n + h \right)$$ for the sake of simplicity. Where $$h$$ is the step in the free variable, equal to $$h = T$$ and here is considered to be constant and $$n$$ is the index of the sample. So if the central sample is $$n$$ then the previous is $$n - h$$ and the next $$n + h$$ and so on.

Denoting the derivative of a function $$x$$ with respect to $$t$$ as $$x_{t}$$, its second derivative with respect to $$t$$ as $$x_{tt}$$ and so on and assuming that these derivatives exist we have to show that the sought result is

$$x' \left( n \right) = \frac{-x \left(n + 2h\right) + 8 x \left(n + h \right) - 8 x \left(n - h \right) + x \left(n - 2h\right)}{12 h} \label{1} \tag{1}$$

where $$x'$$ denotes the first derivative of $$x$$ with respect to the free variable. So, we calculate the (truncated) Taylor series for the points $$\left\{ n - 2h, n - h, n + h, n + 2h \right\}$$.

$$x \left(n + h \right) = x \left( n \right) + h x' \left( n \right) + \frac{h^{2}}{2} x'' \left( n \right) + \frac{h^{3}}{6} x''' \left( n \right) + \mathcal{O}_{1+} \left( h^{4} \right) \label{2} \tag{2}$$

$$x \left(n - h \right) = x \left( n \right) - h x' \left( n \right) + \frac{h^{2}}{2} x'' \left( n \right) - \frac{h^{3}}{6} x''' \left( n \right) + \mathcal{O}_{1-} \left( h^{4} \right) \label{3} \tag{3}$$

$$x \left(n + 2h \right) = x \left( n \right) + 2h x' \left( n \right) + \frac{4h^{2}}{2} x'' \left( n \right) + \frac{8 h^{3}}{6} x''' \left( n \right) + \mathcal{O}_{2+} \left( h^{4} \right) \label{4} \tag{4}$$

$$x \left(n - 2h \right) = x \left( n \right) - 2h x' \left( n \right) + \frac{4h^{2}}{2} x'' \left( n \right) - \frac{8 h^{3}}{6} x''' \left( n \right) + \mathcal{O}_{2-} \left( h^{4} \right) \label{5} \tag{5}$$

Now, evaluating $$x \left( n + h \right) - x \left( n - h \right)$$ we get

$$x \left( n + h \right) - x \left( n - h \right) = 2 h x' \left( n \right) + \frac{ h^{3} }{3} x''' \left( n \right) + \mathcal{O}_{1} \left( h^{4} \right) \label{6} \tag{6}$$

Similarly, $$x \left( n + 2h \right) - x \left( n - 2h \right)$$ is

$$x \left( n + 2h \right) - x \left( n - 2h \right) = 4 h x' \left( n \right) + \frac{ 8 h^{3} }{3} x''' \left( n \right) + \mathcal{O}_{2} \left( h^{4} \right) \label{7} \tag{7}$$

Note that $$\mathcal{O}_{i}$$ refers to the error corresponding to the points $$i$$ steps away from the central point.

Now, in order to eliminate the terms of $$x''' \left( n \right)$$ in \eqref{7} we multiply equation \eqref{6} with $$8$$ and plug it into \eqref{7} to get

$$8 x \left( n + h \right) - 8 x \left( n - h \right) - x \left( n + 2h \right) + x \left( n - 2h \right) = 12 h x' \left( n \right) + \mathcal{O} \left( h^{4} \right) \label{9} \tag{9}$$

which when solved for $$x' \left( n \right)$$ results in equation \eqref{1} which is the solution sought for.

There is a discrepancy with the multiplying factors between this solution and the formula you provided which I am not sure why this is the case. Since I am not familiar with the Pan-Tompkins algorithm and its use I cannot provide more help here. I hope someone else more knowledgeable in the field will be able to shed some more light.