Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm implementing a package for in-house signal processing.

I wrote here a quite trivial function implementing a Hanning windowing of the signal.

HanningFilter[signal_List] := Module[{length},
   length = Length[signal];
   Table[signal[[k]] 2 Sin[π k/length]^2, {k, 1, length}]

I know there is a HannWindow[x] built-in function that can be use. However the question I have can be applied to other filters as well:

What is the most performant implementation of such a function?

Probably the use of a Table[] is not optimal.

share|improve this question
von Hann. Not Hanning. – Oleksandr R. Mar 26 '14 at 15:24
In this case they are synonyms. – Cedric H. Mar 26 '14 at 15:27
In the same sense that Cedring is a synonym for your own name... – Oleksandr R. Mar 26 '14 at 15:28
Except that nobody calls me Cedring while Hanning is commonly used (even if the origin is doubtful: Hann / Hamming => Hanning). – Cedric H. Mar 26 '14 at 15:32
up vote 3 down vote accepted
HanningFilter[signal_List] := 
   With[{len = Length[signal]}, 2 signal Sin[Pi Range[len]/len]^2]

Sin, Times,Power are Listable,that means

Attributes /@ {Sin, Times, Power}
Sin[{a, b}]
{a, b}^2
{a, b} {c, d}
     {Listable, NumericFunction, OneIdentity, Protected}}*)
(*{a^2, b^2}*)
(*{a c,b d}*)
share|improve this answer
Could I suggest adding prec = Max[Precision[signal], MachinePrecision] and using something like len = SetPrecision[Length[signal], prec] instead, in combination with Range[SetPrecision[1, prec], len]? This way Range produces, and Sin sees, a list of inexact numbers, which will be faster than using rationals, especially for a MachinePrecision signal. – Oleksandr R. Mar 26 '14 at 15:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.