KEMBAR78
Cs Lab Codes | PDF | Frequency Modulation | Algorithms
0% found this document useful (0 votes)
21 views20 pages

Cs Lab Codes

Uploaded by

bro665444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Cs Lab Codes

Uploaded by

bro665444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CS LAB CODES

DELTA MODULATION

clc;

clear all;

am = 1; % AMPLITUDE OF MESSAGE SIGNAL

fm = 2; % FREQUENCY OF MESSAGE SIGNAL

fs = 50 * fm; % SAMPLING FREQUENCY

t = 1;

n = 0:1/fs:t;

m = am * sin(2 * pi * fm * n); % MESSAGE SIGNAL

% Plotting the Message Signal

subplot(3,1,1)

plot(n, m);

ylim([-1.5 1.5]);

xlabel('time');

ylabel('m(t)');

title('MESSAGE SIGNAL');

d = 0.15; % Step size

% Initializing for the first sample

e(1) = m(1);

eq(1) = d * sign(e(1));

mp(1) = eq(1);

% Loop for generating delta modulated signal

for i = 2:length(m)

e(i) = m(i) - mp(i-1); % Error signal


eq(i) = d * sign(e(i)); % Quantized error

mp(i) = mp(i-1) + eq(i); % Modulated signal

end

% Plotting the Delta Modulated (Staircase) Signal

subplot(3,1,2)

plot(n, m);

hold on;

stairs(n, mp, 'red'); % Delta modulated signal

legend('MESSAGE SIGNAL', 'STAIRCASE SIGNAL');

ylim([-1.5 1.5]);

xlabel('time');

ylabel('mp(t)');

title('STAIRCASE SIGNAL');

% Generating Transmitting Signal

et = zeros(1, length(eq));

for j = 1:length(eq)

if eq(j) == d

et(j) = 1;

end

end

% Plotting the Transmitted Signal

subplot(3,1,3)

stairs(n, et);

ylim([-0.5 1.5]);

xlabel('time');

ylabel('et(t)');

title('TRANSMITTED SIGNAL');
% Decoding the Received Signal

ed = zeros(1, length(et));

for j = 2:length(et)

if et(j) == 0

ed(j) = -d;

else

ed(j) = d;

end

end

mqd = zeros(1, length(ed));

mqd(1) = 0;

for i = 2:length(ed)

mqd(i) = ed(i) + mqd(i-1);

end

% Smoothing the Received Signal

mn = smooth(mqd, 9, 'moving'); % Using moving average of 9 samples as parameter

% Plotting the Received Signal

figure;

plot(n, mn, n, m);

hold on;

stairs(n, mqd);

legend('RECEIVED SIGNAL', 'MESSAGE SIGNAL', 'RECEIVED STAIRCASE SIGNAL');

xlabel('time');

ylabel('o(t)');

title('ALL SIGNALS');
PCM MODULATION/DEMODULATION

clc;

clear all;

close all;

% Parameters

Ac = 50; % Amplitude of carrier signal

fc = 5; % Frequency of carrier signal

A = 1; % Amplitude of message signal

fm = 2; % Frequency of message signal

fs = 20; % Sampling frequency (samples per second)

n = 3; % Number of bits for quantization

Wc = 2 * pi * fc; % Angular frequency of carrier signal

% Time vector

t = 0:0.0001:1; % Time for continuous signal

ts = 0:1/fs:1; % Time vector for discrete sampling (1 second duration)

% Original carrier signal

c = Ac * cos(Wc * t);

% Message signal (sampled version of the sine wave)

xs = A * cos(2 * pi * fm * ts);

% ---Quantization---

x1 = xs + A; % Normalization (shift signal to be positive)

x1 = x1 / (2 * A); % Scale to the range [0, 1]

L = 2^n - 1; % Number of quantization levels (e.g., for 3 bits, L = 7)

x1 = L * x1; % Scale to quantization levels

xq = round(x1); % Round to nearest integral level

r = xq / L; % Normalize back to range [0, 1]


r = 2 * A * r; % Scale back to the original amplitude

r = r - A; % Shift back to original range

% ---Encoding---

y = []; % Store binary PCM code

for i = 1:length(xq)

d = dec2bin(xq(i), n); % Convert each quantized value to binary

y = [y, double(d) - 48]; % Convert binary string to number and append to y

end

% Bitrate calculation

Bitrate = n * fs;

% ---Demodulation---

% Convert binary signal back to quantized levels

xq_demod = zeros(1, length(ts)); % Initialize demodulated quantized levels

index = 1; % Initialize binary index

for i = 1:length(xq)

bin_str = y(index:index+n-1); % Extract n-bit binary string

xq_demod(i) = bin2dec(num2str(bin_str)); % Convert binary string to decimal

