/* S0720.java * CRフィルタのシミュレーション * (C) H.Ishikawa 2008 */ package simulation; import java.applet.*; import java.awt.*; import java.awt.event.*; import window.Window; public class S0720 extends Applet implements ActionListener { double C = 1.0; double R = 0.1; double E0 = 1.0; /* 信号の振幅 */ double OMEGA = 2.0 * Math.PI * 0.1; /* 信号の角周波数 */ double E_NOISE = 0.1; /* 雑音の標準偏差 */ double T_END = 10.0; /* シミュレーションの終了 */ double DT = 0.01; /* シミュレーションのきざみ */ 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 e, v; double t; double k1, k2, k3, k4; /* スクリーンの大きさを読み取る */ 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) { v = 0.0; for (t = 0.0; t <= T_END; t = t + DT) { e = noise(E_NOISE) + E0 * Math.sin(OMEGA * t); k1 = DT * cr(e, v); //ここから k2 = DT * cr(e, v + k1 / 2.0); k3 = DT * cr(e, v + k2 / 2.0); k4 = DT * cr(e, v + k3); v = v + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; //ここまでルンゲクッタ g.setColor(Color.green); w.lineTo(0, t, e, g); w.lineTo(1, t, v, g); } } stop(); } public double cr(double e, double v) { //回路方程式 return ((e - v) / (C * R)); } public double noise(double en) { //ノイズ発生 double xw = 0.0; for (int i = 1; i <= 12; i++) { xw = xw + Math.random(); } return (en * (xw - 6.0)); } }