努比是有毛病还是我做错了什么?

2024-09-29 11:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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公司功能?你知道吗


Tags: sampleimportnppisqrtfsdotoffset
1条回答
网友
1楼 · 发布于 2024-09-29 11:25:17

好的,我找到答案了,但忘了在这里回复。你知道吗

最重要的是变量x是错的,它不应该是一个np.arange公司(sample),这将提供一个从0到sample的数组,这是错误的。当我看到我意识到我做错了。。。我之所以这么做,是因为我经常使用arange,结果我就这样用了。。。所以我修正为x=np.arange公司(0,1/f,1/Ts),其中Ts=1/fs。在我做了那件事之后,一切都开始完美地运转起来。你知道吗

所以。。。给大家的一个提示,即使是你每天都在使用/做的事情,在你像往常一样使用/做它之前,先想一想,有时你可以防止bug,节省几天的调试时间

相关问题 更多 >