% see
% http://web.eecs.utk.edu/~qi/ece505/project/proj1.pdf
% info on FM synthesis

%   C2   65.4
%   C3   130.8
%   C4   262 Hz (middle C)   
%   D4   294
%	E4   330
%	F4   349
%	G4   392
%	A4   440  
%	B4   494
%	C5   523  
% see: http://www.phy.mtu.edu/~suits/notefreqs.html
clear all
% 
% sample freq and time to simulate
Fs = 8000;
tmax = 3;
t = 0:1/Fs:tmax ;
out = zeros(1,Fs*tmax);

fc = 261 ;
fm = fc * 1.5 ; 
tau_amp = .75 ;
attack_exp = .1 ;
tau_freq = 1 ;
freq_depth = 0.5;

a = (t.^attack_exp) .* exp(-t/tau_amp) ;
I = freq_depth * exp(-t/tau_freq) ;

for i=1:Fs*tmax
    out(i) = a(i) * cos(2*pi*fc*t(i) +I(i) * sin(2*pi*fm*t(i))) ;
end

soundsc(out,Fs)

figure(2)
plot(out)

figure(1); clf
pwelch(out,[],[],[],Fs)
%set(gca, 'xlim', [0 .2])

figure(3); clf
spectrogram(out,[1024],[],[],Fs, 'yaxis')

% drum
% fc = 131 ;
% fm = fc * 1.5 ; 
% tau_amp = .75 ;
% attack_exp = .1 ;
% tau_freq = 1 ;
% freq_depth = 1;

% nasty guitar
% fc = 131 ;
% fm = fc * 1.5 ; 
% tau_amp = .75 ;
% attack_exp = .1 ;
% tau_freq = 1 ;
% freq_depth = 10;


