%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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; % Make sure that the saved image has dimensions in pixels that are a % multiple of 4. Many video encoders require that. % image size in pixels (aspect ratio 16/9) pixels_h = 640; % width pixels_v = 360; % height dpi = 256; % dots per inch f = 500; c = 343; omega = 2*pi*f; v = 120; % speed of sound source M = v / c; t0 = -0.02; dt = .00005; % sec % create spatial grid X = linspace( -3, 3, 500 ); Y = linspace( -3, 3, 500 ); [ x, y ] = meshgrid( X, Y ); z = 0; % create a directory for the snapshots dos( 'md 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 pixels_h pixels_v ] ); %%% This loop calculates T snapshots of the sound field of a moving %%% %%% source and writes them to disc. The equation numbers refer to the %%% %%% book. %%% index = 1; ts = t0 : dt : abs( t0 ); for t = ts % Eq. (5.58) Delta = sqrt( ( x - v*t ).^2 + ( y.^2 + z.^2 ) .* ( 1 - M^2 ) ); % Eq. (5.57) tau = ( M .* ( x - v*t ) + Delta ) / ( c * ( 1 - M^2 ) ); % Eq. (5.60) s = 1 / (4*pi) .* exp( 1i .* omega .* ( t - tau ) ) ./ Delta; imagesc( X, Y, real( s ), [ -.1 .1 ] ); set( gca, 'YDir','normal' ); axis square; hold on; % plot source trajectory plot( [ -3 3 ], [ 0 0 ], 'k:' ) hold off; xlabel( '$x$ (m)', 'interpreter', 'latex' ); ylabel( '$y$ (m)', 'interpreter', 'latex' ); % save snapshot file_name = sprintf( 'images/moving_source_%03d.png', index ); fprintf( 'Saving file %s of %d ...\n', file_name, length( ts ) ); image = getframe( h ); imwrite( image.cdata, file_name, 'png', ... 'ResolutionUnit', 'meter', 'XResolution', dpi, 'YResolution', dpi ); index = index + 1; end