%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This file is part of a tutorial on animation of sound fields, which can % % be obtained from http://soundfieldsynthesis.org/other-resources/#5 % % Author: Jens Ahrens, 2012 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; format = 'gif'; % 'gif' or 'png' T = 24; % no of snapshots to calculate image_size = 380; % in pixels (refers only to the files that are saved) dpi = 256; % dots per inch f = 1000; % emitted frequency c = 343; omega = 2*pi*f; % create spatial grid X = linspace( -2, 2, 500 ); Y = linspace( -2, 2, 500 ); [ x, y ] = meshgrid( X, Y ); r = sqrt( x.^2 + y.^2 ); % dipole, Eq. (2.67), see also the erratum at http://soundfieldsynthesis.org/?page_id=84 S = - x ./ (4*pi) .* ( 1 ./ r + 1i .* omega/c ) .* exp( -1i .* omega/c .* r ) ./ r.^2; % create directory dos( 'mkdir images' ); h = figure; set( gca, 'FontName', 'Times' ); set( gca, 'FontSize', 10 ); set( gcf, 'Color' , [ 1, 1, 1 ] ); % white background % make sure that the saved image has specified size set( gcf, 'PaperUnits', 'inches', 'Position', [ 100 100 image_size image_size ] ); % initialize snapshots = zeros( image_size, image_size, 1, T, 'uint8' ); % this loop creates T snapshots of S at regular intervals covering one % period of S for t = 1 : T % rotate phase of S and plot imagesc( X, Y, real( S .* exp( 1i * t/T * 2*pi ) ), [ -2 2 ] ); set( gca, 'YDir','normal' ); axis square; xlabel( '$x$ (m)', 'interpreter', 'latex' ); ylabel( '$y$ (m)', 'interpreter', 'latex' ); % save snapshot image = getframe( h ); if ( strcmp( format, 'png' ) ) imwrite( image.cdata, sprintf( 'images\\dipole_%02d.png', t ), 'png', ... 'ResolutionUnit', 'meter', 'XResolution', dpi, 'YResolution', dpi ); elseif ( strcmp( format, 'gif' ) ) if ( t == 1 ) % get color map [ snapshots( :, :, 1, t ), map ] = rgb2ind( image.cdata, 256, 'nodither' ); else snapshots( :, :, 1, t ) = rgb2ind( image.cdata, map, 'nodither' ); end else error( 'Unknown format ''%s''.', format ); end end if ( strcmp( format, 'gif' ) ) % write gif-file with 50 ms between adjacent snapshots; loop forever imwrite( snapshots, map, 'images\dipole.gif', 'DelayTime', .05, 'LoopCount', inf ); end