0
$\begingroup$

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:

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Marcus Müller May 5 '18 at 15:14
2
$\begingroup$

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:

enter image description here

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.