Skip to content

Instantly share code, notes, and snippets.

@kumanna
Created February 27, 2026 05:44
Show Gist options
  • Select an option

  • Save kumanna/0ae726cf0f0be5bc8bd374c5d40e1756 to your computer and use it in GitHub Desktop.

Select an option

Save kumanna/0ae726cf0f0be5bc8bd374c5d40e1756 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from scipy import *
from scipy.signal.windows import hann, hamming
import numpy as np
import pylab as p
p.rc('text', usetex=True)
p.rc('font', size=14)
#p.rc('axes', labelsize=20)
#p.rc('legend', fontsize='large')
#p.rc('xtick', labelsize=10)
#p.rc('ytick', labelsize=10)
N = 101
n = p.arange(N)
x = np.cos(0.2 * np.pi * n) + np.sin(0.22 * np.pi * n) + 0.0005 * np.sin(0.6 * np.pi * n)
[w, h1] = signal.freqz(x, [1])
[w, h2] = signal.freqz(x * hamming(N), [1])
[w, h3] = signal.freqz(x * hann(N), [1])
#h1 = h1 / max(abs(h1))
#h2 = h2 / max(abs(h2))
#h3 = h3 / max(abs(h3))
#p.plot(w, 20*log10(abs(h1)), label="Rectangular", linewidth=2)
#p.plot(w, 20*log10(abs(h2)), label="Hamming", linewidth=2)
#p.plot(w, 20*log10(abs(h3)), label="Hanning", linewidth=2)
p.plot(w, 20*np.log10(abs(h1)), 'k-', label="Rectangular", linewidth=2.5)
p.plot(w, 20*np.log10(abs(h2)), 'k:', label="Hamming", linewidth=1.5)
p.plot(w, 20*np.log10(abs(h3)), 'k-', label="Hanning", linewidth=1.25)
p.vlines(0.2 * np.pi, -200, 200, linewidth=1.5, label="Input frequency")
p.vlines(0.22 * np.pi, -200, 200, linewidth=1.5, label="_nolegend_")
p.vlines(0.6 * np.pi, -200, 200, linewidth=1.5, label="_nolegend_")
p.xlabel(r"$\omega$")
p.ylabel(r"$|H(e^{j\omega})|^2$ (dB)")
p.legend(loc='lower left')
#p.legend(loc='upper right')
x = np.arange(0.0, 1.1, 0.2)
axtext = [("$" + str(i)[:3] + r"\pi$") for i in x]
axtext[0] = "0"
axtext[-1] = r"$\pi$"
print(axtext)
p.xticks(x * np.pi, axtext)
#p.xticks(arange(0, 3.1, 0.2))
v = p.axis()
p.axis((0, np.pi, -120, 40))
p.grid()
#p.text(0.565 * pi, -83, '$0.6\alpha$', fontsize=20)
p.title("Magnitude response of signal with various windows", fontsize=20)
p.savefig("win.png")
p.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment