Skip to content
Nov 6 / kkrizka

Creating Animations With Octave

Unlike MATLAB, the open source numerical package GNU Octave does not have support for quickly creating animations. That is quite disappointing, because as a physics student, I like to see what the time-dependent equations I solve look like. For example, I am currently taking a course with a section on boundary value problems. This includes the motion of waves on different surfaces, and following their evolution using Fourier series. As you can imagine, that might look quite cool.

Luckily, with the amount of tools available for GNU/Linux, I’ve managed to hack together a quick script that allows me to create AVI movies with Octave that I can then play back. It might not be as simple as MATLAB’s movie() function, but it works! Here is how:

  1. Create a for loop, with each pass displaying the next frame in the animation.
  2. Save each frame as an png (or some other type) image such that when the file names are ordered alphabetically, they are also ordered chronologically.
  3. Use mencoder (or some other encoder) to stitch together the images into a movie file.
    • mencoder (AVI Movie)
      mencoder mf://*.png -mf w=640:h=480:fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi
    • ffmpeg (MPEG Movie)
      ffmpeg -i "%d5.png" -y output.mpeg
    • ImageMagick’s convert (Animated GIF’s)
      convert -delay 10 -loop 0 *png output.gif
  4. Play the output.avi file to see your animation.

Here is my example of animating the vibrations of a square membrane, that was initially at rest with the displacement of z=x*(1-x)*y*(1-y), but scaled by some constant.

  1. In your project directory, create an empty directory called output.
  2. In your project folder, create an Octave script called simulation.m that contains the code for your simulation. For example, my looks like the following:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    set(0, 'defaultfigurevisible', 'off');
    [x,y]=meshgrid([0:0.01:1],[0:0.01:1]);
     
    for t=[0:1000]
        z=x-x; %Initialize empty z grid
        for nx=[1:10]
            for ny=[1:10]
              coeff=(1-(-1)^nx)*(1-(-1)^ny)/(nx^3*ny^3);
              z=z+coeff*sin(nx*pi.*x).*sin(ny*pi.*y)*cos(sqrt(nx^2+ny^2)*pi*t/100);
            end
        end
     
        surf(x,y,z);
        axis([0 1 0 1 -4 4]);
        shading interp;
        xlabel("x");
        ylabel("y");
        zlabel("u(x,y)");
        filename=sprintf('output/%05d.png',t);
        print(filename);
    end

    Some notes of importance in this code:

    • Line 1 hides the plot display, so it is not redrawn on the screen every time. This speeds up things considerably.
    • Line 19 sets the filename as “#####.png”, a 5 digits frame number. For example, if the frame number is 2, then the filename is 00002.png. This guarantees that the bash shell will order the filenames chronologically.
    • Line 20 actually saves the plot to the file
  3. Run the simulation using octave. octave simulation.m
  4. Stitch together the images into a movie. mencoder mf://output/*.png -mf w=640:h=480:fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi
  5. Watch and enjoy!

    Download AVI

Finally, a few other sources that I would recommend to learn more about animation using octave:

Displaying an animation / “movie” – This thread helped me a lot when I started.
Animating Laplace in Octave – Talks about creating animated GIFs and using ffmpeg to create an MPEG movie.

9 Comments

Leave a comment
  1. loredana / Feb 14 2010

    Thanks for the tips. It works!
    I am using ffmpeg to create a video from png. There is a small
    typo:
    ffmpeg -i “%d5.png” -y output.mpeg
    should be:
    ffmpeg -i “%05d.png” -y output.mpeg
    or else ffmpeg returns a confusing error, stating that the input
    is corrupted.

  2. ostehamster / Mar 23 2010

    Great howto, thanks! :)

  3. helton / Apr 28 2010

    Great tip, concise and complete!

    I created a shell script to run the file.m and automatically run the mencoder command (thanx Ioredana for the typo tip, I am planning to use ffmpeg too). Scripts tend to be good ways of testing animations and similar repetitive tasks without retyping commands.

  4. Kresimir Veselic / Jan 17 2011

    Your octave program produced nice png pictures of the membrane.
    But, unfortunately, both

    ffmpeg -i “%d5.png” -y output.mpeg
    and
    ffmpeg -i “%05d.png” -y output.mpeg

    produced exactly the same error message.

    “%05d.png”: I/O error occured
    Usually that means that input file is truncated and/or corrupted.

    What is wrong?

    Thanks in advance, K. Veselic

  5. Saha / Jul 19 2011

    That sure helps. Thanks a lot!

  6. N3OX / Sep 16 2011

    Thanks for the code! Great timesaver in making animations from antenna simulation results:


    http://youtu.be/5tV_BKImc6s

    VirtualDub works great to make both .avi and animated GIF files of the results.

    http://n3ox.net/projects/n3oxflex/n3oxflex_nearH.gif

  7. Dave Comer / Aug 24 2012

    Thanks Karol. That was a huge help!!!

Trackbacks and Pingbacks

  1. Tip of making animation using octave | $x_{k} = F_{k}x_{k-1} + B_{k}u_{k} + w_{k}$
  2. Animated movies with image or imagesc in GNU Octave | Michael Hirsch: Portfolio
Leave a comment