我在python中创建了两个正弦波,以测试一些算法,特别是测量和修复某些相位之间的延迟。它应该是模拟电源电压和电流
from math import *
from random import randint
import numpy as np
import matplotlib.pyplot as plt
f = 60
fs = f * 144
sample = fs
def wave(peakv, peaki, angle = 0):
x = np.arange(sample)
vrms = 0
irms = 0
rads = 2 * np.pi
if angle == 0:
angulo = 0
offset = 0
else:
angulo = 360 / angle
offset = rads / angulo
tcoffset = - rads * 6/ (360) #+ 1 * error * rads /360
offset = offset - tcoffset
v = peakv * np.sin(2*np.pi*x/fs) * 1.5035 + 0.6
i = peaki * np.sin(2 * np.pi * x / fs - offset) * 1.92 * 20 + 0.15
#rms
vrms = np.sqrt(np.dot(v, v)/sample)
irms = np.sqrt(np.dot(i, i)/sample)
#power
S = vrms * irms
Pa = 0
Pa = np.dot(v, i)
Pa /= sample
PF = Pa/S
print '------------------------------------------'
print 'Vrms = ', vrms
print 'Irms = ', irms
print 'Apparent power = ', S #* (angle * pi / 180)
print 'Power = ', Pa
print 'Power factor = ', PF
print 'theta = ', np.arccos(PF) * 180 / np.pi
print '************************************************'
print
print 'Using calibration ... '
#Calibrsating gain and offset
gv = (peakv/sqrt(2))/vrms
gi = (peaki/sqrt(2))/irms
ov = (max(v) + min(v))/2
oi = (max(i) + min(i))/2
v = gv * v - ov * gv
i = (gi * i - oi * gi)
#
prev = 0
#applying allpass filter
vout = np.arange(sample)
iter = 0
vout = [0] * sample
for n in np.nditer(v, op_flags=['readwrite']):
vout[iter] = n - 0.99332 * (n - vout[iter-1]) + prev
prev = n
#vout[iter] *= 0.49
iter += 1
v = vout
vrms = np.sqrt(np.dot(v, v)/sample) / 149.84
irms = np.sqrt(np.dot(i, i)/sample)
S = (vrms * irms)
newp = np.dot(i, v)/sample / 149.84
newPF = newp/S
print 'Corrected theta allpass = ', np.arccos(newp/S) * 180 / np.pi
print 'Calibrated Voltage = ', vrms
print 'Calibrated Current = ', irms
print 'Calibrated Apparent Power = ', S
print 'Calibrated Active power = ', newp
print 'Calibrated Power Factor = ', newPF
print '------------------------------------------'
if __name__ == "__main__":
r = sqrt(2)
wave(127*r, 5*r, 70)这应该是为了纠正电流互感器在不同的功率因数中增加的相位偏移。它工作@60°,从0-50和90°.因为某种原因当你投入51,52,53,.它计算了两个阶段之间的完全相同的角度,从61-72,然后在80年代,它给出了完全相同的值。
我的标题是误导性的,因为我知道在numpy中出错的可能性非常低,但是我没有想法,当我用大多数的值测试它时,它没有问题,我甚至可以用很多问题来绘制它们,它们看起来很好。我的问题是那些价值观..。我真的不知道发生了什么,也许是np.sin函数的舍入问题?
发布于 2018-03-05 16:01:32
好的,我找到答案了,但忘了在这里回答。
最重要的是,变量x是错误的,它不应该是一个np.arange(示例),这给出了一个从0到sample的数组,这是错误的。当我意识到我做错了.我这么做只是因为我经常使用阿兰奇,最后我就这样使用了它.所以我修正为x= np.arange(0,1/f,1/ Ts ),其中Ts= 1/fs。在我这么做之后,一切都开始完美地运作了。
所以..。给每个人写一条便条,即使这是你每天使用/做的事情,在你像往常一样使用/做它之前想一想,有时候你可以防止bug,节省几天的调试时间。
https://stackoverflow.com/questions/46184523
复制相似问题