% FM.m
%
% A Matlab script to calculate frequency modulated waveforms and
% view their spectra.
%
% by Gary Scavone, McGill University, 2004.

% Signal parameters
fs = 22050;
T = 1/fs;
dur = 4.0;      % seconds
t = 0:T:dur;    % time vector

% FM parameters
fc = 2000;      % center freq
fm = 100;
Imin = 0;
Imax = 20;
I = t.*(Imax - Imin)/dur + Imin;

y = sin(2*pi*fc*t + I.*sin(2*pi*fm*t));
plot(t, y);

sound(y, fs);

dospectrum = input('\nPlot spectrum? Y/[N]:\n\n', 's');

if dospectrum == 'Y',
  specgram(y, 512, fs, [], 256)
end
