format compact

Fs = 48000;		% Sample everything at 8000 Hz
Fo = 660;		% Use 660 Hz as the fundamental freq of each waveform
T_total = 1.2;		% Compute a total of 1.2 seconds so the sound can be played
tt = 0:(1/Fs):T_total;	% Set up the time axis
pos = [263 429 678 208];    % Figure position
pos2 = 	[0.05 0.2 0.9 0.7]; % Axis postion in figure
line = 3;

% =====  First the cosine  =====
y1 = cos(2*pi*Fo*tt);
range = [6*801:6*880];	% Only plot a part of the signal

% subplot('Position',[0.1 0.8 0.8 0.15])
fig=1;
figure(fig); fig=fig+1;
plot(tt(range),y1(range))
	xlabel('TIME (sec)', 'FontWeight', 'Bold')
 	title(['SINE WAVE at ',num2str(Fo),' Hz'], 'FontWeight', 'Bold');
set(gcf, 'Position', pos, 'Color', [1 1 1]);
set(gca, 'Position', pos2);
set(findobj(gca, 'Type', 'line'), 'LineWidth', line);
ff = getframe(gcf);
[XX, map] = frame2im(ff);
imwrite(XX, 'sinet.jpg', 'jpg')
% sound(y1, Fs)

% =====  Square wave  =====
y2 = 0.9*square(2*pi*Fo*tt);

% subplot('Position',[0.1 0.55 0.8 0.15])
figure(fig); fig=fig+1;
plot(tt(range),y2(range))
	xlabel('TIME (sec)', 'FontWeight', 'Bold')
	title(['SQUARE WAVE at ',num2str(Fo),' Hz'], 'FontWeight', 'Bold');
set(gcf, 'Position', pos, 'Color', [1 1 1]);
set(gca, 'Position', pos2);
set(findobj(gca, 'Type', 'line'), 'LineWidth', line);
ff = getframe(gcf);
[XX, map] = frame2im(ff);
imwrite(XX, 'squaret.jpg', 'jpg')
% sound(y2, Fs)

% =====  Sawtooth  =====
y3 = 0.9*sawtooth(2*pi*Fo*tt);

% subplot('Position',[0.1 0.30 0.8 0.15])
figure(fig); fig=fig+1;
plot(tt(range),y3(range))
	xlabel('TIME (sec)', 'FontWeight', 'Bold')
	title(['SAWTOOTH WAVE at ',num2str(Fo),' Hz'], 'FontWeight', 'Bold');
set(gcf, 'Position', pos, 'Color', [1 1 1]);
set(gca, 'Position', pos2);
set(findobj(gca, 'Type', 'line'), 'LineWidth', line);
ff = getframe(gcf);
[XX, map] = frame2im(ff);
imwrite(XX, 'sawt.jpg', 'jpg')
% sound(y3,Fs)

% ======  Beats  =====
Fm = 12;
y4 = sin(2*pi*Fm*tt).*cos(2*pi*Fo*tt);

beatRange = 6*801:6*1600;

% subplot('Position',[0.1 0.05 0.8 0.15])
figure(fig); fig=fig+1;
plot(tt(beatRange),y4(beatRange))
	xlabel('TIME (sec)', 'FontWeight', 'Bold')
	title(['BEATS: F_0 = ',num2str(Fo)], 'FontWeight', 'Bold');
set(gcf, 'Position', pos, 'Color', [1 1 1]);
set(gca, 'Position', [0.05 0.2 0.9 0.67]);
set(findobj(gca, 'Type', 'line'), 'LineWidth', line-2);
ff = getframe(gcf);
[XX, map] = frame2im(ff);
imwrite(XX, 'beatst.jpg', 'jpg')
% sound(y4,Fs)
