[FK]matlab 椭圆拟合

–注意matlab版本选择18版本以上,否则不支持部分函数

[cc lang=”matlab” tab_size=”4″]

function [ellipse] = ellipsefit(x,y)

x=[3
1
-2
2
0
-5
-15
-18
-21
-29
-38
-44
-55
-59
-67
-74
-80
-89
-103
-105
-108
-119
-120
-130
-132
-136
-138
-141
-154
-161
-175
-185
-186
-192
-198
-200
-203
-211
-217
-221
-222
-222
-226
-224
-230
-228
-227
-225
-221
-221
-218
-209
-210
-203
-196
-190
-186
-178
-172
-162
-153
-145
-136
-125
-114
-106
-98
-84
-83
-73
-64
-61
-54
-41
-33
-28
-17
-6
-3
10
26
31
44
50
57
65
71
84
90
98
108
117
115
124
142
153
158
162
168
171
183
187
196
197
205
212
218
218
215
223
224
225
229
224
227
227
220
225
219
218
218
213
209
207
200
197
189
179
167
162
157
149
143
128
121
115
108
99
96
92
88
87
74
70
63
56
47
41
23
20
14
15
13
19
15
8
1
-7
-15
-28
-36
-40
-46
-53
-58
-58
-68
-77
-85
-94
-103
-118
-127
-130

];

y=[-234
-233
-235
-231
-234
-233
-237
-233
-229
-226
-226
-225
-219
-222
-218
-215
-215
-208
-209
-211
-210
-206
-195
-192
-195
-189
-184
-184
-172
-156
-146
-140
-117
-114
-94
-88
-74
-71
-62
-47
-37
-20
-8
5
29
39
44
66
66
82
86
87
99
107
116
129
138
148
155
160
174
179
188
187
197
203
197
208
206
207
210
212
220
226
225
226
235
239
237
227
227
226
223
220
219
221
216
210
203
201
196
194
188
190
182
178
165
165
157
156
149
131
127
119
101
93
89
81
68
50
38
33
29
17
2
-10
-17
-26
-30
-43
-49
-63
-72
-88
-94
-100
-109
-119
-138
-139
-142
-151
-151
-161
-164
-167
-168
-169
-162
-163
-160
-159
-158
-158
-164
-164
-158
-152
-154
-162
-168
-170
-185
-185
-190
-180
-180
-174
-171
-172
-173
-163
-163
-165
-161
-160
-157
-161
-156
-154
-155
-152
-145
-147

];

%x=x+9.9831;
%y=y+26.9348;
%y = y*1.054;
%y = y*1.00134;
grid on;
hold on;
plot(x,y,’*’)

% 采用最小二乘法进行椭圆拟合
% 采用椭圆一般式子:x^2 + A*x*y + B*y^2 + C*x + D*y + E = 0;

xlength = length(x);
xmax = max(x);
ymax = max(y);
if(xlength ~= length(y) | xlength < 5) warning('椭圆拟合至少需要四个点数据'); else M1 = [ sum(x.^2.*y.^2), sum(x.*y.^3), sum(x.^2.*y), sum(x.*y.^2), sum(x.*y) sum(x.*y.^3), sum(y.^4), sum(x.*y.^2), sum(y.^3), sum(y.^2) sum(x.^2.*y), sum(x.*y.^2), sum(x.^2), sum(x.*y), sum(x) sum(x.*y.^2), sum(y.^3), sum(x.*y), sum(y.^2), sum(y) sum(x.*y), sum(y.^2), sum(x), sum(y), xlength] ; %M1满秩才可逆 if rank(M1) == 5 M2 = -[ sum(x.^3.*y); sum(x.^2.*y.^2); sum(x.^3); sum(x.^2.*y); sum(x.^2)]; G = inv(M1)*M2; [A,B,C,D,E] = deal(G(1),G(2),G(3),G(4),G(5)); ellipse = [A,B,C,D,E]; Xp = (A*D-2*B*C)/(A*A-4*B); Yp = (A*C-2*D)/(A*A-4*B); Xc = -Xp; Yc = -Yp; theta_r = 0.5*atan(A/(B-1)); theta_offset = - theta_r; a = sqrt((Xp^2+A*Xp*Yp+B*Yp^2-E)/(cos(theta_r)^2-A*sin(theta_r)*cos(theta_r)+B*sin(theta_r)^2)); b = sqrt((Xp^2+A*Xp*Yp+B*Yp^2-E)/(sin(theta_r)^2+A*sin(theta_r)*cos(theta_r)+B*cos(theta_r)^2)); %绘图 plot(x,y,'bo') fimplicit(@(x,y)x.^2 + A.*x.*y + B.*y.^2 + C.*x + D.*y + E ,'-r','LineWidth', 3) plot(Xc,Yc,'ro','LineWidth', 3); plot([Xc Xc+a*cos(theta_offset)],[Yc Yc+a*sin(theta_offset)],'--p','LineWidth', 3); title(['圆心坐标为(' num2str(Xc) ',' num2str(Yc) '),偏移角为' num2str(theta_offset*180/pi) '°,长轴为' num2str(a) ',短轴为' num2str(b) ... newline 'x^2 + ' num2str(A) 'xy + ' num2str(B) 'y^2 + ' num2str(C) 'x + ' num2str(D) 'y + ' num2str(E) '= 0']); axis equal end end end [/cc]

