タイトル通りです。
必要に迫られたので、単純移動平均法でノイズを除去するスクリプト書きました。
MATLABかScilabを使えば良かったんじゃ……というのは無しでお願いします。
以下ソース
import matplotlib.pyplot as plt
time = [] # Store the Data of Time
data = [] # Store the Data
average = [] # Store the Data of Average
# Reading the Data of Time
file = open( 'noise_time.txt', 'r' )
for line in file:
time.append( line )
file.close
# Reading the Data of Wave
file = open( 'noise_data.txt', 'r' )
for line in file:
data.append( line )
file.close
# Plot the Raw Wave
plt.plot( time, data )
pointAve = 121; # Set the points to use Moving Average
# Delete unused data of time ( Part of Begin )
i = 0
while i < pointAve / 2 :
time.pop(0)
i += 1
# Calcurate the Average
i = pointAve / 2;
while i < ( len( data ) - pointAve / 2 ) :
sum = 0.0;
j = 0;
while j < pointAve:
sum += float(data[ i - ( pointAve / 2 ) + j ])
j += 1
ave = sum / pointAve
average.append( ave )
i += 1
# Delete unused data of time ( Part of End )
i = 0
while i < pointAve / 2:
time.pop()
i += 1
# Plot the Moveng Average Wave
plt.plot( time, average )
# Set Title and Labels
plt.title( "Compare Raw Wave and Moving Averaged Wave" )
plt.xlabel( "Time [sec]" )
plt.ylabel( "Signal [V]" )
plt.legend( ("Raw Wave","Moving Averaged Wave"))
plt.show()
もしも、このスクリプトを実行したいという奇特な方がいらっしゃる場合について。
- まず、グラフ描画の部分でmatplotlibに依存してるので、インストールしてください。
- pointAveの値は、平均を取る時の点数を表しています(つまりn点平均のnです。ご存知でしょうが、奇数の値が望ましいです)
- データの入力はテキスト形式のファイルを読み込む事で行っています。
(データ例1)noise_data.txt
-0.928063521 -0.132489882 -1.467890972 5.364932847 ・ ・ ・
(データ例2)noise_time.txt
0 0.000005 0.00001 0.000015 ・ ・ ・
(このスクリプトではtxtファイルからtimeの情報を読み込んでいますが、
別にわざわざtxtから読み込む必要無いと思います。自分で生成すれば良いと思います)
とまあこんな感じでした。
なお、このスクリプトの実行によって何か不都合が起きたとしても責任は取りかねます。
このスクリプトの実行によって何か良い事が起きたら、僕にも幸せのお裾分けの方をよろしくお願いします。
※コメントアウトの英語がおかしいのは仕様ですのでそっとしておいてください。