2017-06-27 38 views
0

私はRaspberry Pi 3のCPU温度を測定しており、非常にノイズの多い値を取得しています。 Raw values from Temperature sensor in Raspberry Pi 3 温度は1秒ごとに測定され、移動平均(別名スライディングウィンドウ)を介して入力する必要があります。 5秒ごとに、新しいTemp.valueをSQLiteデータベースに入れる必要があります。 これまでデータの静的配列のみの解決策を見つけました。誰もが問題を助けることができますか?毎秒値をとり、5秒ごとに平均値を返す関数が必要です。 このアルゴリズムは、湿度、電圧などの値に適用することができます。 ラズベリーパイ3の温度センサの精度は0.538°Cです。あなただけ「のためにスリープ状態にtimer.sleepを()関数内でループ内の温度を読み、使用することはでき、温度値の移動平均(Python)

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

# Das Programm speist Temperaturen von CPU ins SQLite Datenbank für spätere 
# Darstellung auf einer Webseite 

from subprocess import check_output 
import sqlite3 
import time 
import numpy 

# Pfad zur Datenbank 
dbname = "/var/www/cpuTemp_Database.db" 
temp_array = [] 

def log_temperature(currentTemp): 
    """ Speichern in die Datenbank""" 
    myConnection = sqlite3.connect(dbname) 
    myCursor = myConnection.cursor() 
    myCursor.execute("INSERT INTO temps VALUES (datetime('now', 'localtime'), (?))", (currentTemp,)) 
    myConnection.commit() 
    myConnection.close() 


def abfrage(): 
    """ Macht CPU-Temperaturabfrage und gibt das Wert zurück wie 53.692 """ 
    output = check_output(['cat', '/sys/class/thermal/thermal_zone0/temp']) 
    stripedVal = output.strip() 
    tempVal = float(stripedVal)/1000 
    return tempVal 

def main(): 
    """ Main Function""" 
    while True: 
     for i in range(5): 
      tempValue = abfrage() 
      temp_array.append(tempValue) 
      time.sleep(1) 
     meanTemp = numpy.median(temp_array) 
     log_temperature(meanTemp) 
     temp_array [:] = [] 

if __name__ == "__main__": 
    main() 

Greeringsアレックス

+0

平均はあなたのために計算できます。 [この質問を見る](https://stackoverflow.com/questions/797053/sqlite-how-to-get-average-value)ソートマジックは自分で設定するだけです。 – Kraay89

+0

5ポイントの移動平均は、データポイントの平均値とその両側の4ポイントだと思いましたか?例えば、リスト1,2,3,4,5,6,7は、x、x、3,4,5、x、xの5点移動平均を持つでしょう –

+0

入力をありがとう! 平均値を5秒ごとにDatabaseに書き込むと、SDカードの負荷が軽減され、グラフの精度が向上します。 CPUは大きなラジエーターを持っていますが、温度はそれほど速く変化しません。しかし、極端な状況下では、スライディングウィンドウは3つの値に減らすことができます。 – AlexBaker

答えて

0

:ここ

は私が現時点で持っているものです1回の読み取りと1回の読み取りの間に1秒間、この関数をwhile()ループ内で呼び出して連続して実行することができます。

編集:コメントで判断すると、質問をよく理解していました。もし私がdidntすれば、d o私に教えてください。私はこの答えを削除します。

1

あなたが求めているのは、実際の移動平均ではない過去5秒間の平均読み取り値を得るのに5秒ごとです。これはあなたにその行動を与えるはずです。

import numpy 

# Once you input enough new data points into this function, it will calculate 
# and log the average of the last several data points. 
def log_periodic_average(new_data_point, window, frequency): 
    window.append(new_data_point) 
    if (len(window) >= frequency): # Now is the only time we calculate an average 
    average = numpy.mean(window) 
    log_temperature(average) # Using your function to save the average 
    window = [] # Clear window to collect the next 5 seconds of data 
    return window 

window = [] 
while True: 
    point = abfrage() # using your function to get current temperature 
    window = log_periodic_average(point, window, 5) 
    sleep(1000)