0
0

More than 1 year has passed since last update.

【Python】ノイズを含む信号を2値化する

Posted at

目的

オシロスコープで観測した波形等のノイズを含む信号を2値化する。

課題

ノイズの影響により、変化のタイミングや変化の回数が正しく求まらない。
img_hein_nashi.png

解決策

移動平均でデータを平滑化してから2値化する。

コード

import numpy as np
import matplotlib.pyplot as plt

# 時間行列
t = np.linspace(0,10,100)

# 真値
y_true = np.sin(t) + 1/3*np.sin(3*t) + 1/5*np.sin(5*t)

# 観測値(真値+ノイズ)
y_obs = y_true + np.random.randn(100)*0.3

# 平均化に使用する行列
num = 5 # 移動平均に用いる個数
k = np.ones(num)/num # 平均化に使用する行列の重み。移動平均なので均等

# 観測値を移動平均
y_fil = np.convolve(y_obs, k, mode='same')

# 移動平均を2値化(0より大きければ1,0以下であれば0)
y_bin = np.zeros(len(y_fil))
y_bin[y_fil>0] = 1

# グラフ化
fig, ax = plt.subplots()
ax.plot(t, y_true,'r') # 真値
ax.plot(t, y_obs,'k-') # 観測値
ax.plot(t, y_fil,'b--') # 移動平均
ax.plot(t, y_bin,'g-.') # 二値化
ax.legend(['真値','観測値','移動平均','二値化'], prop={"family":"MS Gothic"})

# グラフを保存
fig.savefig("img.png")

結果

img_hein_ari.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0