c++ - 初学者用 Python 扩展 C(特别是 Numpy)

标签 c++ python c api numpy

我正在开发一个实时音频处理动态链接库,其中有一个代表音频缓冲区的 float 据的二维 C 数组。一维是时间(样本),另一维是 channel 。我想将其作为用于 DSP 处理的 numpy 数组传递给 python 脚本,然后我想将其传递回 C,以便数据可以在 C 中的处理链中继续进行。C++ 中的成员函数执行处理看起来像这样:

void myEffect::process (float** inputs, float** outputs, int buffersize)
{
    //Some processing stuff
}

数组输入输出 大小相等。整数buffersize输入输出 数组中的列数。在 python 方面,我希望通过如下所示的函数执行处理:

class myPyEffect
    ...
    ...
    def process(self,inBuff):
    #inBuff and outBuff should be numpy arrays
        outBuff = inBuff * self.whatever # some DSP stuff
        return outBuff
    ...
    ...

现在,我的问题是,如何以最有效的方式(避免不必要的内存复制等)将数据传入和传出 C?到目前为止,对于简单的参数更改,我一直在使用如下所示的 C-API 调用:

pValue = PyObject_CallMethod(pInstance, "setParameter", "(f)", value);

我是对我的 numpy 数组使用类似的东西还是有更好的方法?感谢阅读。

最佳答案

您可以完全避免处理 NumPy C API。 Python 可以使用 ctypes 模块调用 C 代码,您可以使用数组的 ctypes 属性访问指向 numpy 数据的指针。

这是一个显示一维平方和函数过程的最小示例。

ctsquare.c

#include <stdlib.h>

float mysumsquares(float * array, size_t size) {
    float total = 0.0f;
    size_t idx;
    for (idx = 0; idx < size; ++idx) {
        total += array[idx]*array[idx];
    }
    return total;
}

编译为ctsquare.so

这些命令行适用于 OS X,您的操作系统可能会有所不同。

$ gcc -O3 -fPIC -c ctsquare.c -o ctsquare.o
$ ld -dylib -o ctsquare.so -lc ctsquare.o

ctsquare.py
import numpy
import ctypes

# pointer to float type, for convenience
c_float_p = ctypes.POINTER(ctypes.c_float)

# load the library
ctsquarelib = ctypes.cdll.LoadLibrary("ctsquare.so")

# define the return type and arguments of the function
ctsquarelib.mysumsquares.restype = ctypes.c_float
ctsquarelib.mysumsquares.argtypes = [c_float_p, ctypes.c_size_t]

# python front-end function, takes care of the ctypes interface
def myssq(arr):
    # make sure that the array is contiguous and the right data type
    arr = numpy.ascontiguousarray(arr, dtype='float32')

    # grab a pointer to the array's data
    dataptr = arr.ctypes.data_as(c_float_p)

    # this assumes that the array is 1-dimensional. 2d is more complex.
    datasize = arr.ctypes.shape[0]

    # call the C function
    ret = ctsquarelib.mysumsquares(dataptr, datasize)

    return ret

if __name__ == '__main__':
    a = numpy.array([1,2,3,4])
    print 'sum of squares of [1,2,3,4] =', myssq(a)

关于c++ - 初学者用 Python 扩展 C(特别是 Numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2290007/

相关文章:

您的计算机缺少 C++ boost 线程

c++ YUYV 422 水平和垂直翻转

python - 如何在使用 Python 的 smtplib 发送的电子邮件中获取换行符?

python - 使用 Azure CLI、Rest API 或 Python 在 Azure ADLS gen2 中复制文件

c++ - 确定点是否在 boost::geometry::linear_ring 会计方向内

if 语句中的 C++ 范围问题 : create one of two objects

python - 如何检查是否从脚本中设置了 python 调试选项

c - printf newline 有行缓冲区吗?

c - 多维数组

c - 如何将 .txt 文件的内容复制到 char 数组中?