Telekomödev

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

4.12.

1
% (triplesinc.m)
% Baseband signal for AM
% Usage m = triplesinc(t, Ta)
function m = triplesinc(t, Ta)
% t is the length of the signal
% Ta is the parameter, equaling twice the delay
sig_1 = sinc(2 * t / Ta);
sig_2 = sinc(2 * t / Ta - 1);
sig_3 = sinc(2 * t / Ta + 1);
m = 2 * sig_1 + sig_2 + sig_3;
end

% (Main Script)
ts = 1.e-4; % Time step
t = -0.04:ts:0.04; % Time vector
Ta = 0.01; % Delay parameter
m_sig = triplesinc(t, Ta); % Generate the message signal

% FFT of message signal


Lfft = length(t);
Lfft = 2^ceil(log2(Lfft));
M_fre = fftshift(fft(m_sig, Lfft));
freqm = (-Lfft/2:Lfft/2-1)/(Lfft*ts);

% Modulation
s_dsb = m_sig .* cos(2 * pi * 500 * t); % DSB-SC modulated signal

% FFT of modulated signal


Lfft = length(t);
Lfft = 2^ceil(log2(Lfft) + 1);
S_dsb = fftshift(fft(s_dsb, Lfft));
freqs = (-Lfft/2:Lfft/2-1)/(Lfft*ts);

% Plotting time and frequency domain signals


Trange = [-0.03 0.03 -2 2];
Frange = [-600 600 0 200];

figure(1);

% Time domain for message signal


subplot(221);
plot(t, m_sig, 'LineWidth', 2);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it m}({\it t})');
title('Message Signal in Time Domain');

% Time domain for DSB-SC signal


subplot(223);
plot(t, s_dsb, 'LineWidth', 2);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it s}_{\rm DSB}({\it t})');
title('DSB-SC Signal in Time Domain');

% Frequency domain for message signal


subplot(222);
plot(freqm, abs(M_fre), 'LineWidth', 2);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it M}({\it f})');
title('Message Spectrum');
% Frequency domain for DSB-SC signal
subplot(224);
plot(freqs, abs(S_dsb), 'LineWidth', 2);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it S}_{rm DSB}({\it f})');
title('DSB-SC Spectrum');

% Display the figure


sgtitle('Example Signals in Time and Frequency Domains during DSB-SC
Modulation');

% Parameters
ts = 1.e-4;
t = -0.04:ts:0.04;
Ta = 0.01; % Delay parameter
fc = 300; % Carrier frequency

% Generate the message signal using tripuls for a triangular pulse


m_sig = tripuls((t + 0.01) / 0.01) - tripuls((t - 0.01) / 0.01);

% Modulate the message signal


s_dsb = m_sig .* cos(2 * pi * fc * t);

% Demodulate the DSB-SC signal


s_dem = s_dsb .* cos(2 * pi * fc * t) * 2;

% Design an ideal low-pass filter (LPF)


B_m = 150; % Bandwidth of the signal
h = fir1(40, [B_m * ts]); % FIR filter design
s_rec = filter(h, 1, s_dem); % Recovered signal

% Plotting the time domain signals


Trange = [-0.025 0.025 -2 2];
figure;

subplot(221);
plot(t, m_sig, 'LineWidth', 1.5);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it m}({\it t})');
title('Message Signal');

subplot(222);
plot(t, s_dsb, 'LineWidth', 1.5);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it s}_{\rm DSB}({\it t})');
title('DSB-SC Modulated Signal');

subplot(223);
plot(t, s_dem, 'LineWidth', 1.5);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it e}({\it t})');
title('Demodulated Signal');

subplot(224);
plot(t, s_rec, 'LineWidth', 1.5);
axis(Trange);
xlabel('{\it t} (sec)');
ylabel('{\it m}_d({\it t})');
title('Recovered Signal');
% Parameters
ts = 1.e-4; % Time step
t = -0.04:ts:0.04; % Time vector
Ta = 0.01; % Delay parameter
fc = 300; % Carrier frequency

% Generate the message signal using tripuls for a triangular pulse


m_sig = tripuls((t + 0.01) / 0.01) - tripuls((t - 0.01) / 0.01);

% Modulate the message signal


s_dsb = m_sig .* cos(2 * pi * fc * t);

% Demodulate the DSB-SC signal


s_dem = s_dsb .* cos(2 * pi * fc * t) * 2;

% Design an ideal low-pass filter (LPF)


B_m = 150; % Bandwidth of the signal
h = fir1(40, [B_m * ts]); % FIR filter design
s_rec = filter(h, 1, s_dem); % Recovered signal

% Frequency domain analysis


Lfft = length(t);
Lfft = 2^ceil(log2(Lfft)); % Length of FFT
M_fre = fftshift(fft(m_sig, Lfft)); % FFT of the message signal
freqm = (-Lfft/2:Lfft/2-1)/(Lfft*ts); % Frequency vector

S_dsb = fftshift(fft(s_dsb, Lfft)); % FFT of the modulated signal


freqs = (-Lfft/2:Lfft/2-1)/(Lfft*ts); % Frequency vector for DSB

S_dem = fftshift(fft(s_dem, Lfft)); % FFT of the demodulated signal


S_rec = fftshift(fft(s_rec, Lfft)); % FFT of the recovered signal

% Plotting the frequency domain signals


Frange = [-700 700 0 200]; % Frequency axis limits
figure;

subplot(221);
plot(freqm, abs(M_fre), 'LineWidth', 1.5);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it M}({\it f})');
title('Message Spectrum');

subplot(222);
plot(freqs, abs(S_dsb), 'LineWidth', 1.5);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it S}_{rm DSB}({\it f})');
title('DSB-SC Spectrum');

subplot(223);
plot(freqs, abs(S_dem), 'LineWidth', 1.5);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it E}({\it f})');
title('Spectrum of {\it e}({\it t})');

subplot(224);
plot(freqs, abs(S_rec), 'LineWidth', 1.5);
axis(Frange);
xlabel('{\it f} (Hz)');
ylabel('{\it M}_d({\it f})');
title('Recovered Spectrum');
4.12.2
% ExampleAMdemfilt.m
% This program illustrates AM modulation and demodulation using a triangular
message signal

% Clear workspace
clear all; close all; clc;

% Parameters
ts = 1.e-4; % Sampling interval
t = -0.04:ts:0.04; % Time vector
Ta = 0.01; % Message signal period
fc = 500; % Carrier frequency
function y = triangl(t)
% Generates a triangular waveform
% t is the input time vector, y is the output triangular waveform
y = (1 - abs(t)) .* (abs(t) <= 1); % Generates a triangle wave between -1 and
1
end
% Message signal (triangular waveform using triangl.m function)
m_sig = triangl((t + 0.01) / 0.01) - triangl((t - 0.01) / 0.01);
Lm_sig = length(m_sig);
Lfft = length(t);
Lfft = 2^ceil(log2(Lfft)); % FFT length as a power of 2
M_fre = fftshift(fft(m_sig, Lfft)); % FFT of message signal
freqm = (-Lfft/2:Lfft/2-1) / (Lfft * ts); % Frequency vector for message signal

B_m = 150; % Bandwidth of the message signal (Hz)

% FIR lowpass filter with 150 Hz cutoff (normalized cutoff frequency)


h = fir1(40, B_m*ts*2); % Multiplying B_m*ts by 2 to normalize to
Nyquist frequency

% AM signal generation (with carrier)


s_am = (1 + m_sig) .* cos(2 * pi * fc * t);
Lfft = length(t);
Lfft = 2^ceil(log2(Lfft) + 1); % Update FFT length
S_am = fftshift(fft(s_am, Lfft)); % FFT of AM modulated signal
freqs = (-Lfft/2:Lfft/2-1) / (Lfft * ts); % Frequency vector for AM signal

% Demodulation using rectification (non-coherent)


s_dem = s_am .* (s_am > 0); % Rectified signal
S_dem = fftshift(fft(s_dem, Lfft)); % FFT of rectified signal

% Ideal LPF with bandwidth 150 Hz


