Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 at 15:24
    
In this case they are synonyms. –  Cedric H. Mar 26 at 15:27
    
In the same sense that Cedring is a synonym for your own name... –  Oleksandr R. Mar 26 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 at 15:32

1 Answer 1

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}*)
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 at 15:45

Your Answer

 
discard

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.