I try to implement an Lowpass filter using a Hammond Window. The cut-off frequency it's 0.3 and the order of the filter it's 21. My problem is that I have a straight line and it's not crossing through the represented points. Here it's the code:
from pylab import *
import scipy.signal as signal
import matplotlib.pyplot as plt
#Plot step and impulse response
def impz(b,a=1):
l = len(b)
impulse = repeat(0.,l); impulse[0] =1.
x = arange(0,l)
response = signal.lfilter(b,a,impulse)
subplot(211)
stem(x, response)
ylabel('Amplitude')
xlabel(r'Index')
title(r'Impulse response')
n = 21
a = signal.firwin(n, cutoff = 0.3, window = "hamming")
#Impulse and step response
figure(2)
impz(a)
show()
I've attached you how should the pyplot look like:
Why it's the red line in the last picture not crossing through the points of the plot? Any ideas why? Thanks!
plt.stem
draws dots with vertical lines connected via a baseline, as shown in your second plot. To draw dots and connected line segments, there is plt.plot
with many options.
Note that usually pyplot
is imported as plt
. That way, people can quickly see where the plotting related code is happening, which makes it easier to understand and maintain.
Similarly, numpy
is imported as np
. Numpy functions can work on complete arrays similar to working with single variables ("broadcasting"), which only works fine when all functions and arrays use numpy. The prefix np
helps in visually checking this.
Here is the sample code with the standard way of importing pyplot and numpy. And with a call to plt.plot
to obtain a plot similar to the desired one. The variable in the main code is renamed to b
to make the call to the impz
function easier to follow.
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as signal
# Plot step and impulse response
def impz(b, a=1):
l = len(b)
impulse = np.repeat(0., l)
impulse[0] = 1.
x = np.arange(0, l)
response = signal.lfilter(b, a, impulse)
plt.subplot(211)
# plt.stem(x, response)
plt.plot(x, response, color='blueviolet', marker='o', markerfacecolor='none', markeredgecolor='dodgerblue')
plt.ylabel('Amplitude')
plt.xlabel('Index')
plt.title('Impulse response')
n = 21
b = signal.firwin(n, cutoff=0.3, window="hamming")
# Impulse and step response
plt.figure(2)
impz(b)
plt.show()