s_rec = filter(h, 1, s_dem); % Recovered signal after lowpass filter
S_rec = fftshift(fft(s_rec, Lfft)); % FFT of recovered signal
% Plotting time domain signals
Trange = [-0.025 0.025 -2 2]; % Time range for plotting
figure(1);

% Plot message signal


subplot(221);
td1 = plot(t, m_sig);
axis(Trange);
set(td1, 'LineWidth', 1.5);
xlabel('t (sec)');
ylabel('m(t)');
title('Message signal');

% Plot AM modulated signal


subplot(222);
td2 = plot(t, s_am);
axis(Trange);
set(td2, 'LineWidth', 1.5);
xlabel('t (sec)');
ylabel('s_{DSB}(t)');
title('AM modulated signal');

% Plot rectified signal


subplot(223);
td3 = plot(t, s_dem);
axis(Trange);
set(td3, 'LineWidth', 1.5);
xlabel('t (sec)');
ylabel('e(t)');
title('Rectified signal without local carrier');

% Plot recovered signal after LPF


subplot(224);
td4 = plot(t, s_rec);
Trangelow = [-0.025 0.025 -0.5 1]; % Adjusted axis for detected signal
axis(Trangelow);
set(td4, 'LineWidth', 1.5);
xlabel('t (sec)');
ylabel('m_d(t)');
title('Detected signal');

% Plotting frequency domain signals


Frange = [-700 700 0 200]; % Frequency range for plotting

figure(2)

% Plot message signal spectrum


subplot(221);
fd1 = plot(freqm, abs(M_fre)); % Frequency domain of message signal
axis(Frange); % Make sure 'Frange' is defined
set(fd1, 'LineWidth', 1.5); % Correct tırnak işareti
xlabel('{\it f} (Hz)');
ylabel('{\it M}({\it f})');
title('Message spectrum');

% Plot AM signal spectrum


subplot(222);
fd2 = plot(freqs, abs(S_am)); % Frequency domain of AM modulated signal
axis(Frange);
set(fd2, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)');
ylabel('{\it S}_{\rm AM}({\it f})');
title('AM spectrum');
% Plot rectified signal spectrum
subplot(223);
fd3 = plot(freqs, abs(S_dem)); % Frequency domain of rectified signal
axis(Frange);
set(fd3, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)');
ylabel('{\it E}({\it f})');
title('Rectified spectrum');

% Plot recovered signal spectrum after LPF


subplot(224);
fd4 = plot(freqs, abs(S_rec)); % Frequency domain of recovered signal
axis(Frange);
set(fd4, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)');
ylabel('{\it M}_d({\it f})');
title('Recovered spectrum');
4.12.3
% (ExampleSSBdemfilt.m)
% This program illustrates SSB modulation and demodulation
clear; clf;

ts = 1.e-4;
t = -0.04:ts:0.04;
Ta = 0.01;
fc = 300;

% Define triangular signal


m_sig = (1 - abs(2 * (t / Ta) - 1)); % Normalized triangular waveform
m_sig((t < -Ta/2) | (t > Ta/2)) = 0; % Set outside the triangle to zero

Lm_sig = length(m_sig);
Lfft = length(t);
Lfft = 2^ceil(log2(Lfft));
M_fre = fftshift(fft(m_sig, Lfft));
freqm = (-Lfft/2:Lfft/2-1)/(Lfft*ts);
B_m = 150; % Bandwidth of the signal is B_m Hz.
h = fir1(40, [B_m * ts]);

% DSB modulation
s_dsb = m_sig .* cos(2 * pi * fc * t);
Lfft = length(t);
Lfft = 2^ceil(log2(Lfft) + 1);
S_dsb = fftshift(fft(s_dsb, Lfft));
L_lsb = floor(fc * ts * Lfft);
SSBfilt = ones(1, Lfft);
SSBfilt(Lfft/2 - L_lsb + 1:Lfft/2 + L_lsb) = zeros(1, 2 * L_lsb);
S_ssb = S_dsb .* SSBfilt;
freqs = (-Lfft/2:Lfft/2-1)/(Lfft*ts);
s_ssb = real(ifft(fftshift(S_ssb)));
s_ssb = s_ssb(1:Lm_sig);

