# Get the frequency response curve from FIR filter coefficients + sampling rate

What's the fastest way (if possible in the browser thanks to an online tool, or if not possible easily, with Python), to get the frequency response curve (x-axis: Hz, y-axis: dB), when giving just:

• the sampling rate, example: 44100
• the coefficients, example: 1 and -1, i.e. y[n] = 1 * x[n] + (-1) x[n-1] = x[n] - x[n-1]

?

This would be often useful to quickly have an eye on the frequency response curve of a filter.

Example:

• searching for fir frequency response python directly yields the result I would've expected: scipy.signal.freqz. I'm confident you already found that, too. So, what is the problem with that that you're trying to solve? Because the way you ask this question, it's asking for code that works according to your specification; asking for that is explicitly off-topic here. – Marcus Müller May 5 '18 at 15:14

As suggested by a comment, scipy.signal.freqz gives the solution. Don't forget to use this to do the rad/sample to Hz conversion.

Here is a ready-to-use code, posted here for future reference:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
sr = 44100
w, h = signal.freqz(b=[1, -1], a=1)
x = w * sr * 1.0 / (2 * np.pi)
y = 20 * np.log10(abs(h))
plt.figure(figsize=(10,5))
plt.semilogx(x, y)
plt.ylabel('Amplitude [dB]')
plt.xlabel('Frequency [Hz]')
plt.title('Frequency response')
plt.grid(which='both', linestyle='-', color='grey')
plt.xticks([20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000], ["20", "50", "100", "200", "500", "1K", "2K", "5K", "10K", "20K"])
plt.show()


With logarithmic x-axis:

Another example with a comb filter (COEF = [1] + [0] * 200 + [-1], i.e. y[n] = x[n] - x[n-201]):