« 高周波・RFニュース 2025年9月16日 Microwave Journalは自動車特集、supplimentは軍用レーダ、QualcommがSnapdragon 8 Elite Gen 5を解説、Perasoのミリ波モジュールが90万ドルの受注、ニューラルネットを使ったPAのDPD解説記事、また別のPixel 10分解動画 | トップページ | 阪急逆瀬川駅から宝塚神社でお参り。 »

2025年9月16日 (火)

Javaの数値計算ライブラリApache Commons Mathを使う(3) 高速フーリエ変換(FFT)を実行してJFreeChartでプロットする。

今回はこちらの例題。

 Visual C# (C_sharp)の数学ライブラリ Math.NET Numericsを使う(3) 高速フーリエ変換(FFT)を実行する。FourierOptionsにMatlabとNumerical Recipesがあるのが意外。

コードはこんな感じ。周波数をシフトする関数はなさそうなので手動でやっている。ノーマライズ法はデフォルトにするとMatlabと同じになるようだ。


import org.apache.commons.math3.complex.Complex;
import org.apache.commons.math3.transform.DftNormalization;
import org.apache.commons.math3.transform.FastFourierTransformer;
import org.apache.commons.math3.transform.TransformType;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import java.awt.BorderLayout;
import javax.swing.JFrame;

public class FFT extends JFrame {
    public static void main(String[] args) {
        FFT frame = new FFT();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(10, 10, 640, 480);
        frame.setTitle("FFT");
        frame.setVisible(true);


    }
    public FFT() {
       
        JFreeChart chart =
          ChartFactory.createXYLineChart("Fast Fourier Transform",
                                         "freq",
                                         "result",
                                         createData(),
                                         PlotOrientation.VERTICAL,
                                         true,
                                         false,
                                         false);

        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer =new XYLineAndShapeRenderer();
        renderer.setSeriesShapesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);

        plot.setRenderer(renderer);
        ChartPanel cpanel = new ChartPanel(chart);
        getContentPane().add(cpanel, BorderLayout.CENTER);
    }
    private XYSeriesCollection createData(){
       
        int n = 256;
        double[] t = new double[n];
        double[] y = new double[n];
        Complex[] result = new Complex[n];

        for (int i = 0; i < t.length; i++) {
            t[i] = (double)i;
            y[i] = Math.sin(t[i]);
        }
       
        FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
        result = fft.transform(y, TransformType.FORWARD);
       
        Complex[] resultShift = new Complex[n];
        for (int i = 0; i < t.length/2; i++) {
            resultShift[i] = result[n/2 + i];
            resultShift[n/2+i] = result[i];
        }
       
        XYSeriesCollection data = new XYSeriesCollection();

        XYSeries series1 = new XYSeries("Real part");
        for (int i = 0 ; i < n ; i++){
          series1.add(t[i]/(double)n - 0.5, resultShift[i].getReal());
        }

        XYSeries series2 = new XYSeries("Imaginary part");
        for (int i = 0 ; i < n ; i++){
          series2.add(t[i]/(double)n - 0.5, resultShift[i].getImaginary());
        }
       
        data.addSeries(series1);
        data.addSeries(series2);

        return data;
      }
}

実行するとNumPyの結果と同じになった。

Javafft01

 

 

 

« 高周波・RFニュース 2025年9月16日 Microwave Journalは自動車特集、supplimentは軍用レーダ、QualcommがSnapdragon 8 Elite Gen 5を解説、Perasoのミリ波モジュールが90万ドルの受注、ニューラルネットを使ったPAのDPD解説記事、また別のPixel 10分解動画 | トップページ | 阪急逆瀬川駅から宝塚神社でお参り。 »

パソコン・インターネット」カテゴリの記事

学問・資格」カテゴリの記事

日記・コラム・つぶやき」カテゴリの記事

コメント

コメントを書く

(ウェブ上には掲載しません)

« 高周波・RFニュース 2025年9月16日 Microwave Journalは自動車特集、supplimentは軍用レーダ、QualcommがSnapdragon 8 Elite Gen 5を解説、Perasoのミリ波モジュールが90万ドルの受注、ニューラルネットを使ったPAのDPD解説記事、また別のPixel 10分解動画 | トップページ | 阪急逆瀬川駅から宝塚神社でお参り。 »

最近の記事

2026年1月
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
無料ブログはココログ
フォト