[C#] UDP Socket

static Socket client = null;

//[连接]

client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
client.Bind(new IPEndPoint(IPAddress.Parse(LocalIP.Text.Trim()), int.Parse(LocalPort.Text.Trim())));
client.Connect(IPAddress.Parse(DevIP.Text.Trim()), int.Parse(DevPort.Text.Trim()));
RecordClientInfo("[客户端报告]连接服务器成功!");
}
catch (Exception)
{
RecordClientInfo("[客户端报告]连接服务器失败!");
return;
}

if (threadClientRev == null)
threadClientRev = new Thread(ClientRev);
threadClientRev.IsBackground = true;
threadClientRev.Start();

//[发送]

client.Send(buffer);

//[接收]

 

private void ClientRev()
{
UInt32[] info_error = new UInt32[22]; ;
while (!cancelTokenSource.IsCancellationRequested)
{
try
{ //定义一个1M的内存缓冲区,用于临时性存储接收到的消息
byte[] arrRecvmsg = new byte[1024 * 1024];
//将客户端套接字接收到的数据存入内存缓冲区,并获取长度
int length = client.Receive(arrRecvmsg);
//将套接字获取到的字符数组转换为人可以看懂的字符串
byte [] bufrcs= new byte[length];for (int i = 0; i < length; i++)
{
bufrcs[i] = arrRecvmsg[i];
}

}

}

[SF]1阶和3阶低通滤波对比

采样频率100HZ,截止频率20HZ ,效果很明显

x=[0:1:99];
p1 =x ;
iir1 =x ;

f=x;
fc=100; %采样频率

t=1/fc.*x;

f1=2; %信号频率
w1=2*pi*f1;

f2 =40; %干扰频率1
w2=2*pi*f2;

hold on
grid on

z1 = sin(w1*t); %
z2 = 0.2*sin(w2*t);
zz = z1+z2;

fh1 = 20; %一阶低通截止频率
a1 = 1/(1+(fc/(2*pi*fh1)));

%三阶IIR滤波

n0 = 0.098531160923927052 ;
n1 = 0.29559348277178116 ;
n2 = 0.29559348277178116 ;
n3 = 0.098531160923927052 ;

d0 = 1 ;
d1 = -0.57724052480630283 ;
d2 = 0.42178704868956196 ;
d3 = -0.056297236491842671;

%3阶高通
bn0 = 0.25691560124846347 ;
bn1 = -0.7707468037453904 ;
bn2 = 0.7707468037453904 ;
bn3 = -0.25691560124846347 ;

bd0 = 1 ;
bd1 = -0.57724052480630295 ;
bd2 = 0.42178704868956196 ;
bd3 = -0.056297236491842678 ;

fn0 = 0.42080777983773182 ;
fn1 = 0.42080777983773182 ;

fd0 = 1;
fd1 = -0.15838444032453636;

a2 = 0.5
for n=(2:1:length(x));
p1(n) = (1-a2)*p1(n-1)+ a2 * zz(n);
end

%y(n) = n0*x(n) + n1*x(n-1) +n2*x(n-2)+n3*x(n-3) – d1*y(n-1) – d2*y(n-2)-d3*y(n-3)-d4*y(n-4)
iir1 = zz; %3阶低通
iir2 = zz; %3阶高通
iir3 = zz; %1阶低通

for n=(4:1:length(x));
iir1(n) = n0*zz(n) + n1*zz(n-1) +n2*zz(n-2)+n3*zz(n-3) – d1*iir1(n-1) – d2*iir1(n-2)-d3*iir1(n-3);
end

for n=(2:1:length(x));
iir3(n) = fn0*zz(n) + fn1*zz(n-1)- fd1*iir1(n-1);
end

for n=(4:1:length(x));
iir2(n) = bn0*zz(n) + bn1*zz(n-1) +bn2*zz(n-2)+bn3*zz(n-3) – bd1*iir2(n-1) – bd2*iir2(n-2)-bd3*iir2(n-3);
end

maxtri = zeros(size(repmat(t’,[1 2])));%初始化矩阵

maxtri(:,1)= t’;
maxtri(:,2)= zz’;

maxtri

plot(t,zz,’r’);
plot(t,iir1,’k’);
plot(t,iir2,’b’);

[SF]IIR滤波器

以3阶低通  IIR 巴特沃斯 为例

y(n) = n0*x(n) + n1*x(n-1) +n2*x(n-2)+n3*x(n-3) – d1*y(n-1) – d2*y(n-2)-d3*y(n-3)-d4*(n-4)

其中,例如采样频率Fs = 100HZ   截止频率Fc=20HZ

得出

n0 =0.098531160923927052

n1 =0.29559348277178116

n2 =0.29559348277178116

n3 =0.098531160923927052

d0 = 1

d1= -0.57724052480630283

d2= 0.42178704868956196

d3= -0.056297236491842671

带入公式

[MFC] 添加新窗口

1.在“资源视图”的”Dialog” 上右键,选择”插入Dialog”
2.选中新生成的Dialog,在属性里更改Dialog的名字
3.双击新生成的Dialog,在弹出的”MFC 添加类向导”里输入类名 “C****”,会自动生成 ****.c 和 ****.h ,点击 “完成”
4.添加测试按键,并添加测试
CDlg**** DlgTest;
DlgTest.DoModal();
5.生成后即可实现按键后弹出新对话框
6.如报错,则在报错的文件下加入 include “Resource.h” 和 ****.h