hanning python

pdpi pdpinheiro at gmail.com
Wed Sep 9 10:57:41 EDT 2009


On Sep 9, 3:46 pm, pdpi <pdpinhe... at gmail.com> wrote:
> On Sep 9, 3:27 am, sturlamolden <sturlamol... at yahoo.no> wrote:
>
> > On 9 Sep, 00:24, Steven D'Aprano
>
> > <ste... at REMOVE.THIS.cybersource.com.au> wrote:
> > > A decent vendor-supplied implementation will include error checking that
> > > you otherwise would need to implement yourself, so yes.
>
> > Not for code like this:
>
> > >>> import numpy as np
> > >>> n = np.arange(101)
> > >>> w = 0.5*(1.0-np.cos(2*np.pi*n/(100.)))
>
> Well, I went and dug into NumPy. They write it as 0.5 - 0.5 * cos
> (...), and special case N = 1, and properly error check N < 1. Still,
> probably because of differences in dictionary look ups (because of
> namespace scopes), np.hanning, on average, takes a wee bit over half
> as long as your case, and yours is only a shade faster than
>
> >>> window = [0.5 - math.cos(2 * x * math.pi /100.) for x in range(101)]
>
> (Yes, I know I should've used xrange instead of range)

Sorry, should've been smarter than this.

Raising this to 1 million, rather than 100, nodes in the window, the
timing difference between your version and NumPy's is tiny (but numpy
still edges you out, but just barely), but they trounce my naive
version, being around 7 or 8 times faster the list comprehension I
suggested. So implementing this in vanilla python instead of using
numpy would hurt performance a fair bit, and odds are the OP is going
to put this to use somewhere that involves more maths, which makes
learning about numpy well worth having asked the question here.



More information about the Python-list mailing list