Vectorizing code array modulo indexing

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectorizing code array modulo indexing

Torbjörn Rathsman
Is this kind of thing possible to do without do it in a loop?

     notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
     x=12.*log(f./440)./log(2);
     spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );

f is a vector of 10^7 elements and I want to take each computed index
and generate corresponding value from notes.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Juan Pablo Carbajal
On Tue, Oct 2, 2012 at 9:00 PM, Torbjörn Rathsman
<[hidden email]> wrote:

> Is this kind of thing possible to do without do it in a loop?
>
>     notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>     x=12.*log(f./440)./log(2);
>     spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>
> f is a vector of 10^7 elements and I want to take each computed index and
> generate corresponding value from notes.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave

Can you give more details? I do not see a loop.
I see that you are multiplying spectrum by something and then saving
it back into spectrum. What is the initial value of spectrum.

Sorry for not understanding.


--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Vectorizing code array modulo indexing

Jordi Gutiérrez Hermoso-2
In reply to this post by Torbjörn Rathsman
On 2 October 2012 15:00, Torbjörn Rathsman <[hidden email]> wrote:
> Is this kind of thing possible to do without do it in a loop?
>
>     notes=[1 1 0 1 1 1 0 1 1 0 1 0]';
>     x=12.*log(f./440)./log(2);
>     spectrum=spectrum.*cos(pi.*x).^16.* notes( mod(round(x),12) + 1 );
>
> f is a vector of 10^7 elements and I want to take each computed index and
> generate corresponding value from notes.

I really don't see the problem. The following code runs for me without
error:

    N = 100;
    f = [1:N] + rand(1,N);
    spectrum = [1:N] + rand(1,N);
    s = spectrum;

    notes=[1 1 0 1 1 1 0 1 1 0 1 0];

    x = 12*log(f/440)/log(2);
    spectrum .*= cos(pi*x).^16.*notes( mod(round(x),12) + 1 );

    for k=1:N %N huge
      x = 12*log(f(k)/440)/log(2);
      s(k) *= cos(pi*x).^16.* notes( mod(round(x),12) + 1 );
    endfor

    norm(s-spectrum)

If my f and spectrum variables are making assumptions different from
your own, please give me variables for f and spectrum I can use to
test.

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave