The previous article Python draws the bode diagram of the rc low-pass filter takes the rc low-pass filter as an example to explain the method of drawing the bode diagram with python.
This article introduces how to use python to draw waveform diagrams, and take the rc low-pass filter as an example to draw waveform diagrams respectively to intuitively understand the function of the filter.
Simple sine wave
First draw a 1kHz sine wave:
import numpy as np import matplotlib.pyplot as plt from scipy import signal # Set the sampling frequency fs = 20000 T = 1/fs # Generate sampling points with a duration of 0.01 seconds t = np.arange(0, 0.01, T) # generate signal x1 = np.sin(2*np.pi*1000*t) # time domain waveform plt.plot(t, x1) plt.title("1kHz sin waveform") # draw plt. show
You can also add frequency domain plots:
import numpy as np import matplotlib.pyplot as plt from scipy import signal # Set the sampling frequency fs = 20000 T = 1/fs # Generate sampling points with a duration of 1 second t = np.arange(0, 0.01, T) # generate signal x1 = np.sin(2*np.pi*1000*t) # time domain plot plt. subplot(2, 1, 1) plt.plot(t, x1) plt.title("1kHz sin waveform") # frequency domain plot plt. subplot(2, 1, 2) f = np.arange(0,fs/2,fs/len(x1)) Y = np.fft.fft(x1)/len(x1)*2 plt.plot(f, abs(Y[:len(Y)//2])) plt.title('Frequency Spectrum of 1kHz sin Signal') plt.tight_layout() plt. show()
Composite waveform
The following composites a 50Hz sine wave and a 500Hz sine wave:
import numpy as np import matplotlib.pyplot as plt from scipy import signal # Set the sampling frequency fs = 20000 T = 1/fs # Generate sampling points with a duration of 0.1 seconds t = np.arange(0, 0.1, T) # generate signal x1 = np.sin(2*np.pi*50*t) x2 = np.sin(2*np.pi*500*t) x = x1 + x2 # Time Domain plt.subplot(2,1,1) plt.plot(t, x) plt. title('Time Domain') # frequency domain plt.subplot(2,1,2) f = np.arange(0,fs/2,fs/len(x)) Y = np.fft.fft(x)/len(x)*2 plt.plot(f, abs(Y[:len(Y)//2])) plt. title('Frequency Domain') plt.tight_layout() plt. show()
Low pass filter
Filter the above composite waveform with the filter of the python signal library:
import numpy as np import matplotlib.pyplot as plt from scipy import signal # Set the sampling frequency fs = 20000 T = 1/fs # Generate sampling points with a duration of 0.1 seconds t = np.arange(0, 0.1, T) # generate signal x1 = np.sin(2*np.pi*50*t) x2 = np.sin(2*np.pi*500*t) x = x1 + x2 # Plot the original signal in time domain plt.subplot(3,1,1) plt.plot(t, x) plt. title('Original Signal') # design low pass filter fc = 159 # cutoff frequency 159Hz Wn = (fc/(fs/2)) b, a = signal. butter(1, Wn, 'low') # apply filter y = signal.filtfilt(b, a, x) # Plot the filtered signal in time domain plt.subplot(3,1,2) plt.plot(t, y) plt.title('Low-pass Filtered Signal') # Plot the filtered signal in the frequency domain plt.subplot(3,1,3) f = np.arange(0,fs/2,fs/len(y)) Y = np.fft.fft(y)/len(y)*2 plt.plot(f, abs(Y[:len(Y)//2])) plt.title('Frequency Spectrum of Filtered Signal') plt.tight_layout() plt. show()
It can be seen that the first-order low-pass filter attenuates the 500Hz signal a lot, but it still exists. Without changing the cut-off frequency, we can increase the order of the filter to achieve better filtering effect.
The output of the 2nd-order filter is relatively smooth, which is very close to the sine wave. The filter in the python signal library is a digital filter. We can use it to intuitively understand the filter characteristics, but the drawn waveform will be different from the actual rc circuit. To obtain a waveform closer to the actual circuit, you can use spice simulation .
spice simulation
There are many derivative versions of spice. Here, the simulation tool built in kicad is used for simulation, which is based on the ngspice engine.
First draw the schematic diagram:
In the article on drawing the bode diagram of rc low-pass filter in python, we already know that the cutoff frequency of the first-order RC low-pass filter connected in series with 1kΩ resistor and 1uF is 159.15Hz. In the above schematic diagram, I set the signal source frequency as 100Hz, let’s look at the simulation output first:
It can be seen that the 100Hz signal has been attenuated, let’s try the 50Hz signal again:
50Hz signal attenuation is less. From the previous two simulation waveforms, not only the amplitude change of the output signal can be seen, but also the phase change can be seen. Next, let’s verify the bode diagram in this article to draw the bode diagram of the rc low-pass filter in python, set the signal source frequency to 159.15Hz, and look at the simulation output:
Theoretically, the low-pass filter cutoff frequency amplitude is -3dB, and the phase is -45°. As can be seen from the above figure, the output signal amplitude is about 707mV, which is -3dB of the input, and the phase needs to be calculated:
Δ
t
=
27.477
?
26.7
=
0.777
m
the s
\Delta t = 27.477 – 26.7 = 0.777ms
Δt=27.477?26.7=0.777ms
45° is 1/8 cycle, for a 159.15Hz signal, 45° corresponds to:
t
=
T
8
=
1
8
f
=
1
159.15
x
8
=
0.785
m
the s
t = \frac {T} {8} = \frac {1} {8f} = \frac {1} {159.15 \times 8} = 0.785 ms
t=8T?=8f1?=159.15×81?=0.785ms
Considering that the cursor is moved manually, the simulation results should be consistent with the theoretical results.
Let’s simulate the filtering effect again:
The input signal is consistent with the above python code, 50Hz and 500Hz sine waves, it can be seen that the filtering effect is not very good, the roll-off rate of the first-order rc filter is 10dB/decade, that is, every 10 times the frequency is attenuated by 10dB, we put the high-frequency signal Try again from 500Hz to 5000Hz:
It can be seen that the 5000Hz signal has been attenuated to a very small level.
Public Account | FunIO
Search “funio” on WeChat to find more exciting content.
Personal Blog | boringhex.top