python-scipyHow do I use a Python Scipy Butterworth filter?
To use a Python Scipy Butterworth filter, the following steps should be taken:
- Import the necessary modules:
import scipy.signal as signal
import numpy as np
- Create a signal. For example, a sine wave:
fs = 10e3
N = 1e5
amp = 2*np.sqrt(2)
freq = 1234.0
noise_power = 0.001 * fs / 2
time = np.arange(N) / float(fs)
x = amp*np.sin(2*np.pi*freq*time)
x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
- Create a Butterworth filter of order 3, with a cutoff frequency of 700 Hz:
b, a = signal.butter(3, 700/(fs/2), btype='low')
- Apply the filter to the signal:
filtered_x = signal.lfilter(b, a, x)
- Plot the original and filtered signals:
import matplotlib.pyplot as plt
plt.plot(time, x, label='Noisy signal')
plt.plot(time, filtered_x, label='Filtered signal (%g Hz)' % f0)
plt.xlabel('time (seconds)')
plt.hlines([-amp, amp], 0, time[-1], linestyles='--')
plt.grid(True)
plt.axis('tight')
plt.legend(loc='upper left')
plt.show()
Code explanation
**
import scipy.signal as signal
: This imports thesignal
module from thescipy
package, which contains functions for signal processing.import numpy as np
: This imports thenumpy
package, which provides useful functions for array manipulation.fs = 10e3
: This sets the sampling frequency of the signal to 10 kHz.N = 1e5
: This sets the number of samples in the signal to 100,000.amp = 2*np.sqrt(2)
: This sets the amplitude of the signal to 2√2.freq = 1234.0
: This sets the frequency of the signal to 1234 Hz.noise_power = 0.001 * fs / 2
: This sets the power of the noise to 0.001 times the sampling frequency divided by 2.time = np.arange(N) / float(fs)
: This creates an array of N samples, evenly spaced in time, with the sampling frequency set to fs.x = amp*np.sin(2*np.pi*freq*time)
: This creates a sine wave with the given amplitude, frequency, and sampling frequency.x += np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
: This adds random noise to the signal with the given noise power.b, a = signal.butter(3, 700/(fs/2), btype='low')
: This creates a Butterworth filter of order 3 and cutoff frequency of 700 Hz.filtered_x = signal.lfilter(b, a, x)
: This applies the filter to the signal.import matplotlib.pyplot as plt
: This imports thematplotlib
package, which provides functions for plotting.plt.plot(time, x, label='Noisy signal')
: This plots the noisy signal.plt.plot(time, filtered_x, label='Filtered signal (%g Hz)' % f0)
: This plots the filtered signal.plt.xlabel('time (seconds)')
: This sets the x-axis label to "time (seconds)".plt.hlines([-amp, amp], 0, time[-1], linestyles='--')
: This plots horizontal lines at the given amplitudes.plt.grid(True)
: This turns on the grid.plt.axis('tight')
: This sets the axis limits to the range of the data.plt.legend(loc='upper left')
: This adds a legend to the plot.plt.show()
: This displays the plot.
Relevant Links
More of Python Scipy
- How do I use Python Numpy to create a tutorial?
- How do I calculate a Jacobian matrix using Python and NumPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I create a zero matrix using Python and Numpy?
See more codes...