[SciPy-dev] Smooth filtering method

Andrew Straw strawman at astraw.com
Sun Jan 22 10:02:05 CST 2006


Hi Hugo,

As for as the demos, you could certainly make pages in the new wiki's
Cookbook section. I think using matplotlib is perfectly fine.

A docstring guideline - perhaps you could write one? I'm sure it would
get corrected and included, but it's the initial effort that probably
most of the work.

Argument verification - I'm not sure what you mean. Could you post a
pseudo-code example? And again, this is probably a case where you might
have to do (most of) the work if you want it included.

Cheers!
Andrew

Hugo Gamboa wrote:

> I came across the need of a smoothing function for my processing
> tasks. In attach you can find a smooth.py that I think can useful, and
> could be introduced in the scipy signal package.
>
> In the process of preparing the code in order to be presentable for
> others I collected some scipy development guidelines questions:
>
> 1. Docstrings. Is there (or should exist) a docstring guideline for
> numpy/scipy? In this code I make an approach using items in the
> docstring like: input, output, example, see also and TODO. Is this too
> much. I my opinion a guideline could help newcomers (like me) to
> contribute with standardized documentation to scipy.
>
> 2. Demo methods. I find it interesting to have some of the functions
> accompanied by demo code for demonstration purposes. Have any one
> thought about the structure for such code. Could it be included in a
> demo directory like the tests directory? And is it pacific to use mpl
> in those demos?
>
> 3. Argument verification. Should a guideline for numpy argument
> verification be defined? What I me think is in a wiki page that
> describes the steps for standard verification of the arguments,
> pointing to some helper functions that do some of those tasks (like
> atleast_Xd).
>
> Best regards,
>
> Hugo Gamboa
>
>
>------------------------------------------------------------------------
>
>import numpy
>
>def smooth(x,window_len=10,window='hanning'):
>    """smooth the data using a window with requested size.
>    
>    This method is based on the convolution of a scaled window with the signal.
>    The signal is prepared by introducing reflected copies of the signal 
>    (with the window size) in both ends so that transient parts are minimized
>    in the begining and end part of the output signal.
>    
>    inputs:
>        x: the input signal 
>        window_len: the dimension of the smoothing window
>        window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
>            flat window will produce a moving average smoothing.
>
>    output:
>        the smoothed signal
>        
>    example:
>
>    t=linspace(-2,2,0.1)
>    x=sin(t)+randn(len(t)*0.1
>    y=smooth(x)
>    
>    see also: 
>    
>    numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
>    scipy.signal.lfilter
> 
>    TODO: the window parameter could be the window itself if a array instead of a string   
>    """ 
>     
>    if x.ndim != 1:
>        raise ValueError, "smooth only accepts 1 dimension arrays."
>
>    if x.size < window_len:
>        raise ValueError, "Input vector needs to be bigger than window size."
>        
>
>    
>    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
>        raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
>    
>
>    s=numpy.r_[2*x[0]-x[window_len:1:-1],x,2*x[-1]-x[-1:-window_len:-1]]
>    
>    if window == 'flat': #moving average
>        w=ones(window_len,'d')
>    else:
>        w=eval('numpy.'+window+'(window_len)')
>    
>    y=numpy.convolve(w/w.sum(),s,mode='same')
>    return y[window_len-1:-window_len+1]
>
>    
>    
>    
>from numpy import *
>from pylab import *
>    
>def smooth_demo():    
>    
>    t=linspace(-4,4,100)
>    x=sin(t)
>    xn=x+randn(len(t))*0.1
>    y=smooth(x)
>    
>    ws=31
>    
>    subplot(211)
>    plot(ones(ws))
>    
>    windows=['flat', 'hanning', 'hamming', 'bartlett', 'blackman']
>    
>    hold(True)
>    for w in windows[1:]:
>        eval('plot('+w+'(ws) )')
>    
>    axis([0,30,0,1.1])
>    
>    legend(windows)
>    title("The smoothing windows")
>    subplot(212)
>    plot(x)
>    plot(xn)
>    for w in windows:
>        plot(smooth(xn,10,w))
>    l=['original signal', 'signal with noise']
>    l.extend(windows)
>    
>    legend(l)
>    title("Smoothing a noisy signal")
>    show()
>
>    
>if __name__=='__main__':
>    smooth_demo()
>    
>    
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Scipy-dev mailing list
>Scipy-dev at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-dev
>  
>




More information about the Scipy-dev mailing list