index = index + n; % Move to the next n bits

end

% Decode quantized levels back to the original signal range

x_demod = (xq_demod / L) * (2 * A) - A;

% Interpolation to reconstruct the signal

t_interp = 0:0.0001:1; % Fine time grid for interpolation

x_reconstructed = interp1(ts, x_demod, t_interp, 'previous'); % Zero-order hold interpolation


% Plot the original and sampled signals

figure;

subplot(4,1,1);

plot(t, A * cos(2 * pi * fm * t), 'linewidth', 2);

title('Sampling');

ylabel('Amplitude');

xlabel('Time t (in sec)');

hold on;

stem(ts, xs, 'r', 'linewidth', 2);

hold off;

legend('Original Signal', 'Sampled Signal');

% Plot the quantized signal

subplot(4,1,2);

stem(ts, x1, 'linewidth', 2);

title('Quantization');

ylabel('Levels L');

hold on;

stem(ts, xq, 'r', 'linewidth', 2);

plot(ts, xq, '--r');

plot(t, (A * cos(2 * pi * fm * t) + A) * L / (2 * A), '--b');

grid;

hold off;

legend('Sampled Signal', 'Quantized Signal');

% Plot the encoded binary signal

subplot(4,1,3);

stairs([y, y(end)], 'linewidth', 2); % PCM encoded binary signal

title('Encoding');
ylabel('Binary Signal');

xlabel('Bits');

axis([0 length(y) -1 2]);

grid;

% Plot the reconstructed signal

subplot(4,1,4);

plot(t_interp, x_reconstructed, 'b', 'linewidth', 2);

title('Reconstructed Signal (Demodulated)');

ylabel('Amplitude');

xlabel('Time t (in sec)');

hold on;

plot(t, A * cos(2 * pi * fm * t), '--r'); % Correct: compare with cosine

hold off;

legend('Reconstructed Signal', 'Original Signal');

grid on;
PHASE MODULATION/DEMODULATION

clear all;

close all;

clc;

% Parameters

fc = 5; % Carrier frequency (Hz)

kp = 2 * pi; % Phase deviation constant (radians per unit amplitude of m)

fm = 1; % Message signal frequency (Hz)

Am = 1; % Amplitude of message signal

Ac = 1; % Amplitude of carrier signal

fs = 100; % Sampling frequency (samples per second)

t = 0:1/fs:1; % Time vector (1 second duration)

% Message signal (sine wave)

m = Am * sin(2 * pi * fm * t); % Original message signal

% Phase Modulated Signal

s_pm = Ac * cos(2 * pi * fc * t + kp * m); % Phase modulated signal

% Phase Demodulation using Hilbert Transform

analytic_signal = hilbert(s_pm); % Compute the analytic signal

phase = angle(analytic_signal); % Extract the phase

demodulated_message = unwrap(phase - 2 * pi * fc * t); % Unwrap the phase and remove carrier


phase

% Normalize the demodulated message to match the amplitude of the original message

demodulated_message = demodulated_message / max(abs(demodulated_message)) * Am;

% Plotting the results

figure;
% Plot original message signal

subplot(4, 1, 1);

plot(t, m);

xlabel('Time (s)');

ylabel('Amplitude');

title('Original Message Signal (Sine Wave)');

grid on;

% Plot PM signal

subplot(4, 1, 2);

plot(t, s_pm);

xlabel('Time (s)');

ylabel('Amplitude');

title('Phase Modulated Signal (PM)');

grid on;

% Plot demodulated message signal

subplot(4, 1, 3);

plot(t, demodulated_message);

xlabel('Time (s)');

ylabel('Amplitude');

title('Demodulated Message Signal Using Hilbert Transform');

grid on;

% Compute and plot the FFT for the demodulated message signal

M_demod = fft(demodulated_message);

N_demod = length(demodulated_message); % Number of samples

f_demod = (-N_demod/2:N_demod/2-1)*(fs/N_demod); % Frequency vector

subplot(4, 1, 4);
plot(f_demod, abs(fftshift(M_demod))/N_demod);

xlabel('Frequency (Hz)');

ylabel('Magnitude');

title('Spectrum of Demodulated Message Signal');

grid on;
DSB-SC MODULATION

% Parameters

fs = 100; % Sampling frequency (Hz)

t = 0:1/fs:1-1/fs; % Time vector

fc = 10; % Carrier frequency (Hz)

f_m = 10; % Message signal frequency (Hz)

% Message signal

m_t = cos(2*pi*f_m*t);

% DSB-SC modulation (Double Sideband Suppressed Carrier)

dsbsc = m_t .* cos(2*pi*fc*t);

% Fourier Transform of the DSB-SC signal

