# Porting Mathematica animation to Octave

Hi! I know that Octave and Mathematica are completely different types of programs, but I’m just looking around to see what, if any, Open Source programming language would be easiest for porting an interesting little animation I found of far field diffraction patterns. The source code is available, but it is in Mathematica.

My — perhaps incorrect — understanding is that none of the Open Source symbolic math systems are simple replacements for Mathematica. Since this program appears to be purely numeric calculations, not symbolic manipulations, I see no reason to not use Octave. While I don’t know Octave well, I know it better than I know any of the symbolic mathematics systems. One thing I don’t know is if it has libraries that make animation as simple as in this Mathematica example.

Can anyone tell me if I am barking up the wrong tree by trying to use Octave and, if so, what Open Source language or system would be a better fit?

Thanks!

Attached below is the public domain Mathematica source code:

``````dim = 512;
size = 30;
sinstep[t_] := Sin[\[Pi]/2 t]^2;
stopstep[t_] := t (2 - t);
split[t_] := Round[80*stopstep[t] ];
\[Theta][t_] := \[Pi]*sinstep[t];

framerate = 1/30;

plottolo[ar_] := GraphicsRow[{
ArrayPlot[ar[[1/4 dim ;; 3/4 dim, dim/4 ;; 3/4 dim]],  ColorFunction -> "AvocadoColors", PlotLabel -> "Aperture", LabelStyle -> {White, Bold}, Frame -> False],
ArrayPlot[ RotateRight[ Abs[Fourier[PadRight[ar(*-Mean[Flatten@ar]*), {8 dim, 8 dim}]]]^2, {dim/2, dim/2}][[1 ;; dim, 1 ;; dim]](*[[2/8dim;;6/8dim,2/8dim;;6/8dim]]*), ColorFunction -> "AvocadoColors", PlotRange -> {0, 0.5}, ClippingStyle -> White,     PlotLabel -> "Far field diffraction", LabelStyle -> {White, Bold}, Frame -> False]
}, Background -> Black]

frames1 = Table[
ar = Clip[ RotateRight[DiskMatrix[size, dim], {0, split[t]}] +  RotateRight[DiskMatrix[size, dim], {0, -split[t]}], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames2 = Table[
ar = Clip[ DiskMatrix[size, dim] +  RotateRight[DiskMatrix[size, dim], {0, split[t]}] + RotateRight[DiskMatrix[size, dim], {0, -split[t]}], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames3 = Table[
ar = Clip[ RotateRight[ DiskMatrix[size, dim], {Round[split[1]*Sin[\[Theta][t]]], Round[split[1]*Cos[\[Theta][t]]]}] + RotateRight[       DiskMatrix[size, dim], {-Round[split[1]*Sin[\[Theta][t]]], -Round[split[1]*Cos[\[Theta][t]]]}], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames4 = Table[
ar = Clip[ RotateRight[DiskMatrix[size, dim], {split[t], split[1]}] + RotateRight[DiskMatrix[size, dim], {-split[t], split[1]}] +       RotateRight[DiskMatrix[size, dim], {split[t], -split[1]}] + RotateRight[DiskMatrix[size, dim], {-split[t], -split[1]}], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5 = Table[
ar = Clip[DiskMatrix[size + 3*size*stopstep[t], dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5bis = Table[
ar = Clip[ DiskMatrix[size + 3*size, dim] - DiskMatrix[3*size*stopstep[t], dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5tris = Table[
ar = Clip[ DiskMatrix[size + 3*size*(1 - stopstep[t]), dim] - DiskMatrix[3*size*(1 - stopstep[t]), dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5quater = Table[
trianglesize = 100*stopstep[t] + 10;
ar = Abs[ Rasterize[Graphics[Triangle[{{-1/2, -Tan[\[Pi]/6]/2}, {1/2, -Tan[\[Pi]/6]/2}, {0, Cos[\[Pi]/6]/2}}]], "Data", RasterSize -> {2*trianglesize, 2*trianglesize}][[All, All, 1]] - 255]/255;
ar = Clip[DiskMatrix[size*(1 - stopstep[t]), dim] + ar, {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5penta = Table[
trianglesize = 100*(1 - stopstep[t]) + 10;
ar = Abs[ Rasterize[ Graphics[ Triangle[{{-1/2, -Tan[\[Pi]/6]/2}, {1/2, -Tan[\[Pi]/6]/2}, {0, Cos[\[Pi]/6]/2}}]], "Data", RasterSize -> {2*trianglesize, 2*trianglesize}][[All, All, 1]] - 255]/255;
ar = Clip[BoxMatrix[size*3*(stopstep[t]), dim] + ar, {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames5esa = Table[
ar = Clip[ BoxMatrix[size/2 + size*2.5*(1 - stopstep[t]), dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames6 = Table[
ar = Clip[ BoxMatrix[{size/2, size/2 + Round[(100 - size/2)*sinstep[t]]},  dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames7 = Table[
ar = Clip[ BoxMatrix[{size/2, 100}, dim] + BoxMatrix[{size/2 + Round[(100 - size/2)*sinstep[t]], size/2}, dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames8 = Table[
ar = Clip[ RotateRight[ BoxMatrix[{size/2, 100},  dim], {-Round[(100 - size/2)*sinstep[t] ], 0}] + BoxMatrix[{100, size/2}, dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames9 = Table[
ar = Clip[ RotateRight[ BoxMatrix[{size/2, 100}, dim], {-(100 - size/2), 0}] +  RotateRight[BoxMatrix[{100, size/2}, dim], {0, -Round[(100 - size/2)*sinstep[t] ]}], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
frames10 = Table[
ar = Clip[ BoxMatrix[size/2*(1 - stopstep[t]), dim] + DiskMatrix[size*stopstep[t], dim], {0, 1}];
plottolo[ar]
, {t, 0, 1, framerate}];
ListAnimate[Join[
frames1, Reverse@frames1, frames2, Reverse@frames2, frames1, frames3, frames4, Reverse@frames4, Reverse@frames1, frames5, frames5bis, frames5tris, frames5quater, frames5penta, frames5esa, frames6, frames7, frames8, frames9,  Reverse@frames9, Reverse@frames8, Reverse@frames7, Reverse@frames6, frames10
]]
``````

Yes, that code looks OK for porting to Octave if you want to do so. The code seems to use inline functions heavily but you can either keep them as functions or you can vectorize them. There are multiple ways to create such an animation in Octave, whether rendered in real time in a figure window or saved as a series of images and then turned into a gif or movie with an external program like `ffmpeg`.

1 Like

In addition to @arungiridhar’s comment, you can directly use the video package to produce videos. See the demo provided with the `VideoWriter` class for how to capture a frame in an Octave figure and add it to your video stream:

``````## pkg install -forge video