{"id":11916,"date":"2024-11-16T01:03:58","date_gmt":"2024-11-16T06:03:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=11916"},"modified":"2024-11-16T08:23:10","modified_gmt":"2024-11-16T13:23:10","slug":"lights-camera-action-and-sound","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2024\/11\/16\/lights-camera-action-and-sound\/","title":{"rendered":"Lights, Camera, Action &#8212; and Sound"},"content":{"rendered":"\n<div class=\"content\"><!--introduction-->\n<p>I am very excited to incorporate sound in animations. This blog post has five examples with links to videos enclosed in <tt>'+ + + +'<\/tt>.<\/p>\n<!--\/introduction-->\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li>\n<a href=\"#beb66eab-d057-4b35-b679-ceb8341bac55\">Mertens<\/a>\n<\/li>\n<li>\n<a href=\"#acc96bc1-84ad-4503-af62-3207c0cddc9d\">Lissajous<\/a>\n<\/li>\n<li>\n<a href=\"#c8bcb2e7-342f-4b58-a7ba-b3d7f7034476\">tansin<\/a>\n<\/li>\n<li>\n<a href=\"#7e32cd92-b6a9-46ec-aa1c-980089ff6833\">Van der Pol<\/a>\n<\/li>\n<li>\n<a href=\"#e791b241-63f8-4987-a4d3-4451b5b8f39d\">Blackjack<\/a>\n<\/li>\n<\/ul>\n<\/div>\n<h4>Mertens<a name=\"beb66eab-d057-4b35-b679-ceb8341bac55\"><\/a>\n<\/h4>\n<p>I began making animations with sound while investigating the <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2024\/09\/23\/redheffer-mertens-and-one-million-dollars\/\">Mertens Conjecture<\/a>. The <a href=\"https:\/\/oeis.org\/A002321\" target=\"_blank\">OEIS<\/a> page offers an opportunity to listen to the Mertens sequence, but I wanted more control.<\/p>\n<p>The <tt>resample<\/tt> function is the Signal Processing Toolbox resamples a signal at a fraction, <tt>p\/q<\/tt>, times the original sample rate. The sound in this Mertens example is generated from a recording of piano playing middle C that is resampled to simulate the other notes.<\/p>\n<p>Here is a link to a 40-second video of <tt>M(n)<\/tt> and <tt>sqrt(n)<\/tt> for <tt>n = 200<\/tt>. (Homework: How long would the video last if <tt>n = 10^8<\/tt>? Post your answers in the comments.)<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/mertens.mp4\">https:\/\/blogs.mathworks.com\/cleve\/files\/mertens.mp4<\/a>\n<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>Here is a static graphic, the last frame of the video.<\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/mertens.png\" alt=\"\"> <\/p>\n<h4>Lissajous<a name=\"acc96bc1-84ad-4503-af62-3207c0cddc9d\"><\/a>\n<\/h4>\n<p>You may well have seen <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lissajous_curve\" target=\"_blank\">Lissajous curves<\/a> before, but have you ever <i>listened<\/i> to them? They were first studied extensively by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Jules_Antoine_Lissajous\" target=\"_blank\">Jules Lissajous<\/a>, a nineteenth-century French physicist. There are many Lissajous curves similar to the following, where the trajectory in the <tt>x-y<\/tt> plane is described by two functions of a parameter <tt>t<\/tt>.<\/p>\n<pre class=\"language-matlab\">\nn = 200;\nt = 3*pi*(-1:2\/n:1);\nx = 5*cos(t)+3*cos(5\/3*t);\ny = 5*sin(t)-sin(5\/3*t);\n<\/pre>\n<p>This 40-second video employs sound produced by resampling a note from a xylophone.<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/lissajous.mp4\">https:\/\/blogs.mathworks.com\/cleve\/files\/lissajous.mp4<\/a>\n<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>Here is the last frame.<\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/lissajous.png\" alt=\"\"> <\/p>\n<h4>tansin<a name=\"c8bcb2e7-342f-4b58-a7ba-b3d7f7034476\"><\/a>\n<\/h4>\n<p>I have used variations of <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2012\/09\/24\/supremum\">this function<\/a> many times.<\/p>\n<pre>F = @(x) tan(sin(x))+sin(tan(x));<\/pre>\n<p>When <tt>x<\/tt> approaches an integer multiple of <tt>pi<\/tt>, both <tt>sin(x)<\/tt> and <tt>tan(x)<\/tt> approach zero, so <tt>F(x)<\/tt> also approaches zero. But when <tt>x<\/tt> approaches an odd multiple of <tt>pi\/2<\/tt>, <tt>tan(x)<\/tt> becomes infinite, so both <tt>sin(tan(x))<\/tt> and <tt>F(x)<\/tt> oscillate infinitely often.<\/p>\n<p>Here is a link to a 45-second video of <tt>F(x)<\/tt> for <tt>n = 200<\/tt> evenly spaced values of <tt>x<\/tt> between <tt>-2pi<\/tt> and <tt>2pi<\/tt>. If we increase <tt>n<\/tt>, we would see more of the oscillations at the singularities. The sound comes from resampling a single note played by a trumpet.<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/tansin.mp4\">https:\/\/blogs.mathworks.com\/cleve\/files\/tansin.mp4<\/a>\n<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/tansin.png\" alt=\"\"> <\/p>\n<h4>Van der Pol<a name=\"7e32cd92-b6a9-46ec-aa1c-980089ff6833\"><\/a>\n<\/h4>\n<p>Quoting <a href=\"https:\/\/en.wikipedia.org\/wiki\/Van_der_Pol_oscillator\" target=\"_blank\">Wikipedia<\/a>.<\/p>\n<pre>  In the study of dynamical systems, the van der Pol oscillator\n  (named for Dutch physicist Balthasar van der Pol) is a\n  non-conservative, oscillating system with non-linear damping.<\/pre>\n<p>In this video, we see -- and hear -- the numerical solution, computed by <tt>ode45<\/tt>, of the Van der Pol equation at <tt>n<\/tt> points between <tt>t = 0<\/tt> and <tt>t = 20<\/tt>. The damping parameter is <tt>mu = 2<\/tt>.<\/p>\n<p>The sound comes from a recording of a student practicing a piano. In this case we have scales spanning four octaves, so no resampling is required.<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/vanderpol.mp4\">https:\/\/blogs.mathworks.com\/cleve\/files\/vanderpol.mp4<\/a>\n<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/vanderpol.png\" alt=\"\"> <\/p>\n<h4>Blackjack<a name=\"e791b241-63f8-4987-a4d3-4451b5b8f39d\"><\/a>\n<\/h4>\n<p>The card game Blackjack is another example that I use frequently; see <a href=\"https:\/\/www.mathworks.com\/company\/technical-articles\/simulating-blackjack-with-matlab.html\">\"Simulating Blackjack\"<\/a> and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4404-blackjack\">MATLAB file exchange<\/a>. The video shows a simulation of a few hundred hands, starting each hand with a $10 bet. The sound comes from a piano playing a C-major scale. Again, resampling is not necessary.<\/p>\n<p>The emerging histograms in the video, and the static outcome in the final frame, show the percentages observed for each of ten possible outcomes. The histograms can be compared with those in the <a href=\"https:\/\/www.mathworks.com\/company\/technical-articles\/simulating-blackjack-with-matlab.html\">\"Simulating Blackjack\"<\/a> article that describes the use of the Parallel Computing Toolbox to play 10,000 hands with these results.<\/p>\n<pre class=\"codeinput\">bjpercent\n<\/pre>\n<pre class=\"codeoutput\">    \"   Push, player and dealer have equal totals not exceeding 21             \"\n    \"   Win, player's total is greater than dealer's and does not exceed 21    \"\n    \"   Lose, dealer's total is greater than player's and does not exceed 21   \"\n    \"   Blackjack, player has 21 with two cards, dealer does not               \"\n    \"                                                                          \"\n    \"   $-40     0.03     Split and double, lose both                          \"\n    \"   $-30     0.17     Split and double, lose one                           \"\n    \"   $-20     4.71     Split or double, lose                                \"\n    \"   $-10    42.94     Lose                                                 \"\n    \"   $  0     9.06     Push                                                 \"\n    \"   $+10    32.13     Win                                                  \"\n    \"   $+15     4.44     Blackjack                                            \"\n    \"   $+20     6.19     Split or double, win                                 \"\n    \"   $+30     0.28     Split and double, win one                            \"\n    \"   $+40     0.06     Split and double, win both                           \"\n<\/pre>\n<p>(Homework. This simulation happens to have three hands with the very rare swings of more than $20. Find the hand numbers where such rare events occur. Post your answers in the comments. Hint: watch the emerging histograms.)<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/blackjack.mp4\">https:\/\/blogs.mathworks.com\/cleve\/files\/blackjack.mp4<\/a>\n<\/p>\n<p>\n<b>+ + + + + + + + + + + + + + + + + + + + + + + + + + + +<\/b>\n<\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/blackjack.png\" alt=\"\"> <\/p>\n<p>\n<img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/bj100K.png\" alt=\"\"> <\/p>\n<script language=\"JavaScript\"> <!-- \n    function grabCode_ef605386b1c14b589860f69f1d2032a2() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='ef605386b1c14b589860f69f1d2032a2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ef605386b1c14b589860f69f1d2032a2';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2024 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('<pre>\\n');\n        d.write(code_string);\n\n        \/\/ Add copyright line at the bottom if specified.\n        if (copyright.length > 0) {\n            d.writeln('');\n            d.writeln('%%');\n            if (copyright.length > 0) {\n                d.writeln('% _' + copyright + '_');\n            }\n        }\n\n        d.write('<\/pre>\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\">\n<br>\n<a href=\"javascript:grabCode_ef605386b1c14b589860f69f1d2032a2()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \n      the MATLAB code <noscript>(requires JavaScript)<\/noscript>\n<\/span><\/a>\n<br>\n<br>\n      Published with MATLAB&reg; R2024b<br>\n<\/p>\n<\/div>\n<!--\nef605386b1c14b589860f69f1d2032a2 ##### SOURCE BEGIN #####\n%% Lights, Camera, Action REPLACE_WITH_DASH_DASH and Sound\n% I am very excited to incorporate sound in animations.\n% This blog post has five examples with\n% links to videos enclosed in |'+ + + +'|.\n\n%% Mertens\n% I began making animations with sound while investigating the\n% <https:\/\/blogs.mathworks.com\/cleve\/2024\/09\/23\/redheffer-mertens-and-one-million-dollars\/\n% Mertens Conjecture>.\n% The <https:\/\/oeis.org\/A002321 OEIS> page \n% offers an opportunity to listen to the Mertens sequence, but\n% I wanted more control.\n%\n% The |resample| function is the Signal Processing Toolbox\n% resamples a signal at a fraction, |p\/q|, times the original sample rate.\n% The sound in this Mertens example is generated from a recording of piano\n% playing middle C that is resampled to simulate the other notes.\n%\n% Here is a link to a 40-second video of |M(n)| and |sqrt(n)|\n% for |n = 200|. \n% (Homework: How long would the video last if |n = 10^8|?\n% Post your answers in the comments.)\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/mertens.mp4>\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% Here is a static graphic, the last frame of the video.\n%\n% <<mertens.png>>\n%\n\n%% Lissajous\n% You may well have seen <https:\/\/en.wikipedia.org\/wiki\/Lissajous_curve \n% Lissajous curves> before, but have you ever\n% _listened_ to them?\n% They were first studied extensively by \n% <https:\/\/en.wikipedia.org\/wiki\/Jules_Antoine_Lissajous Jules Lissajous>,\n% a nineteenth-century French physicist.\n% There are many Lissajous curves similar to the following, where \n% the trajectory in the |x-y| plane is described by two functions of \n% a parameter |t|.\n%\n%   n = 200;\n%   t = 3*pi*(-1:2\/n:1);\n%   x = 5*cos(t) + 3*cos(5\/3*t);\n%   y = 5*sin(t) - sin(5\/3*t);\n%\n% This 40-second video employs sound produced by resampling a note\n% from a xylophone.\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/lissajous.mp4>\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% Here is the last frame.\n%\n% <<lissajous.png>>\n%\n\n%% tansin\n% I have used variations of\n% <https:\/\/blogs.mathworks.com\/cleve\/2012\/09\/24\/supremum this function>\n% many times.\n%\n%  F = @(x) tan(sin(x))+sin(tan(x));\n%\n% When |x| approaches an integer multiple of |pi|, both |sin(x)| and |tan(x)|\n% approach zero, so |F(x)| also approaches zero.  But when |x| approaches\n% an odd multiple of |pi\/2|, |tan(x)| becomes infinite, so both |sin(tan(x))| \n% and |F(x)| oscillate infinitely often.\n% \n% Here is a link to a 45-second video\n% of |F(x)| for |n = 200| evenly spaced values of |x| between |-2pi| and\n% |2pi|.\n% If we increase |n|, we would see more of the oscillations at the \n% singularities.  The sound comes from resampling a single note\n% played by a trumpet.\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/tansin.mp4>\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <<tansin.png>>\n%\n\n%% Van der Pol\n% Quoting \n% <https:\/\/en.wikipedia.org\/wiki\/Van_der_Pol_oscillator Wikipedia>.\n%\n%    In the study of dynamical systems, the van der Pol oscillator\n%    (named for Dutch physicist Balthasar van der Pol) is a \n%    non-conservative, oscillating system with non-linear damping.\n% \n% In this video, we see REPLACE_WITH_DASH_DASH and hear REPLACE_WITH_DASH_DASH the numerical solution, computed\n% by |ode45|, of the Van der Pol equation at |n| points between\n% |t = 0| and |t = 20|.  The damping parameter is |mu = 2|.\n%\n% The sound comes from a recording of a student practicing a piano.\n% In this case we have scales spanning four octaves, so no resampling\n% is required.\n%    \n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/vanderpol.mp4>\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <<vanderpol.png>>\n%\n\n%% Blackjack\n% The card game Blackjack is another example that I use frequently; see \n% <https:\/\/www.mathworks.com\/company\/technical-articles\/simulating-blackjack-with-matlab.html\n% \"Simulating Blackjack\"> and\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4404-blackjack\n% MATLAB file exchange>.\n% The video shows a simulation of a few hundred hands,\n% starting each hand with a $10 bet.\n% The sound comes from a piano playing a C-major scale.\n% Again, resampling is not necessary.\n%\n% The emerging histograms in the video, and the static outcome in\n% the final frame, show the percentages observed for each\n% of ten possible outcomes.\n% The histograms can be compared with those in the\n% <https:\/\/www.mathworks.com\/company\/technical-articles\/simulating-blackjack-with-matlab.html\n% \"Simulating Blackjack\">\n% article that describes the use of the  Parallel Computing Toolbox\n% to play 10,000 hands with these results.\n\n   bjpercent\n%%\n% (Homework.  This simulation happens to have three hands  with\n% the very rare swings of more than $20.  Find the hand numbers\n% where such rare events occur.  Post your answers in the comments.\n% Hint: watch the emerging histograms.)\n\n%%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/blackjack.mp4>\n%\n% *+ + + + + + + + + + + + + + + + + + + + + + + + + + + +* \n%\n% <<blackjack.png>>\n%\n% <<bj100K.png>>\n%\n##### SOURCE END ##### ef605386b1c14b589860f69f1d2032a2\n-->\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/lissajous.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction-->\n<p>I am very excited to incorporate sound in animations. This blog post has five examples with links to videos enclosed in <tt>'+ + + +'<\/tt>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2024\/11\/16\/lights-camera-action-and-sound\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":11904,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,5,23,22,39,51,1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/11916"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=11916"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/11916\/revisions"}],"predecessor-version":[{"id":11928,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/11916\/revisions\/11928"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/11904"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=11916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=11916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=11916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}