Appendix C
%Note Pool
N = 600;
x = zeros(4,N+12);
x(:,1) = [0.1;.100000001;.0999999999;.10000001];
for dd=1:4
for n=1:N+11
    a = 4;
    x(dd,n+1) = a*x(dd,n)*(1-x(dd,n));
end
x(dd,:)=ceil(13.*x(dd,:))-1;
  for n=1:N
   if x(dd,n)<1
       x(dd,n)=-400;
   else
   end
  end
end

%Dynamics
vol=zeros(4,N);
xx = zeros(4,101);
for dd=1:4
 for n=1:N
     if n<N*1/2
     a = 2.9+ 1.1*(n/(N*1/2));
     elseif n<N*3/4
     a=4-.5*(n-1/2*N)/(N/4);         
     else
     a=3.5-3.5*(n-3/4*N)/(N/4);
     end
     xx(:,1) = [0.1;.100000001;.0999999999;.10000001];
     for z=1:100   
     xx(dd,z+1)=a*xx(dd,z)*(1-xx(dd,z));
     end
     if abs(xx(dd,91)-xx(dd,99))<.001
        vol(dd,n)=max(xx(dd,90:100));
     else
        vol(dd,n)=xx(dd,100);
     end
 end
end 
vol=vol.^2;

%Melody and Rhythm
q=zeros(N+1, 2);
r=zeros(N+1, 2);
s=zeros(N+1, 2);
t=zeros(N+1, 2);
q(1,:)=[0 0];
r(1,:)=[.0000000000001 0];
s(1,:)=[0 .0000000000001];
t(1,:)=[.0000000000001 .000000000001];
f1=zeros(N, 2);
f2=zeros(N, 2);
f3=zeros(N, 2);
f4=zeros(N, 2);
a=1.4;
b=.3;

for n=1:N
   q(n+1,1)=a-q(n,1)^2+b*q(n,2);
   q(n+1,2)=q(n,1);
   r(n+1,1)=a-r(n,1)^2+b*r(n,2);
   r(n+1,2)=r(n,1);
   s(n+1,1)=a-s(n,1)^2+b*s(n,2);
   s(n+1,2)=s(n,1);
   t(n+1,1)=a-t(n,1)^2+b*t(n,2);
   t(n+1,2)=t(n,1);
%Voice 1   
    if q(n,1)<-5/3
        f1(n,1)=x(1,n);
    elseif q(n,1)<-4/3
        f1(n,1)=x(1,n+1);
    elseif q(n,1)<-1
        f1(n,1)=x(1,n+2);
    elseif q(n,1)<-2/3
        f1(n,1)=x(1,n+3);
    elseif q(n,1)<-1/3
        f1(n,1)=x(1,n+4);
    elseif q(n,1)<0
        f1(n,1)=x(1,n+5);
    elseif q(n,1)<1/3
        f1(n,1)=x(1,n+6);
    elseif q(n,1)<2/3
        f1(n,1)=x(1,n+7);
    elseif q(n,1)<1
        f1(n,1)=x(1,n+8);
    elseif q(n,1)<4/3
        f1(n,1)=x(1,n+9);
    elseif q(n,1)<5/3
        f1(n,1)=x(1,n+10);    
    else 
        f1(n,1)=x(1,n+11);
    end
%Voice 2    
    if r(n,1)<-5/3
        f2(n,1)=x(1,n);
    elseif r(n,1)<-4/3
        f2(n,1)=x(1,n+1);
    elseif r(n,1)<-1
        f2(n,1)=x(1,n+2);
    elseif r(n,1)<-2/3
        f2(n,1)=x(1,n+3);
    elseif r(n,1)<-1/3
        f2(n,1)=x(1,n+4);
    elseif r(n,1)<0
        f2(n,1)=x(1,n+5);
    elseif r(n,1)<1/3
        f2(n,1)=x(1,n+6);
    elseif r(n,1)<2/3
        f2(n,1)=x(1,n+7);
    elseif r(n,1)<1
        f2(n,1)=x(1,n+8);
    elseif r(n,1)<4/3
        f2(n,1)=x(1,n+9);
    elseif r(n,1)<5/3
        f2(n,1)=x(1,n+10);    
    else 
        f2(n,1)=x(1,n+11);
    end
 %Voice 3   
    if s(n,1)<-5/3
        f3(n,1)=x(1,n);
    elseif s(n,1)<-4/3
        f3(n,1)=x(1,n+1);
    elseif s(n,1)<-1
        f3(n,1)=x(1,n+2);
    elseif s(n,1)<-2/3
        f3(n,1)=x(1,n+3);
    elseif s(n,1)<-1/3
        f3(n,1)=x(1,n+4);
    elseif s(n,1)<0
        f3(n,1)=x(1,n+5);
    elseif s(n,1)<1/3
        f3(n,1)=x(1,n+6);
    elseif s(n,1)<2/3
        f3(n,1)=x(1,n+7);
    elseif s(n,1)<1
        f3(n,1)=x(1,n+8);
    elseif s(n,1)<4/3
        f3(n,1)=x(1,n+9);
    elseif s(n,1)<5/3
        f3(n,1)=x(1,n+10);    
    else 
        f3(n,1)=x(1,n+11);
    end
 %Voice 4   
    if t(n,1)<-5/3
        f4(n,1)=x(1,n);
    elseif t(n,1)<-4/3
        f4(n,1)=x(1,n+1);
    elseif t(n,1)<-1
        f4(n,1)=x(1,n+2);
    elseif t(n,1)<-2/3
        f4(n,1)=x(1,n+3);
    elseif t(n,1)<-1/3
        f4(n,1)=x(1,n+4);
    elseif t(n,1)<0
        f4(n,1)=x(1,n+5);
    elseif t(n,1)<1/3
        f4(n,1)=x(1,n+6);
    elseif t(n,1)<2/3
        f4(n,1)=x(1,n+7);
    elseif t(n,1)<1
        f4(n,1)=x(1,n+8);
    elseif t(n,1)<4/3
        f4(n,1)=x(1,n+9);
    elseif t(n,1)<5/3
        f4(n,1)=x(1,n+10);    
    else 
        f4(n,1)=x(1,n+11);
    end
 end

%Timbre
sig=10;                             %Chaotic Lorenz Attractor
rp=28;
b=8/3;
F = @(t, y) [sig*(y(2)-y(1)); rp*y(1)-y(2)-y(1)*y(3); y(1)*y(2)-b*y(3)];   
y1=[5;5;5];
y2=[5.001;5;5];
y3=[5;5.001;5];
y4=[5;5;5.001];
step=.02;
tspan=0:step:step*N;
[ts, ys]=ode45(F, tspan, y1);       
[tt, yt]=ode45(F, tspan, y2);
[tu, yu]=ode45(F, tspan, y3);       
[tv, yv]=ode45(F, tspan, y4);
figure;
plot3(ys(:,1),ys(:,2),ys(:,3));
plot3(yt(:,1),yt(:,2),yt(:,3));
plot3(yu(:,1),yu(:,2),yu(:,3));
plot3(yv(:,1),yv(:,2),yv(:,3));

%Sound Generator
fade=1;
hz=16000;
L=1/4*hz;             
FQ=440;
z1=zeros(L*N,1);
count=0;
q(:,2)=2*(ceil(q(:,2))+2);
for m=1:N
    tot=abs(ys(m,1))+abs(ys(m,2))+abs(ys(m,3));
    if q(m,2)>5;
      q(m,2)=8;
    else
    end

    if m<1/3*N
        fade=1000;
    else
        fade=1;
    end
    
    
    for u=1:((1/q(m,2))*hz);
        z1(count+u)=vol(1,m)*1/fade*1/4*(sin(u/hz*2*pi*2^(f1(m,1)/12)*FQ)+abs(ys(m,3))*.5/tot*sin(2*u/hz*2*pi*2^(f1(m,1)/12)*FQ)+abs(ys(m,1))*.4/tot*sin(3*u/hz*2*pi*2^(f1(m,1)/12)*FQ)+abs(ys(m,2))*.3/tot*sin(4*u/hz*2*pi*2^(f1(m,1)/12)*FQ));
    end
     count=count+((1/q(m,2))*hz);
