Simulink 中的循环缓冲区

ToB*_*oBe 2 code-generation simulink

我想在纯 Simulink 模型中实现一个非常巨大的(10^6 个元素 - 固定大小)循环缓冲区(没有进一步的工具箱,没有 S-Function)。

在某些时候,我需要读取一些元素(任何地方,而不仅仅是开始或结束)。

我无法使用以下解决方案:

  1. “队列块”或“缓冲区块”(我没有可用的信号处理工具箱)
  2. “离散延迟”(我需要一个巨大的缓冲区,并且不会在模型中放置 10^6 的延迟)
  3. “Sim Events”(我需要从这个模型生成代码)

我还没有尝试过“S-Function”,我正在寻找替代解决方案。

您还知道什么进一步的方法?

Phi*_*ard 5

可以使用 MATLAB Fcn 模块创建简单的循环缓冲区:

function y = CircularBuffer(u, N)
%#codegen
% Function to implement a simple N element circular buffer

% Define the internal buffer variable as persistent so
% so that it will maintain its values from one time step
% to the next.
persistent buffer writeIdx

% Initialize the buffer the first time through
if isempty(buffer)
    % Initialize the buffer
    buffer = zeros(N,1);
    writeIdx = 1;
else
    buffer(writeIdx) = u;
    if (writeIdx == N)
        writeIdx = 1;
    else
        writeIdx = writeIdx+1;
    end
end

% Output the buffer
y = buffer;
Run Code Online (Sandbox Code Playgroud)

这完全支持代码生成(并且不执行 memcpy)。

如果需要,您可以轻松更改缓冲区的数据类型,但如果您希望输入和输出信号具有不同的采样率(与信号处理模块集中的缓冲区一样),那么您需要恢复使用 S -功能。