FIFO push all values as vector
3 次查看(过去 30 天)
显示 更早的评论
Tamar
2023-7-11
Hi, I want to pop the fifo every step time (1 sec) and push all the values as vector every 5 steps (5 sec). I didn’t find way to push more then one value every step time
采纳的回答
Jon
2023-7-11
I assume you are using the Queue block in the DSP System Toolbox in Simulink.
If so you should supply the In port with a 5 element vector input, then supply the Push port with the output of Pulse Generator with 5 second period, and the Pop port with the output of a Pulse Generator with a 1 second period.
You may want to delay the start of popping until the buffer is full.
See my attached example
20 个评论
Tamar
2023-7-11
I can’t open your example because I have 2022.
Anyway, how can I do from one signal vector that it’s value are - [1sec, 2sec, 3sec…]?
And what if I want the push be variable? Than I can’t pop the fifo a vector because I don’t know the size that the vector should be
Jon
2023-7-11
Please explain in detail what you are trying to accomplish, ideally with example input and output
Tamar
2023-7-11
I have pulses. Let’s say my step time is 1 sec and all the simulation run 30 sec. My source is something that make pulses. I don’t know the time for each pulse. I would like to get the values for each pulse to Matlab function block. For example: My source is [0 0 3 6 10 6 3 0 0 5 6 10 6 5 0 6…] (every value is one step time) I want to input to Matlab function block one time [3 6 10 6 3] and the second time [5 6 10 6 5]… - do a vector and cut it according the zeros value each pulse
Jon
2023-7-11
What will your Matlab function block output while it is waiting for the first 5 non-zero values? What will it output while it waits to accumulate the next 5 non-zero values?
What is the bigger context here, do you really have to do this in Simulink, or do you have data in a file that could just be processed using MATLAB?
Tamar
2023-7-11
I wish I could do it without simulink but My data is create in simulink and I use simulink blocks also after the Matlab function block, and the time line is important.
I can triggered the Matlab function block according the value of the pulse (if the memory is above zero and the current value is zero), and then don’t use it when I don’t have enough values. I don’t really care if the Matlab function block get inputs all the time steps and output nan values while the input is nan or zero.
Jon
2023-7-11
Will the data of interest alway come as 5 consecutive non-zero values followed by some zeros, or sometimes are there zeros between the values that must be discarded until you accumulate 5 non-zero values, so is it always like your example, or could it be
[0 0 3 6 0 10 6 3 0 0 5 6 10 0 0 6 5 0 6…] (every value is one step time) input to Matlab function block one time [3 6 10 6 3] and the second time [5 6 10 6 5]?
Tamar
2023-7-11
No. Every zero value is new pulse. It’s not always five values of numbers. It’s could be [ 0 3 6 3 0 0 0 5 6 10 6 5 0 1 0 0] and the input to Matlab function block is [3 6 3] and then [5 6 10 6 5] and then [1].
I tried to use buffer block but in this block I have to set size.
Moreover, I tried to use to file block and from file block and restart the file when I want but it’s impossible to use to file block and from file block that have the same name.
Jon
2023-7-12
I think this approach may work for you. I have use a tapped delay line to buffer the values. I detect a zero following a non-zero pulse train (e.g. the zero following 3 6 3 in your example). When I detect this zero, I trigger a triggered subsystem that hold the MATLAB function block. and receives the entire contents of the FIFO (output of the tapped delay line). Inside the function block I extract just the most recent pulse train, with some additional logic, and the compute the desired function of the pulse train values.
I have attached the model demonstrating this saved as version 2022a. Hope you can open and run it Also here is a screen shot for an overview. Below is the code inside of the MATLAB function block.
Note this approach won't find a final non-zero pulse train at the end of the simulation unless the last element in the input is a zero, so you may have to be careful about that edge case.
function y = fcn(u)
% function to do demonstrate doing something
% with group of pulse inputs
% u is output of tapped delay line, which essential acts as FIFO buffer
% Function is executed at end of non-zero pulse train, since u is entire
% contents of FIFO, and non-zero pulse trains are variable length, there
% may be some previous pulse trains and/or zeros in the buffer
% Just keep the newest non-zero values
idx = find(u==0,1,'last');
% Note even though idx has only one element, it seems that Simulink doesn't
% know this and has errors unless you just use the first element of this
% one element vector, so use idx(1), rather than idx
iStart = idx(1) + 1;
x = u(iStart:end,1); % just the most recent pulse train
% Calculate something using the non-zero pulse train, I will compute the
% total, but you can insert what you want here
y = sum(x);
Jon
2023-7-12
If you have Stateflow, you could also implement something in there to handle this type of behavior.
Tamar
2023-7-13
Sorry I didn’t reply, I wasn’t near computer. I don’t know why but I still can’t open it. Can you please send a screenshot of the simulation?
Tamar
2023-7-17
It’s working, thank you!
Another question - how can I do the same to vectors? I have fixed size vector [1 14] every x second (according the pulses) and I want all the vector of 10 seconds for other Matlab function. It’s impossible to do tapped delay to vector :(
Jon
2023-7-17
I'm not understanding the situation with the vectors. Please explain in detail what you are trying to do with the vectors.
Tamar
2023-7-17
Every pulse input to Matlab function and the output is vector [1 14].
I want to do matrix for all the vectors that receive during 10 seconds
Tamar
2023-7-17
I replace your Matlab function with this function:
function y = fcn(u)
idx = find(u==0,1,'last');
iStart = idx(1) + 1;
x = u(iStart:end,1)'; % just the most recent pulse train
y = zeros(1,10);
y(1,1:length(x)) = x.*2;
end
and write it to the workspace in the triggered subsystem.
I got a time of [7,14,16,21,23,28] and the vector of eatch:
7: [2, 4, 6, 8, 10, 12, 14, 0, 0, 0]
14: [10, 12, 20, 12, 10, 0, 0, 0, 0, 0]
16: [12, 0, 0, 0, 0, 0, 0, 0, 0, 0]
21: [2, 4, 0, 0, 0, 0, 0, 0, 0, 0]...
Now I want the vectors of any 10 seconds will be in matrix:
1) [2, 4, 6, 8, 10, 12, 14, 0, 0, 0]
2) [10, 12, 20, 12, 10, 0, 0, 0, 0, 0; 12, 0, 0, 0, 0, 0, 0, 0, 0, 0]...
I don't care if the time step of the matrix be the last time in the 10 seconds (16 in case 2) or the first (14)
Tamar
2023-7-18
I tried to not use buffer. I did something and I think it worked. Thank you anyway, you help me a lot!!
Jon
2023-7-18
Hi Glad you found a solution. I and maybe others would be interested to see your approach without a buffer. Could you attach a simple model that demonstrates the idea.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)