/* S0730.java * 移動平均による信号の分離 * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class S0730 extends Applet implements ActionListener { double X0 = 1.0; /* 信号の振幅 */ double OMEGA = 2.0 * Math.PI * 0.1; /* 信号の角周波数 */ double X_NOISE = 0.1; /* 雑音の標準偏差 */ double T_END = 10.0; /* シミュレーションの終了 */ double DT = 0.01; /* シミュレーションのきざみ */ int M = 11; /* 平均化点数 */ Button button0; Button button1; boolean sw = false; public void init() { button0 = new Button(" 実行 "); button1 = new Button("クリア"); add(button0); add(button1); button0.addActionListener(this); button1.addActionListener(this); } public void actionPerformed(ActionEvent e) { String label = e.getActionCommand(); if (label.equals(" 実行 ")) sw = true; else sw = false; repaint(); } public void paint(Graphics g) { Window w; w = new Window(); int height; int width; int SPACE = 30; double x = 0.0; /* 雑音+信号 */ double u = 0.0; /* 出力 */ double sum = 0.0; /* (1)本文参照 */ double mem[] = new double[M]; /* (2)本文参照 */ int i; /* シミュレーションのクロック */ /* スクリーンの大きさを読み取る */ width = getSize().width; height = getSize().height; /* グラフィックの準備 */ w.setWindow(0, 0.0, -2.0, T_END, 2.0, SPACE, height / 2 - SPACE / 2, width - SPACE, SPACE); w.axis(0, "t", 1.0, "e", 0.5, g); w.setWindow(1, 0.0, -2.0, T_END, 2.0, SPACE, height - SPACE, width - SPACE, height / 2 + SPACE / 2); w.axis(1, "t", 1.0, "v", 0.5, g); w.moveTo(0, 0.0, 0.0, g); w.moveTo(1, 0.0, 0.0, g); /* メイン */ if (sw == true) { for (i = 0; i <= T_END/DT; i ++) { x = noise(X_NOISE) + X0 * Math.sin(OMEGA * i * DT); if (i < M) { /* 0からM-1までの始めの部分 */ sum = sum + x; mem[i] = x; } else { /* M以上の場合 */ sum = sum + x - mem[(i - M) % M]; /* (4)本文参照 */ mem[i % M] = x; /* (3)本文参照 */ } u = sum / M; g.setColor(Color.green); w.lineTo(0, i * DT, x, g); w.lineTo(1, i * DT, u, g); } stop(); } } double noise(double xn) { double xw = 0.0; int i; for (i = 1; i <= 12; i ++){ xw = xw + Math.random(); } return(xn * (xw - 6.0)); } }