Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Improving scaling algorithum

Subject: Improving scaling algorithum

From: JordanRS Rogers

Date: 23 Nov, 2010 12:55:04

Message: 1 of 10

Hello,

Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder

for haPos=1:Nf
   for samplePos=1:N
       if D(samplePos,haPos) < -pi
           while D(samplePos,haPos) < -pi
               D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
           end
       elseif D(samplePos,haPos) > pi
           while D(samplePos,haPos) > pi
               D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
           end
      else
       end
   end
end

this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it

for haPos=1:Nf
    for samplePos=1:N
        if D(samplePos,haPos) < -pi
           
           D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
        elseif D(samplePos,haPos) > pi
     
           D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
           
       else
        end
    end
end
 
this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?

Subject: Improving scaling algorithum

From: Sandro

Date: 24 Nov, 2010 20:56:03

Message: 2 of 10

may be:


clear all,clc
D=randn(1000)*10
state=1
count=0;
while state& count<1e2
    pos=D<-pi;
    D(pos)=D(pos)+2*pi;
    state=sum(pos(:))
    pos=D>pi;
    D(pos)=D(pos)-2*pi;
    state=state+sum(pos(:))
    count=count+1;
end
count

Subject: Improving scaling algorithum

From: TideMan

Date: 24 Nov, 2010 21:46:51

Message: 3 of 10

On Nov 24, 1:55 am, "JordanRS Rogers" <jordanrs...@gmail.com> wrote:
> Hello,
>
> Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder
>
> for haPos=1:Nf
>    for samplePos=1:N
>        if D(samplePos,haPos) < -pi
>            while D(samplePos,haPos) < -pi
>                D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
>            end
>        elseif D(samplePos,haPos) > pi
>            while D(samplePos,haPos) > pi
>                D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>            end
>       else
>        end
>    end
> end
>
> this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it
>
> for haPos=1:Nf
>     for samplePos=1:N
>         if D(samplePos,haPos) < -pi
>
>            D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
>         elseif D(samplePos,haPos) > pi
>
>            D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>
>        else
>         end
>     end
> end
>
> this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?

You can eliminate the for loops altogether like this:
D=D + (D<-pi)*2*pi - (D>pi)*2*pi;

Subject: Improving scaling algorithum

From: Roger Stafford

Date: 24 Nov, 2010 23:27:03

Message: 4 of 10

"JordanRS Rogers" <jordanrs2k1@gmail.com> wrote in message <icgdj8$gh8$1@fred.mathworks.com>...
> Hello,
>
> Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder
>
> for haPos=1:Nf
> for samplePos=1:N
> if D(samplePos,haPos) < -pi
> while D(samplePos,haPos) < -pi
> D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> end
> elseif D(samplePos,haPos) > pi
> while D(samplePos,haPos) > pi
> D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
> end
> else
> end
> end
> end
>
> this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it
>
> for haPos=1:Nf
> for samplePos=1:N
> if D(samplePos,haPos) < -pi
>
> D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> elseif D(samplePos,haPos) > pi
>
> D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>
> else
> end
> end
> end
>
> this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?
- - - - - - - - - -
  D = mod(D+pi,2*pi)-pi;

Roger Stafford

Subject: Improving scaling algorithum

From: Roger Stafford

Date: 24 Nov, 2010 23:56:03

Message: 5 of 10

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <ick707$l2q$1@fred.mathworks.com>...
> D = mod(D+pi,2*pi)-pi;
>
> Roger Stafford
- - - - - - -
  An afterthought. The term "scaling" is inappropriate for this operation in my opinion. It would be better if you referred to it as something like transforming or reducing D to its principal (radian) angles.

Roger Stafford

Subject: Improving scaling algorithum

From: Greg Heath

Date: 25 Nov, 2010 01:33:39

Message: 6 of 10

On Nov 24, 4:46 pm, TideMan <mul...@gmail.com> wrote:
> On Nov 24, 1:55 am, "JordanRS Rogers" <jordanrs...@gmail.com> wrote:
> > Hello,
>
> > Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder
>
> > for haPos=1:Nf
> >    for samplePos=1:N
> >        if D(samplePos,haPos) < -pi
> >            while D(samplePos,haPos) < -pi
> >                D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> >            end
> >        elseif D(samplePos,haPos) > pi
> >            while D(samplePos,haPos) > pi
> >                D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
> >            end
> >       else
> >        end
> >    end
> > end
>
> > this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it
>
> > for haPos=1:Nf
> >     for samplePos=1:N
> >         if D(samplePos,haPos) < -pi
>
> >            D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> >         elseif D(samplePos,haPos) > pi
>
> >            D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>
> >        else
> >         end
> >     end
> > end
>
> > this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?
>
> You can eliminate the for loops altogether like this:
> D=D + (D<-pi)*2*pi - (D>pi)*2*pi

No, as seen below (method 2), that needs to be embedded in a loop.


