Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active April 26, 2017 10:46
Show Gist options
  • Save jniemann66/9afcfa04383b9e768a0c283068ea3f4e to your computer and use it in GitHub Desktop.
Save jniemann66/9afcfa04383b9e768a0c283068ea3f4e to your computer and use it in GitHub Desktop.
Plot Frequency Response of FIR
# -*- coding: utf-8 -*-
# Sampling frequency
fs = 44100
# FFT size
N = 32768
# use Log freq response ? (False: Linear)
logFreq = False
# normalize peak to 0dB ?
peakAtZero = False
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(precision=15, suppress=True)
def fir_freqz(b):
X = np.fft.fft(b, N)
Xm = np.abs(X)
if peakAtZero:
Xdb = 20*np.log10(Xm/Xm.max())
else:
Xdb = 20*np.log10(Xm)
f = np.arange(N)*fs/N
return Xdb, f
if __name__ == "__main__":
# FIR filter coefficients
b = np.array([-1,
2.847, -4.685, 6.214,
-7.184, 6.639, -5.032,
3.263, -1.632, 0.4191
])
Xdb, f = fir_freqz(b)
plt.clf()
# Plot the frequency response
if logFreq:
plt.semilogx(f, Xdb, 'b')
else:
plt.plot(f, Xdb, 'b')
plt.grid(True)
plt.hold(True)
plt.title('Frequency reponse')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude [dB]')
plt.xlim((0, fs/2))
plt.legend()
plt.savefig('e:\\t\\foo.png', bbox_inches='tight')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment