128343285 matlab coding for wave

16
Appendix A Matlab code examples In this appendix, various simple code fragments are provided. All can be viewed as prototypes for physical modeling sound synthesis. The coding style reflects something of a compromise between efficiency, on the one hand, and brevity and intelligibility, on the other. The choice of Matlab as a programming environment definitely reflects the latter sensibility, though the use of Matlab as an actual synthesis engine is not recommended. Some of these examples make use of constructs and features which need not appear in a code fragment intended for synthesis, including various calls to plotting functions, as well as the demonstration of energy conservation in some cases. It should be clear, in all cases, which elements of these examples may be neglected in an actual implementation. For the sake of brevity, these examples are too crude for actual synthesis purposes, but many features, discussed at various points in the texts and exercises, may be added. A.1 The simple harmonic oscillator % matlab script sho.m % finite difference scheme for simple harmonic oscillator %%%%%% begin global parameters SR = 44100; % sample rate (Hz) f0 = 1000; % fundamental frequency (Hz) TF = 1.0; % duration of simulation (s) u0 = 0.3; % initial displacement v0 = 0.0; % initial velocity %%%%%% end global parameters % check that stability condition is satisfied if(SR<=pi*f0) error(’Stability condition violated’); end Numerical Sound Synthesis: Finite Difference Schemes and Simulation in Musical Acoustics Stefan Bilbao © 2009 John Wiley & Sons, Ltd. ISBN: 978-0-470-51046-9

Upload: passme369

Post on 27-Dec-2015

53 views

Category:

Documents


4 download

DESCRIPTION

matlab

TRANSCRIPT

Page 1: 128343285 Matlab Coding for Wave

Appendix A

Matlab code examples

In this appendix, various simple code fragments are provided. All can be viewed as prototypes forphysical modeling sound synthesis. The coding style reflects something of a compromise betweenefficiency, on the one hand, and brevity and intelligibility, on the other. The choice of Matlab asa programming environment definitely reflects the latter sensibility, though the use of Matlab asan actual synthesis engine is not recommended. Some of these examples make use of constructsand features which need not appear in a code fragment intended for synthesis, including variouscalls to plotting functions, as well as the demonstration of energy conservation in some cases.It should be clear, in all cases, which elements of these examples may be neglected in an actualimplementation. For the sake of brevity, these examples are too crude for actual synthesis purposes,but many features, discussed at various points in the texts and exercises, may be added.

A.1 The simple harmonic oscillator

% matlab script sho.m% finite difference scheme for simple harmonic oscillator

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 1000; % fundamental frequency (Hz)TF = 1.0; % duration of simulation (s)u0 = 0.3; % initial displacementv0 = 0.0; % initial velocity

%%%%%% end global parameters

% check that stability condition is satisfied

if(SR<=pi*f0)error(’Stability condition violated’);

end

N ume ric al Sound Sy nthe sis: Finite Diffe re nc e Sc he m e s and Simulation in M usic al Ac oustic s Stefan Bilbao© 2009 John Wiley & Sons, Ltd. ISBN: 978-0-470-51046-9

Page 2: 128343285 Matlab Coding for Wave

392 APPENDIX A

% derived parameters

k = 1/SR; % time stepcoef = 2-k^2*(2*pi*f0)^2; % scheme update coefficientNF = floor(TF*SR); % duration of simulation (samples)

% initialize state of scheme

u1 = u0+k*v0; % last value of time seriesu2 = u0; % one before last value of time series

% initialize readout

out = zeros(NF,1); out(1) = u2; out(2) = u1;

%%%%%% start main loop

for n=3:NFu=coef*u1-u2; % difference scheme calculationout(n) = u; % read value to output vectoru2 = u1; u1 = u; % update state of difference scheme

end

%%%%%% end main loop

% play sound

soundsc(out,SR);

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’); title(’SHO: Scheme Output’); axis tight

A.2 Hammer collision with mass–spring system

% matlab script hammermass.m% hammer collision with a mass-spring system

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)xH0 = -0.001; vH0 = 2; % initial conditions of hammerTF = 0.05; % duration of simulation (s)w0 = 2000; % angular frequency of mass-spring systemMR = 10; % hammer/target mass ratiowH = 1000; % stiffness parameter for hammeralpha = 2; % hammer stiffness nonlinearity exponent

%%%%%% end global parameters

% derived parameters

k = 1/SR;NF = floor(TF*SR);

Page 3: 128343285 Matlab Coding for Wave

APPENDIX A 393

% initialization

uH2 = xH0; uH1 = xH0+k*vH0; % hammeru2 = 0; u1 = 0; % mass-spring systemout = zeros(NF,1); f = zeros(NF,1);out(1) = u2; out(2) = u1;

%%%%%% start main loop

for n=3:NFif(uH1>u1)

f(n-1) = wH^(1+alpha)*(uH1-u1)^alpha;else f(n-1) = 0;enduH = 2*uH1-uH2-k^2*f(n-1);u = 2*u1-u2-w0^2*k^2*u1+MR*k^2*f(n-1);out(n) = u;u2 = u1; u1 = u;uH2 = uH1; uH1 = uH;

end

%%%%%% end main loop

% plots of displacement of target mass and force

subplot(2,1,1)plot([0:NF-1]*k, out, ’k’); title(’Position of Target Mass’); xlabel(’t’);axis tightsubplot(2,1,2)plot([0:NF-1]*k, f, ’k’); title(’Hammer Force/Mass’); xlabel(’t’);axis tight

A.3 Bowed mass–spring system

% matlab script bowmass.m% finite difference scheme for a bowed mass-spring system% soft friction characteristic w/iterative Newton-Raphson method

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 200; % oscillator frequency (Hz)FB = 500; % bow force/mass (m/s^2)TF = 0.1; % simulation duration (s)vB = 0.2; % bow velocity (m/s)sig = 100; % friction law free parameter (1/m^2)tol = 1e-4; % tolerance for Newton-Raphson method

%%%%%% end global parameters

% derived parameters

NF = floor(TF*SR);k = 1/SR;A = exp(1/2)*sqrt(2*sig);

Page 4: 128343285 Matlab Coding for Wave

394 APPENDIX A

% initialize time series/iterative method

u = zeros(NF,1); f = zeros(NF,1); vr = zeros(NF,1);qlast = 0;

% time step restrictions

if(k>min(1/(pi*f0),exp(1)/(FB*sqrt(2*sig))))error(’Time step too large’);

end

%%%%%% start main loop

for n=3:NF% Newton-Raphson method to determine relative velocityb = (2*pi*f0)^2*u(n-1)-(2/k^2)*(u(n-1)-u(n-2))+(2/k)*vB;eps = 1;while eps>tol

q=qlast-(FB*A*qlast*exp(-sig*qlast^2)+2*qlast/k+b)/...(FB*A*(1-2*sig*qlast^2)*exp(-sig*qlast^2)+2/k);

eps = abs(q-qlast);qlast = q;

end% update position of mass and relative bow velocityu(n) = 2*k*(q+vB)+u(n-2); vr(n-1) = q;

end

%%%%%% end main loop

% plot mass displacement and relative bow velocity

tax = [0:NF-1]*k;subplot(2,1,1); plot(tax, u, ’k’);title(’Displacement of Mass’); xlabel(’time (s)’);subplot(2,1,2); plot(tax, vr, ’k’);title(’Relative Bow Velocity’); xlabel(’time (s)’);

A.4 The 1D wave equation: finite difference scheme

% matlab script waveeq1dfd.m% finite difference scheme for the 1D wave equation% fixed boundary conditions% raised cosine initial conditions

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 330; % fundamental frequency (Hz)TF = 1; % duration of simulation (s)ctr = 0.7; wid = 0.1; % center location/width of excitationu0 = 1; v0 = 0; % maximum initial displacement/velocityrp = 0.3; % position of readout (0-1)lambda = 1; % Courant number

%%%%%% end global parameters

Page 5: 128343285 Matlab Coding for Wave

APPENDIX A 395

% begin derived parameters

gamma = 2*f0; % wave equation free parameterk = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)

% stability condition/scheme parameters

h = gamma*k/lambda; N = floor(1/h); h = 1/N; lambda = gamma*k/h;s0 = 2*(1-lambda^2); s1 = lambda^2;

% readout interpolation parameters

rp_int = 1+floor(N*rp); % rounded grid index for readoutrp_frac = 1+rp/h-rp_int; % fractional part of readout location

% create raised cosine

xax = [0:N]’*h;ind = sign(max(-(xax-ctr-wid/2).*(xax-ctr+wid/2),0));rc = 0.5*ind.*(1+cos(2*pi*(xax-ctr)/wid));

% initialize grid functions and output

u2 = u0*rc; u1 = (u0+k*v0)*rc; u = zeros(N+1,1); out = zeros(NF,1);

%%%%%% start main loop

for n=3:NFu(2:N) = -u2(2:N)+s0*u1(2:N)+s1*(u1(1:N-1)+u1(3:N+1)); % scheme calculationout(n) = (1-rp_frac)*u(rp_int)+rp_frac*u(rp_int+1); % readoutu2 = u1; u1 = u; % update of grid variables

end

%%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’); title(’1D Wave Equation: FD Output’); axis tight

% play sound

soundsc(out,SR);

A.5 The 1D wave equation: digital waveguide synthesis% matlab script waveeq1ddw.m% digital waveguide method for the 1D wave equation% fixed boundary conditions% raised cosine initial conditions

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 441; % fundamental frequency (Hz)

Page 6: 128343285 Matlab Coding for Wave

396 APPENDIX A

TF = 1; % duration of simulation (s)ctr = 0.7; wid = 0.1; % center location/width of excitationu0 = 1; v0 = 0; % maximum initial displacement/velocityrp = 0.3; % position of readout (0-1)

%%%%%% end global parameters

% begin derived parameters

k = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)N = floor(0.5*SR/f0); % length of delay linesrp_int = 1+floor(N*rp); % rounded grid index for readoutrp_frac = 1+rp*N-rp_int; % fractional part of readout location

% initialize delay lines and output

wleft = zeros(N,1); wright = zeros(N,1);out = zeros(NF,1);

% create raised cosine and integral

xax = ([1:N]’-1/2)/N;ind = sign(max(-(xax-ctr-wid/2).*(xax-ctr+wid/2),0));rc = 0.5*ind.*(1+cos(2*pi*(xax-ctr)/wid));rcint = zeros(N,1);for qq=2:N

rcint(qq) = rcint(qq-1)+rc(qq)/N;end

% set initial conditions

wleft = 0.5*(u0*rc+v0*rcint/(2*f0));wright = 0.5*(u0*rc-v0*rcint/(2*f0));

%%%%%% start main loop

for n=3:NFtemp1 = wright(N); temp2 = wleft(1);wright(2:N) = wright(1:N-1); wleft(1:N-1) = wleft(2:N);wright(1) = -temp2; wleft(N) = -temp1;% readoutout(n) = (1-rp_frac)*(wleft(rp_int)+wright(rp_int))...

+rp_frac*(wleft(rp_int+1)+wright(rp_int+1));end

%%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’);

title(’1D Wave Equation: Digital Waveguide Synthesis Output’);axis tight

% play sound

soundsc(out,SR);

Page 7: 128343285 Matlab Coding for Wave

APPENDIX A 397

A.6 The 1D wave equation: modal synthesis

% matlab script waveeq1dmod.m% modal synthesis method for the 1D wave equation% fixed boundary conditions% raised cosine initial conditions

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 441; % fundamental frequency (Hz)TF = 1; % duration of simulation (s)ctr = 0.7; wid = 0.1; % center location/width of excitationu0 = 1; v0 = 0; % maximum initial displacement/velocityrp = 0.3; % position of readout (0-1)

%%%%%% end global parameters

% begin derived parameters/temporary storage

k = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)N = floor(0.5*SR/f0); % number of modestemp = 2*pi*[1:N]’*f0/SR; coeff = 2*cos(temp); outexp = sin([1:N]*pi*rp);

% initialize grid functions and output

U = zeros(N,1); U1 = zeros(N,1); U2 = zeros(N,1);out2 = zeros(NF,1);

% create raised cosine and find Fourier coefficients

xax = [0:N-1]’/N;ind = sign(max(-(xax-ctr-wid/2).*(xax-ctr+wid/2),0));rc = 0.5*ind.*(1+cos(2*pi*(xax-ctr)/wid));rcfs = -imag(fft([rc; zeros(N,1)])); rcfs = 2*rcfs(2:N+1)/N;

% set initial conditions

U2(1:N) = u0*rcfs;U1(1:N) = (u0*cos(temp)+v0*sin(temp)./(2*pi*[1:N]’*f0)).*rcfs;

%%%%%% start main loop

for n=3:NFU = -U2+coeff.*U1; % scheme calculationout(n) = outexp*U; % readoutU2 = U1; U1 = U; % update of modal weights

end

%%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’); title(’1D Wave Equation: Modal Synthesis Output’);axis tight

Page 8: 128343285 Matlab Coding for Wave

398 APPENDIX A

% play sound

soundsc(out,SR);

A.7 The ideal bar

% matlab script idealbarfd.m% finite difference scheme for the ideal bar equation% clamped/pivoting boundary conditions% raised cosine initial conditions

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)K = 10; % stiffness parameterTF = 1; % duration of simulation (s)ctr = 0.7; wid = 0.1; % center location/width of excitationu0 = 1; v0 = 0; % maximum initial displacement/velocitymu = 0.5; % scheme free parameterrp = 0.85; % position of readout (0-1)bc = [2 2]; % boundary condition type,

[left right] with% 1: clamped, 2: pivoting

%%%%%% end global parameters

% begin derived parameters

k = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)

% stability condition/scheme parameters

h = sqrt(K*k/mu); N = floor(1/h); h = 1/N; mu = K*k/h^2;s0 = 2*(1-3*mu^2); s1 = 4*mu^2; s2 = -mu^2;

% readout interpolation parameters

rp_int = 1+floor(N*rp); % rounded grid index for readoutrp_frac = 1+rp/h-rp_int; % fractional part of readout location

% create raised cosine

xax = [0:N]’*h;ind = sign(max(-(xax-ctr-wid/2).*(xax-ctr+wid/2),0));rc = 0.5*ind.*(1+cos(2*pi*(xax-ctr)/wid));

% initialize grid functions and output

u2 = u0*rc; u1 = (u0+k*v0)*rc; u = zeros(N+1,1); out = zeros(NF,1);

%%%%%% start main loop

for n=3:NF% scheme calculation (interior)u(3:N-1) = -u2(3:N-1)+s0*u1(3:N-1)+s1*(u1(2:N-2)+u1(4:N))...

Page 9: 128343285 Matlab Coding for Wave

APPENDIX A 399

+s2*(u1(1:N-3)+u1(5:N+1));% calculations at boundary pointsif(bc(1)==2)

u(2) = -u2(2)+(s0-s2)*u1(2)+s1*u1(3)+s2*u1(4);endif(bc(2)==2)

u(N) = -u2(N)+(s0-s2)*u1(N)+s1*u1(N-1)+s2*u1(N-2);endout(n) = (1-rp_frac)*u(rp_int)+rp_frac*u(rp_int+1); % readoutu2 = u1; u1 = u; % update

end

%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’); title(’Ideal Bar Equation: FD Output’);axis tight

% play sound

soundsc(out,SR);

A.8 The stiff string

% matlab script ssfd.m% finite difference scheme for the stiff string% clamped boundary conditions% raised cosine initial conditions% stereo output% implicit scheme: matrix update form% two-parameter frequency-dependent loss

%%%%%% begin global parameters

SR = 44100; % sample rate(Hz)B = 0.001; % inharmonicity parameter (>0)f0 = 100; % fundamental(Hz)TF = 2; % duration of simulation(s)ctr = 0.1; wid = 0.05; % center location/width of excitationu0 = 1; v0 = 0; % maximum initial displacement/velocityrp = [0.3 0.7]; % positions of readout(0-1)loss = [100, 10; 1000, 8]; % loss [freq.(Hz), T60(s), freq.(Hz), T60(s)]theta = 1.0; % implicit scheme free parameter (>0.5)

%%%%%% end global parameters

% begin derived parameters

k = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)gamma = 2*f0; K = sqrt(B)*(gamma/pi); % set parameters

% stability conditions

Page 10: 128343285 Matlab Coding for Wave

400 APPENDIX A

h = sqrt((gamma^2*k^2+sqrt(gamma^4*k^4+16*K^2*k^2*(2*theta-1)))/(2*(2*theta-1)));

N = floor(1/h); h = 1/N; mu = K*k/h^2; lambda = gamma*k/h;

% readout interpolation parameters

rp_int = 1+floor(N*rp); % rounded grid index for readoutrp_frac = 1+rp/h-rp_int; % fractional part of readout location

% set scheme loss parameters

zeta1 = (-gamma^2+sqrt(gamma^4+4*K^2*(2*pi*loss(1,1))^2))/(2*K^2);zeta2 = (-gamma^2+sqrt(gamma^4+4*K^2*(2*pi*loss(2,1))^2))/(2*K^2);sig0 = 6*log(10)*(-zeta2/loss(1,2)+zeta1/loss(2,2))/(zeta1-zeta2);sig1 = 6*log(10)*(1/loss(1,2)-1/loss(2,2))/(zeta1-zeta2);

% create update matrices

M = sparse(toeplitz([theta (1-theta)/2 zeros(1,N-3)]));A = M+sparse(toeplitz([sig1*k/(h^2)+sig0*k/2 -sig1*k/(2*h^2) zeros(1,N-3)]));C = M+sparse(toeplitz([-sig1*k/(h^2)-sig0*k/2 sig1*k/(2*h^2) zeros(1,N-3)]));B = 2*M+sparse(toeplitz([-2*lambda^2-6*mu^2 lambda^2+4*mu^2 -mu^2...

zeros(1,N-4)]));

% create raised cosine

xax = [1:N-1]’*h;ind = sign(max(-(xax-ctr-wid/2).*(xax-ctr+wid/2),0));rc = 0.5*ind.*(1+cos(2*pi*(xax-ctr)/wid));

% set initial conditions

u2 = u0*rc; u1 = (u0+k*v0)*rc; u = zeros(N+1,1); out = zeros(NF,2);

%%%%%% start main loop

for n=3:NFu = A\(B*u1-C*u2);out(n,:) = (1-rp_frac).*u(rp_int)’+rp_frac.*u(rp_int+1)’; % readoutu2 = u1; u1 = u; % update

end

%%%%%% end main loop

% plot output waveform

subplot(2,1,1); plot([0:NF-1]*k, out(:,1), ’k’);xlabel(’t’); ylabel(’u’); title(’Stiff String Equation: FD Output (left)’);subplot(2,1,2); plot([0:NF-1]*k, out(:,2), ’k’);xlabel(’t’); ylabel(’u’); title(’Stiff String Equation: FD Output (right)’);axis tight

% play sound

soundsc(out,SR);

Page 11: 128343285 Matlab Coding for Wave

APPENDIX A 401

A.9 The Kirchhoff–Carrier equation

% matlab script kcfd.m% finite difference scheme for the Kirchhoff-Carrier equation% fixed boundary conditions% triangular initial conditions

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)f0 = 200; % fundamental frequency (Hz)alpha = 10; % nonlinear string parameterTF = 0.03; % duration of simulation (s)ctr = 0.5; % center location of

excitation (0-1)u0 = 0.05; % maximum initial displacementrp = 0.5; % position of readout (0-1)lambda = 0.7; % Courant number

%%%%%% end global parameters

% begin derived parameters

gamma = 2*f0; % wave equation free parameterk = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)

% stability condition

h = gamma*k/lambda; N = floor(1/h); h = 1/N; lambda = gamma*k/h;

% readout interpolation parameters

rp_int = 1+floor(N*rp); % rounded grid index for readoutrp_frac = 1+rp/h-rp_int; % fractional part of readout location

% create triangular function

xax = [0:N]’*h;tri = min(xax/ctr-1,0)+1+min((1-xax)/(1-ctr)-1,0);

% initialize grid functions and output

u2 = u0*tri; u1 = u2; u = zeros(N+1,1); out = zeros(NF,1);

%%%%%% start main loop

for n=1:NF% calculate nonlinearity gu1x = (u1(2:N+1)-u1(1:N))/h; u1xx = (u1x(2:N)-u1x(1:N-1))/h;g = (1+0.5*alpha^2*h*sum(u1x.*u1x))/...

(1+0.25*alpha^2*k^2*gamma^2*h*sum(u1xx.*u1xx));% scheme updateu(2:N) = 2*u1(2:N)-u2(2:N)+g*gamma^2*k^2*u1xx(1:N-1); % calculationout(n) = (1-rp_frac)*u(rp_int)+rp_frac*u(rp_int+1); % readout

Page 12: 128343285 Matlab Coding for Wave

402 APPENDIX A

u2 = u1; u1 = u; % updateend

%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’);xlabel(’t’); ylabel(’u’); title(’Kirchhoff-Carrier Equation: FD Output’);axis tight

A.10 Vocal synthesis

% matlab script vocalfd.m% finite difference vocal tract simulation% radiation loss included% simple glottal source waveform% static pitch

%%%%%% begin global parameters

SR = 44100; % sample rate (Hz)L = 0.17; % tract length (m)S0 = 0.00025; % vocal tract surface area, left end (m^2)c = 340; % wave speed (m/s)f0 = 120; % fundamental frequency (Hz)TF = 1; % simulation duration (s)% vocal tract profile, non-dimensional [pos S] pairs

% /E/S = [0 1;0.09 0.4;0.11 2.4;0.24 2.4;0.26 3.2;0.29 3.2;0.32 4.2;...0.41 4.2;0.47 3.2;0.59 1.8;0.65 1.6;0.71 1.6;0.74 1;0.76 0.8;...0.82 0.8;0.88 2;0.91 2;0.94 3.2;1 3.2];

% /A/% S = [0 1;0.03 0.60;0.09 0.4;0.12 1.6;0.18 0.6;0.29 0.2;0.35 0.4;...% 0.41 0.8;0.47 1;0.50 0.6;0.59 2;0.65 3.2;0.85 3.2;0.94 2;1 2];

%%%%% end global parameters

% begin derived parameters

k = 1/SR; % time stepNF = floor(TF*SR); % sample durationgamma = c/L;

% stability condition/scheme parameters

h = gamma*k; N = floor(1/h); h = 1/N; lambda = gamma*k/h;S = interp1(S(:,1),S(:,2),[0:h:1])’; % interpolate vocal tract profilealf = 2.0881*L*sqrt(1/(S0*S(N+1))); % radiation parameterbet = 0.7407/gamma; % radiation parameterSav = [S(1); 0.25*(S(3:N+1)+2*S(2:N)+S(1:N-1)); S(N+1)];Sr = 1.5*S(N+1)-0.5*S(N);sr = 0.5*lambda^2*((S(2:N)+S(3:N+1))./Sav(2:N));sl = 0.5*lambda^2*((S(2:N)+S(1:N-1))./Sav(2:N));s0 = 2*(1-lambda^2);

Page 13: 128343285 Matlab Coding for Wave

APPENDIX A 403

q1 = alf*gamma^2*k^2*Sr/(Sav(N+1)*h); q2 = bet*gamma^2*k*Sr/(Sav(N+1)*h);r1 = 2*lambda^2/(1+q1+q2); r2 = -(1+q1-q2)/(1+q1+q2);g1 = -(k^2*gamma^2/h/S(1))*(3*S(1)-S(2));

% initialize grid functions and output, generate glottal waveform

Psi = zeros(N+1,1); Psi1 = zeros(N+1,1); Psi2 = zeros(N+1,1);uin = sin(2*pi*[0:NF-1]*k*f0); uin = 0.5*(uin+abs(uin));out = zeros(NF,1);

%%%%%% begin main loop

for n=1:NF;Psi(2:N) = s0*Psi1(2:N)+sl.*Psi1(1:N-1)+sr.*Psi1(3:N+1)-Psi2(2:N);Psi(N+1) = r1*Psi1(N)+r2*Psi2(N+1);Psi(1) = s0*Psi1(1)+2*lambda^2*Psi1(2)-Psi2(1)+g1*uin(n);out(n) = SR*(Psi(N+1)-Psi1(N+1));Psi2 = Psi1; Psi1 = Psi;

end

%%%%%% end main loop

% plot vocal tract profile and output spectrum

subplot(2,1,1); plot([0:h:1], sqrt(S),’k’, [0:h:1], -sqrt(S),’k’)title(’Vocal Tract Profile’); xlabel(’x’); ylabel(’sqrt(S)’);subplot(2,1,2); plot([0:NF-1]*SR/NF, 10*log10(abs(fft(out))), ’k’);title(’Output Spectrum’); xlabel(’f’);ylabel(’pressure (dB)’);

% play sound

soundsc(out, SR);

A.11 The 2D wave equation

% matlab script waveeq2dloss.m% finite difference scheme for the 2D wave equation with loss% fixed boundary conditions% raised cosine initial conditions% bilinear interpolation

%%%%%% begin global parameters

SR = 16000; % sample rate(Hz)gamma = 200 % wave speed (1/s)T60 = 8; % 60 dB decay time (s)epsilon = 1.3; % domain aspect ratioTF = 2; % duration of simulation(s)ctr = [0.3 0.5]; wid = 0.15; % center location/width of excitationu0 = 0; v0 = 1; % maximum initial displacement/velocityrp = [0.5 0.6]; % position of readout([0-1,0-1])lambda = 1/sqrt(2); % Courant number

%%%%%% end global parameters

Page 14: 128343285 Matlab Coding for Wave

404 APPENDIX A

% begin derived parameters

k = 1/SR; % time step

NF = floor(SR*TF); % duration of simulation (samples)sig0 = 6*log(10)/T60; % loss parameter

% stability condition/scheme parameters

h = gamma*k/lambda; % find grid spacingNx = floor(sqrt(epsilon)/h); % number of x-subdivisions of

spatial domain

Ny = floor(1/(sqrt(epsilon)*h)); % number of y-subdivisions ofspatial domain

h = sqrt(epsilon)/Nx; lambda = gamma*k/h; % reset Courant numbers0 = (2-4*lambda^2)/(1+sig0*k); s1 = lambda^2/(1+sig0*k);

t0 = -(1-sig0*k)/(1+sig0*k);

% readout interpolation parameters

rp_int = 1+floor([Nx Ny].*rp); rp_frac = 1+rp/h-rp_int;

% create 2D raised cosine

[X, Y] = meshgrid([0:Nx]*h, [0:Ny]*h); dist = sqrt((X-ctr(1)).^2+(Y-ctr(2)).^2);

ind = sign(max(-dist+wid/2,0)); rc = 0.5*ind’.*(1+cos(2*pi*dist’/wid));

% set initial conditions

u2 = u0*rc; u1 = (u0+k*v0)*rc; u = zeros(Nx+1,Ny+1); out = zeros(NF,2);

%%%%%% start main loop

for n=3:NFu(2:Nx,2:Ny) = s1*(u1(3:Nx+1,2:Ny)+u1(1:Nx-1,2:Ny)+u1(2:Nx,3:Ny+1)+...

u1(2:Nx,1:Ny-1))+s0*u1(2:Nx,2:Ny)+t0*u2(2:Nx,2:Ny);

out(n,:) = (1-rp_frac(1))*(1-rp_frac(2))*u(rp_int(1),rp_int(2))+...(1-rp_frac(1))*rp_frac(2)*u(rp_int(1),rp_int(2)+1)+...

rp_frac(1)*(1-rp_frac(2))*u(rp_int(1)+1,rp_int(2))+...rp_frac(1)*rp_frac(2)*u(rp_int(1)+1,rp_int(2)+1);

u2 = u1; u1 = u;

end

%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’); xlabel(’t’); ylabel(’u’);

title(’2D Wave Equation with Loss: FD Output’); axis tight

% play sound

soundsc(out,SR);

Page 15: 128343285 Matlab Coding for Wave

APPENDIX A 405

A.12 Thin plate

% matlab script plateloss.m% finite difference scheme for the thin plate equation with loss% simply supported boundary conditions% raised cosine initial conditions% vector/matrix update form% zeroth-order interpolation

%%%%%% begin global parameters

SR = 44100; % sample rate(Hz)K = 20; % plate stiffness parameter (1/s)T60 = 8; % 60 dB decay time (s)epsilon = 1.2; % domain aspect ratioTF = 2; % duration of simulation(s)ctr = [0.8 0.9]; wid = 0.3; % center location/width of excitationu0 = 0; v0 = 1; % maximum initial displacement/velocityrp = [0.05 0.7]; % position of readout([0-1,0-1])mu = 0.25; % scheme free parameter

%%%%%% end global parameters

% begin derived parameters

k = 1/SR; % time stepNF = floor(SR*TF); % duration of simulation (samples)sig0 = 6*log(10)/T60; % loss parameter

% stability condition/scheme parameters

h = sqrt(K*k/mu); % find grid spacingNx = floor(sqrt(epsilon)/h); % number of x-subdivisions of spatial domainNy = floor(1/(sqrt(epsilon)*h)); % number of y-subdivisions of spatial domainh = sqrt(epsilon)/Nx;ss = (Nx-1)*(Ny-1); % total grid size

% generate difference matrix/scheme matrices

Dxx = sparse(toeplitz([-2/h^2;1/h^2;zeros(Nx-3,1)]));Dyy = sparse(toeplitz([-2/h^2;1/h^2;zeros(Ny-3,1)]));D = kron(eye(Nx-1), Dyy)+kron(Dxx, eye(Ny-1)); DD = D*D;B = sparse((2*eye(ss)-K^2*k^2*DD)/(1+sig0*k));C = ((1-sig0*k)/(1+sig0*k))*sparse(eye(ss));

% readout interpolation parameters

rp_index = (Ny-1)*floor(rp(1)*Nx)+floor(rp(2)*Ny);

% create 2D raised cosine

[X, Y] = meshgrid([1:Nx-1]*h, [1:Ny-1]*h);dist = sqrt((X-ctr(1)*sqrt(epsilon)).^2+(Y-ctr(2)/sqrt(epsilon)).^2);ind = sign(max(-dist+wid/2,0)); rc = 0.5*ind.*(1+cos(2*pi*dist/wid));rc = reshape(rc, ss,1);

Page 16: 128343285 Matlab Coding for Wave

406 APPENDIX A

% set initial conditions/initialize output

u2 = u0*rc; u1 = (u0+k*v0)*rc; u = zeros(ss,1);out = zeros(NF,1);

%%%%%% start main loop

for n=3:NFu = B*u1-C*u2;u2 = u1; u1 = u;out(n) = u(rp_index);

end

%%%%%% end main loop

% plot output waveform

plot([0:NF-1]*k, out, ’k’); xlabel(’t’); ylabel(’u’);title(’Thin Plate Equation with Loss: FD Output’); axis tight

% play sound

soundsc(out,SR);