fftw3库是快速傅里叶变换FFT/IFFT的开源实现,可以在多个平台编译。在Android app开发项目中需要做FFT信号分析,优先使用JNI的方式,使用原生语言C/C++实现复杂的科学计算任务。fftw3可以在多个平台编译优化,也可以在Android NDK开发时进行编译调用。本文详细介绍了fftw3在Android studio中的编译和使用方法,欢迎大家留言讨论。
下载fftw3的源码
可以从fftw3的官网下载,另外这里提供了一个下载链接
https://download.csdn.net/download/cyz_2014/87863156
将ffw3文件夹复制到app->src->main->cpp路径中
修改CMakeLists.txt
fftw3中的CMakeLists.txt无需修改,只需要修改外层cpp中的CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
option (ENABLE_FLOAT "single-precision" ON)
SET (ENABLE_FLOAT ON)
project("myapplication")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_subdirectory(fftw3)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
target_link_libraries(native-lib fftw3f)
上面的配置文件只添加了以下4条语句:
option (ENABLE_FLOAT "single-precision" ON)
SET (ENABLE_FLOAT ON)
add_subdirectory(fftw3)
target_link_libraries(native-lib fftw3f)
前两条语句是设置fftw3的编译选项,将其编译为单精度浮点库,生成的库的名称是libfftw3f.so
后两条语句是用于编译ffw3,并将其与native-lib链接。
编译和加载fftw3库
编译以后在app->build->intermediates->merged_native_libs->debug->out->lib中有生成的对应的so库
在MainActivity 类中加载生成的fftw3f:
static { System.loadLibrary("fftw3f"); System.loadLibrary("native-lib"); }
fftw3库的使用
编写native函数
#include <jni.h>
#include <string>
#include <android/log.h>
#include <math.h>
#include "fftw3/api/fftw3.h"
#define TAG "Jonathan"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
//以下是native函数部分
jfloat a[8] = {1, 0, 2, 0, 3, 0, 4, 0};
fftwf_complex *fftwa = reinterpret_cast<fftwf_complex *>((fftwf_complex *) a);
fftwf_plan p;
p = fftwf_plan_dft_1d(4, fftwa, fftwa, FFTW_FORWARD, FFTW_ESTIMATE);
fftwf_execute(p);
fftwf_destroy_plan(p);
LOGD("fftwa: %f + %fi, %f + %fi, %f + %fi, %f + %fi,\n", fftwa[0][0], fftwa[0][1],
fftwa[1][0], fftwa[1][1], fftwa[2][0], fftwa[2][1], fftwa[3][0], fftwa[3][1]);