N = length(dsbsc);

f = (-N/2:N/2-1)*(fs/N); % Frequency vector

DSBSC_spectrum = fftshift(fft(dsbsc));

% Filter to obtain the Upper Sideband (USB)

usb_filt = double(f > fc); % Pass frequencies above carrier frequency

USB_spectrum = usb_filt .* DSBSC_spectrum;

% Inverse Fourier Transform to get the time-domain USB signal

usb = ifft(ifftshift(USB_spectrum));

% Filter to obtain the Lower Sideband (LSB)

lsb_filt = double(f < -fc); % Pass frequencies below negative carrier frequency

LSB_spectrum = lsb_filt .* DSBSC_spectrum;

% Inverse Fourier Transform to get the time-domain LSB signal

lsb = ifft(ifftshift(LSB_spectrum));
% Plot the Message Signal, LSB, and USB

figure;

% Time-domain plots

subplot(3,2,1);

plot(t, m_t);

title('Message Signal');

xlabel('Time (s)');

ylabel('Amplitude');

subplot(3,2,3);

plot(t, lsb);

title('Lower Sideband (LSB) Signal');

xlabel('Time (s)');

ylabel('Amplitude');

subplot(3,2,5);

plot(t, usb);

title('Upper Sideband (USB) Signal');

xlabel('Time (s)');

ylabel('Amplitude');

% Frequency-domain plots

subplot(3,2,2);

plot(f, abs(fftshift(fft(m_t))));

title('Message Signal Spectrum');

xlabel('Frequency (Hz)');

ylabel('Magnitude');

subplot(3,2,4);
plot(f, abs(LSB_spectrum));

title('Lower Sideband (LSB) Spectrum');

xlabel('Frequency (Hz)');

ylabel('Magnitude');

subplot(3,2,6);

plot(f, abs(USB_spectrum));

title('Upper Sideband (USB) Spectrum');

xlabel('Frequency (Hz)');

ylabel('Magnitude');
AM SIGNAL UNDER, CRITICAL , OVER MODULATION

T = 1e-3; % Total time duration for the signal

t = 0:T:10; % Time vector from 0 to 10 with step T

N = length(t); % Number of samples

Am = 5; % Amplitude of message signal

ka = 1/10; % Modulation index (under modulation)

fm = 1; % Frequency of message signal

fc = 10; % Frequency of carrier signal

Ac = 1; % Amplitude of carrier signal

ct = Ac*cos(2*pi*fc*t); % Carrier signal

mt = Am*cos(2*pi*fm*t); % Message signal

df = 1/(N*T); % Frequency resolution

f = 0:df:(N-1)*df; % Frequency vector

mf = (1e-3)*abs(fft(mt)); % Fourier transform of message signal

cf = (1e-3)*abs(fft(ct)); % Fourier transform of carrier signal

% Plotting message signal in time domain

subplot(5,2,1);

plot(t, mt);

grid on;

xlabel('t');

ylabel('m(t)');

title('Message Signal');

% Plotting message signal in frequency domain

subplot(5,2,2);

plot(f, mf);

xlim([0,10]);

ylim([0,60]);
grid on;

xlabel('f');

ylabel('M(f)');

title('Message Signal (Frequency Domain)');

% Plotting carrier signal in time domain

subplot(5,2,3);

plot(t, ct);

grid on;

xlabel('t');

ylabel('c(t)');

title('Carrier Signal');

% Plotting carrier signal in frequency domain

subplot(5,2,4);

plot(f, cf);

xlim([0,20]);

ylim([0,60]);

grid on;

xlabel('f');

ylabel('C(f)');

title('Carrier Signal (Frequency Domain)');

% Under Modulation

mod = (1 + ka*mt).*ct;

mod_f = (1e-3)*abs(fft(mod));

subplot(5,2,5);

plot(t, mod);

title('AM Signal for ka = 1/10 (Under Modulation)');

ylabel('s(t)');
xlabel('t');

grid on;

subplot(5,2,6);

plot(f, mod_f);

xlim([0,2*fc]);

xticks(0:1:2*fc);

ylim([0,60]);

ylabel('S(f)');

xlabel('f');

title('AM Spectrum for ka = 1/10 (Under Modulation)');

grid on;

% Perfect Modulation

ka1 = 1/5;

mod1 = (1 + ka1*mt).*ct;

mod_f1 = (1e-3)*abs(fft(mod1));

subplot(5,2,7);

plot(t, mod1);

title('AM Signal for ka = 1/5 (Perfect Modulation)');

xlabel('t');

ylabel('s(t)');

grid on;

subplot(5,2,8);

plot(f, mod_f1);

xlim([0,2*fc]);

xticks(0:1:2*fc);

ylim([0,60]);