end

FQ=220;
z2=zeros(L*N,1);
count=0;
r(:,2)=2*(ceil(r(:,2))+2);
for m=1:N
    tot=abs(yt(m,1))+abs(yt(m,2))+abs(yt(m,3));
    if r(m,2)>5;
      r(m,2)=8;
    else
    end

    if m<1/5*N
        fade=1000;
    else
        fade=1;
    end
      
    for u=1:((1/r(m,2))*hz);
        z2(count+u)=vol(2,m)*1/fade*1/3*(sin(u/hz*2*pi*2^(f2(m,1)/12)*FQ)+abs(yt(m,3))*.4/tot*sin(2*u/hz*2*pi*2^(f2(m,1)/12)*FQ)+abs(yt(m,1))*.4/tot*sin(3*u/hz*2*pi*2^(f2(m,1)/12)*FQ)+abs(yt(m,2))*.4/tot*sin(4*u/hz*2*pi*2^(f2(m,1)/12)*FQ));
    end
     count=count+((1/r(m,2))*hz);
end

FQ=110;
z3=zeros(L*N,1);
count=0;
s(:,2)=2*(ceil(s(:,2))+2);
for m=1:N
    tot=abs(yu(m,1))+abs(yu(m,2))+abs(yu(m,3));
    if s(m,2)>5;
      s(m,2)=8;
    else
    end 
    if m<0*N
        fade=1000;
    else
        fade=1;
    end
   
    
    for u=1:((1/s(m,2))*hz);
        z3(count+u)=vol(3,m)*(1/fade)*1/2*(sin(u/hz*2*pi*2^(f3(m,1)/12)*FQ)+abs(yu(m,3))*.4/tot*sin(2*u/hz*2*pi*2^(f3(m,1)/12)*FQ)+abs(yu(m,1))*.4/tot*sin(3*u/hz*2*pi*2^(f3(m,1)/12)*FQ)+abs(yu(m,2))*.3/tot*sin(4*u/hz*2*pi*2^(f3(m,1)/12)*FQ));
    end
     count=count+((1/s(m,2))*hz);
end

FQ=55;
z4=zeros(L*N,1);
count=0;
t(:,2)=2*(ceil(t(:,2))+2);
for m=1:N
    tot=abs(yv(m,1))+abs(yv(m,2))+abs(yv(m,3));
    if t(m,2)>5;
      t(m,2)=8;
    else
    end
    if m<2/5*N
        fade=1000;
    else
        fade=1;
    end
    for u=1:((1/t(m,2))*hz);
        z4(count+u)=vol(4,m)*1/fade*1*(sin(u/hz*2*pi*2^(f4(m,1)/12)*FQ)+abs(yv(m,3))*.3/tot*sin(2*u/hz*2*pi*2^(f4(m,1)/12)*FQ)+abs(yv(m,1))*.3/tot*sin(3*u/hz*2*pi*2^(f4(m,1)/12)*FQ)+abs(yv(m,2))*.3/tot*sin(4*u/hz*2*pi*2^(f4(m,1)/12)*FQ));
    end
     count=count+((1/t(m,2))*hz);
end

z=[(.75*z1+.4*z3+.5*z4)/4 (.25*z2+.6*z3+.5*z4)/4];    %Panning
z=.99*1/max(max(z)).*z;                               %Normalize Wav


wavwrite(z,hz,'quartetpiece');