function demo301

ex = 0;
while ex~=1
    k = menu('Choose a demo','458 Hz sine','2*458 Hz sine','458 Hz square',...
        '2*458 Hz square','458 Hz Saw-tooth','2*458 Hz Saw-tooth',...
        'Square as sum of sines (201 terms)',...
        'Square as sum of sines (1001 terms)',...
        'Triangular as sum of sines (21 terms)','Recorded Sound',...
        'Handel','Chirp','gong','laughter','splat','train',...
        'Replay','exit');
    
    switch k
        case 1 % 458 Hz sine
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = sin(2*pi*params.t*458);
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 2 % 2*458 Hz sine
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = sin(2*pi*params.t*2*458);
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 3 % 458 Hz square
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = sin(2*pi*params.t*458);
            y = sign(y);
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 4 % 2*458 Hz square
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = sin(2*pi*params.t*2*458);
            y = sign(y);
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 5 % 458 Hz Saw-tooth
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = mod(params.t,1/458);            
            y = y/max(abs(y));
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 6 % 2*458 Hz Saw-tooth
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.Tobs = 1;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = mod(params.t,1/(2*458));            
            y = y/max(abs(y));
            wavplay(y,params.Fs,'async');
            show(y,params,100);
        case 7 % Square as sum of sines (201 terms)
            params.N = 2000;
            params.t = linspace(-1,1,params.N);
            params.Tobs = 2;
            sum = zeros(1,params.N);
            for n=[1:2:201]
                a = sin(n*pi/2)/(n*pi);
                sum = sum+a*cos(params.t*2*pi*n);
                show(sum,params,params.N);
                pause(0.1)
            end
        case 8 % Square as sum of sines (1001 terms)
            params.N = 5000;
            params.t = linspace(-1,1,params.N);
            params.Tobs = 2;
            sum = zeros(1,params.N);
            for n=[1:2:1001]
                a = sin(n*pi/2)/(n*pi);
                sum = sum+a*cos(params.t*2*pi*n);
            end
            show(sum,params,params.N);
        case 9 % Triangular as sum of sines (21 terms)
            params.N = 5000;
            params.t = linspace(-1,1,params.N);
            params.Tobs = 2;
            sum = zeros(1,params.N);
            for n=[1:2:21]
                a = 8/(n*pi)^2;
                sum = sum+a*cos(params.t*2*pi*n);
                show(sum,params,params.N);
                pause(0.5)
            end
        case 10 % ','Recorded Sound'
            params.Tobs = 3;
            params.Fs = 11025;
            params.Ts = 1/params.Fs;
            params.N = params.Tobs*params.Fs;
            params.t = [1:params.N]*params.Ts;
            y = wavrecord(params.N,params.Fs);
            y = y/max(abs(y));
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 11 % Handel
            load handel;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 12 % Chirp
            load chirp;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 13 % gong
            load gong;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 14 % laughter
            load laughter;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 15 % splat
            load splat;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 16 % train
            load train;
            params.Fs = Fs;
            params.N = length(y);
            params.Ts = 1/params.Fs;
            params.Tobs = params.N*params.Ts;
            params.t = [1:params.N]*params.Ts;
            wavplay(y,params.Fs,'async');
            show(y,params,params.N);
        case 17
            wavplay(y,params.Fs,'async');
        case 18
            ex = 1;
    end
end
close all

function show(y,params,chk);
figure(1);
subplot(2,1,1);
plot(params.t(1:chk),y(1:chk));
xlabel('t, sec.');
ylabel('x(t)')
title('Time domain')
Y = fft(y);
Y = Y(1:floor(params.N/2));
f = 1/params.Tobs*[0:floor(params.N/2)-1];
subplot(2,1,2)
plot(f,abs(Y),'.',f,abs(Y));
xlabel('f, Hz.');
ylabel('X(w)')
title('Frequency domain')
drawnow