From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

The Hann window function in LabVIEW is different from what Wikipedia and Matlab tells. What is the reason for this difference?

I am new to signal processing as well as LabVIEW. The Hann window function used in LabVIEW is different from what Wikipedia and Matlab says.A factor N is used in the cosine function in the window function while Wikipedia uses (N-1) where N is the length of the signal or window length. This difference is observed in other cosine window functions  I want to understand what is the reason for this difference? 

0 Kudos
Message 1 of 7
(4,107 Views)

I think the "difference" is largely how the functions are used and described.  Do you know why you have (and may need to use) a Windowing function?  When one does an FFT on a finite sample of data (most of us are too impatient to wait for the infinite data set), the assumption is that we are taking N points from a periodic waveform having period N.  Well, this is rarely true -- point N+1 (the next point, if we had sampled it) rarely "resembles" the first point we acquired.  One way to deal with this is to force the sample to be "similar" at the ends by Windowing it.  Since the point of the exercise is to make the first (index 0) and the N+1 (index N) point identical, we use a window of length N (as LabVIEW says), with indices that go from 0 to N-1.

 

When you are describing the Window in isolation (as Wikipedia does), you describe it as a Window that goes to 0 at points 0 and N-1 (look at the picture).  The function that LabVIEW (correctly) uses goes to zero at 0 and N, hence the difference.  Both descriptions are correct, they are just describing Windows of different sizes used in (slightly) different ways.

 

I haven't looked at Matlab's explanation.

 

Bob Schor

0 Kudos
Message 2 of 7
(4,092 Views)

It probably makes little difference anyway.  If you are doing an FFT, N is probably a fairly large number, perhaps 1000 points?  The difference in results between having N or N-1 in the denominator would be pretty small.

0 Kudos
Message 3 of 7
(4,087 Views)

 

Interesting.  It may be an artifact of the differing conventions for array indexing.   LabVIEW indexing starts from 0 while Matlab indexing defaults to start from 1, as I recall.   If there's a dispute, it looks like it essentially boils down to either including or excluding an endpoint value, one that evaluates to 0 anyway.  

 

 

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
0 Kudos
Message 4 of 7
(4,075 Views)

The question is if the problem is in the documentation (typo) or in the actual implementation.

 

Personally, I would probably prefer N so we don't get a division by zero in the edge case where N=1, but "N-1" is probably mathematically more correct. 😄

0 Kudos
Message 5 of 7
(4,061 Views)

We should probably talk about this "offline" (at the Community Social in August?), but I think neither the documentation or implementation are "wrong", but rather are talking about slightly different (by 1, as it happens) things.  If you have N points in your waveform that you are windowing, you do want a window where you divide by N.  If you want to Window N points such that Point 1 (the first point) agrees with point N+1 (the first point of the next, presumed-identical cycle), you need a Window that starts at 0 and goes to zero for the N+1 point, guaranteeing that Point 1 and Point N+1 agree.  So the spacing of the zeros for the Hanning Window of width N is ... N+1.

 

LabVIEW Hanning:  y(i) = 0.5 x(i) * (1 - cos (2 pi i/N), i = 0 .. N-1.

y(0) = 0.

y(N) = 0.  The set [0 .. N] has N+1 elements.

y(N-1) = x(N-1)/(4 N*N) + smaller terms (first few terms of the Taylor expansion of Cos), not equal to zero (but close ...)

 

Bob Schor 

0 Kudos
Message 6 of 7
(4,046 Views)

Hi Nadhi,

 

LabVIEW offers two types of Hanning window function.  One is Hanning Window VI, which is asymmetric window.  Another is Symmetric Window VI, which provides the symmetric window of all window types, including Hanning Window.

 

The formula, w(n) = 0.5 * (1 - cos(2pi*n/(N-1))), is talking about the symmetric Hanning window.  The Symmetric Window VI uses this formula to design the Hanning window.

 

For asymmetric Hanning window, we can design a symmetric Hanning window of (N+1) points, then remove the last point.

Thus, the formula becomes w(n) = 0.5 * (1 - cos(2pi*n/N)), n = 0, 1, 2, ..., N-1.

 

Please refer to the Symmetric Window VI details help for the relationship between symmetric window and asymmetric window.

 

Best Regards,

Michael

 

 

 

 

 

0 Kudos
Message 7 of 7
(3,967 Views)