如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
MATLAB与c#混合编程教程
(MATLAB采用2010b,一切叙述以文字为准,图片可能不是很准确)
kalman滤波在c#中实现
首先在MATLAB中编写m文件kalmanfilter.m
functionoutput=kalmanfilter(Y)
[m,n]=size(Y);
ifm~=1
end
a=1;
w=randn(1,n);
V=randn(1,n);
q1=std(V);
Rvv=q1.^2;
q3=std(w);
Rww=q3.^2;
c=1;
x(1)=0;
p(1)=100;
fort=2:n
p1(t)=a.^2*p(t-1)+0.05*Rww;
b(t)=c*p1(t)/(c.^2*p1(t)+Rvv);
x(t)=a*x(t-1)+b(t)*(Y(t)-a*c*x(t-1));
p(t)=p1(t)-c*b(t)*p1(t);
end
用MATLAB的deploytool工具箱
键入deploytool打开如下界面
更改名字kalmanfilter.prj
更改location为你想要的文件夹
在target选项中选择.NETAssembly
点ok
MATLAB右侧界面如图
点击addclass
默认名class1
点击addfile
把刚才编写的kalmanfilter文件添加进去然后点击build
然后打开vs2010新建一个wpf文件
界面粗略设计如下
【在这之前还应编写个画图的文件
用MATLAB编写plota.m文件
functionplota(a,b)
plot(a);
holdon
plot(b,'r');
legend('原始','滤波后');
xlabel('序号');
ylabel('数值');
Title('kalmanfilter');
然后用deploytool同样的方法
(name为plota,class1)】
双击开始按钮(button1)
弹出页面开始编辑
下面编写c#文件
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
usingMathWorks.MATLAB.NET.Arrays;
usingMathWorks.MATLAB.NET.Utility;
usingkalmanfilter;
namespaceWpfApplication5
{
///<summary>
///MainWindow.xaml的Ì?交?互£¤逻?辑-
///</summary>
publicpartialclassMainWindow:Window
{
publicMainWindow()
{
InitializeComponent();
}
privatevoidbutton1_Click_1(objectsender,RoutedEventArgse)
{
stringstr="";
string[]v=newstring[textBox1.Text.Length];
intj=0;
char[]char1=textBox1.Text.ToCharArray();
string[]d=newstring[textBox1.Text.Length];
intu=0;
for(inti=0;i<textBox1.Text.Length;i++)
{
if(char1[i]!='')
str=str+char1[i];
else
{
v[j]=str;
str="";
j++;
}
intn=0;
for(intm=0;m<textBox1.Text.Length;m++)
{
if(v[m]!=""&&v[m]!=null)
{
d[n]=v[m];
n++;
}
}
u=n;
}
MWNumericArrayb=newMWNumericArray(MWArrayComplexity.Real,1,u);
for(intq=1;q<=u;q++)
{b[q]=Convert.ToDouble(d[q-1
qw****27
实名认证
内容提供者
最近下载