Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Matlab arrays. Simple task takes forever.

Options
  • 12-12-2008 11:50pm
    #1
    Registered Users Posts: 695 ✭✭✭


    Hey having a problem with matlab, I'm new to matlab but not to programming in general which is why im stumped with this problem.

    I recorded some speech in matlab, and its stored in an array. I added another column to the array to represent the time. Since i sampled at 10000Hz, I want to store the time each sample was taken at in the first column of the array. The second column already contains the eh amplitude.

    So the array currently looks like this:
    0	0.0038147
    0	0.0037231
    0	0.0038757
    0	0.0040283
    0	0.004364
    0	0.0047302
    0	0.0043945
    0	0.0046692
    0	0.004303
    0	0.0043945
    

    I want to make it like this:
    0.0001	0.0038147
    0.0002	0.0037231
    0.0003	0.0038757
    0.0004	0.0040283
    0.0005	0.004364
    0.0006	0.0047302
    0.0007	0.0043945
    0.0008	0.0046692
    0.0009	0.004303
    0.0010	0.0043945
    
    etc etc..

    So should be a simple loop:
    for iCounter=1:size(myarray,1)
    myarray(iCounter,1) = iCounter / 10000;
    end
    

    right? This does seem to work, although it takes an unbelievable long time. The array is quite large though, roughly 25,000 rows. However still expected only a few seconds, however after several seconds maybe 30+ I cancled it to see if it was working at all, and it's only done the first 70 rows :O. I wonder if this is something to do with how its stored in memory since I added the second "column" after the array already existed.

    Any ideas?

    What im ultimately trying to achieve seems trivial but I keep running into problems, mainly because I'm new to matlab. I want to store the speech in a csv file, in the format (time, amplitude) for importing into a Spice simulator


Comments

  • Registered Users Posts: 695 ✭✭✭DaSilva


    Well that's bizarre, I typed in the code slightly wrong in my original post, and corrected it adding a semi colon and the correct variable names.

    I then realised I had forgotten a semi colon in the code i ran in matlab, seriously suprised it didnt go give me an error, and instead did something difirent.

    for iCounter=1:size(myarray,1)
    myarray(iCounter,1) = iCounter / 10000;
    end

    for iCounter=1:size(myarray,1)
    myarray(iCounter,1) = iCounter / 10000
    end

    do somewhat the same thing but one seems to take forever, anyone any idea what is going on?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Matlab runs off Java... (I'm not suggesting that this is the problem, I'm just taking a cheap swipe at Java!)

    Although, what platform are you using and what version of Java are you using? Are you sure that sun-java is the default JVM? It could be that the system has to figure out what resources to allocate and this takes time. Perhaps the OS is too clever for it's own good and some kind of fancy semantic interoperability is going on in the background? Have you tried running a number of matrix manipulation calculations in sequence? How long does this take? I mean, is all the delay concentrated at the beginning of the program's execution?

    Are you working on speech recognition?

    I'm interested in that myself.

    I use Matlab for mashing up complex equations and matrix manipulations. It's much easier than coding up details in C++.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    I may be wrong but if you don't put a semi-colon at the end of a statement, it prints the result out onto the console which may be the cause of the slow down.


  • Registered Users Posts: 4,769 ✭✭✭cython


    Ok, I just ran that on a zeros array on matlab myself, and it executed in less than a second.

    I will suggest that a loop like that isn't entirely necessary though. The same effect can be accomplished by the following one line statement:
    myarray(:,1) = 1:1:size(myarray,1);
    

    I don't know whether this will be any more efficient, but it couldn't hurt to try it


  • Registered Users Posts: 4,769 ✭✭✭cython


    Anima wrote: »
    I may be wrong but if you don't put a semi-colon at the end of a statement, it prints the result out onto the console which may be the cause of the slow down.

    That's actually exactly it. It takes time for it to print out the result of each assignment, especially when on each iteration, it has to print 25000 rows, and there are 25000 iterations, so it's basically printing 25000 x 25000 rows of text!

    The one liner above only has one assignment though, so it will run fast without the semicolon. There really is something to be said for not assigning individual elements if it can be helped!


  • Advertisement
Advertisement