Demonstration of the window functions in scipy.signal

This notebook is not intended to replace the SciPy reference guide but to serve only as a one stop shop for the window functions available in the signal processing module (see http://docs.scipy.org/doc/scipy/reference/signal.html for detailed information). Alternative sources of information can be found in this Wikipedia article and elsewhere (e.g. LabVIEW, MATLAB).

Table of contents

Preamble

Modified Bartlett-Hann window

Bartlett window

Blackman window

Minimum 4-term Blackman-Harris window

Bohman window

Boxcar or rectangular window

Dolph-Chebyshev window

Simple cosine shape window

Flat top window

Gaussian window

Generalized Gaussian shape

Hamming window

Kaiser window

Minimum 4-term Blackman-Harris window according to Nuttall

Parzen window

Digital Slepian (DPSS) window

Triangular window

Odds and ends

Preamble

Since this notebook was created with Python 2.7, we will start by importing a few things from the "future":


In [1]:
from __future__ import division, print_function

We then proceed by importing the major packages needed for this notebook:


In [2]:
import sys
import numpy as np
import scipy as sp
import matplotlib as mpl

Let us check the versions being used:


In [3]:
print('System: {}'.format(sys.version))
for package in (np, sp, mpl):
    print('Package: {} {}'.format(package.__name__, package.__version__))


System: 3.5.2 |Anaconda custom (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
Package: numpy 1.11.2
Package: scipy 0.18.1
Package: matplotlib 1.5.3

Last, we import the signal processing module from the scipy package plus a few additional modules/packages required by this notebook:


In [4]:
from scipy import signal
from scipy.fftpack import fft, fftshift
import matplotlib.pyplot as plt

To make the plots visible in the notebook, we need the following Ipython "magic" command:


In [5]:
%matplotlib inline

Now we are ready for presenting the windows available at scipy.signal (http://docs.scipy.org/doc/scipy/reference/signal.html).

Back to top

Modified Bartlett-Hann window


In [6]:
window = signal.barthann(51)
plt.plot(window)
plt.title("Bartlett-Hann window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [7]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Bartlett-Hann window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Bartlett window


In [8]:
window = signal.bartlett(51)
plt.plot(window)
plt.title("Bartlett window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [9]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Bartlett window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Blackman window


In [10]:
window = signal.blackman(51)
plt.plot(window)
plt.title("Blackman window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [11]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Blackman window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


C:\Users\Paulo\Anaconda3\lib\site-packages\ipykernel\__main__.py:4: RuntimeWarning: divide by zero encountered in log10

Back to top

Minimum 4-term Blackman-Harris window


In [12]:
window = signal.blackmanharris(51)
plt.plot(window)
plt.title("Blackman-Harris window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [13]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Blackman-Harris window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Bohman window


In [14]:
window = signal.bohman(51)
plt.plot(window)
plt.title("Bohman window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [15]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Bohman window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Boxcar or rectangular window


In [16]:
window = signal.boxcar(51)
plt.plot(window)
plt.title("Boxcar window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [17]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the boxcar window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Dolph-Chebyshev window


In [18]:
window = signal.chebwin(51, at=100)
plt.plot(window)
plt.title("Dolph-Chebyshev window (100 dB)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [19]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Dolph-Chebyshev window (100 dB)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Simple cosine shape window


In [20]:
window = signal.cosine(51)
plt.plot(window)
plt.title("Cosine window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [21]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the cosine window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Exponential (or Poisson) window

This window will only be available in SciPy 0.16.0

Back to top

Flat top window


In [22]:
window = signal.flattop(51)
plt.plot(window)
plt.title("Flat top window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [23]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the flat top window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Gaussian window


In [24]:
window = signal.gaussian(51, std=7)
plt.plot(window)
plt.title(r"Gaussian window ($\sigma$=7)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [25]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title(r"Frequency response of the Gaussian window ($\sigma$=7)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Generalized Gaussian shape


In [26]:
window = signal.general_gaussian(51, p=1.5, sig=7)
plt.plot(window)
plt.title(r"Generalized Gaussian window (p=1.5, $\sigma$=7)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [27]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title(r"Freq. resp. of the gen. Gaussian window (p=1.5, $\sigma$=7)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Hamming window


In [28]:
window = signal.hamming(51)
plt.plot(window)
plt.title("Hamming window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [29]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Hamming window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Hann window


In [30]:
window = signal.hann(51)
plt.plot(window)
plt.title("Hann window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [31]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Hann window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


C:\Users\Paulo\Anaconda3\lib\site-packages\ipykernel\__main__.py:4: RuntimeWarning: divide by zero encountered in log10

Back to top

Kaiser window


In [32]:
window = signal.kaiser(51, beta=14)
plt.plot(window)
plt.title(r"Kaiser window ($\beta$=14)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [33]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title(r"Frequency response of the Kaiser window ($\beta$=14)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Minimum 4-term Blackman-Harris window according to Nuttall


In [34]:
window = signal.nuttall(51)
plt.plot(window)
plt.title("Nuttall window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [35]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Nuttall window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Parzen window


In [36]:
window = signal.parzen(51)
plt.plot(window)
plt.title("Parzen window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [37]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Parzen window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Digital Slepian (DPSS) window


In [38]:
window = signal.slepian(51, width=0.3)
plt.plot(window)
plt.title("Slepian (DPSS) window (BW=0.3)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [39]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the Slepian window (BW=0.3)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


Back to top

Triangular window


In [40]:
window = signal.triang(51)
plt.plot(window)
plt.title("Triangular window")
plt.ylabel("Amplitude")
plt.xlabel("Sample")
plt.show()



In [41]:
plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title("Frequency response of the triangular window")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
plt.show()


C:\Users\Paulo\Anaconda3\lib\site-packages\ipykernel\__main__.py:4: RuntimeWarning: divide by zero encountered in log10

Back to top

Odds and ends

This notebook was created by Paulo Xavier Candeias.

Back to top