clear all; clc;
randn('state',0);
D0 = randn(1000)*10;
minmaxgh0 = [min(min(D0)) max(max(D0))]

% Method 1
tic
D1 = D0;
state=1;
count=0;
while state& count<1e2;
    pos=D1<-pi;
    D1(pos)=D1(pos)+2*pi;
    state=sum(pos(:));
    pos=D1>pi;
    D1(pos)=D1(pos)-2*pi;
    state=state+sum(pos(:)) ;
    count=count+1;
end
count
minmaxgh(1,:) = [min(min(D1)) max(max(D1))];
time(1,1) = toc;

% Method 2

tic
D2 = D0;
D2 = D2 + (D2<-pi)*2*pi - (D2>pi)*2*pi;
minmaxgh(2,:) = [min(min(D2)) max(max(D2))];
time(2,1) = toc;

% Method 3
tic
D3 = D0;
D3 = mod(D3+pi,2*pi)-pi;
minmaxgh(3,:) = [min(min(D3)) max(max(D3))];
time(3,1) = toc;

% Method 4

tic
minD0 = min(min(D0));
Dgh = -pi + (2*pi/(max(max(D0))-minD0))*(D0-minD0);
minmaxgh(4,:) = [min(min(Dgh)) max(max(Dgh))];
time(4,1) = toc;

result = [time minmaxgh]

result =

0.734 -3.1416 3.1416
0.344 -42.238 40.082 <== Needs to be looped
0.422 -3.1416 3.1416
0.297 -3.1416 3.1416

Hope this helps.

Greg

Subject: Improving scaling algorithum

From: Greg Heath

Date: 25 Nov, 2010 01:38:58

Message: 7 of 10

On Nov 24, 8:33 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Nov 24, 4:46 pm, TideMan <mul...@gmail.com> wrote:
>
>
>
>
>
> > On Nov 24, 1:55 am, "JordanRS Rogers" <jordanrs...@gmail.com> wrote:
> > > Hello,
>
> > > Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder
>
> > > for haPos=1:Nf
> > >    for samplePos=1:N
> > >        if D(samplePos,haPos) < -pi
> > >            while D(samplePos,haPos) < -pi
> > >                D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> > >            end
> > >        elseif D(samplePos,haPos) > pi
> > >            while D(samplePos,haPos) > pi
> > >                D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
> > >            end
> > >       else
> > >        end
> > >    end
> > > end
>
> > > this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it
>
> > > for haPos=1:Nf
> > >     for samplePos=1:N
> > >         if D(samplePos,haPos) < -pi
>
> > >            D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> > >         elseif D(samplePos,haPos) > pi
>
> > >            D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>
> > >        else
> > >         end
> > >     end
> > > end
>
> > > this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?
>
> > You can eliminate the for loops altogether like this:
> > D=D + (D<-pi)*2*pi - (D>pi)*2*pi
>
> No, as seen below (method 2), that needs to be embedded in a loop.
>
> clear all; clc;
> randn('state',0);
> D0 = randn(1000)*10;
> minmaxgh0 = [min(min(D0)) max(max(D0))]
>
> % Method 1
> tic
> D1 = D0;
> state=1;
> count=0;
> while state& count<1e2;
>     pos=D1<-pi;
>     D1(pos)=D1(pos)+2*pi;
>     state=sum(pos(:));
>     pos=D1>pi;
>     D1(pos)=D1(pos)-2*pi;
>     state=state+sum(pos(:)) ;
>     count=count+1;
> end
> count
> minmaxgh(1,:) = [min(min(D1)) max(max(D1))];
> time(1,1) = toc;
>
> % Method 2
>
> tic
> D2 = D0;
> D2 = D2 + (D2<-pi)*2*pi - (D2>pi)*2*pi;
> minmaxgh(2,:) = [min(min(D2)) max(max(D2))];
> time(2,1) = toc;
>
> % Method 3
> tic
> D3 = D0;
> D3 = mod(D3+pi,2*pi)-pi;
> minmaxgh(3,:) = [min(min(D3)) max(max(D3))];
> time(3,1) = toc;
>
> % Method 4
>
> tic
> minD0 = min(min(D0));
> Dgh = -pi + (2*pi/(max(max(D0))-minD0))*(D0-minD0);
> minmaxgh(4,:) = [min(min(Dgh)) max(max(Dgh))];
> time(4,1) = toc;
>
> result = [time minmaxgh]
>
> result =
>
> 0.734      -3.1416       3.1416
> 0.344      -42.238       40.082 <== Needs to be looped
> 0.422      -3.1416       3.1416
> 0.297      -3.1416       3.1416
>
> Hope this helps.

WHoops!

I didn't read it closely enough to realize that
it is a phase unwrapping problem.

Sorry,

Greg

Subject: Improving scaling algorithum

From: Greg Heath

Date: 25 Nov, 2010 02:29:24

Message: 8 of 10

