% Square wave synthesis class demo
Fs=22050;
% Fs is the sampling rate
t=0:1/Fs:1;
%t is one second divided into 22050 steps
w=2*pi*100;
% w is the angular frequency
y1=1*sin(w*t);
y3=(1/3)*sin(3*w*t);
y5=(1/5)*sin(5*w*t);
y7=(1/7)*sin(7*w*t);
y9=(1/9)*sin(9*w*t);
plot(t(1:400),y1(1:400));
hold
xlabel('Time (s)')
%The pause commands are to allow time for explanation.
%hit the spacebar to continue
pause
plot(t(1:400),y3(1:400),'r');
pause
ytemp=y1+y3;
plot(t(1:400),ytemp(1:400),'k');
pause
hold
plot(t(1:400),y1(1:400));
xlabel('Time (s)')
hold
pause
plot(t(1:400),y3(1:400),'r');
pause
plot(t(1:400),y5(1:400),'g');
pause
ytemp=y1+y3+y5;
plot(t(1:400),ytemp(1:400),'k');
hold
pause
ytemp=y1+y3+y5+y7+y9;
plot(t(1:400),ytemp(1:400),'k');
xlabel('Time (s)')
pause
soundsc(ytemp,Fs)
pause
y1=1*sin(w*t);
y3=(1/3)*sin(3*w*t+pi/0.9);
y5=(1/5)*sin(5*w*t+pi/4.5);
y7=(1/7)*sin(7*w*t+pi/9);
y9=(1/9)*sin(9*w*t+pi/11);
plot(t(1:400),y1(1:400));
hold
xlabel('Time (s)')
pause
plot(t(1:400),y3(1:400),'r');
pause
ytemp2=y1+y3;
plot(t(1:400),ytemp2(1:400),'k');
pause
hold
plot(t(1:400),y1(1:400));
xlabel('Time (s)')
hold
pause
plot(t(1:400),y3(1:400),'r');
pause
plot(t(1:400),y5(1:400),'g');
pause
ytemp2=y1+y3+y5;
plot(t(1:400),ytemp2(1:400),'k');
hold
pause
ytemp2=y1+y3+y5+y7+y9;
plot(t(1:400),ytemp2(1:400),'k');
xlabel('Time (s)')
pause
hold
plot(t(1:400),ytemp(1:400),'r');
pause
ytemp=ytemp*0.5;
ytemp2=ytemp2*0.5;
%note that the two tones sound the same even though they look very
%different when plotted in the time domain. They contain the same 
%fourier components, the only difference is the phase between components
%is alterd. 
sound(ytemp,Fs)
pause
sound(ytemp2,Fs)
pause
sound(ytemp,Fs)
pause
sound(ytemp2,Fs)
pause
sound(ytemp,Fs)
pause
sound(ytemp2,Fs)
pause
hold
fy=fft(ytemp);
fy2=fft(ytemp2);
plot(abs(fy(1:1000)))
pause
hold
plot(abs(fy2(1:1000)),'r--')
hold



