东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 2838|回复: 0
打印 上一主题 下一主题

[C/C++] 源码编译安装C++的DSP框架kfr-4.2.1并使用demo

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
跳转到指定楼层
楼主
发表于 2021-12-15 10:22:31 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
源码编译安装C++的DSP框架kfr-4.2.1并使用demo




注意:KFR 从 4.0 版开始需要 C++17 标准   SET(CMAKE_CXX_STANDARD 17)




cd kfr-4.2.1/
  1. 报错:
  2. FileNotFoundError: [Errno 2] No such file or directory: '../svg/bessel_lowpass12.svg'

  3. 修改:python_plot.hpp文件中第142行:
  4. 查找命令: find ./ -name "python_plot.*"
  5. 输出源码的位置:./include/kfr/io/python_plot.hpp
  6. 位置:-- Installing: /usr/local/include/kfr/io/python_plot.hpp
  7. plot_show(name, x, concat_args(options, "file='../svg/" + name + ".svg'"));
  8. 改为:
  9. plot_show(name, x, concat_args(options, "file='./svg/" + name + ".svg'"));
复制代码



mkdir build && cd build
cmake -D ENABLE_TESTS=ON -D CMAKE_C_COMPILER=/usr/bin/gcc-7 -D CMAKE_CXX_COMPILER=/usr/bin/g++-7 -D CMAKE_BUILD_TYPE=Release ..
输出(这样只有libkfr_io.a 没有libkfr_dft.a):
原因是:DFT is limited to Clang due to ICE in MSVC and broken AVX optimization in GCC 8 and 9.
Once fixed, support will be added 随时关注官网
-- The CXX compiler identification is GNU 7.5.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/g++-7 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting native cpu...  检测本地CPU啊
-- DETECTED_CPU=avx2
-- CPU_ARCH=avx2
-- Could NOT find MPFR (missing: MPFR_INCLUDE_DIR MPFR_LIBRARIES)
-- Found GMP: /usr/include/x86_64-linux-gnu  
-- ARCH_TESTS =
-- Testing for
-- Configuring done
-- Generating done


再来:cmake -DENABLE_TESTS=ON -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release ..
输出(这样都有了:libkfr_dft.a  libkfr_io.a):
-- The CXX compiler identification is Clang 7.0.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting native cpu...
-- DETECTED_CPU=avx2
-- CPU_ARCH=avx2
-- Could NOT find MPFR (missing: MPFR_INCLUDE_DIR MPFR_LIBRARIES)
-- Found GMP: /usr/include/x86_64-linux-gnu  
-- ARCH_TESTS =
-- Testing for
-- Configuring done
-- Generating done


make -j8


测试一下:
cd <path_to_cmake_build_directory>
cd tests
ctest -V
cd ..
安装到系统:
sudo make install




demo例子:


  1. # Copyright (C) 2016 D Levin (http://www.kfrlib.com)
  2. # This file is part of KFR
  3. #
  4. # KFR is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # KFR is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with KFR.

  16. cmake_minimum_required(VERSION 3.15)

  17. SET(CMAKE_CXX_STANDARD 17)

  18. # Binary output directories

  19. file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/svg)

  20. # By not providing "Findkfr_dft.cmake" in CMAKE_MODULE_PATH this project
  21. # find_package(kfr_dft)




  22. add_executable(main iir.cpp)
  23. # target_include_directories(iir INTERFACE "/usr/local/include/kfr/")
  24. # 接口库 类似一个 纯头文件库
  25. # target_link_libraries(main INTERFACE "/usr/local/include/kfr/")
  26. # target_link_libraries(main "/usr/local/include/kfr/")


  27. # cmake方式依赖Python头文件和库文件的配置
  28. # 如果使用的是非系统目录下的 Python 可以通过指定 Python3_ROOT_DIR 改变查找路径
  29. set(Python3_ROOT_DIR "/home/jiang/miniconda3/envs/py3_tf2_torch")
  30. # 这里的环境 与 cmd的环境要一致  conda activate py3_tf2_torch
  31. # 因为 有这个:#!/usr/bin/env python
  32. find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
  33. find_package(Python3 COMPONENTS NumPy)
  34. # find_package(PythonLibs REQUIRED)
  35. message("python3头文件目录:" ${Python3_INCLUDE_DIRS})
  36. message("python3的版本信息:" ${Python3_VERSION})
  37. message("python3中numpy的版本信息:" ${Python3_NumPy_VERSION})
  38. message("python3的库文件信息:" ${Python3_LIBRARIES})

  39. # python的头文件 PYTHON_INCLUDE_DIRS
  40. include_directories(${Python3_INCLUDE_DIRS})
  41. # python的库文件 PYTHON_LIBRARIES   
  42. # 解决报错: fatal error: Python.h: 没有那个文件或目录
  43. target_link_libraries(main ${Python3_LIBRARIES})
