FCWT——小波变换c++库的下载记录

Github链接icon-default.png?t=N7T8https://github.com/fastlib/fCWT最近课业需要,试图在C++上实现CWT(小波变换),从网上查询,发现了这个库,但是苦于文档是英文且网络上暂无安装教程。今天独自折腾了好久,也是四处碰壁,但现在似乎把库安装迁移好了?于是记录于此。

1、背景介绍

根据原仓库所述:

“快速连续小波变换 (fCWT) 是一个高度优化的 C++ 库,用于在 C++、Matlab 和 Python 中非常快速地计算 CWT。

fCWT已登上2022年1月《自然计算科学》杂志封面。在本文中,将fCWT与八种竞争算法进行了比较,测试了抗噪性,并在合成脑电图和体内细胞外局部场电位数据上进行了验证。”

2、安装要求

原文档给出的安装依赖如下:

依赖

构建时的设置依据自己的需求来根据原文档配置,这里就不再赘述了。

3、在 Microsoft Windows 上安装

笔者是在win平台使用的,故此文主要记录win平台。

在开始处,选择并运行 Visual Studio 的开发人员命令提示,依次键入以下命令:

> git clone https://github.com/fastlib/fCWT.git
> cd fCWT
> mkdir -p build
> cd build
> cmake -G "Visual Studio 17 2022" ..    %根据自己的VS版本确定%
> cmake --build .

一个 Visual Studio .SLN 文件现已在 build-folder 中创建。此处我的目录为:“D:\Microsoft\Visual Studio\2022\Community\fCWT\build”

此项目包括多个生成目标。要构建 fCWT 的共享/静态库,请构建“fCWT”目标。若要运行示例代码,请将“fCWT_example”目标设置为启动项目,并将代码设置为“release”。

要安装 fCWT,请在 build-folder 中运行提升的命令提示符(即具有管理员权限的命令提示符),然后键入:

> cmake -P cmake_install.cmake

要使已安装的 DLL 可用于依赖于它的任何应用程序,应指向创建的 fcwt.dll:

  • 在 %SYSTEMROOT%\System32 中,用于 64 位主机上的 64 位 DLL(或 32 位主机上的 32 位 DLL);
  • 在 %SYSTEMROOT%\SysWOW64 中,用于 64 位主机上的 32 位 DLL。

4、配置项目

vs项目配置说明:

笔者的项目中单独建立了bin、include、lib、code等文件夹,所以此处先给出一般的配置方法。

  1. 选中VS项目,右键选择属性进行配置。
  2. bin对应常规下的输出目录和调试下的工作目录,include对应C/C++的附加包含目录,lib对应链接器的附加库目录。
  3. lib目录设置好后,还需要在链接器——》输入的附加依赖项里添加你引用的lib名称。

现在打开步骤3的项目,并开始构建操作,接着在复制以下文件:

  1. “.\fCWT\src\fcwt”中的fcwt.cpp、fcwt.h
  2. “.\fCWT\libs”中的fftw3.h、fftw3f.dll、fftw3f.lib(如果先前项目已配置fftw库,这步可省略)
  3. “.\fCWT\build\Release”中的fCWT.dll、fCWT.lib
  4. 接着按照一般的配置方法将其配置到项目中即可。

5、检验配置

运行以下代码,检验是否配置项目成功。

#include <iostream>
#include <vector>
#include <math.h>
#include <fcwt.h>

int main(int argc, char * argv[]) {
    
    int n = 1000; //signal length
    const int fs = 1000; //sampling frequency
    float twopi = 2.0*3.1415;
    
    //3000 frequencies spread logartihmically between 1 and 32 Hz
    const float f0 = 0.1;
    const float f1 = 20;
    const int fn = 200;

    //Define number of threads for multithreaded use
    const int nthreads = 8;

    //input: n real numbers
    std::vector<float> sig(n);

    //input: n complex numbers
    std::vector<complex<float>> sigc(n);
    
    //output: n x scales x 2 (complex numbers consist of two parts)
    std::vector<complex<float>> tfm(n*fn);
    
    //initialize with 1 Hz cosine wave
    for(auto& el : sig) {
        el = cos(twopi*((float)(&el - &sig[0])/(float)fs));
    }

    //initialize with 1 Hz cosine wave
    for(auto& el : sigc) {
        el = complex<float>(cos(twopi*((float)(&el - &sigc[0])/(float)fs)), 0.0f);
    }
    
    //Start timing
    auto start = chrono::high_resolution_clock::now();
    
    //Create a wavelet object
    Wavelet *wavelet;
    
    //Initialize a Morlet wavelet having sigma=1.0;
    Morlet morl(1.0f);
    wavelet = &morl;

    //Other wavelets are also possible
    //DOG dog(int order); 
    //Paul paul(int order);

    //Create the continuous wavelet transform object
    //constructor(wavelet, nthreads, optplan)
    //
    //Arguments
    //wavelet   - pointer to wavelet object
    //nthreads  - number of threads to use
    //optplan   - use FFTW optimization plans if true
    FCWT fcwt(wavelet, nthreads, true, false);

    //Generate frequencies
    //constructor(wavelet, dist, fs, f0, f1, fn)
    //
    //Arguments
    //wavelet   - pointer to wavelet object
    //dist      - FCWT_LOGSCALES | FCWT_LINSCALES for logarithmic or linear distribution of scales across frequency range
    //fs        - sample frequency
    //f0        - beginning of frequency range
    //f1        - end of frequency range
    //fn        - number of wavelets to generate across frequency range
    Scales scs(wavelet, FCWT_LINFREQS, fs, f0, f1, fn);

    //Perform a CWT
    //cwt(input, length, output, scales)
    //
    //Arguments:
    //input     - floating pointer to input array
    //length    - integer signal length
    //output    - floating pointer to output array
    //scales    - pointer to scales object
    fcwt.cwt(&sigc[0], n, &tfm[0], &scs);
        
    //End timing
    auto finish = chrono::high_resolution_clock::now();

    //Calculate total duration
    chrono::duration<double> elapsed = finish - start;
    
    cout << "=== fCWT example ===" << endl;
    cout << "Calculate CWT of a 100k sample sinusodial signal using a [" << f0 << "-" << f1 << "] Hz linear frequency range and " << fn << " wavelets." << endl;
    cout << "====================" << endl;
    cout << "fCWT finished in " << elapsed.count() << "s" << endl;

    return 0;
}

 6、后记

自己是第一次接触这个库,小波变换的概念还不是很熟悉,使用也没有头绪。此外,库的配置也是随便乱折腾,如有疏忽,还请批评指正。

仍需不断学习。

  • 15
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值