Reputation: 29
I need a bit of help with some matlab code regarding for loops and power spectral densities.
I'm analysing a signal and need to be able to measure the average power in each each 30 second segment. I have split the signal with the function 'buffer'so that 30 seconds worth of data lie in each column of the matrix segment_mat
but I want to be able to store the results of the power spectral density in a matrix using a for loop to loop through the data and to also store the average power of each segment in a vector avpow
. Any thoughts where i might be going wrong?
for j=1:120
Hs=spectrum.periodogram({'Hamming'});
Power=psd(Hs,segment_mat(:,j)',fs',fs);
avpow=mean(Power(j))';
end
Upvotes: 1
Views: 194
Reputation: 3802
Your problem here is that you are overwriting the results in every iteration of the for
loop.
Just modify your code so:
Power(j) = ...
avpow(j) = ...
and you will be storing the results of your calculations. Consider pre-allocating if speed is an issue.
Upvotes: 1