Sliding a fixed window over an array

26 visualizaciones (últimos 30 días)
Edward Ramirez
Edward Ramirez el 10 de Feb. de 2021
Comentada: Sourabh Kondapaka el 2 de Mzo. de 2021
Dear Matlab God,
I am in need of help. I want to slide a 80 MHz window through a 1 GHz bandwith array.
Here is an illustation:
As the 80 MHz window sweeps the 1GHz bandwidth, I need find the max and min ponits, subtract them, and store them in an array.
I tiredusing the diff function, but thatonly works for whole number integers.
I think it would be a nested for loop, with a fixed window. Just not sure how to do it.
Thank You,
Ed

Respuestas (1)

Sourabh Kondapaka
Sourabh Kondapaka el 17 de Feb. de 2021
Editada: Sourabh Kondapaka el 17 de Feb. de 2021
Below is the function which will help in acheiving what you want:
function resultArr = maxMinDiff(arr, windowSize)
% If windowSize is greater than the actual arr, return 0
if windowSize > length(arr)
resultArr = zeros();
% Else compute max - min for each window while sliding it.
else
% Size of the window which is generated when computing max - min for each window while
% sliding it across the array. The size depends on the actual arr and the window size.
resultArr = zeros(1, length(arr) - windowSize + 1);
% Special case for the first window.
resultArr(1) = max(arr(1:windowSize)) - min(arr(1:windowSize));
%Compute Max/Min for all subsequent sliding windows
for i = 2:length(arr) - windowSize + 1
maxCurrentWindow = max(arr(i:i+windowSize -1));
minCurrentWindow = min(arr(i:i+windowSize -1));
resultArr(i) = maxCurrentWindow - minCurrentWindow;
end
end
end
As I do not access to your data, I'm going ahead with an array containing randomnly generated numbers:
arr = randi(1000,1,10); % Generates random number array (with an upper bound of 1000) of size 1x10
windowSize = 5;
resultArr = maxMinDiff(arr, windowSize);
  2 comentarios
Edward Ramirez
Edward Ramirez el 1 de Mzo. de 2021
doesyour code work for a window which isn't a whole number? For example, suppose a window size of 0.8
Sourabh Kondapaka
Sourabh Kondapaka el 2 de Mzo. de 2021
window size only works for positive integers.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by