0
votes

I need to use resample() function to take a variable argument of Q downsampling factor in Simulink. Basically a Simulink fcn block containing this code:

function y = resample(data,Q)
y=resample(data,1000,Q);

On desktop simulation I can get variable Q to work as argument by specifying it as input to a MATLAB interpreted function, but since I need to generate a C code,my only option is to use the fcn block, obviously it won't compile due to above limitation.

error: the downsample factor Q must be constant

I understand this is a documented limitation of the resample function:

resample: The upsampling and downsampling factors must be specified as constants. Expressions or variables are allowed if their values do not change.

Any workaround or different approach to address this? Perhaps other block which is capable of doing the same job? ofc it has to be compatible with Simulink coder.

Thanks!

1

1 Answers

1
votes

resample function would need to design filters and decide output sizes based on the sample factors. After code generation this cannot be changed, which is why this function needs the sampling factors to be constant.

But if the different downsampling factor values you need to support are limited you could use conditional branching with calls to resample in each branch with constant values. For example,

% Declare out as a var-size with max decided by the minimum downsampling factor
% Assuming data is [1000, 1]
coder.varsize('out', [500 1]);
out = zeros(500,1);
if Q == 2
   out = resample(data,1000,2);
elseif Q == 4
   out = resample(data,1000,4);
elseif ...
...
end

You also need to deal with variable sized data "out" in the rest of your MATLAB code and Simulink model if this is an output variable from MATLAB Function block.