0% found this document useful (0 votes)
11 views20 pages

DC LAB

The document outlines three experiments related to signal processing: Natural PAM and Flat-top PAM sampling, PCM modulation and demodulation, and Mu-law companding. Each section includes MATLAB code for generating signals, sampling, quantization, encoding, and reconstructing signals, along with visualizations of the results. The experiments demonstrate the effects of different modulation techniques and quantization methods on signal quality and reconstruction.
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)
11 views20 pages

DC LAB

The document outlines three experiments related to signal processing: Natural PAM and Flat-top PAM sampling, PCM modulation and demodulation, and Mu-law companding. Each section includes MATLAB code for generating signals, sampling, quantization, encoding, and reconstructing signals, along with visualizations of the results. The experiments demonstrate the effects of different modulation techniques and quantization methods on signal quality and reconstruction.
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

EXP 1 PAM NATURAL FLATTOP SAMPLING

%Natural Sampling & Flat-top Sampling


clc;
clear all;
close all;

%Date and Time of Simulation


date = string(datetime("now"));

%Sampling Frequency
FS = 1000;
TS = 1/FS;
t = 0:TS:2-TS;

%Message Signal
F0 = 1;
msg = sin(2*pi*F0*t);

%Pulse Carrier
FC = 20*F0;
dutycycle = 50;
pulse = square(2*pi*FC*t, dutycycle);
pulse(pulse<0) = 0;

%Natural Pulse Amplitude Modulation


Natural_PAM = msg.*pulse;

%Flat-top Pulse Amplitude Modulation


Flat_top_PAM = flat_top_pam(t, FC, msg, dutycycle);

%Demodulation of PAM with Natural sampling


F_PB = F0; %Cut-off frequency
demod_Natural_PAM = signal_lpf(F_PB, FS, Natural_PAM);
Lowpass_Natural_PAM = lowpass(Natural_PAM, F_PB, FS);

%Demodulation of PAM with Natural sampling


demod_Flat_top_PAM = signal_lpf(F_PB, FS, Flat_top_PAM);
Lowpass_Flat_top_PAM = lowpass(Flat_top_PAM, F_PB, FS);

figure(1);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Message Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

