計算誤差に対して安定な再帰型移動平均フィルタ

\[ % 汎用 \newcommand{\ctext}[1]{\raise0.2ex\hbox{\textcircled{\scriptsize{#1}}}} % 数学 % 汎用 \DeclarePairedDelimiterX{\parens}[1]{\lparen}{\rparen}{#1} \DeclarePairedDelimiterX{\braces}[1]{\lbrace}{\rbrace}{#1} \DeclarePairedDelimiterX{\bracks}[1]{\lbrack}{\rbrack}{#1} \DeclarePairedDelimiterX{\verts}[1]{|}{|}{#1} \DeclarePairedDelimiterX{\Verts}[1]{\|}{\|}{#1} \newcommand{\as}{{\quad\textrm{as}\quad}} \newcommand{\st}{{\textrm{ s.t. }}} \DeclarePairedDelimiterX{\setComprehension}[2]{\lbrace}{\rbrace}{#1\,\delimsize\vert\,#2} \newcommand{\naturalNumbers}{\mathbb{N}} \newcommand{\integers}{\mathbb{Z}} \newcommand{\rationalNumbers}{\mathbb{Q}} \newcommand{\realNumbers}{\mathbb{R}} \newcommand{\complexNumbers}{\mathbb{C}} \newcommand{\field}{\mathbb{F}} \newcommand{\func}[2]{{#1}\parens*{#2}} \newcommand{\argmax}{\mathop{\textrm{arg~max}}} \newcommand{\argmin}{\mathop{\textrm{arg~min}}} % 集合論 \newcommand{\range}[2]{\braces*{#1,\dotsc,#2}} \providecommand{\complement}{}\renewcommand{\complement}{\mathrm{c}} \newcommand{\ind}[2]{\mathbbm{1}_{#1}\parens*{#2}} \newcommand{\indII}[1]{\mathbbm{1}\braces*{#1}} % 数論 \newcommand{\abs}[1]{\verts*{#1}} \newcommand{\combi}[2]{{_{#1}\mathrm{C}_{#2}}} \newcommand{\perm}[2]{{_{#1}\mathrm{P}_{#2}}} \newcommand{\GaloisField}[1]{\mathrm{GF}\parens*{#1}} % 解析学 \newcommand{\NapierE}{\mathrm{e}} \newcommand{\sgn}[1]{\operatorname{sgn}\parens*{#1}} \newcommand{\cl}[1]{\operatorname{cl}#1} \newcommand{\Img}[1]{\operatorname{Img}\parens*{#1}} \newcommand{\dom}[1]{\operatorname{dom}\parens*{#1}} \newcommand{\norm}[1]{\Verts*{#1}} \newcommand{\floor}[1]{\left\lfloor#1\right\rfloor} \newcommand{\ceil}[1]{\left\lceil#1\right\rceil} \newcommand{\expo}[1]{\exp\parens*{#1}} \newcommand{\sinc}{\mathop{\textrm{sinc}}} \newcommand{\GammaFunc}[1]{\Gamma\parens*{#1}} \newcommand{\erf}{\mathop{\mathrm{erf}}} % 逆三角関数 \newcommand{\asin}[1]{\operatorname{Sin}^{-1}{#1}} \newcommand{\acos}[1]{\operatorname{Cos}^{-1}{#1}} \newcommand{\atan}[1]{\operatorname{{Tan}^{-1}}{#1}} \newcommand{\atanEx}[2]{\atan{\parens*{#1,#2}}} % 畳み込み \newcommand{\cycConv}[2]{{#1}\underset{\text{cyc}}{*}{#2}} % 微分 \newcommand{\deriv}[3]{\frac{\operatorname{d}^{#3}#1}{\operatorname{d}{#2}^{#3}}} \newcommand{\derivLong}[3]{\frac{\operatorname{d}^{#3}}{\operatorname{d}{#2}^{#3}}#1} \newcommand{\partDeriv}[3]{\frac{\operatorname{\partial}^{#3}#1}{\operatorname{\partial}{#2}^{#3}}} \newcommand{\partDerivLong}[3]{\frac{\operatorname{\partial}^{#3}}{\operatorname{\partial}{#2}^{#3}}#1} \newcommand{\partDerivIIHetero}[3]{\frac{\operatorname{\partial}^2#1}{\partial#2\operatorname{\partial}#3}} \newcommand{\partDerivIIHeteroLong}[3]{{\frac{\operatorname{\partial}^2}{\partial#2\operatorname{\partial}#3}#1}} % 積分 \newcommand{\integrate}[5]{\int_{#1}^{#2}{#3}{\mathrm{d}^{#4}}#5} \newcommand{\LebInteg}[4]{\int_{#1} {#2} {#3}\parens*{\mathrm{d}#4}} % 複素解析 \newcommand{\conj}[1]{\overline{#1}} \providecommand{\Re}{}\renewcommand{\Re}[1]{{\operatorname{Re}{\parens*{#1}}}} \providecommand{\Im}{}\renewcommand{\Im}[1]{{\operatorname{Im}{\parens*{#1}}}} \newcommand{\Arg}[1]{\operatorname{Arg}{\parens*{#1}}} \newcommand{\Log}[1]{\operatorname{Log}{#1}} % ラプラス変換 \newcommand{\LPLC}[1]{\operatorname{\mathcal{L}}\parens*{#1}} \newcommand{\ILPLC}[1]{\operatorname{\mathcal{L}}^{-1}\parens*{#1}} % 離散Fourier変換 \newcommand{\DFT}[1]{\mathrm{DFT}\parens*{#1}} % Z変換 \newcommand{\ZTrans}[1]{\operatorname{\mathcal{Z}}\parens*{#1}} \newcommand{\IZTrans}[1]{\operatorname{\mathcal{Z}}^{-1}\parens*{#1}} % 線形代数 \newcommand{\bm}[1]{{\boldsymbol{#1}}} \newcommand{\matEntry}[3]{#1\bracks*{#2}\bracks*{#3}} \newcommand{\matPart}[5]{\matEntry{#1}{#2:#3}{#4:#5}} \newcommand{\diag}[1]{\operatorname{diag}\parens*{#1}} \newcommand{\tr}[1]{\operatorname{tr}{\parens*{#1}}} \newcommand{\inprod}[2]{\left\langle#1,#2\right\rangle} \newcommand{\HadamardProd}{\odot} \newcommand{\HadamardDiv}{\oslash} \newcommand{\Span}[1]{\operatorname{span}\bracks*{#1}} \newcommand{\Ker}[1]{\operatorname{Ker}\parens*{#1}} \newcommand{\rank}[1]{\operatorname{rank}\parens*{#1}} % ベクトル % 単位ベクトル \newcommand{\vix}{\bm{i}_x} \newcommand{\viy}{\bm{i}_y} \newcommand{\viz}{\bm{i}_z} % 確率論 \newcommand{\PDF}[2]{\operatorname{PDF}\bracks*{#1,\;#2}} \newcommand{\Ber}[1]{\operatorname{Ber}\parens*{#1}} \newcommand{\Beta}[2]{\operatorname{Beta}\parens*{#1,#2}} \newcommand{\ExpDist}[1]{\operatorname{ExpDist}\parens*{#1}} \newcommand{\ErlangDist}[2]{\operatorname{ErlangDist}\parens*{#1,#2}} \newcommand{\PoissonDist}[1]{\operatorname{PoissonDist}\parens*{#1}} \newcommand{\GammaDist}[2]{\operatorname{Gamma}\parens*{#1,#2}} \newcommand{\cind}[2]{\ind{#1\left| #2\right.}} %条件付き指示関数 \providecommand{\Pr}{}\renewcommand{\Pr}[1]{\operatorname{Pr}\parens*{#1}} \newcommand{\cPr}[2]{\operatorname{Pr}\cPrParens{#1}{#2}} \newcommand{\E}[2]{\operatorname{E}_{#1}\bracks*{#2}} \newcommand{\cE}[3]{\E{#1}{\left.#2\right|#3}} \newcommand{\Var}[2]{\operatorname{Var}_{#1}\bracks*{#2}} \newcommand{\Cov}[2]{\operatorname{Cov}\bracks*{#1,#2}} \newcommand{\CovMat}[1]{\operatorname{Cov}\bracks*{#1}} % グラフ理論 \newcommand{\neighborhood}{\mathcal{N}} % プログラミング \newcommand{\plpl}{\mathrel{++}} \newcommand{\pleq}{\mathrel{+}=} \newcommand{\asteq}{\mathrel{*}=} \]

はじめに

リアルタイム信号処理に於いて、移動平均を高速に計算し続けたいシーンはよくある。窓をずらしながらステップ毎に総和の計算を行うと、1ステップあたりの計算量が$O(N)$($N$は窓の幅)であるが、これを何とかして再帰的に計算できれば1ステップあたりの計算量を窓の幅に依存しないようにできる。信号の長さが有限で、かつそれほど長くなければ、尺取り法で実現できる。しかし信号が入力され続ける場合、計算誤差が蓄積してしまう。本記事では真の移動平均とは少しだけ異なる近似値を計算するかわりに、再帰的に計算する方法を述べる。

方法1

方法1のブロック図
方法1のブロック図

計算量を定数にするために再帰型フィルタで構成する。$N\in\mathbb{N},\;\alpha\in(0,1)$ とする。

計算誤差を無視した場合の理想的な移動平均フィルタは $\alpha=1$ に対応するが、計算誤差のある現実の計算機で $\alpha=1$ とすると、時間の経過とともにループ部分に誤差が際限なく蓄積する。それを防ぐために $\alpha$ を1に十分近く(例えば0.999)とり、ループ部分の開ループゲインの絶対値を1未満にすることで、刻一刻生じる誤差がそれぞれ0に収束するようにする。入力 $u(n)$ の分岐で $N$ ステップ遅延させた信号に乗じている $\alpha^N$ は、ループ部分に入ったパルスを $N$ ステップ後にちょうど消し去るためのスケーリングである。

方法1の限界

$N$ が大きくなる(数十程度)につれ、適切な $\alpha$ を作れなくなる。$\alpha^N$ が1に比べて無視できないほど小さくなってしまうからである。$N$ を1に近づけることで緩和されるが、計算誤差に対する安定性が劣化する。

方法2

方法2のブロック図
方法2のブロック図

方法1の欠点であった、大きい $N$ に対応できないという問題を克服する。ただし後述するがこのシステムは時不変ではない。

方法1で定義した記号は引き続き用いる。$\%$ は割り算の余りを表す。ループ部分は $N$ の倍数の時刻毎に開ループゲインが $\alpha$ となり、それ以外の時刻では1である。よって、任意の時刻にフィルタに入力されたパルスは、その $N$ ステップ遅延の $\alpha$ 倍がループ部分に逆符号で入るまでの間にループ部分でちょうど1回だけ $\alpha$ 倍されるため、両者が相殺する。刻一刻生じる計算誤差はループ部分で定期的に $\alpha$ 倍され縮小するため、フィルタが発散することはない。このフィルタのインパルス応答は時間に依存するため時不変ではないが、$\alpha$ が1に十分近ければ、理想応答との差は微々たるものである。

投稿者: motchy

An embedded software and FPGA engineer for measuring instrument.

コメントを残す