MATLAB Code
% Superheterodyne Receiver Simulator in MATLAB
% Parameters
fm = 5; % Message frequency (Hz)
fc = 50; % Carrier frequency (Hz)
fIF = 100; % Intermediate frequency (Hz)
Fs = 1000; % Sampling frequency (Hz)
T = 1; % Simulation duration (seconds)
Ka = 0.8; % AM modulation index (0 < Ka <= 1)
% Time vector
t = (0:1/Fs:T-1/Fs)';
% Local Oscillator frequency (high-side injection)
fLO = fc + fIF;
% Original message signal m(t)
message = sin(2*pi*fm*t);
% AM RF signal: RF(t) = (1 + Ka*m(t)) * cos(2*pi*fc*t)
rf = (1 + Ka*message) .* cos(2*pi*fc*t);
% Local Oscillator signal
lo = cos(2*pi*fLO*t);
% Mixed IF signal: RF * LO
ifSignal = rf .* lo;
% Low-pass filter to extract IF component around fm*2 Hz
filtered = lowPass(ifSignal, fm*2, Fs);
% Envelope detection:
% 1. Rectify
rectified = abs(filtered);
% 2. Low-pass filter to smooth envelope (cutoff slightly above fm)
recovered = lowPass(rectified, fm*2, Fs);
% Plotting results
figure;
subplot(5,1,1);
plot(t, message);
title('Original Message: m(t)');
xlabel('Time (s)'); ylabel('Amplitude');
subplot(5,1,2);
plot(t, rf);
title(sprintf('AM RF Signal: (1 + %.1f*m(t)) * cos(2\\pi %d t)', Ka, fc));
xlabel('Time (s)'); ylabel('Amplitude');
subplot(5,1,3);
plot(t, ifSignal);
title('Mixed IF Signal: RF(t) * LO(t)');
xlabel('Time (s)'); ylabel('Amplitude');
subplot(5,1,4);
plot(t, filtered);
title(sprintf('Filtered IF (~%d Hz): bandpass output', fIF));
xlabel('Time (s)'); ylabel('Amplitude');
subplot(5,1,5);
plot(t, recovered);
title('Recovered Signal (Envelope Detection): m(t)');
xlabel('Time (s)'); ylabel('Amplitude');
% Display first 10 samples in console
fprintf('Original m(t): %s\n', mat2str(round(message(1:10),3)'));
fprintf('RF(t) [AM]: %s\n', mat2str(round(rf(1:10),3)'));
fprintf('IF(t): %s\n', mat2str(round(ifSignal(1:10),3)'));
fprintf('Filtered IF: %s\n', mat2str(round(filtered(1:10),3)'));
fprintf('Rectified: %s\n', mat2str(round(rectified(1:10),3)'));
fprintf('Recovered m(t): %s\n', mat2str(round(recovered(1:10),3)'));
fprintf('Local Oscillator f_LO: %.2f Hz\n', fLO);
fprintf('Intermediate Frequency f_IF: %.2f Hz\n', fIF);
% --- Local function below ---
function y = lowPass(signal, cutoff, Fs)
RC = 1/(2*pi*cutoff);
dt = 1/Fs;
alpha = dt/(RC+dt);
y = zeros(size(signal));
for k = 2:length(signal)
y(k) = alpha*signal(k) + (1-alpha)*y(k-1);
end
end
Output