我正在尝试在用 C 编写的 Turbodecode 算法中实现精度索引。在找到一种方法之后,我试图嵌入一个 python 函数以便你使用 matplotlib 并绘制一个显示解码器精度的图表。
问题是尝试在 PYImport_Import() 处嵌入停止,而无需进一步操作且不提供任何错误消息。
我的 python 文件名为 plot.py ,里面很简单:
import matplotlib.pyplot as plt
def show_plt(max_x, y):
print("enter")
fig, ax1 = plt.subplots()
x_array = range(0,max_x+1)
ax1.plot(x_array, y)
plt.show()
而我试图调用这个python函数的C部分代码是这样写的:
#include <python2.7/Python.h>
...
if (decision)
{
printf("here1\n");
Py_Initialize();
//PyRun_SimpleString("import sys; sys.path.append('.')");
decoded = malloc(packet_length * sizeof *decoded);
double est[packet_length];
PyObject *tuple = PyList_New(packet_length);
for (int i = 0; i < packet_length; ++i)
{
double one = app[1][i] + extrinsic[1][i];
double zero = app[0][i] + extrinsic[0][i];
double delta = abs(one + zero);
if (delta > 150)//THRESHHOLD TO DEFINE
delta = 150;
est[i] = (150 - delta) / 150;
PyList_Append(tuple,PyFloat_FromDouble(est[i]));
decoded[i] = one > zero;
}
printf("here2\n");
PyObject *myModuleString = PyString_FromString((char *)"plot");
printf("here3\n");
PyObject *myModule = PyImport_Import(myModuleString);
printf("here4\n");
PyObject *myFunction = PyObject_GetAttrString(myModule, (char *)"show_plt");
printf("here5\n");
PyObject *args = PyTuple_Pack(2, packet_length, tuple);
printf("here6\n");
PyObject_CallObject(myFunction, args);
printf("here7\n");
}
请注意,代码在 printf("here3") 之后停止,没有错误。
我不熟悉在 C 中调用 python 函数,所以任何帮助都将不胜感激。