Sign in

username:

password:



Not a member?

Search Online Books



Search tips

Free Online Books

Ads

Chapters

Chapter Contents:

Search Spectral Audio Signal Processing

  

Book Index | Global Index


Would you like to be notified by email when Julius Orion Smith III publishes a new entry into his blog?

  

Matlab for the Hann Window

In Matlab or Octave, a length $ M$ Hann window is designed by the statement

w = hanning(M);
which, in Matlab only is equivalent to
w = .5*(1 - cos(2*pi*(1:M)'/(M+1)));
For $ M=3$, hanning(3) returns the following:
>> hanning(3)
ans =
     0.5
     1
     0.5
Note the curious use of M+1 in the denominator instead of M as we would expect from the family definition in (3.1). This perturbation serves to avoid using zero samples in the window itself. (Why bother to multiply explicitly by zero?) Thus, the Hann window as returned by Matlab hanning function reaches zero one sample beyond the endpoints to the left and right. The minus sign, which differs from (3.2), serves to make the window causal instead of zero phase.

The Matlab (v6.1) Signal Processing Tool Box also includes a hann function which is defined to include the zeros at the window endpoints. For example,

>> hann(3)
ans =
     0
     1
     0
This case is equivalent to the following matlab expression:
w = .5*(1 - cos(2*pi*(0:M-1)'/(M-1)));
The use of $ M-1$ is necessary to include zeros at both endpoints. The Matlab hann function is a special case of what Matlab calls ``generalized cosine windows'' (type gencoswin).

In Matlab, both hann(3,'periodic') and hanning(3,'periodic') produce the window

>> hann(3,'periodic')
ans =
     0
     0.75
     0.75
This case is equivalent to
w = .5*(1 - cos(2*pi*(0:M-1)'/M)));
which agrees (finally) with definition (3.2). We see that in this case, the left zero endpoint is included in the window, while the one on the right lies one sample outside to the right. In general, the 'periodic' window option asks for a window that can be overlapped and added to itself at certain time displacements ($ (M-1)/2=1$ samples in this case) to produce a constant function. Use of ``periodic'' windows in this way is introduced in §6.3 and discussed more fully in Chapters 8 and 9.

In Octave 2.1.36, the hanning function includes the endpoint zeros, hann is undefined, and the 'periodic' option is not supported. (Many other window types defined in Matlab have also not been implemented in Octave.)

In practical applications, it is safest to write your own window functions in the matlab language in order to ensure portability and consistency. After all, they are only one line of code!

In comparing window properties below, we will speak of the Hann window as having a main-lobe width equal to $ 4\Omega_M$, and a side-lobe width $ \Omega_M$, even though in practice they may really be $ 4\Omega_{M\pm1}$ and $ \Omega_{M\pm1}$, respectively. These remarks apply equally well to all windows in the generalized Hamming family, as well as the Blackman-Harris family introduced in §3.3.


Order a Hardcopy of Spectral Audio Signal Processing

Previous: Hann or Hanning or Raised Cosine
Next: Summary of Hann window properties:

written by Julius Orion Smith III
Julius Smith's background is in electrical engineering (BS Rice 1975, PhD Stanford 1983). He is presently Professor of Music and Associate Professor (by courtesy) of Electrical Engineering at Stanford's Center for Computer Research in Music and Acoustics (CCRMA), teaching courses and pursuing research related to signal processing applied to music and audio systems. See http://ccrma.stanford.edu/~jos/ for details.


Comments


No comments yet for this page


Add a Comment
You need to login before you can post a comment (best way to prevent spam). ( Not a member? )