% experiment with sound and signals

fs = 10000;
t = 0:1/fs:1.5;
x = .5*cos(440*2*pi*t);  
sound(x,fs)
wavwrite(x,fs,'e:\web_pages\book\sig1')
figure(1)
subplot(221),plot(t,x)
axis([0 .01 -1 1])
title('.5cos(440(2pi)t)')
xlabel('Time (sec)')
ylabel('x(t)')
subplot(222),stem([-440 440],[.25 .25])
axis([-500 500 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')
pause

x1 = .5+x;  % what happens if you add a DC component
sound(x1,fs)
wavwrite(x1,fs,'e:\web_pages\book\sig2')
subplot(223),plot(t,x1)
title('.5+.5cos(440(2pi)t)')
axis([0 .01 -1 1])
xlabel('Time (sec)')
ylabel('x(t)')
subplot(224),stem([-440 0 440],[.25 .5 .25])
axis([-500 500 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')
pause

x2 = x+.5*cos(880*2*pi*t);  % what happens when you add a harmonic
sound(x2,fs)
wavwrite(x2,fs,'e:\web_pages\book\sig3')
figure(2)
subplot(221),plot(t,x2)
title('.5(cos(440(2pi)t+cos(880(2pi)t))')
axis([0 .01 -1 1])
xlabel('Time (sec)')
ylabel('x(t)')
subplot(222),stem([-880 -440 440 880],[.25 .25 .25 .25])
axis([-1000 1000 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')

pause

% now add several harmonics with equal magnitude
x3 = x2+.5*cos(440*3*2*pi*t)+.5*cos(440*4*2*pi*t)+.5*cos(440*5*2*pi*t);
x3 = x3*.4;  % scale to avoid clipping
sound(x3,fs)
wavwrite(x3,fs,'e:\web_pages\book\sig4')
subplot(223),plot(t,x3)
title('Equal Harmonics')
axis([0 .01 -1 1])
xlabel('Time (sec)')
ylabel('x(t)')
f = 440*[-5 -4 -3 -2 -1 1 2 3 4 5];
cn = .1*ones(size(f));
subplot(224),stem(f,cn)
axis([-440*5 440*5 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')
pause

figure(3)
% now consider several harmonics with decreasing magnitude
x4 = x+.4*cos(440*2*2*pi*t)+.3*cos(440*3*2*pi*t)+.2*cos(440*4*2*pi*t)+.1*cos(440*5*2*pi*t);
x4 = x4*.6;  %scale to avoid clipping
sound(x4,fs)
wavwrite(x4,fs,'e:\web_pages\book\sig5')
subplot(221),plot(t,x4)
title('Decreasing Harmonics')
axis([0 .01 -1 1])
xlabel('Time (sec)')
ylabel('x(t)')
f = 440*[-5 -4 -3 -2 -1 1 2 3 4 5];
cn = .6*[.05:.05:.25,.25:-.05:.05];
subplot(222),stem(f,cn)
axis([-440*5 440*5 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')

pause

% now listen to a square wave
w0 = 440*2*pi;
N = 11;
xN = zeros(size(t));
n = 1:N;
cn =  1./n/pi.*sin(n*pi/2);  
c_n = cn;
xN = cn*exp(j*n'*w0*t)+ c_n*exp(-j*n'*w0*t);
sound(xN,fs)
wavwrite(xN,fs,'e:\web_pages\book\sig6')
subplot(223),plot(t,xN)
title('Square Wave')
axis([0 .01 -1 1])
ylabel('x(t)')
xlabel('Time (sec)')
f = 440*[-N:-1,1:N];
cn = [cn(length(cn):-1:1) cn];
subplot(224),stem(f,abs(cn))
axis([-440*11 440*11 0 .5])
xlabel('Frequency (Hz)')
title('Complex Spectrum')
pause

% example of amplitude modulation (beating)
figure(4)
x = cos(10*2*pi*t).*cos(440*2*pi*t);
sound(x,fs)
wavwrite(x,fs,'e:\web_pages\book\sig7')
subplot(221),plot(t,x)
xlabel('Time (sec)')
axis([0 .2 -1 1])
ylabel('x(t)')
title('Amplitude Modulation')
pause

% example of chirp signal (frequency changes over time)
f = 1000*t+440;  
x=cos(f.*t);
subplot(222),plot(t,x)
title('Chirp Signal')
xlabel('Time (Sec)')
ylabel('x(t)')
axis([0 .3 -1 1])
sound(x,fs)
wavwrite(x,fs,'e:\web_pages\book\sig8')
subplot(111)









