1

I want to generate a c++ code for DCT function using Matlab coder. I wrote this simple function and tried to convert it to c++.

function output_signal = my_dct(input_signal)
    output_signal = dct(input_signal);
end

When I use a fixed size type for the input argument (such as double 1x64), there is no problem; however, a variable-sized type (such as double 1x:64) for the input argument results in these errors:

The preceding error is caused by: Non-constant expression..

The input to coder.const cannot be reduced to a constant.

Can anyone please help me? Thanks in advance.

1 Answer 1

1

The documentation is a bit vague for DCT in Coder, but it implies that the input size must be a constant power of 2 along the dimension of the transform. From DCT help:

C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™. Usage notes and limitations:

C and C++ code generation for dct requires DSP System Toolbox™ software.

The length of the transform dimension must be a power of two. If specified, the pad or truncation value must be constant. Expressions or variables are allowed if their values do not change.

It doesn't directly say that the length of the variable (at least along the dimension being transformed) going into the dct function must be a constant, but given how coder works, it really probably has to be. Since that's the error it's returning, it appears that's a limitation.

You could always modify your calling function to zero pad to a known maximum length, thus making the length constant.

For instance, something like this might work:

function output_signal = my_dct(input_signal)
    maxlength = 64;
    tinput = zeros(1,maxlength);
    tinput(1:min(end, numel(input_signal)))  = input_signal(1:min(end, maxlength));
    output_signal = dct(tinput);
end

That code will cause tinput to always have a size of 1 by 64 elements. Of course, the output will also always be 64 elements long also, which means it'll be scaled and may have a difference frequency scale than you're expecting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.