Skip to content

Latest commit

 

History

History

fftw

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

JavaCPP Presets for FFTW

Introduction

This directory contains the JavaCPP Presets module for:

Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.

Documentation

Java API documentation is available here:

Sample Usage

Here is a simple example of FFTW ported to Java from this C source file:

We can use Maven 3 to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the pom.xml and Example.java source files below, simply execute on the command line:

 $ mvn compile exec:java

The pom.xml build file

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.bytedeco.fftw</groupId>
    <artifactId>example</artifactId>
    <version>1.5</version>
    <properties>
        <exec.mainClass>Example</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>fftw-platform</artifactId>
            <version>3.3.8-1.5</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>.</sourceDirectory>
    </build>
</project>

The Example.java source file

/* Start reading here */

import org.bytedeco.javacpp.*;
import static java.lang.Math.*;
import static org.bytedeco.fftw.global.fftw3.*;

public class Example {

    static final int NUM_POINTS = 64;


    /* Never mind this bit */

    static final int REAL = 0;
    static final int IMAG = 1;

    static void acquire_from_somewhere(DoublePointer signal) {
        /* Generate two sine waves of different frequencies and amplitudes. */

        double[] s = new double[(int)signal.capacity()];
        for (int i = 0; i < NUM_POINTS; i++) {
            double theta = (double)i / (double)NUM_POINTS * PI;

            s[2 * i + REAL] = 1.0 * cos(10.0 * theta) +
                              0.5 * cos(25.0 * theta);

            s[2 * i + IMAG] = 1.0 * sin(10.0 * theta) +
                              0.5 * sin(25.0 * theta);
        }
        signal.put(s);
    }

    static void do_something_with(DoublePointer result) {
        double[] r = new double[(int)result.capacity()];
        result.get(r);
        for (int i = 0; i < NUM_POINTS; i++) {
            double mag = sqrt(r[2 * i + REAL] * r[2 * i + REAL] +
                              r[2 * i + IMAG] * r[2 * i + IMAG]);

            System.out.println(mag);
        }
    }


    /* Resume reading here */

    public static void main(String args[]) {
        Loader.load(org.bytedeco.fftw.global.fftw3.class);

        DoublePointer signal = new DoublePointer(2 * NUM_POINTS);
        DoublePointer result = new DoublePointer(2 * NUM_POINTS);

        fftw_plan plan = fftw_plan_dft_1d(NUM_POINTS, signal, result,
                                          FFTW_FORWARD, (int)FFTW_ESTIMATE);

        acquire_from_somewhere(signal);
        fftw_execute(plan);
        do_something_with(result);

        fftw_destroy_plan(plan);
    }
}