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

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

% FM parameters
fc = 1;      % center freq
fm = 3;
Dmin = 0;
Dmax = 20;
D = t.*(Dmax - Dmin)/dur + Imin;

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

sound(y, fs);

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

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