Misterious problem with rewriting for loop into matrix operation
Misterious problem with rewriting for loop into matrix operation
(OP)
Hello!
I'm writing a phase vocoder in MATLAB, with synthesis of sum of sinusoids. As the for loop is very slow in MATLAB, to process a 10 seconds long wav it takes 6 secs with "for" loop, while with a matrix operation it's only 0.3 secs. The problem is that with matrix operation the output is just some noise, the original sound is almost unrecognizable.
The part of the code is the following:
With for loop:
for k=1:step_size
r0 = r0+delta_r;
psi = psi+delta_psi;
res(k) = r0'*cos(psi);
end
with matrix op.:
res=(r0 + (1:step_size)*delta_r).*cos(psi + (1:step_size)*delta_psi);
psi=psi+step_size*delta_psi;
I've tried out the codes, and they seem to give the completely same result, that's why it's misterious why it doesn't work.
The whole .m file is attached, I'd be very pleased if anyone could tell me what the problem is, it'd be quite important.
I'm writing a phase vocoder in MATLAB, with synthesis of sum of sinusoids. As the for loop is very slow in MATLAB, to process a 10 seconds long wav it takes 6 secs with "for" loop, while with a matrix operation it's only 0.3 secs. The problem is that with matrix operation the output is just some noise, the original sound is almost unrecognizable.
The part of the code is the following:
With for loop:
for k=1:step_size
r0 = r0+delta_r;
psi = psi+delta_psi;
res(k) = r0'*cos(psi);
end
with matrix op.:
res=(r0 + (1:step_size)*delta_r).*cos(psi + (1:step_size)*delta_psi);
psi=psi+step_size*delta_psi;
I've tried out the codes, and they seem to give the completely same result, that's why it's misterious why it doesn't work.
The whole .m file is attached, I'd be very pleased if anyone could tell me what the problem is, it'd be quite important.





RE: Misterious problem with rewriting for loop into matrix operation
phi0 = zeros(step_size,1);
r0 = zeros(step_size,1);
psi = phi0;
and your looped version looks to be incrementing the whole of r0 and the whole of psi in each loop by the same amount. So every element in r0 is identical, as is every element in psi. Now you do a mtrix multiply:
res(k) = r0'*cos(psi);
the value of res(k) will be the summation of step_size identical multiplications. Equal to
res(k)=step_size*r0(1)*cos(psi(1))
Looks wrong to me. It doesn't seem to do anything sensible.
- Steve