% modulate.m
%
% A simple Matlab script to demonstrate AM.

fs = 44100;    % sample rate
T = 1/fs;      % sample period
t = [0:T:1.0]; % signal duration (seconds)
fm = 220;      % modulation frequency
fc = 440;      % carrier frequency
A = 1;         % unmodulated carrier amplitude
m = 0.5;       % modulation index

% modulation signal
xm = m * A * sin(2*pi*fm*t);

% amplutide modulation
xc = (A + xm) .* sin(2*pi*fc*t);

% ring modulation
%xc = xm .* sin(2*pi*fc*t);

subplot(2, 1, 1)
plot(t, xc)
xlabel('Time (seconds)')
ylabel('Amplitude');
axis([0, 0.2, -1.6, 1.6]);     % zoom in display

subplot(2, 1, 2)
N = length(t);                 % N = length of fft
f = [0:N-1] * fs / N;          % frequency vector
X = abs(fft(xc)) / N;
plot(f, X);
xlabel('Frequency (Hertz)');
ylabel('Magnitude Gain');
axis([0,1000,0,max(X)]);       % zoom in display
grid
