% short script to create a sine wave sound in Matlab

clear all;

Fs = 44100;     % sampling frequency
dur = 1;        % duration of sound (in sec)


% time vector
t = [0 : 1/Fs : dur-1/Fs];  % 1 second --> length(t) = 44100


% frequency of sound (in Hz)
freq = 440;


% Amplitude of sound
A = 1;


% phase of sound
phase = 0;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sound vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f1 = A * sin ( 2*pi * freq * t + phase);

% play sound
% sound(f1,Fs)

% pause(1.1)

% plot 2 periods of sound ( period = 1/freq (*Fs) )
% figure(1)
plot( t(1 : 2*round(1/freq*Fs)) , f1(1 : 2* round(1/freq*Fs)) )






% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % sound f2 that is 1 octave higher than f1 (1 octave = double the frequency)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% f2 = sin ( 2*pi * (2*freq) * t + phase );
%
% % sound(f2,Fs)
% %
% % hold on
% % plot( t(1 : 2*round(1/freq*Fs)) , f2(1 : 2* round(1/freq*Fs))  , 'r')
% %
%



% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % % play consecutively
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % f12 = [f1 f2];
% % sound(f12,Fs)
% %
% %
% %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % play together/superimposed:
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% f_12 = [f1+f2];
%     % or:
%     % f_12 = sum([f1;f2]);
% sound(f_12,Fs);
% plot( t(1 : 2*round(1/freq*Fs)) , f1(1 : 2*round(1/freq*Fs)) , 'b' ); hold on
% plot( t(1 : 2*round(1/freq*Fs)) , f2(1 : 2*round(1/freq*Fs)) , 'g' ); hold on
% plot( t(1 : 2*round(1/freq*Fs)) , f_12(1 : 2*round(1/freq*Fs)) , 'r' )


%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % play complex sound with multiple harmonics
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% n_harm = 10; % number of harmonics
% f0 = 400;   % also try with 800, 1600
% for n = 1:n_harm
%     f_h(n) = f0 * n;
%     f_comp(n,:) = sin ( 2*pi * f_h(n) * t ) ;
% end
% f_complex = sum(f_comp);
% f_complex = 0.2 * f_complex;
% sound(f_complex,Fs)
% % plot each component
% % for m = 1:size(f_comp,1)
% %     plot( t(1 : 2*round(1/freq*Fs)) , f_comp(m, 1 : 2*round(1/freq*Fs)))
% %     hold on
% % end
% plot( t(1 : 2*round(1/freq*Fs)) , f_complex(1 : 2*round(1/freq*Fs)) , 'r')




% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % play sounds with variable phase and play them together
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% % phase of the sound (in radians (in terms of pi))
% phase1 = 0*pi;
% phase2 = 2*pi;  % play around with different phases
%
% f1 = .2* sin ( 2*pi * freq * t + phase1 );
% f2 = .2* sin ( 2*pi * freq * t + phase2 );      % play around with different frequencies
%
% f = (f1 + f2);
% sound(f,Fs)
%
% % plot 2 periods of sound ( period = 1/freq (*Fs) )
% figure
% plot( t(1 : 2*round(1/freq*Fs)) , f1(1 : 2* round(1/freq*Fs)) , 'b'); hold on
% plot( t(1 : 2*round(1/freq*Fs)) , f2(1 : 2* round(1/freq*Fs)) , 'g'); hold on
% plot( t(1 : 2*round(1/freq*Fs)) , f(1 : 2* round(1/freq*Fs)) , 'r')