复制代码


  1. /**
  2. * KFR (http://kfrlib.com)
  3. * Copyright (C) 2016  D Levin
  4. * See LICENSE.txt for details
  5. */

  6. #include <kfr/base.hpp>
  7. #include <kfr/dsp.hpp>
  8. #include <kfr/io.hpp>
  9. #include <iostream>

  10. using namespace kfr;

  11. int main()
  12. {
  13.     println(library_version());

  14.     constexpr size_t maxorder = 32;

  15.     const std::string options = "phaseresp=True, log_freq=True, freq_dB_lim=(-160, 10), padwidth=8192";

  16.     univector<fbase, 1024> output;
  17.     {
  18.         zpk<fbase> filt                       = iir_lowpass(bessel<fbase>(24), 1000, 48000);
  19.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  20.         output                                = biquad<maxorder>(bqs, unitimpulse());
  21.     }
  22.     std::cout << "画图之前退出吧!" << std::endl;
  23.     // exit(0);
  24.     // c++调用了python Plot data using python and save to file
  25.     plot_save("bessel_lowpass24", output, options + ", title='24th-order Bessel filter, lowpass 1khz'");

  26.     {
  27.         zpk<fbase> filt                       = iir_lowpass(bessel<fbase>(12), 1000, 48000);
  28.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  29.         output                                = biquad<maxorder>(bqs, unitimpulse());
  30.     }
  31.     plot_save("bessel_lowpass12", output, options + ", title='12th-order Bessel filter, lowpass 1khz'");

  32.     {
  33.         zpk<fbase> filt                       = iir_lowpass(bessel<fbase>(6), 1000, 48000);
  34.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  35.         output                                = biquad<maxorder>(bqs, unitimpulse());
  36.     }
  37.     plot_save("bessel_lowpass6", output, options + ", title='6th-order Bessel filter, lowpass 1khz'");

  38.     {
  39.         zpk<fbase> filt                       = iir_lowpass(butterworth<fbase>(24), 1000, 48000);
  40.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  41.         output                                = biquad<maxorder>(bqs, unitimpulse());
  42.     }
  43.     plot_save("butterworth_lowpass24", output,
  44.               options + ", title='24th-order Butterworth filter, lowpass 1khz'");

  45.     {
  46.         zpk<fbase> filt                       = iir_lowpass(butterworth<fbase>(12), 1000, 48000);
  47.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  48.         output                                = biquad<maxorder>(bqs, unitimpulse());
  49.     }
  50.     plot_save("butterworth_lowpass12", output,
  51.               options + ", title='12th-order Butterworth filter, lowpass 1khz'");

  52.     {
  53.         zpk<fbase> filt                       = iir_highpass(butterworth<fbase>(12), 1000, 48000);
  54.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  55.         output                                = biquad<maxorder>(bqs, unitimpulse());
  56.     }
  57.     plot_save("butterworth_highpass12", output,
  58.               options + ", title='12th-order Butterworth filter, highpass 1khz'");

  59.     {
  60.         zpk<fbase> filt                       = iir_bandpass(butterworth<fbase>(12), 0.1, 0.2);
  61.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  62.         output                                = biquad<maxorder>(bqs, unitimpulse());
  63.     }
  64.     plot_save("butterworth_bandpass12", output,
  65.               options + ", title='12th-order Butterworth filter, bandpass'");

  66.     {
  67.         zpk<fbase> filt                       = iir_bandstop(butterworth<fbase>(12), 0.1, 0.2);
  68.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  69.         output                                = biquad<maxorder>(bqs, unitimpulse());
  70.     }
  71.     plot_save("butterworth_bandstop12", output,
  72.               options + ", title='12th-order Butterworth filter, bandstop'");

  73.     {
  74.         zpk<fbase> filt                       = iir_bandpass(butterworth<fbase>(4), 0.005, 0.9);
  75.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  76.         output                                = biquad<maxorder>(bqs, unitimpulse());
  77.     }
  78.     plot_save("butterworth_bandpass4", output, options + ", title='4th-order Butterworth filter, bandpass'");

  79.     {
  80.         zpk<fbase> filt                       = iir_lowpass(chebyshev1<fbase>(8, 2), 0.09);
  81.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  82.         output                                = biquad<maxorder>(bqs, unitimpulse());
  83.     }
  84.     plot_save("chebyshev1_lowpass8", output,
  85.               options + ", title='8th-order Chebyshev type I filter, lowpass'");

  86.     {
  87.         zpk<fbase> filt                       = iir_lowpass(chebyshev2<fbase>(8, 80), 0.09);
  88.         std::vector<biquad_params<fbase>> bqs = to_sos(filt);
  89.         output                                = biquad<maxorder>(bqs, unitimpulse());
  90.     }
  91.     plot_save("chebyshev2_lowpass8", output,
  92.               options + ", title='8th-order Chebyshev type II filter, lowpass'");

  93.     println("SVG plots have been saved to svg directory");

  94.     return 0;
  95. }
复制代码

例子直接用 : cmake ..
输出:
  1. -- The C compiler identification is GNU 8.4.0
  2. -- The CXX compiler identification is GNU 8.4.0
  3. -- Detecting C compiler ABI info
  4. -- Detecting C compiler ABI info - done
  5. -- Check for working C compiler: /usr/bin/cc - skipped
  6. -- Detecting C compile features
  7. -- Detecting C compile features - done
  8. -- Detecting CXX compiler ABI info
  9. -- Detecting CXX compiler ABI info - done
  10. -- Check for working CXX compiler: /usr/bin/c++ - skipped
  11. -- Detecting CXX compile features
  12. -- Detecting CXX compile features - done
  13. -- Found Python3: /home/jiang/miniconda3/envs/py3_tf2_torch/bin/python3.6 (found version "3.6.10") found components: Interpreter Development Development.Module Development.Embed
  14. -- Found Python3: /home/jiang/miniconda3/envs/py3_tf2_torch/include/python3.6m (found version "3.6.10") found components: NumPy Interpreter Development.Module
  15. python3头文件目录:/home/jiang/miniconda3/envs/py3_tf2_torch/include/python3.6m
  16. python3的版本信息:3.6.10
  17. python3中numpy的版本信息:1.19.5
  18. python3的库文件信息:/home/jiang/miniconda3/envs/py3_tf2_torch/lib/libpython3.6m.so
  19. -- Configuring done
  20. -- Generating done
  21. -- Build files have been written to: /home/jiang/jjj_kfrlib_works/kfr-4.2.1/example_jjj/build
复制代码

后 make   之后 执行程序(千万注意当前的python环境啊):
  1. (py3_tf2_torch) jiang@jiang-Ubuntu:~/jjj_kfrlib_works/kfr-4.2.1/example_jjj/build$ ./main
  2. KFR 4.2.0 sse2 64-bit (gcc-8.4.0/linux) +in
  3. 画图之前退出吧!
  4. bessel_lowpass24...done
  5. bessel_lowpass12...done
  6. bessel_lowpass6...done
  7. butterworth_lowpass24...done
  8. butterworth_lowpass12...done
  9. butterworth_highpass12...done
  10. butterworth_bandpass12...done
  11. butterworth_bandstop12...done
  12. butterworth_bandpass4...done
  13. chebyshev1_lowpass8...done
  14. chebyshev2_lowpass8...done
  15. SVG plots have been saved to svg directory