% Demodulation
s_dem = s_ssb .* cos(2 * pi * fc * t) * 2;
S_dem = fftshift(fft(s_dem, Lfft));

% Using an ideal LPF with bandwidth 150 Hz


s_rec = filter(h, 1, s_dem);
S_rec = fftshift(fft(s_rec, Lfft));

Trange = [-0.025 0.025 -1 1];

% Time domain plots


figure(1)
subplot(221); td1 = plot(t, m_sig);
axis(Trange); set(td1, 'Linewidth', 1.5);
xlabel('{t} (sec)'); ylabel('{m}(t)');
title('message signal');

subplot(222); td2 = plot(t, s_ssb);


axis(Trange); set(td2, 'Linewidth', 1.5);
xlabel('{t} (sec)'); ylabel('{s}_{SSB}(t)');
title('SSB-SC modulated signal');
subplot(223); td3 = plot(t, s_dem);
axis(Trange); set(td3, 'Linewidth', 1.5);
xlabel('{t} (sec)'); ylabel('{e}(t)');
title('after multiplying local carrier');

subplot(224); td4 = plot(t, s_rec);


axis(Trange); set(td4, 'Linewidth', 1.5);
xlabel('{t} (sec)'); ylabel('{m}_d(t)');
title('Recovered signal');

Frange = [-700 700 0 200];

% Frequency domain plots


figure(2)
subplot(221); fd1 = plot(freqm, abs(M_fre));
axis(Frange); set(fd1, 'Linewidth', 1.5);
xlabel('{f} (Hz)'); ylabel('{M}(f)');
title('message spectrum');

subplot(222); fd2 = plot(freqs, abs(S_ssb));


axis(Frange); set(fd2, 'Linewidth', 1.5);
xlabel('{f} (Hz)'); ylabel('{S}_{rm DSB}(f)');
title('upper sideband SSB-SC spectrum');

subplot(223); fd3 = plot(freqs, abs(S_dem));


axis(Frange); set(fd3, 'Linewidth', 1.5);
xlabel('{f} (Hz)'); ylabel('{E}(f)');
title('detector spectrum');

subplot(224); fd4 = plot(freqs, abs(S_rec));


axis(Frange); set(fd4, 'Linewidth', 1.5);
xlabel('{f} (Hz)'); ylabel('{M}_d(f)');
title('recovered spectrum');

4.12.4
function y = triangl(t)
% Triangular function
y = max(1 - abs(t), 0);
end
function y = triplesinc(t, Ta)
% Triple sinc function
y = sinc(t/Ta) .* sinc(t/Ta) .* sinc(t/Ta);
end
% (ExampleQAMdemfilt.m)
% This program uses triangl.m and triplesinc.m
% to illustrate QAM modulation and demodulation
% of two message signals

clear; clf;
ts = 1.e-4; % Sampling interval
t = -0.04:ts:0.04; % Time vector
Ta = 0.01; % Message signal period
fc = 300; % Carrier frequency

% Use triangl.m and triplesinc.m to generate


% two message signals of different shapes and spectra
m_sig1 = triangl((t + 0.01) / 0.01) - triangl((t - 0.01) / 0.01);
m_sig2 = triplesinc(t, Ta); % Generate the second message signal

Lm_sig = length(m_sig1);
Lfft = length(t); Lfft = 2^ceil(log2(Lfft));

% Compute FFT of the message signals


M1_fre = fftshift(fft(m_sig1, Lfft));
M2_fre = fftshift(fft(m_sig2, Lfft));
freqm = (-Lfft/2:Lfft/2-1)/(Lfft*ts);

B_m = 150; % Bandwidth of the signal

% Design a simple lowpass filter with bandwidth B_m Hz.


h = fir1(40, B_m/(0.5/ts)); % Correct normalization for FIR filter

% QAM signal generated by adding a carrier to DSB-SC


