将采样值放入矩阵 - simulink
Putting sampled values into a matrix- simulink
如何将值放入 simulink 中的矩阵中?例如,如果我每 0.5s 采样一个正弦波,并将该值放入大小为 N 的固定大小矩阵中。矩阵满后,它将覆盖最旧的值。
假设您正在谈论将矩阵用作缓冲区,那么如果您拥有 DSP Blockset,则可以使用 Buffer 模块。否则,使用 MATLAB 功能块(请参见下面的代码)非常简单。如果矩阵是由模型的另一部分构建的,则可以将其作为第二个输入传递给 MATLAB Function 块,并适当修改(新元素)插入代码。
function y = custom_buffer(u)
%#codegen
persistent buffer next_index
buf_size = 1000;
% Define initial values
if isempty(buffer)
buffer = zeros(buf_size,1);
next_index = 1;
end
% Populate the buffer
buffer(next_index) = u;
% Increment the location to write to at the next time
if next_index < buf_size
next_index = next_index + 1;
else
next_index = 1;
end
% Populate the output
y = buffer;
如何将值放入 simulink 中的矩阵中?例如,如果我每 0.5s 采样一个正弦波,并将该值放入大小为 N 的固定大小矩阵中。矩阵满后,它将覆盖最旧的值。
假设您正在谈论将矩阵用作缓冲区,那么如果您拥有 DSP Blockset,则可以使用 Buffer 模块。否则,使用 MATLAB 功能块(请参见下面的代码)非常简单。如果矩阵是由模型的另一部分构建的,则可以将其作为第二个输入传递给 MATLAB Function 块,并适当修改(新元素)插入代码。
function y = custom_buffer(u)
%#codegen
persistent buffer next_index
buf_size = 1000;
% Define initial values
if isempty(buffer)
buffer = zeros(buf_size,1);
next_index = 1;
end
% Populate the buffer
buffer(next_index) = u;
% Increment the location to write to at the next time
if next_index < buf_size
next_index = next_index + 1;
else
next_index = 1;
end
% Populate the output
y = buffer;