[Numpy-discussion] downsample vector with averaging

Scott Ransom sransom at nrao.edu
Mon May 8 18:36:03 EDT 2006


How about this:

---------------------------------------------
import numpy as Num

def downsample(vector, factor):
    """
    downsample(vector, factor):
        Downsample (by averaging) a vector by an integer factor.
    """
    if (len(vector) % factor):
        print "Length of 'vector' is not divisible by 'factor'=%d!" % factor
        return 0
    vector.shape = (len(vector)/factor, factor)
    return Num.mean(vector, axis=1)
---------------------------------------------

Scott


On Mon, May 08, 2006 at 01:17:16PM -0400, Ryan Krauss wrote:
> I need to downsample some data while averaging it.  Basically, I have
> a vector and I want to take for example every ten points and average
> them together so that the new vector would be made up of
> newvect[0]=oldvect[0:9].mean()
> newvect[1]=oldevect[10:19].mean()
> ....
> 
> Is there a built-in or vectorized way to do this?  I default to
> thinking in for loops, but that can lead to slow code.
> 
> Thanks,
> 
> Ryan
> 
> 
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job 
> easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd_______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

-- 
-- 
Scott M. Ransom            Address:  NRAO
Phone:  (434) 296-0320               520 Edgemont Rd.
email:  sransom at nrao.edu             Charlottesville, VA 22903 USA
GPG Fingerprint: 06A9 9553 78BE 16DB 407B  FFCA 9BFA B6FF FFD3 2989




More information about the NumPy-Discussion mailing list