Delay line for code generation

Hi,
I am doing audio processing algorithms in matlab code that should support code generation. What is a good choice for a simple delay? The delay does not need to change during processing, it should be efficient, have a fixed upper bound and the actual delay set during initialization from a parameter (before playback starts).
I would like to avoid reinventing the wheel or at least reinventing delay lines. Is dsp.VariableIntegerDelay a good choice for this purpose?
dsp.VariableIntegerDelay seems to work for processing in matlab but I have not tried codegen yet.
When I check the size of my dsp.VariableIntegerDelay as a member of my processor object using whos, I get 8 bytes. Seems weird since I instantiated it using dsp.VariableIntegerDelay('MaximumDelay',256);
Thanks

1 个评论

When I check the size of my dsp.VariableIntegerDelay as a member of my processor object using whos, I get 8 bytes.
Likely you are checking the size of a handle object. whos reflects the size of the handle pointer, 8 bytes, not the size of the underlying object.

请先登录,再进行评论。

回答(1 个)

jibrahim
jibrahim 2024-12-13
There are multiple objects you can use to model delays:
They all support code generation.
You can't use 'whos' to check how much memory the object is consuming, as 'whos' just returns the size of the handle variable pointing to the object.

13 个评论

Thanks. Which one would be likely to generate the most efficient code (cpu & mem)?
Hi Mattias,
This depends on your use case. Make sure you use the minimum required resources for your problem (e.g. number of filter states, delay length, etc), and the generated code will honor your selections.
Looks like dsp.Delay cannot change delay once instantiated, and has no upper bound so it cannot work with static memory allocation. The dsp.VariableIntegerDelay on the other hand takes the delay as a real-time runtime parameter which is more than I need. Seems like I would need a dsp.Delay wit an upper bound and an option to set the delay in an existing instance. I don't think I can use dsp.Delay since it needs to be fixed size for codegen to work. So either dsp.VariableIntegerDelay has to be really efficient (the generated c code) or I need to implement yet another delayline myself, which is what I wanted to avoid.
I think you should use dsp.VariableIntegerDelay and set its MaximumDelay to the value that works for you.
I have created matlab code for a processor based on a struct passed around to various functions. I am able to generate code for it.
If I add a dsp.VariableIntegerDelay to the struct, code generation does not work. This line,
obj.delay_line_1 = dsp.VariableIntegerDelay('MaximumDelay',128);
, causes the error.
And excercising the delay does not help. I have traced the error back to this single line.
It looks very much like dsp.VariableIntegerDelay simply does not support code generation. Or it is not allowed to be a member of struct. Any ideas?
Hi Mattias,
dsp.VariableIntegerDelay supports code generation. For example, this generates code fine:
function y=foo(x)
delay_line_1 = dsp.VariableIntegerDelay('MaximumDelay',128);
y = delay_line_1(x,2);
end
% codegen foo -args {1}
The error must be caused by some other line of code, maybe by how the structure is handled. If you share a function or class where I can reproduce the issue, I can take a look.
Hi,
Thanks for offering to have a look at the code! Unfortunately I cannot simply share my code since I work for a company.
Essentially I define the struct in a function, use it in another. I traced the problem to the single line where initialization happens, if it is commented out, codegen runs. If not, I get the error message.
The example you provided is unfortunately not relevant for real-time audio processing, it would cause gaps in the audio as it starts with an empty delay buffer zero every call. The delaylines have to be initialized once and then called for every new buffer that is to be processed with the delay. They thus need to be members of the struct (or a class I guess, but I am using structs like in C)
The error message is related to the parsing of function arguments. One of which is the struct. And again, if I remove the delayline from the struct, it works.
I have successfully used this code structure with many different structs, it seems like problems begin when I try to use system objects like my own objects.
Hi Mattias,
To make the function friendly to streaming scenarios, simply declare the object as persistent:
function y=foo(x)
persistent delay_line_1
if isempty(delay_line_1)
delay_line_1 = dsp.VariableIntegerDelay('MaximumDelay',128);
end
y = delay_line_1(x,2);
end
This is a very common pattern with system objects and objects with state in general.
Thanks, but how would that work for multiple instances? It looks like the above example would create a global/singleton accessible only from that function scope. Unfortunately, that would make it useless since delaylines are used in many places. Moreover, I definitely want to avoid using global variables and instead make the delaylines members of structs or classes.
If that global thing is considered a pattern by Mathworks, it certainly looks very problematic. I am beginning to suspect that codegen for system objects may not work for my intended (real-world) application.
I definitely want to avoid using global variables and instead make the delaylines members of structs or classes.
If you were to create struct or class variables representing the integer delay, and were to somehow import them into the function, then you would run across the problem that simulink signals cannot be System objects and so you cannot store system objects in simulink variables. As a result, the best you could do would be to import a variable that is an integer or double, and create the dsp.VariableIntegerDelay every time though the function.
I am not sure how Simulink variables and signals relate to what I am doing since I am using Matlab.

请先登录,再进行评论。

产品

版本

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by