Hello, I really need to optimize this block of code: for(i = 1:33) E(frame,i)=sum(power(samp_borders(i):samp_borders(i+1)).^2); end; I wanted to use array indexing instead of the for loop, since it should be faster, but everything I try does not work. power() is the array I'm working on. It is power spectrum data. The squaring and the summing 33 times is just taking way too long for what I'm doing. (I'm computing the energy for 33 bands of a power spectrum, but I'm doing it thousands of times.) Thanks.
Matlab: Optimizing energy computation code
Started by ●July 23, 2008
Reply by ●July 23, 20082008-07-23
On 23 Jul., 09:43, "jfrog" <ajmcg...@gmail.com> wrote:> Hello, > > I really need to optimize this block of code: > > for(i = 1:33) > E(frame,i)=sum(power(samp_borders(i):samp_borders(i+1)).^2); > end; > > I wanted to use array indexing instead of the for loop, since it should be > faster, but everything I try does not work. power() is the array I'm > working on. It is power spectrum data. > > The squaring and the summing 33 times is just taking way too long for what > I'm doing. (I'm computing the energy for 33 bands of a power spectrum, but > I'm doing it thousands of times.) > > Thanks.As long as samp_borders is not equispaced, I see no possibility for speeding up this code, despite rewriting it in C. Greetings, Uwe.
Reply by ●July 23, 20082008-07-23
>On 23 Jul., 09:43, "jfrog" <ajmcg...@gmail.com> wrote: >> Hello, >> >> I really need to optimize this block of code: >> >> for(i = 1:33) >> E(frame,i)=sum(power(samp_borders(i):samp_borders(i+1)).^2); >> end; >> >> I wanted to use array indexing instead of the for loop, since it shouldbe>> faster, but everything I try does not work. power() is the array I'm >> working on. It is power spectrum data. >> >> The squaring and the summing 33 times is just taking way too long forwhat>> I'm doing. (I'm computing the energy for 33 bands of a power spectrum,but>> I'm doing it thousands of times.) >> >> Thanks. > >As long as samp_borders is not equispaced, I see no possibility for >speeding >up this code, despite rewriting it in C.Ok. Thanks. samp_borders is logarithmically spaced, though. That wouldn't help by any chance, would it?
Reply by ●July 23, 20082008-07-23
On 2008-07-23, jfrog <ajmcgraw@gmail.com> wrote:> Hello, > > I really need to optimize this block of code: > > for(i = 1:33) > E(frame,i)=sum(power(samp_borders(i):samp_borders(i+1)).^2); > end; > > I wanted to use array indexing instead of the for loop, since it should be > faster, but everything I try does not work. power() is the array I'm > working on. It is power spectrum data.You can certainly move the squaring out of the loop, which will help unless you're only using a very sparse subset of it, so: power2 = power .^ 2; Hoewver, assuming samp_borders is constant, you can probably do better by constructing a mapping matrix from it and re-using that. # you need to know length(power) in advance, but I don't know: samp_borders_map = zeros(length(power), 33); for (i = 1:33) samp_borders_map(samp_borders(i):samp_borders(i+1),i) += 1; end Then your equation becomes: E(frame)=(power.^2) * samp_borders_map; -- Ben Jackson AD7GD <ben@ben.com> http://www.ben.com/






