matlab集成了很多算法代码,如声品质的各种计算,想直接使用matlab的代码可将起代码封装为函数进行导出库
一、MATLAB导出
选择导出的代码与路径
完全定义输入输出变量大小
用于测试代码,可跳过
选择静态库生成
生成成功
二、调用静态库
将生成的include和lib进行包含引用,添加依赖库
//部分声明
static emxArray_real_T* argInit_Unboundedx2_real_T(void);
static emxArray_real_T* argInit_Unboundedx2_real_Test(const char* FileName, int row, int col);
static emxArray_real_T* c_argInit_UnboundedxUnbounded_train(double* data, int row, int col);
emxArray_real_T* mapA;
emxArray_real_T* xouttrain;
emxArray_real_T* xintrain;
emxArray_real_T* xouttest;
emxArray_real_T* xinttest;
emxArray_real_T* coeff;
emxArray_real_T* train_mean;
//部分定义
double* ClassificationSVM::intacoustictrain(double* data, int row, int col)
{
emxInitArray_real_T(&xouttrain, 2);
emxInitArray_real_T(&coeff, 2);
emxInitArray_real_T(&train_mean, 1);
mapA = argInit_Unboundedx2_real_T();
xintrain = c_argInit_UnboundedxUnbounded_train(data, row, col);
acoustifeat(xintrain, 51200, 1, mapA, 1, coeff, train_mean, xouttrain);
double* resture = new double[col * 15];
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < col; j++)
{
resture[i+j*15] = xouttrain->data[i * col + j];
}
}
FILE* fp;
fopen_s(&fp, "mapA", "wb");
if (fp == NULL) return 0;
size_t written_size = fwrite(mapA->data, 166, 8, fp);
fclose(fp);
FILE* fp2;
fopen_s(&fp2, "coeff", "wb");
if (fp2 == NULL) return 0;
size_t written_size2 = fwrite(coeff->data, 3569, 8, fp2);
fclose(fp2);
FILE* fp3;
fopen_s(&fp3, "train_mean", "wb");
if (fp3 == NULL) return 0;
size_t written_size3 = fwrite(train_mean->data, 83, 8, fp3);
fclose(fp3);
return resture;
}
double* ClassificationSVM::intacoustictest(double* data, int row, int col)
{
emxInitArray_real_T(&xouttest, 2);
mapA = argInit_Unboundedx2_real_Test("mapA",83,2);
coeff = argInit_Unboundedx2_real_Test("coeff", 83, 43);
train_mean = argInit_Unboundedx2_real_Test("train_mean", 83, 1);
xinttest = c_argInit_UnboundedxUnbounded_train(data, row, col);
acoustifeat(xinttest, 51200, 0, mapA, 1, coeff, train_mean, xouttest);
double* resture = new double[col * 15];
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < col; j++)
{
resture[i + j * 15] = xouttest->data[i * col + j];
}
}
return resture;
}
emxArray_real_T* ClassificationSVM::argInit_Unboundedx2_real_T(void)
{
emxArray_real_T* result;
double* result_data;
int idx0;
int idx1;
/* Set the size of the array.
Change this size to the value that the application requires. */
result = emxCreate_real_T(2, 2);
result_data = result->data;
/* Loop over the array to initialize each element. */
for (idx0 = 0; idx0 < result->size[0U]; idx0++) {
for (idx1 = 0; idx1 < 2; idx1++) {
/* Set the value of the array element.
Change this value to the value that the application requires. */
result_data[idx0 + result->size[0] * idx1] = 0;
}
}
return result;
}
emxArray_real_T* ClassificationSVM::argInit_Unboundedx2_real_Test(const char* FileName,int row,int col)
{
double* data = new double[row*col];
FILE* fp;
fopen_s(&fp, FileName, "rb");
if (fp == NULL) return 0; //空指针则返回0,文件打开失败
fread(data, row * col, 8, fp);
fclose(fp);
emxArray_real_T* result;
result = emxCreate_real_T(row, col);
result->data = data;
return result;
}
//初始化训练数据
//第一个变量,第二个变量,串行。
emxArray_real_T* ClassificationSVM::c_argInit_UnboundedxUnbounded_train(double* data, int row, int col)
{
emxArray_real_T* result;
double* result_data;
int idx0;
int idx1;
/* Set the size of the array.
Change this size to the value that the application requires. */
result = emxCreate_real_T(row, col);
result_data = result->data;
/* Loop over the array to initialize each element. */
cout << result->size[0U] << " " << result->size[1U] << endl;
int i = 0;
for (idx0 = 0; idx0 < result->size[0U]; idx0++) {
for (idx1 = 0; idx1 < result->size[1U]; idx1++) {
/* Set the value of the array element.
Change this value to the value that the application requires. */
result_data[idx0 + result->size[0] * idx1] = data[i];
i++;
}
}
return result;
}
头文件中声明matlab函数的输入输出变量结构体。定义转换函数,转换成matlab中的矩阵形式。
生成中带有例子example,可以看下仿照写转换函数。
三、动态库链接生成
新建项目文件,在项目属性中选择release版,将配置类型更改为动态库(dll),
在代码生成中选择多线程(MT)运行库
在-链接器-输入中添加附加依赖项,并添加模块定义文件。
模块定义文件内容如下,
LIBRARY "NOISE_SVM"//生成库名称
EXPORTS //函数导出
test1 //所要导出的函数名称
导出函数头如下