saving a value and reseting other variables in a loop

1 visualización (últimos 30 días)
zozo
zozo el 5 de Mzo. de 2012
I have the following situation:
for d=1:numel(Time_delay) % time delay is a (8x1000) matrix
if rem(d,8)==0;
mic_out(1,:)=mic_in(1,:);
out= sum(sum(mic_out,1).^2,2) ; %computing the energies
mic_out=zeros(size(mic_out));
end
mic_out(rem(d,8)+1,:)=del(mic_in(rem(d,8)+1,:),Time_delay(d+1));
end
Here, 'del' is a function which delays the 8 signals in 'mic_in(8x10000)' according to delays specified in Time_delay'
The first row in Time_delay is all zeros that is, no delay applied to signals in 'mic_in'. Hence, I replace it with 1st row of signal that is, with mic_in(1,10000). Each time I change column in Time_delay (jump from column 1 to column 2 and so on), I need to add the 8 delayed signals in mic_out row-wise,square the elements in the row hence obtained and add the result colum-wise (i.e. calculate energy) and store only the energy values in a variable 'out', reset everything to zero and move on for the next set of 8 signals (that is next column in matrix 'Time_delay').
This process is repeated each time I jump from 1 colum to another of 'Time_delay', till I reach the last column. At the end I should be only left with the matrix 'out' containing the energies (for plotting and further analysis). I have problem in indexing the above variables according to my requirement.
Whats the most efficient way to achive this?

Respuesta aceptada

Geoff
Geoff el 5 de Mzo. de 2012
I think you're asking two questions:
a) Why is the indexing broken?
b) What is a better way to do this?
I'll ask you my own: Are you a C programmer? =) There seems to be a confusion over 0-based and 1-based indexing in this code.
For example:
Time_delay(d+1)
This will overrun on the last element, and I'm sure you don't mean to add 1 to the index.
Next:
rem(d,8)+1
Is this the desired behaviour?
The sequence will go:
2, 3, 4, 5, 6, 7, 8, 1, 2, 3, ...
Did you mean:
rem(d-1,8)+1
Now, the matrix out. Presumably you want to be putting a single energy value into it after processing each row (ie every 8 times around). So you would end up with a 1x10000 vector. At the moment you are just overwriting it each time.
It looks to me like this code would be a lot clearer if you used nested for-loops over the columns and rows. Perhaps like so:
% Initialise output vector with same number of columns as time delay.
out = zeros(1,size(Time_delay,2));
for col = 1:size(Time_delay,2)
% Zero the accumulator and copy first (non-delayed) row.
mic_out = zeros(size(mic_out));
mic_out(1,:) = mic_in(1,:);
% Accumulate delayed signals for all other rows
for row = 2:size(Time_delay,1)
mic_out(row,:) = del( mic_in(row,:), Time_delay(row,col) );
end
% Store energy value for current set of delays
out(col) = sum( sum(mic_out,1).^2, 2 );
end
I suspect that the calculation for out might not be quite the same as you described. But maybe the above code makes it more obvious where your mistakes are, so you can fix them more easily.
Cheers, -g-
  2 comentarios
zozo
zozo el 5 de Mzo. de 2012
@Geoff: thank you so much for your answer. I got it all sorted out. Actually, your answer was exactly the solution I was looking for. It was just perfecT..!! :) :)
zozo
zozo el 6 de Mzo. de 2012
I was wondering if the above lines could be improved for computational speed? (avoiding loops?)
Because the above process has to run 360x180=64800 times.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by