I need to create a Simulink running signal minimum function

1 次查看(过去 30 天)
I don't have the DSP toolbox, and I'm trying to write a custom Matlab function to compute the minimum of the signal throughout its running history without depending on this toolbox as it could be used on different machines for people with different licenses. I have a working function for a windowed sample of the signal, but is there any good way to implement this for the entirety of the signal? This is my windowed minimum signal custom Matlab function (it also outputs the sum of the input signal), where n is the number of samples for the windowed minimum, and u is the input signal. S is the output sum, and m is the output minimum:
function [S,m] = sum_and_min(u,n)
%#codegen
persistent interm;
if(isempty(interm) || isnan(u))
interm = 0;
else
interm = u;
end
persistent sum buf iter;
if isempty(sum)
sum = 0;
buf = zeros(1,n);
iter = 1;
end
% for each incoming sample u:
sum = sum + interm;
S = sum;
buf(iter) = sum;
iter = iter + 1;
if iter > numel(buf)
iter = 1;
end
m = min(buf);
end

回答(2 个)

Derek Miller
Derek Miller 2017-11-20
Figured it out myself. For anyone looking at something similar, I used the below method. Feel free to offer any insight or improvements still.
function [S,m] = sum_and_min(u)
%#codegen
persistent old_min;
persistent sum;
persistent interim;
if(isempty(interim) || isnan(u))
interim = 0;
else
interim = u;
end
if isempty(sum)
sum = 0;
old_min = 0;
end
% for each incoming sample u:
sum = sum + interim;
old_min = min([old_min,sum]);
S = sum;
m = old_min;
end

umichguy84
umichguy84 2017-11-20
Hello, Here is an alternative to a Matlab function. See the attached image below for an example in Simulink.
How you handle the write and read of the data across simulations is up to you and would depend on your situation. But taking the last value of your MinValueOut, writing to a mat file with MinValueFromLastRun in a post sim callback, and then reading in that mat file in a pre sim callback would work.

类别

Help CenterFile Exchange 中查找有关 C Code Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by