(法二)
法一在上一篇
初始信息跟上一篇都一样。
设计:
原数据:三个周期的电压数据
截取结果:是从原波形中第一个周期,找到<0,下一个>0,的点。取这个大于0的值,截取波形,两个周期波。
结果为正弦波。
相关信息:采样频率选择3200Hz,按照20ms64个采样点。
主要方法代码:
#找到数组的负数和正数的分界点,返回,这个正数的索引和值
def find_start_point(array):
for i in range(len(array)):
if(array[i]<0 and array[i+1]>0):
return [i+1,array[i+1]]
代码:
#导包
import numpy as np
from matplotlib import pyplot as plt
# 三个周期的电压数据
u = [-2329, -3686, -5160, -6304, -7552, -8708, -9800, -11070, -11755, -12183, -12709, -13141, -13351, -13429, -13436, -13486, -13513, -13557, -13526, -12928, -11901, -11177, -10017, -8669, -7523, -6470, -5255, -4019, -2520, -1143, -263, 1122, 2503, 3837, 5364, 6552, 7773, 8918, 10007, 11236, 12025, 12382, 12941, 13320, 13490, 13550, 13638, 13663, 13691, 13717, 13619, 13044, 12088, 11312, 10176, 8807, 7647, 6623, 5400, 4137, 2716, 1437, 345, -970,
-2374, -3710, -5196, -6383, -7697, -8623, -9850, -11115, -11779, -12202, -12780, -13172, -13330, -13402, -13452, -13478, -13512, -13456, -13530, -12858, -11856, -11112, -9959, -8634, -7436, -6401, -5252, -3971, -2452, -1234, -179, 1219, 2528, 3916, 5411, 6523, 7821, 8978, 10057, 11315, 11938, 12396, 12937, 13340, 13519, 13555, 13637, 13711, 13682, 13772, 13712, 13060, 12027, 11282, 10112, 8747, 7792, 6579, 5403, 4095, 2633, 1363, 322, -1039,
-2383, -3776, -5256, -6425, -7621, -8771, -9907, -11052, -11772, -12260, -12751, -13159, -13345, -13423, -13318, -13532, -13488, -13528, -13478, -12799, -11810, -11079, -9882, -8584, -7465, -6374, -5241, -3774, -2443, -1159, -123, 1219, 2558, 3940, 5473, 6538, 7845, 8971, 10090, 11326, 11952, 12446, 12957, 13291, 13510, 13557, 13627, 13611, 13751, 13694, 13600, 12949, 11994, 11300, 10026, 8704, 7580, 6538, 5374, 3999, 2595, 1328, 217, -1078]
#列表转换成数组
u_arr = np.array(u)
#x轴
Fs = 3200; # 采样频率(按照20ms64个采样点)
T = 1 / Fs; # 采样周期
L = 192; # 信号长度
t = [i * T for i in range(L)]
t = np.array(t)
# 找到与0值最近的点的,索引,值
#参数:两个,要操作的数组(一维数组),要找与谁最近的值
#输出:一个列表,[index,nearest_value]
def find_start_point(array):
for i in range(len(array)):
if(array[i]<0 and array[i+1]>0):
return [i+1,array[i+1]]
start = find_start_point(u_arr[:64])#start[0]为波起始位置
final = start[0]+128#波形两个周期结束位置
y_u = u_arr[start[0]:final]
#x轴是时间,取两个周期的时间即可
x = t[:128]
#绘制波形图
plt.figure()
plt.plot(x, y_u)
plt.xlabel('t')
plt.ylabel("v")
plt.title('v')
plt.grid()
plt.show()