MATLAB Modulation–Demodulation Lab Exam Guide
MATLAB Modulation–Demodulation Lab
Exam Guide
Self-contained beginner note for last-night study
Designed for writing tomorrow’s lab exam from memory
This note starts from zero MATLAB knowledge, shows the two master templates, and then gives
the exact code patterns for AM, FM, PAM, PWM, PPM, ASK, FSK and PSK.
How to study this PDF tonight
• First memorize the common structure: Time → Message → Carrier → Modulate → Demodulate/Extra → Plot.
• Then memorize the one special formula for each experiment. Do not try to memorize eight full programs
separately.
• At the end, revise only the “one-line memory sheet” and viva answers.
Important: in all sine/cosine formulas, frequency is in Hz and time is in seconds. That is why MATLAB uses
terms like sin(2*pi*f*t).
Rubin — exam handover copy
Page 1
MATLAB Modulation–Demodulation Lab Exam Guide
1. MATLAB from zero: enough to survive the exam
If someone has never touched MATLAB, these are the only commands they truly need for your lab record
style programs.
MATLAB line Meaning
clc clear the command window
clear remove old variables from memory
close all close old graph windows
fs = 10000; sampling frequency = 10000 samples per second
t = 0:1/fs:1; time vector from 0 to 1 second
fm = 5; message frequency = 5 Hz
fc = 100; carrier frequency = 100 Hz
m = sin(2*pi*fm*t); create the message wave
c = cos(2*pi*fc*t); create the carrier wave
plot(t,x) plot signal x versus time
subplot(4,1,k) make 4 plots in one figure and select the kth plot
grid on show background grid
title(...) write graph title
bits = [1 0 1 1 0]; digital input message
repelem(bits,spb) stretch each bit into many samples so it becomes a waveform
Three tiny ideas you must never forget
• Analog experiments use a sine-wave message, usually: m = sin(2*pi*fm*t).
• Digital experiments use bits, then convert them into a waveform using repelem(bits, spb).
• The four graph outputs are always some version of: message, carrier, modulated, demodulated/extra.
2. The one master memory map
Family Experiments Core idea to remember
Multiply family AM, ASK, PAM Message controls amplitude, so something gets multiplied.
Compare family PWM Compare message with a triangle: output becomes 0 or 1.
Integrate family FM Message goes inside phase through cumsum(m).
Shift family PPM Message changes pulse position.
Switch family FSK Bit 1 uses one frequency, bit 0 uses another frequency.
Sign-flip family PSK Convert 0/1 into -1/+1, then multiply carrier.
This is the most important page in the whole PDF. If you remember only this table, you can reconstruct almost
every program live in the exam.
Page 2
MATLAB Modulation–Demodulation Lab Exam Guide
3. Analog codes template (AM, FM, PAM, PWM, PPM)
Step Common line What it means
1 clc; clear; close all; Reset everything.
2 fs = 10000; Sampling frequency.
3 t = 0:1/fs:1; Time vector.
4 fm = 5; Message frequency.
5 m = sin(2*pi*fm*t); Message signal.
6 fc or fp = ...; Carrier frequency or pulse frequency.
7 c = ...; Carrier signal.
8 s = ...; Modulated signal.
9 d = ...; Demodulated signal.
10 subplot(...) and plot(...) Four outputs.
Analog template rule
• For analog experiments, the structure stays the same. Only four lines really change: m, c, s and d.
• AM and FM use sine carrier. PAM and PPM use pulse-train carrier. PWM uses a triangular carrier.
Experimen
Carrier c Modulated signal s Demod idea
t
Multiply again by carrier and
AM cos(2*pi*fc*t) (1 + ka*m).*c
smooth
FM cos(2*pi*fc*t) cos(2*pi*fc*t + 2*pi*kf*cumsum(m)/fs) Hilbert → phase → frequency
PAM pulse train m .* p Smooth the pulses
PWM triangle wave double(m > tri) Smooth and remove DC
Measure pulse delay and
PPM reference pulse train shift pulse position
reconstruct
Page 3
MATLAB Modulation–Demodulation Lab Exam Guide
4. Digital codes template (ASK, FSK, PSK)
Step Common line What it means
1 clc; clear; close all; Reset everything.
2 fs = 1000; Sampling frequency.
3 Tb = 1; Bit duration.
4 bits = [1 0 1 1 0 0 1 0]; Input binary message.
5 spb = fs*Tb; Samples per bit.
6 m = repelem(bits,spb); Convert bits into waveform.
7 t = 0:1/fs:Tb*length(bits)-1/fs; Time vector.
8 c = ...; Carrier signal.
9 s = ...; Modulated signal.
10 d or extra = ...; Demodulated / extra output.
Experimen
Carrier Modulated signal Fourth plot
t
ASK cos(2*pi*fc*t) m .* c Demodulated bits
Second carrier or extra
FSK Two carriers: c1 and c0 Use f1 for bit 1, use f0 for bit 0
reference
PSK cos(2*pi*fc*t) sm .* c where sm = 2*m - 1 Shifted message sm
Digital template rule
• In digital experiments, bits are first expanded into a waveform using repelem(bits, spb).
• ASK is digital AM, FSK switches frequencies, PSK flips phase by multiplying the carrier with +1 or -1.
Page 4
MATLAB Modulation–Demodulation Lab Exam Guide
5. Why every sine formula looks like 2*pi*f*t
A signal like sin(2*pi*f*t) needs f in hertz and t in seconds. Then 2πft becomes an angle in radians,
which is valid for sine and cosine.
Symbol Meaning Unit
t time seconds (s)
f, fm, fc, fp, f1, f0 frequency hertz (Hz)
fs sampling frequency samples/second = Hz
Tb bit duration seconds (s)
Example: if fc = 100, then the carrier completes 100 cycles in one second.
6. AM modulation and demodulation
What it is: AM means amplitude modulation. The amplitude of the carrier changes according to the
message.
Main memory key: AM = amplitude changes, so the message controls carrier amplitude: (1 + ka*m).*c
What changes from the common template
• Common analog template stays same.
• Special modulation line: s = (1 + ka*m).*c
• Demodulation idea: multiply by carrier again and smooth with movmean.
clc; clear; close all;
fs = 10000;
t = 0:1/fs:1;
fm = 5; % message frequency
fc = 100; % carrier frequency
ka = 0.7; % modulation index
m = sin(2*pi*fm*t); % message signal
c = cos(2*pi*fc*t); % carrier signal
s = (1 + ka*m).*c; % AM modulation
x = 2*s.*c; % coherent detection
d = movmean(x, 200); % low-pass filtering
d = d - mean(d); % remove DC
d = d / max(abs(d)); % normalize
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,c,'LineWidth',1.2); grid on; title('Carrier Wave');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('AM Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• In AM, frequency and phase stay same, but carrier amplitude changes.
• ka is the modulation index.
• Demodulation recovers the envelope / message.
Page 5
MATLAB Modulation–Demodulation Lab Exam Guide
7. FM modulation and demodulation
What it is: FM means frequency modulation. The carrier frequency changes according to the message.
Main memory key: FM = frequency changes, so message enters the phase after integration: cumsum(m)
What changes from the common template
• The special FM memory key is cumsum(m).
• The carrier line itself is normal cosine.
• Demodulation uses hilbert → phase → instantaneous frequency.
clc; clear; close all;
fs = 10000;
t = 0:1/fs:1;
fm = 5; % message frequency
fc = 100; % carrier frequency
kf = 40; % frequency sensitivity
m = sin(2*pi*fm*t); % message signal
c = cos(2*pi*fc*t); % carrier signal
s = cos(2*pi*fc*t + 2*pi*kf*cumsum(m)/fs); % FM modulation
z = hilbert(s);
phase = unwrap(angle(z));
inst_freq = [0 diff(phase)]*fs/(2*pi);
d = (inst_freq - fc)/kf;
d = movmean(d, 200);
d = d / max(abs(d));
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,c,'LineWidth',1.2); grid on; title('Carrier Wave');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('FM Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• In FM, carrier amplitude remains constant.
• Message changes the instantaneous frequency of the carrier.
• cumsum is used because frequency is related to derivative of phase, so phase uses integral of message.
8. PAM modulation and demodulation
What it is: PAM means pulse amplitude modulation. The amplitude of each pulse follows the message
value.
Main memory key: PAM = message × pulse train
What changes from the common template
• The carrier is not a sine wave here. It is a pulse train.
• Special modulation line: s = m .* p
• Demodulation is just smoothing the pulse train output.
Page 6
MATLAB Modulation–Demodulation Lab Exam Guide
clc; clear; close all;
fs = 10000;
t = 0:1/fs:1;
fm = 5; % message frequency
fp = 50; % pulse frequency
duty = 10; % pulse width in percent
m = sin(2*pi*fm*t); % message signal
Tp = 1/fp;
p = double(mod(t,Tp) < (duty/100)*Tp); % pulse train
s = m .* p; % PAM modulation
d = movmean(s, round(fs/fp));
d = d / max(abs(d));
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,p,'LineWidth',1.2); grid on; title('Carrier Pulse Train');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('PAM Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• In PAM, pulse amplitude changes according to the message.
• Pulse width and pulse position remain fixed.
• PAM is the pulse version of amplitude control.
9. PWM modulation and demodulation
What it is: PWM means pulse width modulation. The pulse amplitude remains same, but pulse width
changes with the message.
Main memory key: PWM = compare message with triangle → s = double(m > tri)
What changes from the common template
• The carrier is a triangular wave.
• The special modulation line is a comparator: s = double(m > tri).
• Demodulation is done by smoothing and removing DC.
Page 7
MATLAB Modulation–Demodulation Lab Exam Guide
clc; clear; close all;
fs = 10000;
t = 0:1/fs:1;
fm = 5; % message frequency
fp = 50; % carrier frequency
m = 0.8*sin(2*pi*fm*t); % message signal
Tp = 1/fp;
frac = mod(t,Tp)/Tp;
tri = 4*abs(frac - 0.5) - 1; % triangular carrier
s = double(m > tri); % PWM modulation
d = movmean(s, round(fs/fp));
d = d - mean(d);
d = d / max(abs(d));
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,tri,'LineWidth',1.2); grid on; title('Triangular Carrier Wave');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('PWM Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• In PWM, width changes but amplitude stays constant.
• Comparator compares message with triangular carrier.
• Larger message value gives wider output pulse.
10. PPM modulation and demodulation
What it is: PPM means pulse position modulation. Width stays fixed, amplitude stays fixed, but pulse
position shifts.
Main memory key: PPM = shift pulse position according to message amplitude
What changes from the common template
• Remember only the idea clearly: pulse location moves left/right inside each frame.
• A reference pulse train is first created.
• Demodulation measures the pulse delay, then reconstructs the message using interpolation.
Page 8
MATLAB Modulation–Demodulation Lab Exam Guide
clc; clear; close all;
fs = 10000;
t = 0:1/fs:1;
fm = 5; % message frequency
fp = 50; % pulse repetition frequency
m = sin(2*pi*fm*t); % message signal
Tp = 1/fp;
pw = 0.1*Tp; % pulse width
shiftMax = 0.5*Tp; % maximum shift
p = double(mod(t,Tp) < pw); % reference pulse train
s = zeros(size(t));
nFrames = floor(t(end)/Tp);
for k = 1:nFrames
t0 = (k-1)*Tp;
idx0 = round(t0*fs) + 1;
msg_sample = m(idx0);
delay = ((msg_sample + 1)/2)*shiftMax;
start_time = t0 + delay;
idx = (t >= start_time) & (t < start_time + pw);
s(idx) = 1;
end
rec_samples = zeros(1,nFrames);
rec_times = zeros(1,nFrames);
for k = 1:nFrames
t0 = (k-1)*Tp;
idx = find(t >= t0 & t < t0 + Tp);
local_t = t(idx) - t0;
local_s = s(idx);
firstPulse = find(local_s == 1, 1, 'first');
if isempty(firstPulse)
delay = 0;
else
delay = local_t(firstPulse);
end
rec_samples(k) = 2*(delay/shiftMax) - 1;
rec_times(k) = t0 + Tp/2;
end
d = interp1(rec_times, rec_samples, t, 'linear', 'extrap');
d = d / max(abs(d));
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,p,'LineWidth',1.2); grid on; title('Reference Pulse Train');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('PPM Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• In PPM, neither pulse amplitude nor width carries information.
• Only the pulse position carries the message.
• PPM can be thought of as a shifted-pulse version of PWM.
11. ASK modulation and demodulation
What it is: ASK means amplitude shift keying. A digital bit controls the amplitude of the carrier.
Main memory key: ASK = digital AM = m .* c
Page 9
MATLAB Modulation–Demodulation Lab Exam Guide
What changes from the common template
• The message is digital, so start with bits and repelem(bits, spb).
• The modulation line is exactly like digital AM: s = m .* c
• Demodulation uses carrier multiplication, smoothing and threshold.
clc; clear; close all;
fs = 10000;
fc = 100; % carrier frequency
bits = [1 0 1 1 0 1 0 0 1 1]; % message bits
Tb = 0.2; % bit duration
spb = Tb*fs; % samples per bit
m = repelem(bits, spb);
t = 0:1/fs:(length(m)-1)/fs;
c = cos(2*pi*fc*t); % carrier signal
s = m .* c; % ASK modulation
x = 2*s.*c;
y = movmean(x, round(spb/4));
d = double(y > 0.5); % threshold detector
figure;
subplot(4,1,1); plot(t,m,'LineWidth',1.2); grid on; title('Message Wave');
subplot(4,1,2); plot(t,c,'LineWidth',1.2); grid on; title('Carrier Wave');
subplot(4,1,3); plot(t,s,'LineWidth',1.2); grid on; title('ASK Modulated Wave');
subplot(4,1,4); plot(t,d,'LineWidth',1.2); grid on; title('Demodulated Wave');
Viva one-liners to memorize
• Bit 1 means carrier present; bit 0 means carrier absent or reduced.
• ASK is the digital version of amplitude modulation.
• Amplitude is the parameter that changes with the bit.
12. FSK modulation
What it is: FSK means frequency shift keying. Bit 1 and bit 0 are sent using two different frequencies.
Main memory key: FSK = 1 → f1, 0 → f0
What changes from the common template
• There are two carrier waves here: c1 and c0.
• During each bit interval, the program chooses one of the two frequencies.
• For your exam-style output, the four plots are message, carrier for bit 1, carrier for bit 0, and FSK modulated
wave.
Page 10
MATLAB Modulation–Demodulation Lab Exam Guide
clc; clear; close all;
fs = 10000;
bits = [1 0 1 1 0 1 0 0 1 1]; % message bits
Tb = 0.2; % bit duration
f1 = 80; % frequency for bit 1
f0 = 160; % frequency for bit 0
spb = Tb*fs; % samples per bit
m = repelem(bits, spb); % message wave
t = 0:1/fs:(length(m)-1)/fs;
c1 = cos(2*pi*f1*t); % carrier for bit 1
c0 = cos(2*pi*f0*t); % carrier for bit 0
s = zeros(size(t));
for k = 1:length(bits)
idx = (k-1)*spb + 1 : k*spb;
if bits(k) == 1
s(idx) = cos(2*pi*f1*t(idx));
else
s(idx) = cos(2*pi*f0*t(idx));
end
end
figure;
subplot(4,1,1);
plot(t,m,'LineWidth',1.2);
grid on;
title('Message Wave');
subplot(4,1,2);
plot(t,c1,'LineWidth',1.2);
grid on;
title('Carrier Wave for Bit 1');
subplot(4,1,3);
plot(t,c0,'LineWidth',1.2);
grid on;
title('Carrier Wave for Bit 0');
subplot(4,1,4);
plot(t,s,'LineWidth',1.2);
grid on;
title('FSK Modulated Wave');
Viva one-liners to memorize
• In FSK, amplitude is not the key information parameter; frequency is.
• Bit 1 is represented by one carrier frequency and bit 0 by another.
• The modulated signal is built by switching between two cosines.
13. PSK modulation
What it is: PSK means phase shift keying. In simple BPSK, the phase flips by 180° depending on the bit.
Main memory key: PSK = convert 0/1 into -1/+1, then multiply carrier
What changes from the common template
• The one magical line is: sm = 2*m - 1
• This converts 1 into +1 and 0 into -1.
• Then psk = sm .* c, so the carrier either stays same or flips phase.
Page 11
MATLAB Modulation–Demodulation Lab Exam Guide
clc; clear; close all;
fs = 1000; % sampling frequency
Tb = 1; % bit duration
bits = [1 0 1 1 0 0 1 0]; % message bits
spb = fs*Tb; % samples per bit
t = 0:1/fs:Tb*length(bits)-1/fs;
m = repelem(bits, spb); % message signal
sm = 2*m - 1; % shifted message: 1 -> +1, 0 -> -1
fc = 5; % carrier frequency
c = cos(2*pi*fc*t); % carrier signal
psk = sm .* c; % BPSK signal
figure;
subplot(4,1,1);
plot(t, m, 'LineWidth', 1.5);
grid on;
title('Message Signal');
ylim([-0.5 1.5]);
subplot(4,1,2);
plot(t, c, 'LineWidth', 1.5);
grid on;
title('Carrier Signal');
subplot(4,1,3);
plot(t, sm, 'LineWidth', 1.5);
grid on;
title('Shifted Message');
ylim([-1.5 1.5]);
subplot(4,1,4);
plot(t, psk, 'LineWidth', 1.5);
grid on;
title('PSK Signal');
Viva one-liners to memorize
• In BPSK, the carrier phase changes by 180° according to the binary bit.
• Bit 1 gives normal carrier and bit 0 gives phase-reversed carrier.
• Shifted message means polar message: +1 and -1 levels.
Page 12
MATLAB Modulation–Demodulation Lab Exam Guide
14. One-line memory sheet
Experiment One line to memorize
AM s = (1 + ka*m).*c
FM s = cos(2*pi*fc*t + 2*pi*kf*cumsum(m)/fs)
PAM s = m .* p
PWM s = double(m > tri)
PPM shift pulse position according to message
ASK s = m .* c
FSK if bit = 1 use f1, else use f0
PSK sm = 2*m - 1; psk = sm .* c
If you blank out in the exam, rebuild the code with logic
• Need analog? Use t = 0:1/fs:1 and m = sin(2*pi*fm*t).
• Need digital? Use bits, spb, repelem(bits, spb).
• Need amplitude change? Multiply.
• Need frequency change? Use cumsum(m) for FM or switch between f1 and f0 for FSK.
• Need phase change? Convert bits into -1 and +1, then multiply carrier.
• Need pulse-width change? Compare message with triangle.
15. Final viva sheet
Question One-line answer
What is the unit of frequency in the code? Hertz (Hz).
Why do we write sin(2*pi*f*t)? Because sine takes angle in radians and 2πft gives the correct phase angle.
Difference between AM and FM? AM changes amplitude; FM changes frequency.
PAM changes pulse amplitude, PWM changes pulse width, PPM changes
Difference between PAM, PWM and PPM?
pulse position.
What is ASK? Amplitude shift keying: binary data changes carrier amplitude.
What is FSK? Frequency shift keying: binary data selects one of two carrier frequencies.
What is PSK? Phase shift keying: binary data changes carrier phase.
What is shifted message in PSK? Polar message, where 1 becomes +1 and 0 becomes -1.
Use this final rule for memorization: AM/ASK/PAM = multiply, PWM = compare, FM = integrate message,
FSK = two frequencies, PSK = sign flip, PPM = pulse shift.
End of study copy. Read this once fully, then revise only Sections 2, 3, 4 and 14 twice.
Page 13