Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Help me optimize a line of code?

Subject: Help me optimize a line of code?

From: bogfrog

Date: 23 Jul, 2008 07:41:00

Message: 1 of 4

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, 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.

Subject: Help me optimize a line of code?

From: Gavrilo Bozovic

Date: 23 Jul, 2008 08:13:02

Message: 2 of 4

bogfrog <jmcgraw@rcn.com> wrote in message
<1845647.1216798890667.JavaMail.jakarta@nitrogen.mathforum.org>...
> 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,
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.

okay. So you want to have the squared sum of each two
consecutive elements in your array samp_borders.

I'd suggest the following:

squared_samp_borders=samp_borders.^2;
squared_samp_borders_2=zeros(max(size(samp_borders))+1);
squared_samp_borders_2(2:end)=squared_samp_borders;

E(frame)= squared_samp_borders_2+squared_samp_borders;

would that work for you? I didn't try it, but it should be a
*lot* faster.

Note that the squaring is done only once. Alternatively, you
could make a shifted vector, and then square and sum both,
which would lead to a loss of time.

Cheers,

Gavrilo

Subject: Help me optimize a line of code?

From: Peter Boettcher

Date: 23 Jul, 2008 13:15:29

Message: 3 of 4

bogfrog <jmcgraw@rcn.com> writes:

> 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, 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.)

Is there anything special about samp_borders? Are they evenly spaced?
Same number of samples per band? Just arbitrary? Are they static for
the whole program, or do they change?

If they're static, you could set up a matrix to multiply to do the
summation. Initialize that matrix like:

T = zeros(samp_borders(end), 33);

for i=1:33
  T(samp_borders(i):samp_borders(i+1), i) = 1;
end

And don't touch it again.

Then each of the 1000 loops, square "power" in one shot, and multiply by
T.

E(frame,:) = T * power.^2;

Have you preallocated E before the loop starts?

-Peter

Subject: Help me optimize a line of code?

From: bogfrog

Date: 24 Jul, 2008 06:30:15

Message: 4 of 4

> Is there anything special about samp_borders? Are
> they evenly spaced?
> Same number of samples per band? Just arbitrary?
> Are they static for
> the whole program, or do they change?

They are not evenly spaced, but they are logarithmically spaced. (so the number of samples per band is not the same) And they are static for the whole program.

 
> T = zeros(samp_borders(end), 33);
>
> for i=1:33
> T(samp_borders(i):samp_borders(i+1), i) = 1;
> end
>
> And don't touch it again.
>
> Then each of the 1000 loops, square "power" in one
> shot, and multiply by
> T.
>
> E(frame,:) = T * power.^2;

This seemed like a great idea. I was excited to try it, but it didn't speed things up at all! I'm pretty shocked. :(

 
> Have you preallocated E before the loop starts?

Yes. Otherwise it literally takes an eternity. lol

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics