encoding utf-8 import numpy as np import matplotlib pyplot as plt from

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# encoding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack, signal
n_Sample = 100
f_Signal = 50
f_sampling = f_Signal*n_Sample
n_Periods = 3
plt_counter = 421
def show_signal(x, y, title):
global plt_counter
plt.subplot(plt_counter)
plt.plot(x, y), plt.title(title), plt.xlabel('t, c')
plt.ylabel('U, В'), plt.grid(), plt.axhline(color='black')
y_fft = abs(fftpack.fft(y))*2/n_Sample
x_fft = np.linspace(0, f_sampling, len(y_fft))
plt.subplot(plt_counter+1)
plt.stem(x_fft, y_fft), plt.title(title), plt.xlabel('f, Гц')
plt.ylabel('U, В'), plt.grid(), plt.xlim(0,f_Signal*50)
plt_counter += 2
t = np.linspace(0, 1/f_Signal*n_Periods, n_Sample*n_Periods)
sawtooth = signal.sawtooth(2*np.pi*50*t)/2+0.5
show_signal(t,sawtooth,'Пила')
square = signal.square(2*np.pi*50*t)/2+0.5
show_signal(t,square,'Меандр')
triangle = signal.sawtooth(2*np.pi*50*t, 0.5)/2+0.5
show_signal(t,triangle,'Триугольник')
trapmf = [1 if x>1 else x for x in signal.sawtooth(2*np.pi*50*t, 0.5)/1.5+1/1.5]
show_signal(t,trapmf,'Трапеция')
plt.show()