首页 > 解决方案 > C:将包含矩阵的多个向量复制到单个向量中时遇到问题

问题描述

包:GNU C

我正在尝试将函数 iceemdan() 添加到开源包 libeemd ( https://bitbucket.org/luukko/libeemd )。它编译但产生错误的结果。iceemdan() 基于同一个包中的 ceemdan() 和 eemd()。首先,代码的简化版本:

libeemd_error_code iceemdan(double const* restrict input, size_t N,
        double* restrict output, size_t M,
        unsigned int ensemble_size, double noise_strength, unsigned int
        S_number, unsigned int num_siftings, unsigned long int rng_seed) {
    ...
    // Initialize output data to zero
    memset(output, 0x00, M*N*sizeof(double));
    // Storage for white noise modes
    double* wnmodes = malloc(M*ensemble_size*N*sizeof(double));
    ...
        for (size_t en_i=0; en_i<ensemble_size; en_i++) {
            ...
            // Provide a pointer to the wnnodes vector where these modes will be stored
            double* const wn = &wnmodes[en_i*M*N];
            double* r_i = malloc(M*N*sizeof(double));
            memset(r_i, 0x00, M*N*sizeof(double));
            // Generate white noise
            ...
            // Extract white noise modes to r_i
            emd_err = _emd(w->x, w->emd_w, r_i, M, S_number, num_siftings);
            ...
            array_copy(r_i, N, wn);
            ...
        }
    ...
    // Lots more processing
}

iceemdan() 的参数 *output 包含输出。第一个 memset() 将其初始化为 0。 *wnnodes 有效地包含 *output 的 ensemble_size 版本(在 for 循环中定义的 *r_i)。每次迭代首先初始化 r_i,集合成员 i 的 *output 版本和 wn,指向 wnnodes 中存储 r_i 的位置的指针。array_copy() 将 r_i 复制到 wn。

这是array_copy的定义:

inline static void array_copy(double const* restrict src, size_t n, double* restrict dest) {
    memcpy(dest, src, n*sizeof(double));
}

问题是 wnnodes 最终得到 ensemble_size 的废话副本。它在第一行有零,_emd 不能用非零数据生成。我怀疑问题出在 wn 的定义上。我试图从其定义中删除“&”并得到一个错误。对于有经验的人来说,这可能非常明显。

标签: cvectorcopy

解决方案


推荐阅读