Search code examples
pythonnumpyscipyfilteringsmoothing

Smooth circular data


I have an array of data Y such that Y is a function of an independent variable X (another array).

The values in X vary from 0 to 360, with wraparound.

The values in Y vary from -180 to 180, also with wraparound.

(That is, these values are angles in degrees around a circle.)

Does anyone know of any function in Python (in numpy, scipy, etc.) capable of low-pass filtering my Y values as a function of X?

In case this is at all confusing, here's a plot of example data:

enter image description here


Solution

  • Say you start with

    import numpy as np
    
    x = np.linspace(0, 360, 360)
    y = 5 * np.sin(x / 90. * 3.14) + np.random.randn(360)
    
    plot(x, y, '+');
    

    enter image description here

    To perform a circular convolution, you can do the following:

    yy = np.concatenate((y, y))
    smoothed = np.convolve(np.array([1] * 5), yy)[5: len(x) + 5]
    

    This uses, at each point, the cyclic average with the previous 5 points (inclusive). Of course, there are other ways of doing so.

    >>> plot(x, smoothed)
    

    enter image description here