hanning python

P

Pierre

Hello,

anyone knows what is the python equivalent of the matlab's hanning
function.

Note that in matlab hann and hanning are different.

Thanks !
 
P

pdpi

Hello,

anyone knows what is the python equivalent of the matlab's hanning
function.

Note that in matlab hann and hanning are different.

Thanks !

I assume you mean the tapering function mentioned here:
http://mathworld.wolfram.com/HanningFunction.html

Python is a general purpose language, unlike the maths-specialized
MATLAB. I suggest you look into numpy, in which, a quick googling
suggests, an implementation of a the Hanning function is provided. In
fact, if you're using python to replace matlab in any meaningful way,
you'll probably want to use numpy anyway.
 
S

sturlamolden

anyone knows what is the python equivalent of the matlab's hanning
function.

Note that in matlab hann and hanning are different.

If you don't know how to compute a von Hann window, you are not
competent to do any scientific programming. Seriously!

I assume you are using NumPy and SciPy, so consider
scipy.signal.hanning for convinience.
 
P

pdpi

If you don't know how to compute a von Hann window, you are not
competent to do any scientific programming. Seriously!

Come, come. I think it's a good rule that, where available, a vendor-
supplied implementation is the preferable choice until proven
otherwise.
 
S

sturlamolden

Come, come. I think it's a good rule that, where available, a vendor-
supplied implementation is the preferable choice until proven
otherwise.

Even for the simplest of equations?
 
A

Andreas Waldenburger

Even for the simplest of equations?
Yes. It might be implemented in some clever way that you didn't think
of, and thereby work much faster or more precisely than your own
implementation.

Or it could come with a whole library that might help you with other
tasks related to what you're doing.

And just a matter of personal opinion: I think the phrase "you are not
competent to do any scientific programming" was overly harsh. Not that
the general sentiment of "this is actually easy" shouldn't be
expressed at all, but bringing in estimations of competence based on
two sentences might hurt feelings that were in no need to be hurt.


/W
 
S

Steven D'Aprano

Even for the simplest of equations?

A decent vendor-supplied implementation will include error checking that
you otherwise would need to implement yourself, so yes.

Also, given the oddities of floating point, a decent vendor-supplied
implementation is likely to work successfully on all the corner cases
where floats act bizarrely, or at least fail less disastrously than a
naive implementation will.

Third, it's very easy to use the wrong formula, especially for something
like the Hann window function which is known by two different names and
is commonly expressed as three different versions, two of which fail for
a window width of 1.

http://en.wikipedia.org/wiki/Window_function#Hann_window
http://en.wikipedia.org/wiki/Hann_function
http://mathworld.wolfram.com/HanningFunction.html


And finally, no matter how simple the equation, why re-invent the wheel?
 
S

sturlamolden

A decent vendor-supplied implementation will include error checking that
you otherwise would need to implement yourself, so yes.

Not for code like this:
 
P

pdpi

Not for code like this:

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)
 
P

pdpi

Not for code like this:

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.
 
S

sturlamolden

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.

"Premature optimization is the root of all evil in computer
programming."

Speed is not the issue here.
 
P

pdpi

"Premature optimization is the root of all evil in computer
programming."

Speed is not the issue here.

Sure it is. And safety. And practicality. And all the other reasons
why people use libraries rather than reinventing the wheel every time
they fire up their editors. Given the OP's question, a snarky "you're
not competent enough to do scientific computing" serves no purpose,
where pointing him to NumPy (and SciPy, I forgot to mention that bit)
serves him much better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top