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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# encoding: utf-8
import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack
f_Signal = 50
n_Sample = 10**2
f_Sampling = f_Signal * n_Sample
amp = 10
def get_rms(signal_):
return np.sqrt(np.mean([s**2 for s in signal_]))
def get_fft(signal_):
fft = fftpack.fft(signal_)
return abs(fft/f_Sampling)*2
time_line = np.linspace(0.0, 1/f_Signal, f_Sampling)
signal = [amp if t < 1/2/f_Signal else -amp for t in time_line]
freq_line = [x*f_Signal for x in range(f_Sampling)]
for index in range(f_Sampling//2):
signal[index], signal[-index], signal[f_Sampling // 2-index], signal[f_Sampling // 2 + index] = 0, 0, 0, 0
rms = get_rms(signal)
if rms <= amp/3:
break
timer = 0
for x in signal:
if x:
timer += 1
print('t/T = {}'.format(timer/f_Sampling))
print('Amp/RMS = {}'.format(max(signal) if max(signal) > abs(min(signal)) else abs(min(signal)/get_rms(signal))))
fft_signal = get_fft(signal)
print("СКЗ по спектру: {}".format(np.sqrt(sum([x**2 for x in fft_signal]))/2))
print("СКЗ по отсчетам: {}".format(get_rms(signal)))
plt.subplot(211)
plt.plot(time_line, signal)
plt.subplot(212)
plt.stem(freq_line[:18*5], fft_signal[:18*5]),plt.xlim(0,18*5*f_Signal)
plt.show()