Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

clc;

clear;

%% Transmitter
% Step 1: Read audio file and plot in time and frequency domain
[signal, Fs] = audioread('eric.wav');
t = (0:length(signal)-1) / Fs;
figure;
subplot(1,2,1);
plot(t, signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('Audio Signal in Time Domain');

signal_freq = fftshift(fft(signal));
f = linspace(-Fs/2, Fs/2, length(signal_freq));
subplot(1,2,2);
plot(f, abs(signal_freq));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Audio Signal in Frequency Domain');

% Step 2: Ideal low pass filter to remove frequencies above 4kHz


LPF = zeros(size(signal_freq));
f_cutoff = 4000; % Hz
LPF(abs(f) <= f_cutoff) = 1;
signal_filtered_freq = signal_freq .* LPF;

% Step 3: Convert filtered signal back to time domain


signal_filtered = ifft(ifftshift(signal_filtered_freq));

% Step 4: Resample filtered signal to match carrier frequency


fc = 100000; % Carrier frequency
Fs_new = 5 * fc; % New sampling frequency
signal_resampled = resample(signal_filtered, Fs_new, Fs);

% Step 5: Modulate carrier with filtered signal for DSB-TC and DSB-SC
t_resampled = (0:length(signal_resampled)-1) / Fs_new;
carrier = cos(2*pi*fc*t_resampled);

% DSB-TC modulation
A = 2 * max(signal_resampled);
dsbtc_modulated = carrier .* (A + signal_resampled);

% DSB-SC modulation
dsbsc_modulated = carrier .* signal_resampled;

%% Plot modulated signals in frequency domain


figure;
subplot(2,1,1);
plot(linspace(-Fs_new/2, Fs_new/2, length(dsbtc_modulated)),
abs(fftshift(fft(dsbtc_modulated))));
title('DSB-TC Modulated Signal in Frequency Domain');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
subplot(2,1,2);
plot(linspace(-Fs_new/2, Fs_new/2, length(dsbsc_modulated)),
abs(fftshift(fft(dsbsc_modulated))));
title('DSB-SC Modulated Signal in Frequency Domain');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

%% Receiver (DSB-SC only)


% Envelope detection
envelope = abs(hilbert(dsbsc_modulated));

% Low pass filtering


LPF_receiver = zeros(size(envelope));
LPF_receiver(abs(t_resampled) <= (length(t_resampled) / Fs_new) * f_cutoff) = 1;
envelope_filtered = envelope .* LPF_receiver;

% Resample to original sampling frequency


signal_received = resample(envelope_filtered, Fs, Fs_new);

% Plot received signal in time domain


figure;
subplot(2,1,1);
plot(t, real(signal_received));
xlabel('Time (s)');
ylabel('Amplitude');
title('Received Signal in Time Domain');

% Plot received signal in frequency domain


subplot(2,1,2);
signal_received_freq = fftshift(fft(signal_received));
f_received = linspace(-Fs/2, Fs/2, length(signal_received_freq));
plot(f_received, abs(signal_received_freq));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Received Signal in Frequency Domain');

% Play received audio


sound(real(signal_received), Fs);

You might also like