figure(2);
plot(t, pulse, 'b', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Pulse Carrier Signal', date);
axis([0 2 0 1.05]);
grid on;

figure(3);
plot(t, Natural_PAM, 'k', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Natural Sampled PAM Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

figure(4);
subplot(3,1,1);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Message Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

subplot(3,1,2);
plot(t, pulse, 'b', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Pulse Carrier Signal', date);
axis([0 2 0 1.05]);
grid on;

subplot(3,1,3);
plot(t, Natural_PAM, 'k', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Natural Sampling', date);
axis([0 2 -1.05 1.05]);
grid on;

figure(5);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, 2*demod_Natural_PAM, 'cy', 'LineWidth', 2);
%hold on;
%plot(t, Lowpass_PAM, 'm', 'LineWidth', 2);
%hold off;
xlabel('time');
ylabel('amplitude');
title('Demodulated PAM Signal', date);
axis([0 2 -1.05 1.05]);
legend('Message Signal', 'Demodulated PAM Signal');
%legend('Message Signal', 'Demodulated PAM Signal', 'Low Pass Filtered');
grid on;

figure(6);
plot(t, Flat_top_PAM, 'r', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Flat-top PAM Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

figure(7);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, 2*demod_Flat_top_PAM, 'cy', 'LineWidth', 2);
hold on;
%plot(t, Lowpass_Flat_top_PAM, 'm', 'LineWidth', 2);
%hold off;
xlabel('time');
ylabel('amplitude');
title('Demodulated Flat-top PAM Signal', date);
axis([0 2 -1.05 1.05]);
legend('Message Signal', 'Demodulated Flat-top PAM Signal');
%legend('Message Signal', 'Demodulated Flat-top PAM Signal', 'Low Pass Filtered');
grid on;

figure(8);
subplot(4,2,[1,2]);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Message Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

subplot(4,2,[3,4]);
plot(t, pulse, 'b', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Pulse Carrier Signal', date);
axis([0 2 0 1.05]);
grid on;

subplot(4,2,5);
plot(t, Natural_PAM, 'k', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Natural Sampled PAM Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

subplot(4,2,6);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, 2*demod_Natural_PAM, 'cy', 'LineWidth', 2);
%hold on;
%plot(t, Lowpass_PAM, 'm', 'LineWidth', 2);
%hold off;
xlabel('time');
ylabel('amplitude');
title('Demodulated PAM Signal', date);
axis([0 2 -1.05 1.05]);
legend('Message Signal', 'Demodulated PAM Signal');
%legend('Message Signal', 'Demodulated PAM Signal', 'Low Pass Filtered');
grid on;

subplot(4,2,7);
plot(t, Flat_top_PAM, 'r', 'LineWidth', 2);
xlabel('time');
ylabel('amplitude');
title('Flat-top PAM Signal', date);
axis([0 2 -1.05 1.05]);
grid on;

subplot(4,2,8);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, 2*demod_Flat_top_PAM, 'cy', 'LineWidth', 2);
hold on;
%plot(t, Lowpass_Flat_top_PAM, 'm', 'LineWidth', 2);
%hold off;
xlabel('time');
ylabel('amplitude');
title('Demodulated Flat-top PAM Signal', date);
axis([0 2 -1.05 1.05]);
legend('Message Signal', 'Demodulated Flat-top PAM Signal');
%legend('Message Signal', 'Demodulated Flat-top PAM Signal', 'Low Pass Filtered');
grid on;

EXP2 PCM MODULATION AND DEMODULATION

clc;
clear all;
close all;

%Date and Time


date = string(datetime('now'));

FS = 1000;%Sampling Frequency
TS = 1/FS;
t = 0:TS:2-TS;

%Generation of Sinusoidal Signal


Am = 1;%Amplitude of the Sinusoidal Signal
Fm = 1;%Frequency of the Sinusoidal Signal
msg = Am*sin(2*pi*Fm*t);

sample_factor = 20;
tn = downsample(t,sample_factor);
sampled_msg = downsample(msg,sample_factor);

%Quantization
n = 4;%no. of bits for quantization
L = 2^n;%no. of levels
Vmax = max(msg);
Vmin = min(msg);
del = (Vmax-Vmin)/L;
partions = Vmin+del:del:Vmax-del;%Defining the levels
approx_val = Vmin+(del/2):del:Vmax-(del/2);%Defining the quantized values in the
% respective levels
[index, quant_vals] = quantiz(msg, partions, approx_val);%index and quantized values

%Encoding the quantized data


[coded_data] = PCM_enc(index,n);

%Signal-to-Noise Quantization Ratio


signal_power = mean(msg.^2);
quant_noise_power = (del^2)/12;
sqnr = signal_power/quant_noise_power;%Signal to Quantization Noise Ratio
sqnr_dB_Th = 10*log10(sqnr);%Theoretical SQNR calculation
sqnr_dB_Pr = (6.02*n)+1.77;%Practical SQNR calculation

%PCM Decoding
[decoded_data] = PCM_dec(coded_data,n);

%Dequantization of the decoded data


dequant_vals = Vmin + (del/2) + (del*decoded_data);

%Reconstructing the Original message signal back


cutoff_frequency = Fm;%Cutoff frequency is eaual to maximum message signal frequency
reconst_signal = signal_lpf(cutoff_frequency, FS, dequant_vals);

figure(1);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Message Signal');
axis([0 2 -1.05 1.05]);
grid on;
figure(2);
stem(tn, sampled_msg, 'b', 'filled', 'LineWidth', 2);
hold on;
plot(t, msg, 'r--', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Sampled Message Signal');
legend('Sampled Signal','Original Message Signal');
axis([0 2 -1.05 1.05]);
grid on;

figure(3);
stem(tn, sampled_msg, 'b', 'filled', 'LineWidth', 1);
hold on;
stairs(t, quant_vals, 'r', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Quantized Sequence');
%legend('Sampled Signal', 'Quantized Signal','Fontsize', 14);
axis([0 2 -1.05 1.05]);
grid on;

figure(4);
stairs(coded_data, 'k', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Encoded data');
axis([0 700 -0.05 1.05]);
grid on;

figure(5);
plot(t, decoded_data, 'b', 'LineWidth', 2);
hold on;
xlabel('Time');
ylabel('Amplitude');
title('Decoded Signal');
axis([0 2 0 15]);
grid on;

figure(6);
plot(t, dequant_vals, 'b', 'LineWidth', 2);
hold on;
plot(t, msg, 'r--', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Dequantization');
legend('Dequantized Signal', 'Original Message Signal');
axis([0 2 -1.05 1.05]);
grid on;

figure(7);
plot(t, reconst_signal, 'b', 'LineWidth', 2);
hold on;
plot(t, msg, 'r--', 'LineWidth', 2);
xlabel('Time');
ylabel('Amplitude');
title('Dequantization');
legend('Reconstructed Signal', 'Original Message Signal');
axis([0 2 -1.05 1.05]);
grid on;

EXP3 MU LAW COMPANDING

clc;
clear all;
close all;

FS = 1000;
TS = 1/FS;
t= 0:TS:2-TS;

Am = 8;
Fm = 1;
%msg = Am*sin(2*pi*Fm*t);
msg = Am*exp(-2*t).*cos(2*pi*Fm*t);

sampled_msg = downsample(msg, 20);


sampled_time = downsample(t, 20);

n = 4;
L = 2^n;
Vmax = max(msg);
Vmin = min(msg);
del = (Vmax-Vmin)/L;
partition_boundaries = Vmin+del:del:Vmax-del;
approx_vals = Vmin+(del/2):del:Vmax-(del/2);

[index, qunat_vals] = quantiz(msg, partition_boundaries,approx_vals);

[coded_data] = PCM_enc(index, n);

[decoded_data] = PCM_dec(coded_data, n);

dequant_data = Vmin+(del/2)+(decoded_data*del);

reconst_msg = signal_lpf(Fm, FS, dequant_data);

mu = 255;
norm_msg = msg./Vmax;
abs_norm_msg = abs(norm_msg);
abs_msg = abs(msg);
%comp_samples = sign(norm_msg).*(log(1+(mu*abs_norm_msg))/log(1+mu));
%comp_samples = sign(msg).*(log(1+(mu*abs_norm_msg))/log(1+mu));
if (-1 <= msg) & (msg <= 1)
comp_samples = sign(msg).*(log(1+(mu*abs_msg))/log(1+mu));
else
comp_samples = msg;
end

comp_Vmax = max(comp_samples);
comp_Vmin = min(comp_samples);
comp_del = (comp_Vmax-comp_Vmin)/L;
comp_partition_boundaries = comp_Vmin+comp_del:comp_del:comp_Vmax-comp_del;
comp_approx_vals = comp_Vmin+(comp_del/2):comp_del:comp_Vmax-(comp_del/2);

[comp_index, comp_qunat_vals] = quantiz(comp_samples, comp_partition_boundaries,


comp_approx_vals);

[comp_coded_data] = PCM_enc(comp_index, n);

[comp_decoded_data] = PCM_dec(comp_coded_data, n);

comp_dequant_data = comp_Vmin+(comp_del/2)+(comp_decoded_data*comp_del);

max_comp_dequant_data = max(comp_dequant_data);
norm_comp_dequant_data = comp_dequant_data./max_comp_dequant_data;
abs_norm_dequant_vals = abs(norm_comp_dequant_data);
%expand_vals = sign(norm_comp_dequant_data).*((((1+mu).^(abs_norm_dequant_vals))-1)/mu);
%expand_vals = sign(comp_dequant_data).*((((1+mu).^(abs_norm_dequant_vals))-1)/mu);
if (-1 <= comp_dequant_data) & (comp_dequant_data <= 1)
expand_vals = sign(comp_dequant_data).*((((1+mu).^(abs(comp_dequant_data)))-1)/mu);
else
expand_vals = comp_dequant_data;
end
comp_reconst_msg = signal_lpf(Fm, FS, expand_vals);

%Signal-to-Noise Quantization Ratio of original message samples


msg_signal_power = mean(msg.^2);
msg_quant_noise_power = (del^2)/12;
msg_sqnr = msg_signal_power/msg_quant_noise_power;%Signal to Quantization Noise Ratio
msg_sqnr_dB_Th = 10*log10(msg_sqnr);%Theoretical SQNR calculation
msg_sqnr_dB_Pr = (6.02*n)+1.77;%Practical SQNR calculation

%Signal-to-Noise Quantization Ratio of compressed samples


comp_signal_power = mean(comp_samples.^2);
comp_quant_noise_power = (del^2)/12;
comp_sqnr = comp_signal_power/comp_quant_noise_power;%Signal to Quantization Noise Ratio
comp_sqnr_dB_Th = 10*log10(comp_sqnr);%Theoretical SQNR calculation
comp_sqnr_dB_Pr = (6.02*n)+4.77-20*log10(log(1+mu));%Practical SQNR calculation

figure(1);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(2);
stem(sampled_time, sampled_msg, 'b', 'filled', 'LineWidth', 2);
hold on;
plot(t, msg, 'r--', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Sampled Signal vs Original Message Signal');
legend('Sampled Signal', 'Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(3);
plot(t, msg, 'r', 'LineWidth', 2);
hold on;
plot(t, comp_samples, 'b', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Compressed Samples vs Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(4);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
stairs(t, comp_qunat_vals, 'm', 'LineWidth', 2);
hold on;
stairs(t, qunat_vals, 'b', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Original Message Signal vs Compressed Quantized Signal vs Original Quantized Signal');
legend('Sampled Signal', 'Compressed Quantized Signal', 'Original Quantized Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(5);
stairs(comp_coded_data, 'k', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Coded Data');
%axis([0 700 -0.05 1.05]);
grid on;

figure(6);
plot(t, comp_decoded_data, 'r', 'LineWidth', 2);
hold on;
plot(t, decoded_data, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Compressed Decoded Data vs Original Decoded Data');
legend('Compressed Decoded Data', 'Original Decoded Data');
%axis([0 2 -1.05 1.05]);
grid on;

figure(7);
plot(t, comp_dequant_data, 'r', 'LineWidth', 2);
hold on;
plot(t, dequant_data, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Dequnatized Data');
legend('Compressed Dequnatized Data', 'Original Dequnatized Data');
%axis([0 2 -1.05 1.05]);
grid on;

figure(8);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, comp_reconst_msg, 'k', 'LineWidth', 2);
hold on;
plot(t, reconst_msg, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Reconstructed Message vs Original Message Signal');
legend('Original Message Signal', 'Compressed Reconbstructed Signal', 'Original Reconbstructed
Signal')
%axis([0 2 -1.05 1.05]);
grid on;

EXP 4 A LAW COMPANDING

clc;
clear all;
close all;

FS = 1000;
TS = 1/FS;
t= 0:TS:2-TS;

Am = 8;
Fm = 1;
%msg = Am*sin(2*pi*Fm*t);
msg = Am*exp(-2*t).*cos(2*pi*Fm*t);

sampled_msg = downsample(msg, 20);


sampled_time = downsample(t, 20);

n = 4;
L = 2^n;
Vmax = max(msg);
Vmin = min(msg);
del = (Vmax-Vmin)/L;
partition_boundaries = Vmin+del:del:Vmax-del;
approx_vals = Vmin+(del/2):del:Vmax-(del/2);

[index, qunat_vals] = quantiz(msg, partition_boundaries,approx_vals);

[coded_data] = PCM_enc(index, n);

[decoded_data] = PCM_dec(coded_data, n);

dequant_data = Vmin+(del/2)+(decoded_data*del);

reconst_msg = signal_lpf(Fm, FS, dequant_data);

A = 87.56;
abs_msg = abs(msg);
if abs_msg < (1/A)
abs_comp_samples = (A*abs_msg)/(1+log(A));
comp_samples = sign(msg).*abs_comp_samples;
elseif ((1/A) <= abs_msg) & (abs_msg <= 1)
abs_comp_samples = (1+log(A*abs_msg))/(1+log(A));
comp_samples = sign(msg).*abs_comp_samples;
else
comp_samples = msg;
end

comp_Vmax = max(comp_samples);
comp_Vmin = min(comp_samples);
comp_del = (comp_Vmax-comp_Vmin)/L;
comp_partition_boundaries = comp_Vmin+comp_del:comp_del:comp_Vmax-comp_del;
comp_approx_vals = comp_Vmin+(comp_del/2):comp_del:comp_Vmax-(comp_del/2);

[comp_index, comp_qunat_vals] = quantiz(comp_samples, comp_partition_boundaries,


comp_approx_vals);

[comp_coded_data] = PCM_enc(comp_index, n);

[comp_decoded_data] = PCM_dec(comp_coded_data, n);

comp_dequant_data = comp_Vmin+(comp_del/2)+(comp_decoded_data*comp_del);

abs_comp_dequant_data = abs(comp_dequant_data);
if abs_comp_dequant_data < (1/(1+log(A)))
expand_vals_temp = (abs_comp_dequant_data.*(1+log(A)))/(A);
expand_vals = sign(comp_dequant_data).*expand_vals_temp;
elseif ((1/(1+log(A))) <= abs_comp_dequant_data) & (abs_comp_dequant_data < 1)
expand_vals_temp = exp(-1+(abs_comp_dequant_data.*(1+log(A))))/(A);
expand_vals = sign(comp_dequant_data).*expand_vals_temp;
else
expand_vals = comp_dequant_data;
end

comp_reconst_msg = signal_lpf(Fm, FS, expand_vals);

%Signal-to-Noise Quantization Ratio of original message samples


msg_signal_power = mean(msg.^2);
msg_quant_noise_power = (del^2)/12;
msg_sqnr = msg_signal_power/msg_quant_noise_power;%Signal to Quantization Noise Ratio
msg_sqnr_dB_Th = 10*log10(msg_sqnr);%Theoretical SQNR calculation
msg_sqnr_dB_Pr = (6.02*n)+1.77;%Practical SQNR calculation

%Signal-to-Noise Quantization Ratio of compressed samples


comp_signal_power = mean(comp_samples.^2);
comp_quant_noise_power = (del^2)/12;
comp_sqnr = comp_signal_power/comp_quant_noise_power;%Signal to Quantization Noise Ratio
comp_sqnr_dB_Th = 10*log10(comp_sqnr);%Theoretical SQNR calculation
comp_sqnr_dB_Pr = (6.02*n)+4.77-20*log10(1+log(A));%Practical SQNR calculation

figure(1);
plot(t, msg, 'r', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(2);
stem(sampled_time, sampled_msg, 'b', 'filled', 'LineWidth', 2);
hold on;
plot(t, msg, 'r--', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Sampled Signal vs Original Message Signal');
legend('Sampled Signal', 'Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(3);
plot(t, msg, 'r', 'LineWidth', 2);
hold on;
plot(t, comp_samples, 'b', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Compressed Samples vs Original Message Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(4);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
stairs(t, comp_qunat_vals, 'm', 'LineWidth', 2);
hold on;
stairs(t, qunat_vals, 'b', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Original Message Signal vs Compressed Quantized Signal vs Original Quantized Signal');
legend('Sampled Signal', 'Compressed Quantized Signal', 'Original Quantized Signal');
%axis([0 2 -1.05 1.05]);
grid on;

figure(5);
stairs(comp_coded_data, 'k', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Coded Data');
%axis([0 700 -0.05 1.05]);
grid on;

figure(6);
plot(t, comp_decoded_data, 'r', 'LineWidth', 2);
hold on;
plot(t, decoded_data, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Compressed Decoded Data vs Original Decoded Data');
legend('Compressed Decoded Data', 'Original Decoded Data');
%axis([0 2 -1.05 1.05]);
grid on;

figure(7);
plot(t, comp_dequant_data, 'r', 'LineWidth', 2);
hold on;
plot(t, dequant_data, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Dequnatized Data');
legend('Compressed Dequnatized Data', 'Original Dequnatized Data');
%axis([0 2 -1.05 1.05]);
grid on;

figure(8);
plot(t, msg, 'r--', 'LineWidth', 2);
hold on;
plot(t, comp_reconst_msg, 'k', 'LineWidth', 2);
hold on;
plot(t, reconst_msg, 'g', 'LineWidth', 2);
xlabel('Time-->');
ylabel('Amplitude -->');
title('Reconstructed Message vs Original Message Signal');
legend('Original Message Signal', 'Compressed Reconbstructed Signal', 'Original Reconbstructed
Signal')
%axis([0 2 -1.05 1.05]);
grid on;

EXP 5 DELTA MODULATION DEMODULATION

clc;
clear all;
close all;

%% Preparation of a time vector


Fs = 1000;%Sampling frequency
Ts = 1/Fs;%Sampling time
t = 0:Ts:2-Ts;%Time vector
%%

%% Preparing a Message Signal


Am = 1;%Message signal's amplitude
Fm = 1;%Message signal's frequency
msg = Am*cos(2*pi*Fm*t);%Sinusoidal signal
%%

%% Sampled Message Signal


sample_factor = 20;
tn = downsample(t,sample_factor);
sampled_msg = downsample(msg,sample_factor);
%%

%% Delat Modulation
mod_pred_input = zeros(1,length(sampled_msg));
error = zeros(1,length(sampled_msg));
quantiz_err = zeros(1,length(sampled_msg));
DM_enc_sequence = zeros(1,length(sampled_msg));
del = (Am)*2*pi*Fm*sample_factor*Ts;
%del = 0.15;

error(1) = sampled_msg(1);
DM_enc_sequence(1) = 0;
for i = 2:length(sampled_msg)
error(i) = sampled_msg(i)-mod_pred_input(i-1);
quantiz_err(i) = del*sign(error(i));
mod_pred_input(i) = mod_pred_input(i-1)+quantiz_err(i);
DM_enc_sequence(i) = (sign(quantiz_err(i))+1)/2;
end

%DM_enc_sequence(2:end) = (sign(quantiz_err(2:end))+1)/2;
%%

%% Delat Demodulation
DM_dec_sequence = zeros(1,length(sampled_msg));
dequantiz_err = zeros(1,length(sampled_msg));
demod_pred_input = zeros(1,length(sampled_msg));

DM_dec_sequence(1) = DM_enc_sequence(1);
demod_pred_input(1) = del*sign(DM_dec_sequence(1));
for i = 2:length(sampled_msg)
DM_dec_sequence(i) = DM_enc_sequence(i);
dequantiz_err(i) = del*(2*DM_dec_sequence(i)-1);

demod_pred_input(i) = dequantiz_err(i) + demod_pred_input(i-1);


end
%%

%% Message Reconstruction
cutoff_freq = Fm;%Cutoff frequency is same as Message Signal Frequency
reconst_msg = signal_lpf(cutoff_freq,Fs/sample_factor,demod_pred_input);
%%

%% SQNR
signal_power = mean(sampled_msg.^2);
noise_power = (del^2)/3;
SQNR_linear = signal_power/noise_power;
SQNR_dB = 10*log10(SQNR_linear);
%%

figure(1);
plot(t,msg,'r','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Message Signal');
grid on;

figure(2);
stem(tn,sampled_msg,'r--','LineWidth',2);
hold on;
stairs(tn,mod_pred_input,'b','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Sampled Message Signal vs Approximated Signal');
legend('Sampled Message Signal', 'Approximated Signal');
grid on;

figure(3);
%stairs(t,DM_enc_sequence,'b','LineWidth',2);
stairs(DM_enc_sequence,'b','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Delta Modulation Encoded Sequence');
axis([0 100 -0.1 1.1]);
grid on;
figure(4);
stairs(DM_dec_sequence,'b','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Delta Modulation Decoded Sequence');
axis([0 100 -0.1 1.1]);
grid on;

figure(5);
plot(tn,sampled_msg,'r--','LineWidth',2);
hold on;
stairs(tn,demod_pred_input,'b','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Predicted Signal at RXer');
legend('Sampled Message Signal', 'Predicted Signal at RXer');
grid on;

figure(6);
plot(t,msg,'r--','LineWidth',2);
hold on;
plot(tn,reconst_msg,'b','LineWidth',2);
xlabel('Time-->');
ylabel('Amplitude-->');
title('Message Signal vs Reconstructed Signal');
legend('Original Message Signal', 'Reconstructed Signal');
axis([0 2 -1.1 1.1]);
grid on;

EXP 6 PULSE SHAPING

clc;
clear all;
close all;

NS = 1000; %No. of samples


upsample_factor = 32;%Up Sampling by a factor of 32

% Case1: Generation of Uni-Polar data


unipolar_data = round(rand(1,NS));

% Case2: Generation of Bi-Polar data


bipolar_data = sign(randn(1,NS));

% Case-3: Unipolar to Bipolar conversion


converted_bipolar_data = (2*unipolar_data)-1;

% Case-4: Unipolar to Bipolar conversion


converted_unipolar_data = (bipolar_data+1)/2;

% Case-5: Upsampling the Bipolar Data


upsampled_bipolar_data = upsample(bipolar_data,upsample_factor);

% Case-6: Non-return to zero


nrz = ones(1,upsample_factor);

% Case-7: Non-return to zero Polar


nrz_polar = conv(upsampled_bipolar_data, nrz);

% Case-8: Return to zero Signal


rz = [zeros(1,upsample_factor/4) ones(1,upsample_factor/2) zeros(1,upsample_factor/4)];

% Case-9: Return to zero Polar Signal


rz_polar1 = conv(upsampled_bipolar_data, rz);
rz_polar2 = conv(bipolar_data, rz);

% Case-10: Half Sinusoid Pulse


half_sinusoid = sin(pi*((0:upsample_factor-1)/upsample_factor));

% Case-11: Half Sinusoid Polar Waveform


half_sinusoid_polar = conv(upsampled_bipolar_data, half_sinusoid);

% Case-12: Raised Cosine Pulse


Td = 4;%Truncating the Raised cosine to 4 periods
roff_factor = 0.5;%Roll-off factor
rcos = rcosdesign(roff_factor,Td,upsample_factor,'normal');
rcos = rcos/max(rcos);

% Case-13: Raised Cosine Pulse Train


rcos_train = conv(upsampled_bipolar_data, rcos);
delay1 = finddelay(upsampled_bipolar_data, rcos_train);
rcos_train = rcos_train(delay1+1:end);

% Case-14: Root-Raised Cosine Pulse


rrcos = rcosdesign(roff_factor,Td,upsample_factor,'sqrt');
rrcos = rrcos/max(rrcos);

% Case-15: Root-Raised Cosine Pulse Train


rrcos_train1 = conv(upsampled_bipolar_data, rrcos);
delay2 = finddelay(upsampled_bipolar_data, rrcos_train1);
rrcos_train2 = rrcos_train1(delay2+1:end);

figure(1);
stairs(unipolar_data, 'r', 'LineWidth', 2);
title('Unipolar Random Bits');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 50 -0.1 1.1]);% selected only 50 to visualize the data
grid on;

figure(2);
stairs(bipolar_data, 'b', 'LineWidth', 2);
title('Bipolar Random Bits');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 50 -1.1 1.1]);% selected only 50 to visualize the data
grid on;

figure(3);
stairs(converted_bipolar_data, 'k', 'LineWidth', 2);
title('Unipolar to Bipolar Converted Bits');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 50 -1.1 1.1]);% selected only 50 to visualize the data
grid on;

figure(4);
stairs(converted_unipolar_data, 'm', 'LineWidth', 2);
title('Bipolar to Unipolar Converted Bits');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 50 -0.1 1.1]);% selected only 50 to visualize the data
grid on;

figure(5);
stairs(upsampled_bipolar_data, 'g', 'LineWidth', 2);
title('Upsampled Bipolar data');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 1600 -1.1 1.1]);% selected only 1600 to visualize the data
grid on;

figure(6);
stairs(nrz, 'cy', 'LineWidth', 2);
title('NRZ Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 upsample_factor -0.1 1.1]);
grid on;

figure(7);
stairs(nrz_polar, 'r', 'LineWidth', 2);
title('Pulse shaped NRZ');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([0 1600 -1.1 1.1]);
grid on;

figure(8);
stairs(rz, 'b', 'LineWidth', 2);
title('RZ Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 upsample_factor -0.1 1.1]);
grid on;

figure(9);
stairs(rz_polar1, 'b', 'LineWidth', 2);
title('RZ Polar Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 1600 -1.1 1.1]);
grid on;

figure(10);
plot(half_sinusoid, 'b', 'LineWidth', 2);
title('Half Sinusoid Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 upsample_factor -0.1 1.1]);
grid on;

figure(11);
plot(half_sinusoid_polar, 'b', 'LineWidth', 2);
title('Half Sinusoid Polar Waveform');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 1600 -1.1 1.1]);
grid on;

figure(12);
plot(rcos, 'b', 'LineWidth', 2);
title('Raised Cosine Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 length(rcos) -0.15 1.05]);
grid on;

figure(13);
plot(rcos_train, 'b', 'LineWidth', 2);
title('Raised Cosine Pulse Train');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 1600 -1.5 1.5]);
grid on;

figure(14);
plot(rrcos, 'b', 'LineWidth', 2);
title('Root-Raised Cosine Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 length(rrcos) -0.15 1.05]);
grid on;
figure(15);
plot(rrcos_train1, 'b', 'LineWidth', 2);
hold on
plot(rrcos_train2, 'k', 'LineWidth', 2);
hold on
title('Root-Raised Cosine Pulse Train');
xlabel('Time-->');
ylabel('Amplitude-->');
axis([1 1600 -1.5 1.5]);
grid on;

figure(16);
stairs(rz_polar2, 'b', 'LineWidth', 2);
title('RZ Polar Pulse');
xlabel('Time-->');
ylabel('Amplitude-->');
%axis([1 50 -5 5]);
grid on;

You might also like