MATLAB和c#混合编程实现心电图显示软件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MATLAB和c#混合编程实现心电图显示软件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9329字,纯文字阅读大概需要14分钟。
内容图文
[ 在此处输入文章标题 ]
由于 MATLAB 自带的 GUI 平台设计的界面不是很美观而且设计过程并不是很方便,我们选择了用 c# 来做软件界面的实现。我们用 MATLAB 做信号处理封装成函数,把函数编译成 dll 格式,然后用 c# 调用 MATLAB 的函数即可。在设计过程中遇到两个主要的麻烦,一个是 MATLAB 和 c# 数值类型的转化问题,而且 c# 函数多输出、多输入问题是从来没有遇到过的,另一个问题是实现动态的绘制心电曲线,我最后通过一个定时器不断的刷新画图解决了这个问题。下面详细介绍实现过程。
1 c# 调用 matlab 函数
matlab 版本 2013a , c# 版本 vs2010.
1.1 matlab 函数编译成 dll 文件
( 1 )首先编写一个函数的 m 文件,如 MatrixOpera 表示两个矩阵相加和相减。
%--------------------------------------------------------
function [addRlt,minusRlt]=MatrixOpera(a,b)
% 矩阵相加 , 相减
[m1,n1]=size(a);
[m2,n2]=size(b);
if m1~=m2 || n1~=n2
display(‘ 矩阵大小不相同 ‘)
error(‘ 参数错误 ‘);
end
addRlt=a+b;
minusRlt=a-b;
end
%-----------------------------------------------------------
两个输入参数,两个输出参数,并且都是矩阵形式
( 2 )在 matlab 命令窗口输入 deploytool ,弹出如下窗口
或者点击 matlab 的主菜单 desktop → deploy tool 获得如下面图所示的窗口,然后在 file → new → deployment project 中点击。获得上图窗口,修改工程名称和文件后缀名(必须是, .Net Assembly 格式)
( 3 )新建了一个叫 matPrj 的工程(它相当于 c# 中的命名空间 namespace );然后给它增加 class 或类(它就是 c# 中的类),点 [add class] 比如 myMathClass ;之后再给它添加方法(它就是函数了),点 [add files] ,如下图所示。本实例中给它添加了 MatrixOpera 等函数。
( 4 )完了之后就可以编译了。编译出来后需要一两分钟的时间。
( 5 )找到该工程存放的文件夹,从里面拷出 matPrj.dll 文件。同时还要从 G:\Program Files\MATLAB\R2009b\toolbox\dotnetbuilder\bin\win32\v2.0 拷贝出 MWArray.dll 和 ManagedCPPAPI.netmodule 。第二个文件必须要哦,否则可能会出错。
1.2 c# 添加 matlab 的 dll 引用
( 1 )打开 vs2008 ,新建一个窗体应用程序。
( 2 )将刚才拷贝到的那 3 个文件一起放在 vs 工程( CallMatlabDllApp )的 debug 文件夹下面。然后右键下图中的引用,选择添加引用,弹出一个窗口,选择其中的浏览页面,分别添加 matPrj.dll 文件和 MWArray.dll 文件。
( 3 )最后在前面,代码的前面添加下面的命名空间即可。
using MathWorks.MATLAB.NET.Arrays;// 在MWArray.dll,最常用的
using MathWorks.MATLAB.NET.Utility;// 在MWArray.dll,最常用的
using matPrj;// 这个就是我们自己定义的,里面有matlab函数
如果 matlab 函数复杂,还需要用到其他的空间,则视情况而定,自己凭经验添加。
至此,已经可以利用 c# 调用我们用 matlab 编写的函数了。
1.3 函数调用
函数调用前必须注意:
( 1 )将 c# 的参数输入到 matlab 函数时,要将参数转化为 matlab 的参数形式,通常是 MWArray 类型。
( 2 ) matlab 返回的参数,也要转化为 c# 用的类型,比如数组或者数值类型。
直接将值传递给已经初始化的 MWArray 数组中的成员
直接将数据类型赋值给已经初始化的 MWNumericArray 变量。
直接将字符串类赋值给已经初始化的 MWCharArray 变量。
如果是数组类型:
直接赋值给 MWNumericArray 变量;
赋值给 MWArray 变量,则在前面加上类型转换如:( MWNumericArray )进行强制转换。
总之, MWArray 是总类型,其它的以 MW 开头,以 Array 结尾的变量类型都可以直接对它进行赋值或取值。
M 类型到 C++/C# 数据类型
MWArray
M 类型,它是 M 文件的编译后内部的标准类型,一切 C++/C# 类型都要最终转换成此类型,方可作为参数调用 M 语言函数。
MWCharArray
M 的字符串类型,使用它可以将 M 中的字符类型转换成 C++/C# 的字符串类型。
MWNumericArray
MWNumericArray 是 MWArray 与 C# 等语言的转换中间类型。
常用的转换函数:
① public Array ToArray(MWArrayComponent component);
将 M 类型转换成 C# 的 Array 类型,然后可以直接转换成其它类型的数组。
② public byte ToScalarByte();
将 M 类型转换成 C# 的字节类型;
③ public double ToScalarDouble();
将 M 类型转换成 C# 的双精度类型;
double temp = ((MWNumericArray)(mwArgout[0])).ToScalarDouble();
④ public float ToScalarFloat();
将 M 类型转换成 C# 的单精度类型;
⑤ public int ToScalarInteger();
将 M 类型转换成 C# 的整型类型;
⑥ public long ToScalarLong();
将 M 类型转换成 C# 的长整 C/C++/C# 数据型类型;
⑦ public short ToScalarShort();
将 M 类型转换成 C# 的短整型类型;
⑧ public override string ToString();
将 M 类型转换成 C# 的字符串类型; string arror = mwArgout[2].ToString();
⑨ public Array ToVector(MWArrayComponent component);
将 M 类型转换成 C# 的 Array 类型,然后可以直接转换成其它类型的数组。
下面使用调试过的代码示例表述①⑨两个函数的区别:
①
double[,] Temp1 = new double[1,3];
Temp1= (double[,])((MWNumericArray)mwArgout[1]).ToArray(MWArrayComponent.Real);
⑨
double[] s1 = new double[2];
s1 = (double[])((MWNumericArray)mwArgout[1]).ToVector(MWArrayComponent.Real);
( 3 )必须注意到多参数输入和多参数返回的问题。刚开始碰到这个这个问题比较头疼,后来经过不懈的努力,终于从网上找到答案。
声明这部分是我参考别人的想法自己写的额,网上百度知道也有我( lwq123_321 )回答的。
// 输入这里想传入的 2 个输入参数,为了支持矩阵好通用,所以得弄成 Array
double[] a = { 1, 2, 3, 4, 5, 6 };// 输入参数 1
double[] b = { 2, 4, 6, 8, 10, 12 };// 输入参数 2
double[,] c = new double[3, 2];// 输出参数 1
double[,] d = new double[3, 2];// 输出参数 2
// 这些参数都是矩阵
MWNumericArray ma = new MWNumericArray(3, 2, a);// 转换成 matlab 需求的格式
MWNumericArray mb = new MWNumericArray(3, 2, b);
// 输出参数是一个 MWArray 数组
MWArray[] agrsOut = new MWArray[2];// 两个输出参数,一定要写数量
// 输出几个输出参数可以是不同类型的,比如第一个元素是矩阵,第二个是数值
// 同理,输入参数也是一个 MWArray 数组
MWArray[] agrsIn = new MWArray[] { ma,mb};
// 调用函数,输出参数需要加 ref 关键字
myFun.MatrixOpera(2, ref agrsOut, agrsIn);
//2 表示输入参数的个数,输出结构都在 argsOut 中,类似于 c 的指针参数输入
// 转换得到实际的输出参数
MWNumericArray x1 = agrsOut[0] as MWNumericArray;
MWNumericArray x2 = agrsOut[1] as MWNumericArray;
c = (double[,])x1.ToArray();
d = (double[,])x2.ToArray();
// 一定要注意最后 c 和 d 的转化,不同类型的转换差异很大厄
//ToArray() 对应 n*m 的数组
//ToScalarDouble() 对应单个数值
//ToVetor() 对应1维数组
到此,已经实现了c#调用matlab函数的整个过程。
2 软件界面的设计
2.1 软件设计
C# 有很方便的界面设计平台。下图是我最后完成的效果:
我实现了心电曲线的实时绘制,正常 QT 间期范围设置、 QT 间期、 QR 间期的实时显示、可以显示一分钟之内的平均 QT 间期、 QT 间期状态一栏可实现间期预警,如果 QT 间期大于正常 QT 间期范围则状态颜色由绿色变成红色。用了一个 100ms 的定时器,每次 100ms 之后触发一次画图,让曲线往右移一格,由于 100ms 很短,所以在视觉上形成了连续移动的效果。核心代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MathWorks.MATLAB.NET.Arrays;// 在 MWArray.dll ,最常用的
using MathWorks.MATLAB.NET.Utility;// 在 MWArray.dll ,最常用的
using third;
namespace MsPaint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double[,] rbegin;
double[,] pend;
double[,] rpk;
double qt_mean;
double[] ce = new double[5];
MWArray d1;
double[,] f1;
MWArray[] agrsOut = new MWArray[4];// 两个输出参数,一定要写数量
public Point[] ptlist;// 存放点的数组
Point[] data = new Point[12000];
//Random rm = new Random();// 随机数产生器
Timer mytimer = new Timer();// 定时器
third.Class1 pcl = new Class1();
int 网格间距 = 12; // 网格间距
int 网格偏移 = 0; // 网格偏移
Pen 网格颜色 = new Pen(Color.FromArgb(0x00, 0x80, 0x40));
Pen 曲线颜色 = new Pen(Color.Lime);
Pen R 颜色 = new Pen(Color.Red,1);
Pen Q 颜色 = new Pen(Color.DeepSkyBlue, 1);
Pen T 颜色 = new Pen(Color.DeepPink, 1);
int time_count = 20;
// 窗口加载时调用
private void Form1_Load(object sender, EventArgs e)
{
// 设置控件的样式和行为,以减少图像的闪烁
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.UpdateStyles();
}
draw drawtest = new draw();// 创建类 draw 的实例
private void 打开 OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog d = new OpenFileDialog();
d.Filter = "(*.mat)|*.mat| 所有文件 (*.*)|*.*"; if (d.ShowDialog() == DialogResult.OK)
{
FileStream fs = File.OpenRead(d.FileName); StreamReader sr = new StreamReader(fs); string s;
string filename = d.FileName;
d1 =pcl.loaddata(filename);
// MessageBox.Show(filename);
MWArray[] agrsIn = new MWArray[] {d1};
pcl.pces(4, ref agrsOut, agrsIn);
MWNumericArray x1 = agrsOut[0] as MWNumericArray;
MWNumericArray x2 = agrsOut[1] as MWNumericArray;
MWNumericArray x3 = agrsOut[2] as MWNumericArray;
MWNumericArray x4 = agrsOut[3] as MWNumericArray;
rbegin = (double[,])x1.ToArray();
pend = (double[,])x2.ToArray();
rpk = (double[,])x3.ToArray();
f1 = (double[,])d1.ToArray();
qt_mean = x4.ToScalarDouble();
textBox4.Text = qt_mean.ToString();
for (int i = 0; i < 12000; i++)
{
data[i].X = (int)i;// 强制类型转换,将 double 转为 int ,可能会丢失数据
data[i].Y = (int)((1000-f1[0, i*5]) * 250 / 4500 + 100);
}
this.timer1.Enabled = true;// 可以使用
this.timer1.Interval = 100;// 定时时间为 100 毫秒
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.timer1.Start();// 启动定时器
}
}
private void button1_Click(object sender, EventArgs e)
{
// 动态添加一个定时器
timer1.Start();// 启动定时器
textBox1.Enabled = false;
textBox5.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
textBox1.Enabled = true;
textBox5.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (time_count == 1100)
time_count = 0;
else
time_count++;
网格偏移 = ( 网格偏移 + 1000) % 网格间距 ;
Invalidate();
Point[] temp = new Point[600];
for (int i = 0; i < 600; i++)
{
temp[i].X = i;
temp[i].Y = data[time_count * 10 + i].Y;
}
// 调用绘图函数 , 这里的参数可以根据不同的测量给定不同的实参
//drawtest.DrawLineS(Color.Blue, 1200, 600, pictureBox1, ptlist);
int index = (int)time_count / 20;
double QT = (pend[0, index]-rbegin[0, index])/1000;
Graphics g2 = pictureBox2.CreateGraphics();// 创建 PictureBox 窗体的画布
if ((QT < double.Parse(textBox1.Text)) || (QT > double.Parse(textBox5.Text)))
{
g2.FillRectangle(Brushes.Red, g2.ClipBounds);
}
else
{
g2.FillRectangle(Brushes.LightGreen, g2.ClipBounds);
}
textBox2.Text = Convert.ToString(QT);
double QR = (rpk[0, index]-rbegin[0, index])/1000;
textBox3.Text = Convert.ToString(QR);
// drawtest.DrawLineS(Color.Blue, 400, 2400, pictureBox1, data);
Graphics g1 = pictureBox1.CreateGraphics();// 创建 PictureBox 窗体的画布
g1.FillRectangle(Brushes.Black, g1.ClipBounds);
// 绘制纵线 从右向左绘制
for (int i = pictureBox1.Width - 网格偏移 ; i >= 0; i -= 网格间距 )
g1.DrawLine( 网格颜色 , i, 0, i, pictureBox1.Height);
// 绘制横线
for (int i = pictureBox1.Height; i >= 0; i -= 网格间距 )
g1.DrawLine( 网格颜色 , 0, i, pictureBox1.Width, i);
for (int i = 0; i < 60; i++)
{
int line = (int)(rpk[0, i]/5-time_count * 10);
g1.DrawLine(R 颜色 , line, 0, line, pictureBox1.Height);
}
for (int i = 0; i < 60; i++)
{
int line = (int)(pend[0, i] / 5 - time_count * 10);
g1.DrawLine(T 颜色 , line, 0, line, pictureBox1.Height);
}
for (int i = 0; i < 60; i++)
{
int line = (int)(rbegin[0, i] / 5 - time_count * 10);
g1.DrawLine(Q 颜色 , line, 0, line, pictureBox1.Height);
}
// 绘制曲线 若想曲线从右向左移动,则必须先绘制后面的
Pen p = new Pen(Color.Yellow, 1);// 画笔
g1.DrawLines(p, temp);// 五点绘图,直线连接
}
}
}
2.2 软件的使用
在 \MsPaint\bin\Debug 目录下,双击:实时绘制曲线 .exe 运行如下图:
单击:文件 - 打开
由于我们只做了最后一个的处理,故只能选择最后一个打开,打开之后则可看到程序运行效果:
项目github:https://github.com/HUSTLX/electrocardiogram
原文:http://www.cnblogs.com/hustlx/p/5264723.html
内容总结
以上是互联网集市为您收集整理的MATLAB和c#混合编程实现心电图显示软件全部内容,希望文章能够帮你解决MATLAB和c#混合编程实现心电图显示软件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。