2
$\begingroup$

I was playing with filters in SciPy and noticed something that I'm not that familiar with:

I'm using the code:

import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt


def butter_lowpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y


# Filter requirements.
order = 6
fs = 30.0       # sample rate, Hz
cutoff = 3.667  # desired cutoff frequency of the filter, Hz

# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)

# Plot the frequency response.
w, h = freqz(b, a, worN=8000)

taken from: https://stackoverflow.com/a/25192640/4959635

And doing some plots of the given freqz results:

plt.plot(w,h)
plt.show()

enter image description here

plt.plot(w,abs(h))
plt.show()

enter image description here

So why must I take abs to get "the plot that I want"? And what's the first one with negative amplitudes?

$\endgroup$
5
$\begingroup$

Frequency response has two parts: amplitude response and the phase response. Both of these are represented as a complex signal when you get the response from freqz. In order to plot the amplitude response you need to use abs. Otherwise I doubt it only shows you the real part which I think is what you see in the first figure. Note that when dealing with the amplitude response, in most cases you will do a further step and convert it to $\textrm{dB}$ scale.

$\endgroup$
  • $\begingroup$ Ah of course. So frequency response is always defined in the complex plane with magnitude and phase and to get a cartesian plot one must take the norm which for complex numbers $z=a+bi$ is $|z|=\sqrt{a^2+b^2}$. $\endgroup$ – mavavilj Aug 30 '16 at 10:54
  • $\begingroup$ Yes that is right. $\endgroup$ – msm Aug 30 '16 at 11:34

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.