Regarding buffer and stack in simulink

4 views (last 30 days)
ana
ana on 26 May 2016
Answered: BhaTTa on 19 Nov 2024 at 12:02
Hi, I have fixed amount of data. For example: I have input matrix of size [16800 * 1] and one stack of size zeros([26 *1]). At each time instant, I have to shift 40 samples from my input matrix to buffer. And at each next time instant I have send last 26 samples from the current 40 samples to stack. for example: at first time instant I have to send first 40 input samples to buffer and at next time instant I have to send last 26 samples from first instant to stack . i.e. samples from index 15 to index 40 = 26 samples. And I have to repeat this for next 40 input samples. Can anyone help me on this.

Answers (1)

BhaTTa
BhaTTa on 19 Nov 2024 at 12:02
Hey @ana, you can achieve the above requirement by implementing a loop that processes your input matrix in chunks of 40 samples. At each iteration, you will:
  1. Transfer the next 40 samples from the input matrix to a buffer.
  2. Extract the last 26 samples from this buffer and move them to the stack.
Here is the sample code , Please take it as a referecne and modify it accordingly.
% Initialize your input matrix and stack
input_matrix = rand(168, 1); % Replace with your actual data
stack = zeros(26, 1);
% Define the size of each chunk and the number of samples to send to the stack
chunk_size = 40;
stack_size = 26;
% Calculate the number of iterations needed
num_iterations = floor(length(input_matrix) / chunk_size);
% Process each chunk
for i = 1:num_iterations
% Calculate the start and end indices for the current chunk
start_idx = (i-1) * chunk_size + 1;
end_idx = start_idx + chunk_size - 1;
% Transfer 40 samples to the buffer
buffer = input_matrix(start_idx:end_idx);
% Extract the last 26 samples from the buffer to the stack
stack = buffer(end-stack_size+1:end);
% Do something with the stack if needed
% For example, display it or process it further
fprintf('Iteration %d - Stack samples:\n', i);
disp(stack);
end
% Handle any remaining samples if the input matrix size is not a multiple of 40
remaining_samples = mod(length(input_matrix), chunk_size);
if remaining_samples > 0
buffer = input_matrix(end-remaining_samples+1:end);
if length(buffer) >= stack_size
stack = buffer(end-stack_size+1:end);
fprintf('Final Stack samples:\n');
disp(stack);
end
end

Community Treasure Hunt

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

Start Hunting!