On Nov 24, 8:38 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Nov 24, 8:33 pm, Greg Heath <he...@alumni.brown.edu> wrote:
>
>
>
>
>
> > On Nov 24, 4:46 pm, TideMan <mul...@gmail.com> wrote:
>
> > > On Nov 24, 1:55 am, "JordanRS Rogers" <jordanrs...@gmail.com> wrote:
> > > > Hello,
>
> > > > Im quite new to matlab and programming in general. Im writing algorithum for scaling the values of a matrix to between pi and -pi and this is what iv got where Nf is the number of analysis frames and N is the window size in samples of the phase vocoder
>
> > > > for haPos=1:Nf
> > > >    for samplePos=1:N
> > > >        if D(samplePos,haPos) < -pi
> > > >            while D(samplePos,haPos) < -pi
> > > >                D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> > > >            end
> > > >        elseif D(samplePos,haPos) > pi
> > > >            while D(samplePos,haPos) > pi
> > > >                D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
> > > >            end
> > > >       else
> > > >        end
> > > >    end
> > > > end
>
> > > > this algorithum works great its just very slow in computation. I tried simplifying it and rewriting it
>
> > > > for haPos=1:Nf
> > > >     for samplePos=1:N
> > > >         if D(samplePos,haPos) < -pi
>
> > > >            D(samplePos,haPos) = D(samplePos,haPos) + 2*pi;
> > > >         elseif D(samplePos,haPos) > pi
>
> > > >            D(samplePos,haPos) = D(samplePos,haPos) - 2*pi;
>
> > > >        else
> > > >         end
> > > >     end
> > > > end
>
> > > > this is quick but isnt right as there are lots of unwanted artificats in output sound. Can anyone guide me in improving the execution speed of the top algorithum?
>
> > > You can eliminate the for loops altogether like this:
> > > D=D + (D<-pi)*2*pi - (D>pi)*2*pi
>
> > No, as seen below (method 2), that needs to be embedded in a loop.
>
> > clear all; clc;
> > randn('state',0);
> > D0 = randn(1000)*10;
> > minmaxgh0 = [min(min(D0)) max(max(D0))]
>
> > % Method 1
> > tic
> > D1 = D0;
> > state=1;
> > count=0;
> > while state& count<1e2;
> >     pos=D1<-pi;
> >     D1(pos)=D1(pos)+2*pi;
> >     state=sum(pos(:));
> >     pos=D1>pi;
> >     D1(pos)=D1(pos)-2*pi;
> >     state=state+sum(pos(:)) ;
> >     count=count+1;
> > end
> > count
> > minmaxgh(1,:) = [min(min(D1)) max(max(D1))];
> > time(1,1) = toc;
>
> > % Method 2
>
> > tic
> > D2 = D0;
> > D2 = D2 + (D2<-pi)*2*pi - (D2>pi)*2*pi;
> > minmaxgh(2,:) = [min(min(D2)) max(max(D2))];
> > time(2,1) = toc;
>
> > % Method 3
> > tic
> > D3 = D0;
> > D3 = mod(D3+pi,2*pi)-pi;
> > minmaxgh(3,:) = [min(min(D3)) max(max(D3))];
> > time(3,1) = toc;
>
> > % Method 4
>
> > tic
> > minD0 = min(min(D0));
> > Dgh = -pi + (2*pi/(max(max(D0))-minD0))*(D0-minD0);
> > minmaxgh(4,:) = [min(min(Dgh)) max(max(Dgh))];
> > time(4,1) = toc;
>
> > result = [time minmaxgh]
>
> > result =
>
> > 0.734      -3.1416       3.1416
> > 0.344      -42.238       40.082 <== Needs to be looped
> > 0.422      -3.1416       3.1416
> > 0.297      -3.1416       3.1416
>
> > Hope this helps.
>
> WHoops!
>
> I didn't read it closely enough to realize that
> it is a phase unwrapping problem.

WHOOPS again. I need some sleep.

It is a phase wrapping problem and

sign(sin(D0)).*acos(cos(D0);

is not fast.

Greg

Subject: Improving scaling algorithum

From: Sandro

Date: 26 Nov, 2010 19:18:03

Message: 9 of 10

test:

clear all,clc
D=randn(100)*5;
pos=D>pi;
D(pos)=D(pos)-2*pi*(fix((D(pos)-pi)/(2*pi))+1);
pos=D<-pi;
D(pos)=D(pos)+2*pi*(abs(fix((D(pos)+pi)/(2*pi)))+1)

Subject: Improving scaling algorithum

From: JordanRS Rogers

Date: 29 Nov, 2010 14:05:05

Message: 10 of 10

Hey guys

thanks for the replys iv tried all the mehods mentioned and the one that produced the best result in terms of efficeny and not causing any artificats (some for some reason didnt work perfectly) was rogers

D(samplePos, haPos) = mod(D(samplePos, haPos)+pi,2*pi)-pi;

Cheers for all the help

Jordan

Tags for this Thread

No tags are associated with this thread.

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.

Contact us