复制代码


报错:ImportError: No module named scipy


// c++调用了python Plot data using python and save to file
plot_save("bessel_lowpass24", output, options + ", title='24th-order Bessel filter, lowpass 1khz'");

inline std::string python_prologue()
{
    return "#!/usr/bin/env python\n"
           "import sys\n"
           "import os\n"
           "sys.path.append(os.path.abspath(__file__ + '/../../../../dspplot/dspplot'))\n"
           "sys.path.append(os.path.abspath(__file__ + '/../../../dspplot/dspplot'))\n"
           "import dspplotting as dspplot\n\n";
}

解决:这里的dsp的画图库 实际上是调用了python的
/kfr-4.2.1/dspplot/setup.py
在环境(envs/py3_tf2_torch)中执行的 python3 dspplot/setup.py develop
输出:
  1. running develop
  2. running egg_info
  3. creating dspplot.egg-info
  4. writing dspplot.egg-info/PKG-INFO
  5. writing dependency_links to dspplot.egg-info/dependency_links.txt
  6. writing requirements to dspplot.egg-info/requires.txt
  7. writing top-level names to dspplot.egg-info/top_level.txt
  8. writing manifest file 'dspplot.egg-info/SOURCES.txt'
  9. package init file 'dspplot/__init__.py' not found (or not a regular file)
  10. reading manifest file 'dspplot.egg-info/SOURCES.txt'
  11. writing manifest file 'dspplot.egg-info/SOURCES.txt'
  12. running build_ext
  13. Creating /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages/dspplot.egg-link (link to .)
  14. Adding dspplot 0.0.3 to easy-install.pth file

  15. Installed /home/jiang/jjj_kfrlib_works/kfr-4.2.1
  16. Processing dependencies for dspplot==0.0.3
  17. Searching for scipy==1.5.4
  18. Best match: scipy 1.5.4
  19. Adding scipy 1.5.4 to easy-install.pth file

  20. Using /home/jiang/.local/lib/python3.6/site-packages
  21. Searching for numpy==1.19.5
  22. Best match: numpy 1.19.5
  23. Adding numpy 1.19.5 to easy-install.pth file
  24. Installing f2py script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin
  25. Installing f2py3 script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin
  26. Installing f2py3.6 script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin

  27. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  28. Searching for matplotlib==3.3.4
  29. Best match: matplotlib 3.3.4
  30. Adding matplotlib 3.3.4 to easy-install.pth file

  31. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  32. Searching for cycler==0.10.0
  33. Best match: cycler 0.10.0
  34. Adding cycler 0.10.0 to easy-install.pth file

  35. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  36. Searching for python-dateutil==2.8.1
  37. Best match: python-dateutil 2.8.1
  38. Adding python-dateutil 2.8.1 to easy-install.pth file

  39. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  40. Searching for pyparsing==2.4.7
  41. Best match: pyparsing 2.4.7
  42. Adding pyparsing 2.4.7 to easy-install.pth file

  43. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  44. Searching for Pillow==8.1.0
  45. Best match: Pillow 8.1.0
  46. Adding Pillow 8.1.0 to easy-install.pth file

  47. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  48. Searching for kiwisolver==1.3.1
  49. Best match: kiwisolver 1.3.1
  50. Adding kiwisolver 1.3.1 to easy-install.pth file

  51. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  52. Searching for six==1.15.0
  53. Best match: six 1.15.0
  54. Adding six 1.15.0 to easy-install.pth file

  55. Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
  56. Finished processing dependencies for dspplot==0.0.3
复制代码

#!/usr/bin/python
#!/usr/bin/env python

个人感觉应该优先使用 #!/usr/bin/env python 环境里的python
有图展示,终于知道为什么一直找不到ImportError: No module named scipy  还是各种python环境的问题














env里的python.png (129.69 KB, 下载次数: 115)

env里的python.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-5 23:40 , Processed in 0.188426 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表