Skip to content

Commit

Permalink
building up radae_tx.c
Browse files Browse the repository at this point in the history
  • Loading branch information
drowe67 committed Sep 20, 2024
1 parent 3455639 commit 4f23cdc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
98 changes: 98 additions & 0 deletions embed/radae_tx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Top level C program for embedded version of radae_tx.py */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h"

#define NARGS 2

int main(void)
{
PyObject *pName, *pModule, *pFunc;
PyObject *pValue;
//PyObject *pArgs;
char *python_name = "radae_tx";
char *func_name = "get_nb_floats";

Py_Initialize();

// need import array for numpy
int ret = _import_array();
fprintf(stderr, "import_array returned: %d\n", ret);

// name of Python script
pName = PyUnicode_DecodeFSDefault(python_name);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);

if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, func_name);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
#ifdef T
pArgs = PyTuple_New(NARGS);

// first two args from command line
pValue = PyLong_FromLong(atol(argv[3]));
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyLong_FromLong(atol(argv[4]));
PyTuple_SetItem(pArgs, 1, pValue);

// 3rd Python function arg - set up numpy array
long dims = 3;
float arr_in[] = {1.0,2.0,3.0};
pValue = PyArray_SimpleNewFromData(1, &dims, NPY_FLOAT, arr_in);
if (pValue == NULL) {
PyErr_Print();
fprintf(stderr,"Error setting up numpy array\n");
}
PyTuple_SetItem(pArgs, 2, pValue);

// 4th Python arg is a numpy array used for output to C
float arr_out[] = {0.0,0.0,0.0};
pValue = PyArray_SimpleNewFromData(1, &dims, NPY_FLOAT, arr_out);
if (pValue == NULL) {
PyErr_Print();
fprintf(stderr,"Error setting up numpy array\n");
}
PyTuple_SetItem(pArgs, 3, pValue);
#endif

// do the function call
pValue = PyObject_CallObject(pFunc, NULL);
//Py_DECREF(pArgs);
if (pValue != NULL) {
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
Py_DECREF(pValue);

// not sure how to return arrays but can modify input arrays in place as a hack
//printf("returned array: %f %f %f\n", arr_out[0], arr_out[1], arr_out[2]);
}
else {
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", func_name);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", python_name);
return 1;
}
if (Py_FinalizeEx() < 0) {
return 120;
}
return 0;
}
8 changes: 6 additions & 2 deletions embed/radae_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@
transmitter = transmitter_one(model.latent_dim,model.enc_stride,model.Nzmf,model.Fs,model.M,model.Ncp,
model.Winv,model.Nc,model.Ns,model.w,model.P,model.bottleneck,model.pilot_gain)

# number of input floats per processing frame
# number of input floats per processing frame (TOOD refactor to more sensible variable names)
nb_floats = model.Nzmf*model.enc_stride*nb_total_features
# number of output csingles per processing frame
Nmf = int((model.Ns+1)*(model.M+model.Ncp))

def get_nb_floats():
return nb_floats
def get_Nmf():
return Nmf

def do_radae_tx(buffer_f32,tx_out):

with torch.inference_mode():
Expand All @@ -95,7 +100,6 @@ def do_radae_tx(buffer_f32,tx_out):
tx_out = np.zeros(Nmf,dtype=np.csingle)
while True:
buffer = sys.stdin.buffer.read(nb_floats*struct.calcsize("f"))
#print(len(buffer), file=sys.stderr)
if len(buffer) != nb_floats*struct.calcsize("f"):
break
buffer_f32 = np.frombuffer(buffer,np.single)
Expand Down

0 comments on commit 4f23cdc

Please sign in to comment.