Demo.java
/*
* $Id: Demo.java,v 1.36 2008/07/17 13:35:52 koga Exp $
*
* Copyright (C) 2004 Koga Laboratory. All rights reserved.
*/
package org.mklab.tool.signal;
import java.io.IOException;
import org.mklab.nfc.matrix.DoubleComplexMatrix;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.matrix.IntMatrix;
import org.mklab.nfc.util.Pause;
import org.mklab.tool.Menu;
import org.mklab.tool.graph.gnuplot.Canvas;
import org.mklab.tool.graph.gnuplot.Gnuplot;
import org.mklab.tool.matrix.Conv;
/**
* 信号処理ツールボックスのデモンストレーションを行うクラスです。
*
* @author koga
* @version $Revision: 1.36 $
*/
public class Demo {
/**
* メインメソッド
*
* @param args コマンドライン引数
*/
public static void main(String[] args) {
new Demo().run();
}
/**
* デモンストレーションを行います。
*/
public void run() {
final String title = Messages.getString("Demo.0"); //$NON-NLS-1$
final String options[] = new String[] {Messages.getString("Demo.1"), Messages.getString("Demo.2"), Messages.getString("Demo.3"), Messages.getString("Demo.4")}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
int i = 1;
while (true) {
try {
i = Menu.menu(title, options, i);
switch (i) {
case 1:
runSpectralAnalysisExample();
break;
case 2:
runFourierExpansionExample();
break;
case 3:
runConvolutionExample();
break;
default:
throw new IllegalArgumentException();
}
} catch (@SuppressWarnings("unused") IOException e) {
//
}
if (i == 0 || i == options.length) {
break;
}
}
}
/**
* FFT によるスペクトル解析を行います。
*
* @throws IOException キーボードから入力できない場合
*/
private void runSpectralAnalysisExample() throws IOException {
printNewLine();
System.out.println(Messages.getString("Demo.6")); //$NON-NLS-1$
System.out.print(Messages.getString("Demo.7")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.8")); //$NON-NLS-1$
printNewLine();
Pause.pause();
System.out.println(Messages.getString("Demo.10")); //$NON-NLS-1$
System.out.print(Messages.getString("Demo.11")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.12")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix t = RealMatrix.series(0,0.25,0.001);"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleMatrix t = DoubleMatrix.series(0, 0.25, 0.001);
printNewLine();
System.out.println(Messages.getString("Demo.17")); //$NON-NLS-1$
System.out.println("\tMatrix x = ((RealMatrix)t.multiply(2*Math.PI*40)).sinElementWise()."); //$NON-NLS-1$
System.out.println("\t add(((RealMatrix)t.multiply(2*Math.PI*130)).sinElementWise());\n"); //$NON-NLS-1$
Pause.pause();
DoubleMatrix x = t.multiply(2 * Math.PI * 40).sinElementWise().add(t.multiply(2 * Math.PI * 130).sinElementWise());
printNewLine();
System.out.println(Messages.getString("Demo.21")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix y = x.add(RealMatrix.randomNormal(t));"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleMatrix y = x.add(DoubleMatrix.normalRandom(t));
printNewLine();
System.out.println(Messages.getString("Demo.26")); //$NON-NLS-1$
printNewLine();
System.out.println("\tGnuplot gp = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp.title(\"Noisy time domain signal\");"); //$NON-NLS-1$
System.out.println("\tgp.xlabel(\"time [s]\");"); //$NON-NLS-1$
System.out.println("\tgp.plot(t.getSubVector(1,50),y.getSubVector(1,50),new String[]{\"noisy signal\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
Gnuplot gp = null;
gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.setTitle("Noisy time domain signal"); //$NON-NLS-1$
canvas.setXLabel("time [s]"); //$NON-NLS-1$
canvas.plot(t.getSubVector(1, 50), y.getSubVector(1, 50), new String[] {"noisy signal"}); //$NON-NLS-1$
printNewLine();
printNewLine();
System.out.println(Messages.getString("Demo.38")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.39")); //$NON-NLS-1$
printNewLine();
System.out.println(Messages.getString("Demo.41")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.42")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix Y = ((NumericalMatrix)y).fft(256);"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleComplexMatrix Y = y.fft(256);
printNewLine();
System.out.println(Messages.getString("Demo.47")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.48")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix Pyy = Y.multiplyElementWise(Y.conjugate()).getRealPart();"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleMatrix Pyy = Y.multiplyElementWise(Y.conjugate()).getRealPart();
printNewLine();
System.out.println(Messages.getString("Demo.53")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix f = IntegerMatrix.series(0,127).multiply(1000.0/256);"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleMatrix f = new DoubleMatrix(IntMatrix.series(0, 127)).multiply(1000.0 / 256);
printNewLine();
System.out.println(Messages.getString("Demo.58")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.59")); //$NON-NLS-1$
printNewLine();
System.out.println("\tgp.title(\"Power spectral density\");"); //$NON-NLS-1$
System.out.println("\tgp.xlabel(\"Frequency (Hz)\");"); //$NON-NLS-1$
System.out.println("\tgp.plot(f,Pyy.getSubVector(1,128),new String[]{\"power spectral density\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
canvas.reset();
canvas.setTitle("Power spectral density"); //$NON-NLS-1$
canvas.setXLabel("Frequency (Hz)"); //$NON-NLS-1$
canvas.plot(f, Pyy.getSubVector(1, 128), new String[] {"power spectral density"}); //$NON-NLS-1$
printNewLine();
System.out.println(Messages.getString("Demo.69")); //$NON-NLS-1$
printNewLine();
System.out.println("\tgp.title(\"Power spectral density\");"); //$NON-NLS-1$
System.out.println("\tgp.xlabel(\"Frequency (Hz)\");"); //$NON-NLS-1$
System.out.println("\tgp.plot(f.getSubVector(1,50),Pyy.getSubVector(1,50),new String[]{\"power spectral density\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
canvas.reset();
canvas.setTitle("Power spectral density"); //$NON-NLS-1$
canvas.setXLabel("Frequency (Hz)"); //$NON-NLS-1$
canvas.plot(f.getSubVector(1, 50), Pyy.getSubVector(1, 50), new String[] {"power spectral density"}); //$NON-NLS-1$
Pause.pause();
gp.close();
}
/**
* 改行します。
*/
private void printNewLine() {
System.out.println(""); //$NON-NLS-1$
}
/**
* 正弦波による矩形波の合成を行います。
*
* @throws IOException キーボードから入力できない場合
*/
private void runFourierExpansionExample() throws IOException {
printNewLine();
System.out.println(Messages.getString("Demo.79")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.80")); //$NON-NLS-1$
printNewLine();
System.out.print(Messages.getString("Demo.5")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.9")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix t = RealMatrix.series(0,10,0.1);"); //$NON-NLS-1$
System.out.println("\tMatrix y = t.sinElementWise();"); //$NON-NLS-1$
printNewLine();
System.out.println(Messages.getString("Demo.88")); //$NON-NLS-1$
printNewLine();
System.out.println("\tGnuplot gp = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp.plot(t,y,new String[]{\"sin(t)\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
DoubleMatrix t = DoubleMatrix.series(0, 10, 0.1);
DoubleMatrix y = t.sinElementWise();
Gnuplot gp = null;
gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.plot(t, y, new String[] {"sin(t)"}); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.94")); //$NON-NLS-1$
printNewLine();
System.out.println("\ty = ((RealMatrix)t.sinElementWise().add(t.multiply(3)).sinElementWise().divide(3));"); //$NON-NLS-1$
System.out.println("\tgp.plot(t,y,new String[]{\"sin(t)+sin(3*t)/3\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
y = t.sinElementWise().add(t.multiply(3)).sinElementWise().divide(3);
canvas.plot(t, y, new String[] {"sin(t)+sin(3*t)/3"}); //$NON-NLS-1$
System.out.print(Messages.getString("Demo.100")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.101")); //$NON-NLS-1$
printNewLine();
System.out.println("\ty = t.sinElementWise().add(t.multiply(3).sinElementWise().divide(3))."); //$NON-NLS-1$
System.out.println("\t add(((RealMatrix)t.multiply(5)).sinElementWise().divide(5))."); //$NON-NLS-1$
System.out.println("\t add(((RealMatrix)t.multiply(7)).sinElementWise().divide(7))."); //$NON-NLS-1$
System.out.println("\t add(((RealMatrix)t.multiply(9)).sinElementWise().divide(9));"); //$NON-NLS-1$
System.out.println("\tgp.plot(t,y,new String[]{\"\"});"); //$NON-NLS-1$
printNewLine();
Pause.pause();
y = t.sinElementWise().add(t.multiply(3).sinElementWise().divide(3)).add((t.multiply(5)).sinElementWise().divide(5)).add((t.multiply(7)).sinElementWise().divide(7))
.add((t.multiply(9)).sinElementWise().divide(9));
canvas.plot(t, y, new String[] {""}); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.110")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.111")); //$NON-NLS-1$
printNewLine();
System.out.println("\tt = RealMatrix.series(0,3.14,0.02);"); //$NON-NLS-1$
System.out.println("\ty = new RealMatrix(10,t.length());"); //$NON-NLS-1$
System.out.println("\tMatrix x = RealMatrix.Z(t);"); //$NON-NLS-1$
printNewLine();
System.out.println("\tfor (int k = 1; k <= 19; k = k + 2) {"); //$NON-NLS-1$
System.out.println("\t\tx = x.add(((RealMatrix)t.multiply(k)).sinElementWise().divide(k));"); //$NON-NLS-1$
System.out.println("\t\ty.setRowVector((k+1)/2,x);"); //$NON-NLS-1$
System.out.println("\t}"); //$NON-NLS-1$
printNewLine();
Pause.pause();
t = DoubleMatrix.series(0, 3.14, 0.02);
y = new DoubleMatrix(10, t.length());
DoubleMatrix x = DoubleMatrix.zero(t);
for (int k = 1; k <= 19; k = k + 2) {
x = x.add(t.multiply(k).sinElementWise().divide(k));
y.setRowVector((k + 1) / 2, x);
}
System.out.print(Messages.getString("Demo.122")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.123")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.124")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix yy = y.getRowVectors(IntegerMatrix.series(1,9,2));"); //$NON-NLS-1$
System.out.println("\tgp.plot(t, yy, new String[]{\"1st\",\"5rd\",\"9th\",\"13th\",\"17th\"});"); //$NON-NLS-1$
System.out.println("\tgp.title(\"The building of a square wave: Gibbs effect\");"); //$NON-NLS-1$
System.out.println("\n"); //$NON-NLS-1$
Pause.pause();
DoubleMatrix yy = y.getRowVectors(IntMatrix.series(1, 9, 2));
canvas.plot(t, yy, new String[] {"1st", "5rd", "9th", "13th", "17th"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
canvas.setTitle("The building of a square wave: Gibbs effect"); //$NON-NLS-1$
Pause.pause();
gp.close();
}
/**
* 畳み込みの(パーシバル)定理の確認を行います。
*
* @throws IOException キーボードから入力できない場合
*/
private void runConvolutionExample() throws IOException {
printNewLine();
System.out.println(Messages.getString("Demo.137")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.138")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.139")); //$NON-NLS-1$
printNewLine();
System.out.println(Messages.getString("Demo.141")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix x = new RealMatrix(new double[]{1, 2, 3, 4});"); //$NON-NLS-1$
System.out.println("\tMatrix y = new RealMatrix(new double[]{5, 4, 3, 2, 1});"); //$NON-NLS-1$
Pause.pause();
DoubleMatrix x = new DoubleMatrix(new double[] {1, 2, 3, 4});
DoubleMatrix y = new DoubleMatrix(new double[] {5, 4, 3, 2, 1});
printNewLine();
System.out.println(Messages.getString("Demo.146")); //$NON-NLS-1$
printNewLine();
System.out.println("\tMatrix ztime = Conv.conv(x,y);"); //$NON-NLS-1$
printNewLine();
DoubleMatrix ztime = Conv.conv(x, y);
Pause.pause();
printNewLine();
System.out.println(Messages.getString("Demo.151")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.152")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.153")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.154")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.155")); //$NON-NLS-1$
printNewLine();
System.out.println("\tx = x.appendRight(new RealMatrix(1,4));"); //$NON-NLS-1$
System.out.println("\ty = y.appendRight(new RealMatrix(1,3));"); //$NON-NLS-1$
System.out.println("\tMatrix X = ((NumericalMatrix)x).fft();"); //$NON-NLS-1$
System.out.println("\tMatrix Y = ((NumericalMatrix)y).fft();"); //$NON-NLS-1$
System.out.println("\tMatrix XY = X.multiplyElementWise(Y);"); //$NON-NLS-1$
System.out.println("\tMatrix zfreq = ((NumericalMatrix)XY).ifft().getRealPart();"); //$NON-NLS-1$
Pause.pause();
x = x.appendRight(new DoubleMatrix(1, 4));
y = y.appendRight(new DoubleMatrix(1, 3));
DoubleComplexMatrix X = x.fft();
DoubleComplexMatrix Y = y.fft();
DoubleComplexMatrix XY = X.multiplyElementWise(Y);
DoubleMatrix zfreq = XY.ifft().getRealPart();
printNewLine();
System.out.println(Messages.getString("Demo.164")); //$NON-NLS-1$
printNewLine();
System.out.println("\tztime.print(\"ztime\");"); //$NON-NLS-1$
System.out.println("\tzfreq.roundElementWise().print(\"round(zfreq)\");"); //$NON-NLS-1$
printNewLine();
Pause.pause();
ztime.print("ztime"); //$NON-NLS-1$
printNewLine();
zfreq.roundElementWise().print("round(zfreq)"); //$NON-NLS-1$
printNewLine();
System.out.println(Messages.getString("Demo.173")); //$NON-NLS-1$
System.out.print(Messages.getString("Demo.174")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.175")); //$NON-NLS-1$
Pause.pause();
printNewLine();
System.out.println(Messages.getString("Demo.177")); //$NON-NLS-1$
printNewLine();
System.out.println("\tztime.subtract(zfreq).print(\"ztime - zreqs\");"); //$NON-NLS-1$
printNewLine();
ztime.subtract(zfreq).print("ztime - zreqs"); //$NON-NLS-1$
double max = (((ztime.subtract(zfreq)).absElementWise())).max().doubleValue();
printNewLine();
System.out.println(Messages.getString("Demo.183") + max + Messages.getString("Demo.13")); //$NON-NLS-1$ //$NON-NLS-2$
}
}