0
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ von Hann. Not Hanning. $\endgroup$ – Oleksandr R. Mar 26 '14 at 15:24
  • $\begingroup$ In this case they are synonyms. $\endgroup$ – Cedric H. Mar 26 '14 at 15:27
  • $\begingroup$ In the same sense that Cedring is a synonym for your own name... $\endgroup$ – Oleksandr R. Mar 26 '14 at 15:28
  • $\begingroup$ Except that nobody calls me Cedring while Hanning is commonly used (even if the origin is doubtful: Hann / Hamming => Hanning). $\endgroup$ – Cedric H. Mar 26 '14 at 15:32
3
$\begingroup$
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,Protected},{Flat,Listable,
     NumericFunction,OneIdentity,Orderless,Protected},
     {Listable, NumericFunction, OneIdentity, Protected}}*)
(*{Sin[a],Sin[b]}*)
(*{a^2, b^2}*)
(*{a c,b d}*)
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. Mar 26 '14 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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