当前位置:   article > 正文

fftw3库在Android Studio中的编译和使用_fft3w库

fft3w库

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

  1. # For more information about using CMake with Android Studio, read the
  2. # documentation: https://d.android.com/studio/projects/add-native-code.html
  3. # Sets the minimum version of CMake required to build the native library.
  4. cmake_minimum_required(VERSION 3.10.2)
  5. # Declares and names the project.
  6. option (ENABLE_FLOAT "single-precision" ON)
  7. SET (ENABLE_FLOAT ON)
  8. project("myapplication")
  9. # Creates and names a library, sets it as either STATIC
  10. # or SHARED, and provides the relative paths to its source code.
  11. # You can define multiple libraries, and CMake builds them for you.
  12. # Gradle automatically packages shared libraries with your APK.
  13. add_subdirectory(fftw3)
  14. add_library( # Sets the name of the library.
  15. native-lib
  16. # Sets the library as a shared library.
  17. SHARED
  18. # Provides a relative path to your source file(s).
  19. native-lib.cpp )
  20. # Searches for a specified prebuilt library and stores the path as a
  21. # variable. Because CMake includes system libraries in the search path by
  22. # default, you only need to specify the name of the public NDK library
  23. # you want to add. CMake verifies that the library exists before
  24. # completing its build.
  25. find_library( # Sets the name of the path variable.
  26. log-lib
  27. # Specifies the name of the NDK library that
  28. # you want CMake to locate.
  29. log )
  30. # Specifies libraries CMake should link to your target library. You
  31. # can link multiple libraries, such as libraries you define in this
  32. # build script, prebuilt third-party libraries, or system libraries.
  33. target_link_libraries( # Specifies the target library.
  34. native-lib
  35. # Links the target library to the log library
  36. # included in the NDK.
  37. ${log-lib} )
  38. 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函数

  1. #include <jni.h>
  2. #include <string>
  3. #include <android/log.h>
  4. #include <math.h>
  5. #include "fftw3/api/fftw3.h"
  6. #define TAG "Jonathan"
  7. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__);
  8. //以下是native函数部分
  9. jfloat a[8] = {1, 0, 2, 0, 3, 0, 4, 0};
  10. fftwf_complex *fftwa = reinterpret_cast<fftwf_complex *>((fftwf_complex *) a);
  11. fftwf_plan p;
  12. p = fftwf_plan_dft_1d(4, fftwa, fftwa, FFTW_FORWARD, FFTW_ESTIMATE);
  13. fftwf_execute(p);
  14. fftwf_destroy_plan(p);
  15. LOGD("fftwa: %f + %fi, %f + %fi, %f + %fi, %f + %fi,\n", fftwa[0][0], fftwa[0][1],
  16. fftwa[1][0], fftwa[1][1], fftwa[2][0], fftwa[2][1], fftwa[3][0], fftwa[3][1]);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/105109
推荐阅读
相关标签
  

闽ICP备14008679号