How to return matrix in Matlab using codegen with no c++ memory allocation?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Paul Hoffrichter
el 8 de Ag. de 2024
Respondida: Paul Hoffrichter
el 8 de Ag. de 2024
I do not want any dynamic memory allocation to be done in the C++ codegen. Here is the relevant Matlab code:
% Calculate cross-correlation matrix from a set of vectors (whose length
% can vary from one call to another). All 4 channels will have the length.
function [R] = xcorr_matrix(ch0,ch1,ch2,ch3,channelCount)
% channelCount is 2 or 4
% R is either 2x2 or 4x4 complex double matrix.
% ch0 ... ch3 are complex single vectors.
N = int32(size(ch0,1));
% Convert ch0, ..., ch3 to complex double
x_in = coder.nullcopy(complex(zeros(N,channelCount,'double')));
switch channelCount
case 2
x_in(1:N,1) = ch0(1:N);
x_in(1:N,2) = ch1(1:N);
case 4
x_in(1:N,1) = ch0(1:N);
x_in(1:N,2) = ch1(1:N);
x_in(1:N,3) = ch2(1:N);
x_in(1:N,4) = ch3(1:N);
end
R = (x_in' * x_in); % Compute cross-correlation matrix
Here is the C++ codegen result:
void xcorr_matrix(const creal32_T ch0_data[], const int ch0_size[1], % all the 4 ch_size will be the same value
const creal32_T ch1_data[], const int ch1_size[1],
const creal32_T ch2_data[], const int ch2_size[1],
const creal32_T ch3_data[], const int ch3_size[1],
int channelCount,
::coder::array<creal_T, 2U> &R)
{
::coder::array<creal_T, 2U> x_in;
R.set_size(channelCount, channelCount);
x_in.set_size(N, channelCount);
...
}
I think I can eliminate the x_in.set_size by not using x_in, and replace the matrix multiply with nested for-loops and using double casting; but am unsure how to define R (either 2x2 or 4x4) so as to remove the R.set_size allocations.
One idea I had was to try making R a fixed length 16 element vector, and just use 4 elements for the 2x2 R, and all 16 for the 4x4 R. Would be nicer to be able to use two indices for rows and columns.
Thanks in advance for your help.
Paul
1 comentario
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Coder en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!