【问题标题】:Best python method for calculating kw/h from data set从数据集中计算kw/h的最佳python方法
【发布时间】:2016-05-23 22:51:26
【问题描述】:

所以我有一个系统以设定的时间间隔(大约每 30 秒)从我的功率计读取电流。

然后我想在一个多小时内获取这些读数并计算使用的千瓦数。

我在 stakoverflow 上看到了 2x 方法,想知道哪种方法最好。

方法 1 从这里开始: https://electronics.stackexchange.com/a/225641

方法 2 从这里开始: https://stackoverflow.com/a/16331700/5151115

这是我的测试代码(这是在将电流转换为功率之后) 我正在模拟加热器和每 30 秒和 29 秒的读数。 读数只是 1350w -> 1400w -> 1350w。

# build sample data
time = 0
samples = [[0, 1350]]
for i in range(0, 61):
   samples.append([time+30, 1400])
   samples.append([time+59, 1350])
   time += 59

n_samples = len(samples)

# METHOD 1
total_ws = 0.0
for i in range(1, n_samples):
   dt = samples[i][0] - samples[i-1][0]
   average = (samples[i][1] + samples[i-1][1]) / 2
   total_ws += average * dt

print("Total w/s: %0.2f" % total_ws)
print("Total w/h: %0.2f" % (total_ws/samples[-1][0]))


# METHOD 2
total_ws = 0.0
for i in range(0, n_samples - 1):
   total_ws += (samples[i][1] + samples[i + 1][1]) / 2;

total_ws *= (samples[-1][0] - samples[0][0]) / n_samples;

print("Total w/s: %0.2f" % total_ws)
print("Total w/h: %0.2f" % (total_ws/samples[-1][0]))

方法一 总 w/s: 4948625.00 总 w/h:1375.00 = 1.375 Kw/h

方法二: 总 w/s:4908392.28 总 w/h:1363.82 = 1.363 Kw/h

哪种方法更准确/最好?

第一种方法对我来说似乎更好,因为它给了我 1375。 这是 2 倍不同读数(1350 和 1400)的平均值。

【问题讨论】:

  • python 在浮点数和浮点除法方面出了名的糟糕,看看 pandas 和 numpy 以获得更好的数字支持
  • 只是想。对于每 30 秒一次的采样率,在我看来,您已经失去了足够多的可变性,以至于您使用哪种方法都无关紧要。
  • 谢谢。我更关心上述 2x 中的最佳方法。对我来说,第一种方法似乎更好,因为它给出了 1375,这是我在测试数据(1350 和 1400)中使用的 2 倍读数的平均值。我也可能会增加阅读间隔。我使用的是电池供电的 ESP8266,所以它可以休眠的时间越长 - 电池持续时间越长。

标签: python


【解决方案1】:

仅当测量之间的 dt 是恒定的并且我不喜欢它时才可以使用第二种方法,因为它是错误的来源。第一种方法对于不同的 dt 和精度要好得多,但我更喜欢另一种。看这张图,

M1 站在第一路。
“我的”方法是 EQUAL。

# build sample data
time = 0
samples = [[0, 1350]]
for i in range(0, 200):
   samples.append([time+30, 1400])
   samples.append([time+60, 1350])
   time += 60

n_samples = len(samples)

# METHOD 1
total_ws = 0.0
total_t=0
for i in range(1, n_samples):
   dt = samples[i][0] - samples[i-1][0]
   average = (samples[i][1] + samples[i-1][1]) / 2
   total_ws += average * dt
   total_t+=dt
print("Total ws: %0.2f" % total_ws)
print("Total w: %0.2f" % (total_ws/total_t))

# METHOD 2
total_ws = 0.0
for i in range(0, n_samples - 1):
   total_ws += (samples[i][1] + samples[i + 1][1]) / 2;
total_ws *= (samples[-1][0] - samples[0][0]) / n_samples;
print("Total ws: %0.2f" % total_ws)
print("Total w: %0.2f" % (total_ws/samples[-1][0]))


# "My" METHOD 
total_ws = 0.0
total_t=0
for index in range(len(samples)):
    if index==0:dt=samples[index+1][0]/2.
    elif index==len(samples)-1:dt=(samples[index][0]-samples[index-1][0])/2.
    else:dt=(samples[index+1][0]-samples[index-1][0])/2.
    total_t+=dt
    total_ws+=samples[index][1] * dt
print("Total ws: %0.2f" % total_ws)
print("Total w: %0.2f" % (total_ws/total_t))

请注意我在方法1中所做的修改

>>Total ws: 16500000.00
>>Total w: 1375.00
>>Total ws: 15950000.00
>>Total w: 1329.17
>>Total ws: 16500000.00
>>Total w: 1375.00

【讨论】:

  • 太棒了。所以方法2出来了。我真的看不出 Method1 和“我的”方法之间的区别。
  • 是的,方法 2 已经过时了,它们实际上是相同的(但 CPU 需求有点不同)。表面积相等。但我更喜欢另一个(只是个人原因:更多图形和更逻辑)
  • 在极端精度的情况下,您可以设置一个偏移比,在这个例子中它被设置为 0.5(中间时间在两点之间)。如果您知道导数,则可以设置偏移量以获得更好的精度。(非线性插值)
  • 有什么想法可以在计算小时的时间间隔内“加权”吗?例如。在 3500 秒内计算的 1500w 应被视为高于在 3400 秒内计算的 1500w。
  • 我觉得你的句子里少了一个词:我听不懂
【解决方案2】:

t2-t1 = 1 小时
S1 是从 t1 到 t2 的蓝色曲线下的面积(又名积分)。
E1(焦耳)=S1(w)/3600(s),所以它代表了当地下一小时的总耗电量。
然后,您只需选择一天中的最大 E1 即可获得最耗电的时间,在这种情况下 h=7 到 7+1 !

【讨论】:

    猜你喜欢
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多