# Propose Fixed-Point Data Types Using an Instrumented Mex Function

2 views (last 30 days)
dang hong on 10 Mar 2014
Answered: Ryan Livingston on 2 Apr 2014
This is my function:
coder.extrinsic('imfilter');
%-- compute Variances of the RGB and IR patches
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
mean2_I = imfilter(I.^2,ones(win_var)/win_var/win_var,'same','symmetric','conv');
std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);
% (filters are not complete at the border)
% add the area where the vignetting is too strong
return
When trying to build Build "Instrumented MEX Function" I get these errors: ??* ? Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.*
Error in ==> computeMask Line: 21 Column: 18: std_I= real(sqrt(mean2_I-mean_I.^2)+1e-5);

Ryan Livingston on 2 Apr 2014
My first suggestion is if you are using R2014a, then imfilter is supported for code generation so you may be able to avoid declaring it as an extrinsic function:
If you need to use it as an extrinsic function for other reasons please read on.
When using the output of an extrinsic function, as you are doing with imfilter, it may often be necessary to preinitialize the variable to which it is assigned. Doing so is what tells the code generation software about the type, size and complexity of the value returned. If foo is extrinsic, then you could use:
x = zeros(3,4);
x = foo(y);
to call foo extrinsically and assign the output as a 3-by-4 array. See:
for more details.
So you would just need to assign the values of the calls to imfilter to be the correct size, type etc. From the documentation for imfilter, the output is the same size and type as the input. So you should be able to try something like:
mean_I = zeros(size(I),'like',I);
mean_I = imfilter(I,ones(win_var)/win_var/win_var,'same','symmetric','conv');
mean2_I = zeros(size(I),'like',I);
mean2_I = imfilter(I.^2,ones(win_var)/win_var/win_var,'same','symmetric','conv')