ylabel('S(f)');
xlabel('f');

title('AM Spectrum for ka = 1/5 (Perfect Modulation)');

grid on;

% Over Modulation

ka2 = 1;

mod2 = (1 + ka2*mt).*ct;

mod_f2 = (1e-3)*abs(fft(mod2));

subplot(5,2,9);

plot(t, mod2);

title('AM Signal for ka = 1 (Over Modulation)');

xlabel('t');

ylabel('s(t)');

grid on;

subplot(5,2,10);

plot(f, mod_f2);

xlim([0,2*fc]);

xticks(0:1:2*fc);

ylim([0,60]);

ylabel('S(f)');

xlabel('f');

title('AM Spectrum for ka = 1 (Over Modulation)');

grid on;
AWGN AM SIGNAL MOD/DEMOD

% MATLAB code to demodulate an AM signal with AWGN

% Parameters

Fs = 1e5; % Sampling frequency (Hz)

Fc = 10e3; % Carrier frequency (Hz)

Fm = 1e3; % Message signal frequency (Hz)

Am = 1; % Amplitude of message signal

Ac = 2; % Amplitude of carrier signal

SNR = 10; % Signal-to-Noise Ratio (dB)

% Time vector (1 second duration)

t = 0:1/Fs:1-1/Fs;

% Message signal (sine wave instead of cosine)

message_signal = Am * sin(2*pi*Fm*t); % Sine wave message signal

% Carrier signal (high-frequency signal)

carrier_signal = Ac * cos(2*pi*Fc*t);

% AM signal (modulated signal)

AM_signal = (Ac + message_signal) .* cos(2*pi*Fc*t);

% Add AWGN noise manually

signal_power = var(AM_signal); % Power of the AM signal

noise_power = signal_power / (10^(SNR/10)); % Power of noise

noise = sqrt(noise_power) * randn(size(t)); % Gaussian noise

% Noisy AM signal

noisy_signal = AM_signal + noise;


% Create a figure with multiple subplots

figure;

% Zooming into a smaller time window (e.g., 0 to 0.02 seconds)

zoom_time_window = [0, 0.02]; % Zoom into the first 0.02 seconds

% Plot the original AM signal (subplot 1)

subplot(4, 1, 1);

plot(t, AM_signal, 'LineWidth', 0.5); % Thinest line possible (default or 0.5)

title('Original AM Signal');

xlabel('Time (s)');

ylabel('Amplitude');

xlim(zoom_time_window); % Zoom into the first 0.02 seconds

ylim([-Ac-0.5, Ac+0.5]); % Adjust amplitude to see the full signal

grid on; % Add grid for better visibility

% Plot the noisy AM signal (subplot 2)

subplot(4, 1, 2);

plot(t, noisy_signal, 'LineWidth', 0.5); % Thinest line possible

title('Noisy AM Signal with AWGN');

xlabel('Time (s)');

ylabel('Amplitude');

xlim(zoom_time_window); % Zoom into the first 0.02 seconds

ylim([-Ac-0.5, Ac+0.5]); % Adjust amplitude to see the full signal

grid on; % Add grid

% Demodulate the noisy AM signal (envelope detection using Hilbert transform)

demodulated_signal = abs(hilbert(noisy_signal));

% Apply a low-pass filter to remove high-frequency components (carrier)

% Create a low-pass filter (e.g., with cutoff frequency Fm)


cutoff_freq = 2 * Fm; % Set cutoff to just above the message frequency

[b, a] = butter(6, cutoff_freq / (Fs / 2), 'low'); % 6th-order Butterworth filter

filtered_demodulated_signal = filter(b, a, demodulated_signal);

% Plot the filtered demodulated signal (subplot 3)

subplot(4, 1, 3);

plot(t, filtered_demodulated_signal, 'LineWidth', 0.5); % Thinest line possible

title('Filtered Demodulated Signal (Envelope Detection + Low-pass Filter)');

xlabel('Time (s)');

ylabel('Amplitude');

xlim(zoom_time_window); % Zoom into the first 0.02 seconds

ylim([-Ac-0.5, Ac+0.5]); % Adjust vertical limits

grid on; % Add grid

% Extract the message signal by removing the carrier component

demodulated_message = filtered_demodulated_signal - Ac;

% Plot the extracted message signal (subplot 4)

subplot(4, 1, 4);

plot(t, demodulated_message, 'LineWidth', 0.5); % Thinest line possible

title('Extracted Message Signal');

xlabel('Time (s)');

ylabel('Amplitude');

xlim(zoom_time_window); % Zoom into the first 0.02 seconds

ylim([-Am-0.5, Am+0.5]); % Adjust amplitude to focus on the message signal

grid on; % Add grid

You might also like