clear all
close all

c = 1/pi; 
f = 440.0;
L = c*pi/f; 
nx = 4000;
xplot = linspace(0,L,nx);
xsound = (0:nx-1)/8192;

overtone = @(f,x) sin(f*x/c);    
for n=1:10
    figure
    plot(xplot,overtone(n*f,xplot),'r','LineWidth',2)
    xlim([0,L])
    ylim([-1.5,1.5])
    set(gca,'XTickLabel',{'0','L'},'XTick',[0 L],'YTickLabel',{},'YTick',zeros(1,0))
    switch n
        case 1
            title('Fundamental mode')
        case 2
            title('Mode n=2, 2nd overtone')
        case 3
            title('Mode n=3, 3rd overtone')
        otherwise
            title(sprintf('Mode n=%d, %dth overtone',n,n-1))
    end    
    sound(overtone(n*f,xsound))
    pause
end

figure
mixtone = @(f,x) (overtone(f,x)+overtone(2*f,x)+overtone(3*f,x)+...
    overtone(4*f,x)+overtone(5*f,x))/5;
plot(xplot,mixtone(f,xplot),'r','LineWidth',2)
xlim([0,L])
ylim([-1.5,1.5])
set(gca,'XTickLabel',{'0','L'},'XTick',[0 L],'YTickLabel',{},'YTick',zeros(1,0))
title(sprintf('Mixing all overtones (equal weights)'))
sound(mixtone(f,xsound))
pause

for n=1:5
    figure
    w = rand(5,1);
    mixtone = @(f,x) (w(1)*overtone(f,x)+w(2)*overtone(2*f,x)+w(3)*overtone(3*f,x)+...
        w(4)*overtone(4*f,x)+w(5)*overtone(5*f,x))/sum(w);
    plot(xplot,mixtone(f,xplot),'r','LineWidth',2)
    xlim([0,L])
    ylim([-1.5,1.5])
    set(gca,'XTickLabel',{'0','L'},'XTick',[0 L],'YTickLabel',{},'YTick',zeros(1,0))
    title(sprintf('Mixing all overtones (random weights)'))
    sound(mixtone(f,xsound))
    if n < 5
        pause
    end
end