s_qam = m_sig1 .* cos(2 * pi * fc * t) + m_sig2 .* sin(2 * pi * fc * t);
Lfft = length(t); Lfft = 2^ceil(log2(Lfft) + 1);
S_qam = fftshift(fft(s_qam, Lfft));
freqs = (-Lfft/2:Lfft/2-1)/(Lfft*ts);
% Demodulation begins by using a rectifier
s_dem1 = s_qam .* cos(2 * pi * fc * t) * 2;
S_dem1 = fftshift(fft(s_dem1, Lfft));

% Demodulate the 2nd signal


s_dem2 = s_qam .* sin(2 * pi * fc * t) * 2;
S_dem2 = fftshift(fft(s_dem2, Lfft));

% Using an ideal LPF with bandwidth 150 Hz


s_rec1 = filter(h, 1, s_dem1);
S_rec1 = fftshift(fft(s_rec1, Lfft));

s_rec2 = filter(h, 1, s_dem2);


S_rec2 = fftshift(fft(s_rec2, Lfft));

% Time range for plotting


Trange = [-0.025 0.025 -2 2];
Trange2 = [-0.025 0.025 -2 4];

% Time domain plots


figure(1)
subplot(221);
td1 = plot(t, m_sig1);
axis(Trange); set(td1, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it m}({\it t})');
title('Message signal 1');

subplot(222);
td2 = plot(t, s_qam);
axis(Trange); set(td2, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it s}_{\rm DSB}({\it t})');
title('QAM modulated signal');
subplot(223);
td3 = plot(t, s_dem1);
axis(Trange2); set(td3, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it x}({\it t})');
title('First demodulator output');

subplot(224);
td4 = plot(t, s_rec1);
axis(Trange); set(td4, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it m}_{d1}({\it t})');
title('Detected signal 1');

% Second signal plots


figure(2)
subplot(221);
td5 = plot(t, m_sig2);
axis(Trange); set(td5, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it m}({\it t})');
title('Message signal 2');

subplot(222);
td6 = plot(t, s_qam);
axis(Trange); set(td6, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it s}_{\rm DSB}({\it t})');
title('QAM modulated signal');

subplot(223);
td7 = plot(t, s_dem2);
axis(Trange2); set(td7, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it e}_1({\it t})');
title('Second demodulator output');
subplot(224);
td8 = plot(t, s_rec2);
axis(Trange); set(td8, 'LineWidth', 1.5);
xlabel('{\it t} (sec)'); ylabel('{\it m}_{d2}({\it t})');
title('Detected signal 2');

% Frequency domain plots


Frange = [-700 700 0 250];
figure(3)
subplot(221);
fd1 = plot(freqm, abs(M1_fre));
axis(Frange); set(fd1, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it M}({\it f})');
title('Message 1 spectrum');

subplot(222);
fd2 = plot(freqs, abs(S_qam));
axis(Frange); set(fd2, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it S}_{rm AM}({\it f})');
title('QAM spectrum magnitude');

subplot(223);
fd3 = plot(freqs, abs(S_dem1));
axis(Frange); set(fd3, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it E}_1({\it f})');
title('First demodulator spectrum');

subplot(224);
fd4 = plot(freqs, abs(S_rec1));
axis(Frange); set(fd4, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it M}_{d1}({\it f})');
title('Recovered spectrum 1');

figure(4)
subplot(221);
fd1 = plot(freqm, abs(M2_fre));
axis(Frange); set(fd1, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it M}({\it f})');
title('Message 2 spectrum');

subplot(222);
fd2 = plot(freqs, abs(S_qam));
axis(Frange); set(fd2, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it S}_{rm AM}({\it f})');
title('QAM spectrum magnitude');

subplot(223);
fd7 = plot(freqs, abs(S_dem2));
axis(Frange); set(fd7, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it E}_2({\it f})');
title('Second demodulator spectrum');

subplot(224);
fd8 = plot(freqs, abs(S_rec2));
axis(Frange); set(fd8, 'LineWidth', 1.5);
xlabel('{\it f} (Hz)'); ylabel('{\it M}_{d2}({\it f})');
title('Recovered spectrum 2');

You might also like