Kindly also tell me how to write assert commands to be able to declare both x and y as complex dynamic arrays
MATLAB code to C++ conversion
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
hi,
I have tried a lot to convert this m file to C++ (.cpp) file. But I am having some issues. I have tried a lot to make dynamic arrays as inputs but I have always got error. I wish someone could just convert this code to .cpp file for me using MATLAB in built C++ converter. Please note that the inputs y and x have to be dynamic and complex arrays, tol and L to be real double scalars. Please help me with it.
Below is the code!
%#eml
function [b, a, f] = mpencil(y, x, tol, L)
%eml.varsize('x', [1 10]);
%leny = inf;
%assert (isa(y,'double') && ~isreal(y) && all(size(y) == [1 5]) && isa(x,'double') && ~isreal(x) && isvector(x) && isa(tol,'double') && isa(L,'double') );
assert( isa(y,'double') && ~isreal(y) && all(size(y) == [1 inf]) && isa(x,'double') && ~isreal(x) && all(size(y) == [1 size(y,2)]) && isa(tol,'double') && isa(L,'double') );
% function [B, A, F] = mpencil(Y, X, [TOL, L])
%MPENCIL Fits a sum of exponential functions to data by a Matrix Pencil method
%
% B = MPENCIL(Y,X) approximates Y(i) by a weighted sum of
% exponentials sum_j=1^p A(j)*exp(B(j)*X(i)). The X(i) are
% assumed equally spaced.
%
% The number of exponential is defined with the third optional
% parameter TOL. If TOL < 1 (and > 0) the number of terms is the same
% as the number of such singular values (of the data) that are larger
% than TOL*biggest singular value. If TOL >= 1 and an integer, the
% number of exponential terms is TOL. The default TOL value is 1e-3.
%
% The last optional parameter L is a parameter for the Matrix Pencil
% method. Values between N/3 and N/2 are preferred where N is the
% number of data (Y).
% The default L is the ceil(1/2*(ceil(N/3)+floor(N/2))).
%
% The A(i) and the fitted values F(i) are optionally returned by
% [B,A,F] = MPENCIL(Y,X).
% References:
% Sarkar, T.K. and Pereira, O.
% Using the Matrix Pencil Method to Estimate the Parameters of a Sum
% of Complex Exponentials.
% IEEE Antennas and Propagation Magazine, Vol. 37, No 1, Feb. 1995
% Matti Taskinen, Rolf Nevanlinna Institute, University of Helsinki
% 06.03.1996. Latest revision 01.04.1996.
dd = x;
x = zeros(size(y));
x = dd;
% Check the vector sizes:
N = size(y, 1);
if size(x, 1) ~= N
error('length(Y) should be length(X)');
end
if size(y, 2) ~= 1
error('Y should be column vector');
end
if size(x, 2) ~= 1
error('X should be column vector');
end
% Fill in the missing parameters:
if nargin < 4
L1=ceil(1/3*N);
L2=floor(1/2*N);
L = ceil((L1 + L2) / 2);
if nargin < 3
tol = 1e-3;
end
end
% Check the parameter values:
M_given = round(tol) == tol;
if (tol < 0) || ((~M_given) && (tol > 1))
error('TOL should be either >= 1 and an integer or < 1 and > 0');
end
if L > N/2
error('L shoud be < N/2');
end
if M_given && (L < tol)
error('TOL should be <= L');
end
% X are assumed to be equally spaced:
T = diff(x(1:2));
Y = zeros(N-L, L+1);
Y = complex(Y,Y);
ind = 0:N-L-1;
for j = 1:L+1
Y(:,j) = y(ind+j);
end
[U,S,V] = svd(Y,0); % Economy size!
if M_given
% The number of exponential terms may be given in TOL:
M = tol;
else
% Otherwise figure it out from the singular values:
D = diag(S);
for M = 1:length(D)-1
if (abs(D(M+1)/D(1)) <= tol)
break;
end
end
end
SM = S(:,1:M);
VM = V(:,1:M);
V1 = VM(1:L,:);
V2 = VM(2:L+1,:);
Y1 = U*SM*V1';
Y2 = U*SM*V2';
A = pinv(Y1)*Y2;
z = eig(A);
z = z(1:M);
b = 1/T * log(z);
if nargout > 1
Z = exp(x*b.');
a = Z\y;
if nargout > 2
f = Z*a;
end
end
Haris
Respuestas (2)
Rick Rosson
el 24 de Ag. de 2011
Can you please answer a few questions:
- What version of MATLAB (i.e. Rnnnnx) are you using?
- Do you have access to Real-Time Workshop?
- Do you want dynamic memory allocation for the variable-sized arrays, or can you specify a fixed upper bound on the size of each variable-size array?
Thanks!
Rick
Rick Rosson
el 24 de Ag. de 2011
I would recommend trying a three-phase approach:
- Generate code assuming fixed size arrays for all variables. Choose a size for x and y that are typical of your ultimate application, and easily manageable for testing and debugging purposes. Create build script to make the code generation process repeatable, and create a test script to do at least an adequate level of testing to convince yourself that the code is working correctly both interpreted and compiled.
- Modify the MATLAB code and/or your build script (create one if you don't have one already) so that you can generate code for variable size arrays with fixed upper bound. Generate code and compile to a MEX function so that you can test the code for runtime errors within the MATLAB environment.
- Once you have successfully generated code for fixed upper bound, then consider whether you really need dynamic memory allocation. If so, relax the upper bound conditions and re-generate the code with dynamic memory turned "on".
Generating C code from MATLAB can be challenging. It's much easier to take it one step at-a-time, rather than trying to get everything to work on the first attempt. Crawl before you walk, walk before you jog, jog before you sprint...
0 comentarios
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!