Error in HDL Coder- Fixed point conversion "MATLAB expression 'r' is not of the correct class: expected 'double', found 'sym'."

2 次查看(过去 30 天)
Hi,I tried basic examples of MATLAB to HDL conversions like levenberg . Then I tried running this code.
Code
function xc=LevenbergMarquardt(r,x,x0,epsilon,n,lambda)
%Levenberg Marquardt Method for nonlinear least squares
%Input: r is the nonlinear fitting function model
% x is the coefficients in the fitting function that needs to be
% determined
% x0 is the initial values
% epsilon is the tolerance
% lambda is the regularization term coefficient
%Output: xc is the determined coefficients
r=double(0);
x0=x0(:);
x=x(:);
coder.extrinsic('jacobian');
coder.extrinsic('subs');
J=jacobian(r,x);
a=1;
A0=double(0);
A0=double(subs(J,x,x0));
r0=double(subs(r,x,x0));
v=double(0);
v=-(A0'*A0+lambda*eye(length(x)))\(A0'*r0);
w=double(0);
w=A0'*r0;
x0=x0+v;
while(norm(w)>epsilon)
A0=double(subs(J,x,x0));
r0=double(subs(r,x,x0));
v=-(A0'*A0+lambda*eye(length(x)))\(A0'*r0);
x0=x0+v;
a=a+1;
if a>n break;
end
end
xc=x0;
end
Please help me ,i write a test bench. I am getting error when I try to validate the fixed point type conversion . It fails giving errors in simulation output of test "
MATLAB expression 'r' is not of the correct class: expected 'double', found 'sym'.".
My test bench is
clear all;
clc;close all;
syms c1 c2 c3 t y
Y=c1*exp(-c2*(t-c3)^2)-y;
x1=[-1 0 1 3 6];
x1=x1';
z1=[1 5 10 8 1];
z1=z1';
x2=[1 2 4 5 6 8 9 11];
x2=x2';
z2=[1 3 7 12 13 5 2 1];
z2=z2';
c=[c1;c2;c3];
epsilon=0.5e-8;
n=30;
display('********************************');
display('For exercise (a)');
display('********************************');
r1=subs(Y,{t,y},{x1,z1});
c0=[10;0;2];
lambda=1;
c_root1=LevenbergMarquardt(r1,c,c0,epsilon,n,lambda);
display('The coefficients found by using LevenbergMarquardt method are:');
display(['c=[c1;c2;c3]=' mat2str(c_root1)]);
f1=c_root1(1).*exp(-c_root1(2).*(x1-c_root1(3)).^2);
aError=f1-z1;
aRMSE=norm(aError,2);
display(['RMSE=' num2str(aRMSE)]);
figure(1);
plot(x1,z1,'o','MarkerFaceColor','g','MarkerSize',8);
hold on;
a=-2:0.1:8;
g1=c_root1(1).*exp(-c_root1(2).*(a-c_root1(3)).^2);
plot(a,g1,'b');
title('Levenberg-Marquardt fitting');
display('********************************');
display('For exercise (b)');
display('********************************');
r2=subs(Y,{t,y},{x2,z2});
c0=[10;0;2];
lambda=0.5;
c_root2=LevenbergMarquardt(r2,c,c0,epsilon,n,lambda);
display('The coefficients found by using LevenbergMarquardt method are:');
display(['c=[c1;c2;c3]=' mat2str(c_root2)]);
f2=c_root2(1).*exp(-c_root2(2).*(x2-c_root2(3)).^2);
bError=f2-z2;
bRMSE=norm(bError,2);
display(['RMSE=' num2str(bRMSE)]);
figure(2);
plot(x2,z2,'o','MarkerFaceColor','g','MarkerSize',8);
hold on;
a=0:0.01:12;
g2=c_root2(1).*exp(-c_root2(2).*(a-c_root2(3)).^2);
plot(a,g2,'b');
title('Levenberg-Marquardt fitting');

采纳的回答

Steven Lord
Steven Lord 2020-8-24
None of the functions from Symbolic Math Toolbox are on the list of functions supported for HDL code generation. I haven't used HDL Coder before but I'm virtually certain you will not be able to generate HDL code from code that uses Symbolic Math Toolbox functionality.
  3 个评论
Steven Lord
Steven Lord 2020-8-24
I don't work with VHDL at all so I don't have any recommendations. Please contact Technical Support using the telephone icon in the upper-right corner of this page to ask how (or if) you can implement the Levenberg-Marquardt algorithm in VHDL.

请先登录,再进行评论。

更多回答(1 个)

Kiran Kintali
Kiran Kintali 2020-8-25
You may want to check how to code this algorithm to be compatible with HDL Coder and FPGA Modeling Guidelines. There are some good references here.

类别

Help CenterFile Exchange 中查找有关 HDL Coder 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by