|
源码编译安装C++的DSP框架kfr-4.2.1并使用demo
注意:KFR 从 4.0 版开始需要 C++17 标准 SET(CMAKE_CXX_STANDARD 17)
cd kfr-4.2.1/- 报错:
- FileNotFoundError: [Errno 2] No such file or directory: '../svg/bessel_lowpass12.svg'
- 修改:python_plot.hpp文件中第142行:
- 查找命令: find ./ -name "python_plot.*"
- 输出源码的位置:./include/kfr/io/python_plot.hpp
- 位置:-- Installing: /usr/local/include/kfr/io/python_plot.hpp
- plot_show(name, x, concat_args(options, "file='../svg/" + name + ".svg'"));
- 改为:
- 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例子:
- # Copyright (C) 2016 D Levin (http://www.kfrlib.com)
- # This file is part of KFR
- #
- # KFR is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # KFR is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with KFR.
- cmake_minimum_required(VERSION 3.15)
- SET(CMAKE_CXX_STANDARD 17)
- # Binary output directories
- file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/svg)
- # By not providing "Findkfr_dft.cmake" in CMAKE_MODULE_PATH this project
- # find_package(kfr_dft)
- add_executable(main iir.cpp)
- # target_include_directories(iir INTERFACE "/usr/local/include/kfr/")
- # 接口库 类似一个 纯头文件库
- # target_link_libraries(main INTERFACE "/usr/local/include/kfr/")
- # target_link_libraries(main "/usr/local/include/kfr/")
- # cmake方式依赖Python头文件和库文件的配置
- # 如果使用的是非系统目录下的 Python 可以通过指定 Python3_ROOT_DIR 改变查找路径
- set(Python3_ROOT_DIR "/home/jiang/miniconda3/envs/py3_tf2_torch")
- # 这里的环境 与 cmd的环境要一致 conda activate py3_tf2_torch
- # 因为 有这个:#!/usr/bin/env python
- find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
- find_package(Python3 COMPONENTS NumPy)
- # find_package(PythonLibs REQUIRED)
- message("python3头文件目录:" ${Python3_INCLUDE_DIRS})
- message("python3的版本信息:" ${Python3_VERSION})
- message("python3中numpy的版本信息:" ${Python3_NumPy_VERSION})
- message("python3的库文件信息:" ${Python3_LIBRARIES})
- # python的头文件 PYTHON_INCLUDE_DIRS
- include_directories(${Python3_INCLUDE_DIRS})
- # python的库文件 PYTHON_LIBRARIES
- # 解决报错: fatal error: Python.h: 没有那个文件或目录
- target_link_libraries(main ${Python3_LIBRARIES})
复制代码
- /**
- * KFR (http://kfrlib.com)
- * Copyright (C) 2016 D Levin
- * See LICENSE.txt for details
- */
- #include <kfr/base.hpp>
- #include <kfr/dsp.hpp>
- #include <kfr/io.hpp>
- #include <iostream>
- using namespace kfr;
- int main()
- {
- println(library_version());
- constexpr size_t maxorder = 32;
- const std::string options = "phaseresp=True, log_freq=True, freq_dB_lim=(-160, 10), padwidth=8192";
- univector<fbase, 1024> output;
- {
- zpk<fbase> filt = iir_lowpass(bessel<fbase>(24), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- std::cout << "画图之前退出吧!" << std::endl;
- // exit(0);
- // c++调用了python Plot data using python and save to file
- plot_save("bessel_lowpass24", output, options + ", title='24th-order Bessel filter, lowpass 1khz'");
- {
- zpk<fbase> filt = iir_lowpass(bessel<fbase>(12), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("bessel_lowpass12", output, options + ", title='12th-order Bessel filter, lowpass 1khz'");
- {
- zpk<fbase> filt = iir_lowpass(bessel<fbase>(6), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("bessel_lowpass6", output, options + ", title='6th-order Bessel filter, lowpass 1khz'");
- {
- zpk<fbase> filt = iir_lowpass(butterworth<fbase>(24), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_lowpass24", output,
- options + ", title='24th-order Butterworth filter, lowpass 1khz'");
- {
- zpk<fbase> filt = iir_lowpass(butterworth<fbase>(12), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_lowpass12", output,
- options + ", title='12th-order Butterworth filter, lowpass 1khz'");
- {
- zpk<fbase> filt = iir_highpass(butterworth<fbase>(12), 1000, 48000);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_highpass12", output,
- options + ", title='12th-order Butterworth filter, highpass 1khz'");
- {
- zpk<fbase> filt = iir_bandpass(butterworth<fbase>(12), 0.1, 0.2);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_bandpass12", output,
- options + ", title='12th-order Butterworth filter, bandpass'");
- {
- zpk<fbase> filt = iir_bandstop(butterworth<fbase>(12), 0.1, 0.2);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_bandstop12", output,
- options + ", title='12th-order Butterworth filter, bandstop'");
- {
- zpk<fbase> filt = iir_bandpass(butterworth<fbase>(4), 0.005, 0.9);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("butterworth_bandpass4", output, options + ", title='4th-order Butterworth filter, bandpass'");
- {
- zpk<fbase> filt = iir_lowpass(chebyshev1<fbase>(8, 2), 0.09);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("chebyshev1_lowpass8", output,
- options + ", title='8th-order Chebyshev type I filter, lowpass'");
- {
- zpk<fbase> filt = iir_lowpass(chebyshev2<fbase>(8, 80), 0.09);
- std::vector<biquad_params<fbase>> bqs = to_sos(filt);
- output = biquad<maxorder>(bqs, unitimpulse());
- }
- plot_save("chebyshev2_lowpass8", output,
- options + ", title='8th-order Chebyshev type II filter, lowpass'");
- println("SVG plots have been saved to svg directory");
- return 0;
- }
复制代码
例子直接用 : cmake ..
输出:
- -- The C compiler identification is GNU 8.4.0
- -- The CXX compiler identification is GNU 8.4.0
- -- Detecting C compiler ABI info
- -- Detecting C compiler ABI info - done
- -- Check for working C compiler: /usr/bin/cc - skipped
- -- Detecting C compile features
- -- Detecting C compile features - done
- -- Detecting CXX compiler ABI info
- -- Detecting CXX compiler ABI info - done
- -- Check for working CXX compiler: /usr/bin/c++ - skipped
- -- Detecting CXX compile features
- -- Detecting CXX compile features - done
- -- 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
- -- Found Python3: /home/jiang/miniconda3/envs/py3_tf2_torch/include/python3.6m (found version "3.6.10") found components: NumPy Interpreter Development.Module
- python3头文件目录:/home/jiang/miniconda3/envs/py3_tf2_torch/include/python3.6m
- python3的版本信息:3.6.10
- python3中numpy的版本信息:1.19.5
- python3的库文件信息:/home/jiang/miniconda3/envs/py3_tf2_torch/lib/libpython3.6m.so
- -- Configuring done
- -- Generating done
- -- Build files have been written to: /home/jiang/jjj_kfrlib_works/kfr-4.2.1/example_jjj/build
复制代码
后 make 之后 执行程序(千万注意当前的python环境啊):
- (py3_tf2_torch) jiang@jiang-Ubuntu:~/jjj_kfrlib_works/kfr-4.2.1/example_jjj/build$ ./main
- KFR 4.2.0 sse2 64-bit (gcc-8.4.0/linux) +in
- 画图之前退出吧!
- bessel_lowpass24...done
- bessel_lowpass12...done
- bessel_lowpass6...done
- butterworth_lowpass24...done
- butterworth_lowpass12...done
- butterworth_highpass12...done
- butterworth_bandpass12...done
- butterworth_bandstop12...done
- butterworth_bandpass4...done
- chebyshev1_lowpass8...done
- chebyshev2_lowpass8...done
- 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
输出:
- running develop
- running egg_info
- creating dspplot.egg-info
- writing dspplot.egg-info/PKG-INFO
- writing dependency_links to dspplot.egg-info/dependency_links.txt
- writing requirements to dspplot.egg-info/requires.txt
- writing top-level names to dspplot.egg-info/top_level.txt
- writing manifest file 'dspplot.egg-info/SOURCES.txt'
- package init file 'dspplot/__init__.py' not found (or not a regular file)
- reading manifest file 'dspplot.egg-info/SOURCES.txt'
- writing manifest file 'dspplot.egg-info/SOURCES.txt'
- running build_ext
- Creating /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages/dspplot.egg-link (link to .)
- Adding dspplot 0.0.3 to easy-install.pth file
- Installed /home/jiang/jjj_kfrlib_works/kfr-4.2.1
- Processing dependencies for dspplot==0.0.3
- Searching for scipy==1.5.4
- Best match: scipy 1.5.4
- Adding scipy 1.5.4 to easy-install.pth file
- Using /home/jiang/.local/lib/python3.6/site-packages
- Searching for numpy==1.19.5
- Best match: numpy 1.19.5
- Adding numpy 1.19.5 to easy-install.pth file
- Installing f2py script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin
- Installing f2py3 script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin
- Installing f2py3.6 script to /home/jiang/miniconda3/envs/py3_tf2_torch/bin
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for matplotlib==3.3.4
- Best match: matplotlib 3.3.4
- Adding matplotlib 3.3.4 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for cycler==0.10.0
- Best match: cycler 0.10.0
- Adding cycler 0.10.0 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for python-dateutil==2.8.1
- Best match: python-dateutil 2.8.1
- Adding python-dateutil 2.8.1 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for pyparsing==2.4.7
- Best match: pyparsing 2.4.7
- Adding pyparsing 2.4.7 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for Pillow==8.1.0
- Best match: Pillow 8.1.0
- Adding Pillow 8.1.0 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for kiwisolver==1.3.1
- Best match: kiwisolver 1.3.1
- Adding kiwisolver 1.3.1 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- Searching for six==1.15.0
- Best match: six 1.15.0
- Adding six 1.15.0 to easy-install.pth file
- Using /home/jiang/miniconda3/envs/py3_tf2_torch/lib/python3.6/site-packages
- 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环境的问题
|
|