#!/usr/bin/env python # coding: utf-8 # # A Gallery of SciPy's Window Functions # ## This is mostly for quick visual reference and comparison between windows # In[1]: import matplotlib.pyplot as plt import scipy.signal as sig get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: n_points = 1025 # ##1. Bartlett-Hann Window # ## $ w(n) = a_{0} - a_{1} \left|\frac{n}{N - 1} - \frac{1}{2}\right| - \cos(\frac{2\pi n}{N - 1})$ # ## $a_{0} = 0.62, a_{1} = 0.48, a_{2} = 0.38$ # In[3]: window = sig.barthann(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Bartlett-Hann') # ##2. Bartlett Window # ##$ w(n) = \frac{2}{M - 1}(\frac{M - 1}{2} - \left|n - \frac{M - 1}{2}\right|) $ # In[4]: window = sig.bartlett(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Bartlett') # ##3. Blackman Window # ## $ w(n) = 0.42 - 0.5 \cos(2\pi n/M) + 0.08 \cos(4\pi n/M) $ # In[5]: window = sig.blackman(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Blackman') # ##4. Blackman-Harris Window # ##$ w(n) = a_{0} - a_{1}\cos(\frac{2\pi n}{N - 1}) + a_{2}\cos(\frac{4\pi n}{N - 1}) - a_{3}\cos(\frac{6\pi n}{N - 1}) $ # ##$ a_{0} = 0.38575, a_{1} = 0.48829, a_{2} = 0.14128, a_{3} = 0.01168 $ # In[6]: window = sig.blackmanharris(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Blackman-Harris') # ##5. Bohman Window # ##$ w(n) = (1 - |x|)\cos(\pi|x|) + \frac{1}{\pi}\sin(\pi|x|) $ # ##$ x \in [-1, 1] $ # In[7]: window = sig.bohman(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Bohman') # ##6. Boxcar window # (from the docs: included for correctness, equivalent to no window at all.) # In[8]: window = sig.boxcar(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Boxcar') # ##7. Dolph-Chebyshev Window # ##$ W(k) = \frac # {\cos\{M \cos^{-1}[\beta \cos(\frac{\pi k}{M})]\}} # {\cosh[M \cosh^{-1}(\beta)]} $ # ##$ \beta = \cosh \left [\frac{1}{M} # \cosh^{-1}(10^\frac{A}{20}) \right ] $ # In[9]: window = sig.chebwin(n_points, at=100) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Dolph-Chebyshev (attenutation 100 dB)') # ##8. Cosine Window # ##$ w(n) = \sin(\frac{\pi n}{N - 1}) $ # In[10]: window = sig.cosine(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Cosine') # ##9. Flat Top Window # ##$ w(n) = \sum\limits_{k=0}^4 (-1)^{k} a_{k}\cos(\frac{2\pi kn}{N-1}) $ # ##$ a_{0} = 0.2156, a_{1} = 0.4160, a_{2} = 0.2781, a_{3} = 0.0836, a_{4} = 0.0069 $ # In[11]: window = sig.flattop(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Flat Top') # ##10. Gaussian Window # ##$ w(n) = e^{ -\frac{1}{2}\left(\frac{n}{\sigma}\right)^2 } $ # In[12]: window = sig.gaussian(n_points, std=130) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Gaussian ($\sigma$=130)') # ##11. Generalized Gaussian # ##$ w(n) = e^{ -\frac{1}{2}\left|\frac{n}{\sigma}\right|^{2p} } $ # In[13]: window = sig.general_gaussian(n_points, sig=150, p=1.5) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Generalized Gaussian ($\sigma$=150, $p=1.5$)') # ##12. Hamming Window # ##$ w(n) = 0.54 - 0.46 \cos\left(\frac{2\pi{n}}{M-1}\right) \qquad 0 \leq n \leq M-1 $ # In[14]: window = sig.hamming(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Hamming') # ##13. Hanning(Hann) Window # ##$w(n) = 0.5 - 0.5 \cos\left(\frac{2\pi{n}}{M-1}\right) # \qquad 0 \leq n \leq M-1$ # In[15]: window = sig.hann(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Hann') # ##14. Kaiser Window # ##$ w(n) = I_0\left( \beta \sqrt{1-\frac{4n^2}{(M-1)^2}} \right)/I_0(\beta) $ # ##$ \quad -\frac{M-1}{2} \leq n \leq \frac{M-1}{2}, $ # ###where $ I_{0} $ is the modified 4th order Bessel Function # In[16]: window = sig.kaiser(n_points, beta=14) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Kaiser (beta=14)') # ##15. Nuttall Window # ##$ w(n) = \sum\limits_{k=0}^3 (-1)^{k} a_{k}\cos(\frac{2\pi kn}{N-1}) $ # ##$a_{0} = 0.3635819, a_{1} = 0.4891775, a_{2} = 0.1365995, a_{3} = 0.0106411$ # # In[17]: window = sig.nuttall(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Nuttall') # ##16. Parzen Window # ### 4 - fold convolution of a rectangular window # In[18]: window = sig.parzen(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Parzen') # ##17. Slepian (DPSS) Window # In[19]: window = sig.slepian(n_points, width=0.02) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('DPSS (width=0.002)') # ##18. Triangular Window # In[20]: window = sig.triang(n_points) plt.figure(figsize=(12, 8)) plt.plot(window) plt.xlim([0, n_points]) plt.title('Triangular') # #Overlay of all windows # In[21]: arg_map = { 'barthann': [sig.barthann, dict(M=n_points)], 'bartlett': [sig.bartlett, dict(M=n_points)], 'blackman': [sig.blackman, dict(M=n_points)], 'blackmanharris': [sig.blackmanharris, dict(M=n_points)], 'bohman': [sig.bohman, dict(M=n_points)], 'boxcar': [sig.boxcar, dict(M=n_points)], 'chebwin': [sig.chebwin, dict(M=n_points, at=100)], 'cosine': [sig.cosine, dict(M=n_points)], 'flattop': [sig.flattop, dict(M=n_points)], 'gaussian': [sig.gaussian, dict(M=n_points, std=130)], 'general_gaussian': [sig.general_gaussian, dict(M=n_points, sig=150, p=1.5)], 'hamming': [sig.hamming, dict(M=n_points)], 'hann': [sig.hann, dict(M=n_points)], 'kaiser': [sig.kaiser, dict(M=n_points, beta=14)], 'nuttall': [sig.nuttall, dict(M=n_points)], 'parzen': [sig.parzen, dict(M=n_points)], 'selpian': [sig.slepian, dict(M=n_points, width=0.02)], 'triang': [sig.triang, dict(M=n_points)] } plt.figure(figsize=(15, 15)) plt.xlim([0, n_points]) for name, calldata in arg_map.iteritems(): function, args = calldata window = function(**args) plt.plot(window, label=name) plt.legend() # In[ ]: