Numerical differentiation: Difference formulas for the first derivative
The central difference method via cross-correlation and applied to measurement data
Suppose \(x\) is a finite discrete signal consisting of values of a continuous signal \(X(t)\) measured at equal intervals, say with sampling frequency \(f_s\). So the time step size is given by \(h=f_s^{-1}\). In other words, \(x_n=X(t_n)\) with \(t_n=t_0+n\cdot h\) for \(n=0,1,2,\ldots N\) and some natural number \(N\).
The central difference formula states that the derivative \(x'_n=X'(t_n)\) for \(0<n<N\) is approximated by: \[x'_n=\frac{x_{n+1}-x_{n-1}}{2h}=\frac{f_s}{2}(x_{n+1}-x_{n-1})\] Let \(w\) be the filter defined by \[w_n=\left\{\begin{array}{ll} n & \mathrm{if\;}|n|=1\\ 0 & \mathrm{otherwise}\end{array}\right.\] In a finite vectorial description we choose \(w=(-1\;\;0\;\;1)\). When we extend the measurement data with zeros on the left- and right-hand side. if necessary, we can als write the formula for the approximation of the first derivative in the measurement points in terms of a cross correlation: \[x' = \frac{f_s}{2}\cdot x \otimes w\] When a programming language has built-in functions for these operations, we can use them to calculate first derivatives. If you don't want to extend measurement data with zeros, then you have to be happy with a derivative in fewer points than you measured because the edge points in the dataset cannot be handled in this case.