Hi Tomas,
I understand that you are facing issues with window calculation and not getting the expected results.
It seems that the issue lies in how the loop is structured and how the indices are updated. The code provided is intended to process the signal in overlapping windows, but the loop only runs once because the indices are not updated correctly within the loop.
Here's the corrected code with a "for" loop that iterates over each segment of the signal:
% Analysis in 5 min, with 50% overlap
winsize = 300 * fsr;
overlap = winsize / 2;
% Preallocate arrays for efficiency
num_windows = floor((length(SAPr) - winsize) / overlap);
meanSAP = zeros(num_windows, 1);
meanMAP = zeros(num_windows, 1);
% ... (Do this for all other variables you're computing)
j = 1;
for i = 1:overlap:(length(SAPr) - winsize)
% Define temporal series of each sample
SAPw = SAPr(i:(i + winsize) - 1);
MAPw = MAPr(i:(i + winsize) - 1);
% ... (Do this for all other signals)
% Perform your analysis here
% ...
% Periodogram
[PSAP, FS] = periodogram(SAPw, rectwin(length(SAPw)), length(SAPw), fsr);
% ... (Do this for all other signals)
% Band power
LFS(j) = bandpower(PSAP, FS, [0.033, 0.15], 'psd');
% ... (Do this for all other bands and signals)
% Update the index for the results
j = j + 1;
end
% After the loop, you can calculate the mean of the non-zero elements if needed
meanSAPu = mean(nonzeros(meanSAP));
% ... (Do this for all other variables)
% Save results in an Excel file
results = table(meanSAP, stdSAP, LFrelS, HFrelS, meanMAP, stdMAP, LFrelM, HFrelM, meanDAP, stdDAP, LFrelD, HFrelD, meanHR, stdHR, LFrelH, HFrelH, LFHFS, LFHFM, LFHFD, LFHFH);
results2 = splitvars(results);
resultadosfinal = rows2vars(results2);
writetable(resultadosfinal, "Prueba Matlab final.csv");
Here's what was changed:
- The loop is now a "for" loop that iterates over the starting indices of each window, with the correct step size to account for the overlap.
- The "j" index is initialized outside the loop and incremented inside the loop to store the results for each window.
- The preallocation of result arrays to improve performance and ensure that they are of the correct size.
- The calculation of mean values for non-zero elements and the saving of results to an Excel file are done after the loop, ensuring that all windowed results are included.
With these changes, the code should now process each 5-minute window of your signal and store the results in the corresponding arrays. After the loop, you can calculate the mean across all windows if needed and then save the results to a file.
I hope it helps!