# [SciPy-user] Second order differencing, periodic functions

Pearu Peterson pearu at scipy.org
Tue Mar 21 06:15:12 CST 2006

```
On Tue, 21 Mar 2006, "Johannes Löhnert" wrote:

> Hi,
>
> I want to calculate the 2nd derivative of a periodic function. If I have the
>
> values given as array f_arr, I would like to do this with 2nd order
> differencing. For a non-periodic function I could use
>
> d2f = convolve(f_arr, array([-.25, .5, -.25]), mode=2),
>
> but with this approach f_arr is zero-padded. Is there a similar function for
>
> periodic boundary conditions for f_arr? I know about fftpack.diff, but I
> would rather use SOD, since diff yields "strange" results for non-contiguous
> functions.

scipy.sandbox.fdfpack[*] contains a function

periodic_finite_difference(x, h=2*pi/len(x), k=1, m=1)

that returns k-th derivative of len(x)*h periodic sequence x using
m-th order finite difference formulae [**]. The error of derivative is
O(h^(2*(m-1))) within numerical accuracy. This function is faster than
fftpack.diff, returns less numerical noise for very long periodic
sequences (ffpack.diff is almost unusable for calculating higher order
of derivatives of long periodic sequences).

[*] To build fdfpack, you must add the following line