0% found this document useful (0 votes)
13 views52 pages

DCT Lab AAT

The document outlines a series of MATLAB simulation experiments conducted for a Digital Communication Theory course at B.M.S. College of Engineering. It includes simulations for various digital communication techniques such as PCM, DPCM, and different modulation methods like BASK and BPSK, along with their respective algorithms, codes, and observations. The experiments aim to demonstrate the efficiency and recovery of signals through these techniques.

Uploaded by

ojas.images.23
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)
13 views52 pages

DCT Lab AAT

The document outlines a series of MATLAB simulation experiments conducted for a Digital Communication Theory course at B.M.S. College of Engineering. It includes simulations for various digital communication techniques such as PCM, DPCM, and different modulation methods like BASK and BPSK, along with their respective algorithms, codes, and observations. The experiments aim to demonstrate the efficiency and recovery of signals through these techniques.

Uploaded by

ojas.images.23
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/ 52

B.M.S.

COLLEGE OF ENGINEERING
(Autonomous College Affiliated to Visvesvaraya Technological University, Belgaum)
Bull Temple Road, Basavanagudi, Bangalore-560019

DCT LAB SIMULATION EXPERIMENTS ON MATLAB

DIGITAL COMMUNICATION THEORY [23EC5PCDCT]


SUBMITTED BY:

NAME USN

Ojas Kudari 1BM22EC154

SEM: 5

SECTION: C

Course Instructor

Dr. Maligi Anantha Sunil

Assistant Professor

ECE , BMSCE
SL.NO. TITLE

1. SIMULATION OF TECHNIQUES LEARNT IN MODULE-1:


PCM, DPCM

2. SIMULATION OF TECHNIQUES LEARNT IN MODULE-2:


DIFFERENT MODULATIONS

3. SIMULATION OF TECHNIQUES LEARNT IN MODULE-3:


SPREAD SPECTRUM

4. SIMULATION OF TECHNIQUES LEARNT IN MODULE-4:


SOURCE CODING

5. SIMULATION OF TECHNIQUES LEARNT IN MODULE-5:


CHANNEL CODING

6. SIMULATION OF END-TO-END COMMUNICATION SYSTEMS WITH


BER PLOTS(in AWGN)
1. SIMULATION OF TECHNIQUES IN MODULE-1:

1) PCM

ALGORITHM:
The three-step algorithm used for PCM is sampling quantization and Encoding.
We are choosing a sampling rate of 500Hz for a time interval of 1 second and
then create 2 sine waves of frequencies 1 and 3Hz. To study the efficiency, we
are doing 4 level and 16 level quantization basically, we are setting the number
of discrete levels and then the output is taken and demodulated and quantization
signal to noise ratio is found for both levels and are compared with the input
sine signal.
CODE:

clc;
clear all;
close all;
td=0.002; %original sampling rate
500 Hz t=0:td:1; %time interval of
1 second
xsig=sin(2*pi*t)-sin(6*pi*t); % 1Hz +3Hz
sinusoids Lsig=length(xsig);
Lfft=2^ceil(log2(Lsig)+1);
Xsig=fftshift(fft(xsig,Lfft));
Fmax=1/(2*td);
Faxis=linspace(-
Fmax,Fmax,Lfft); ts=0.02;
%new sampling rate = 50Hz
Nfact=ts/td;
% send the signal through a 16-level uniform quantizer
[s_out,sq_out,sqh_outl,Delta,SQNR]=sampandquant(xsig,16,td,ts )
; figure(1);
subplot (2,1,1) ;
sfigl=plot(t,xsig,'k',t,sqh_outl(1:Lsig ),'b');
set(sfigl,'Linewidth',2);
title('Signal and its 16 level PCM signal')
xlabel('time(sec.)');
% send the signal through a 4-level uniform quantizer
[s_out,sq

out,sqh_out2,Delta,SQNR]=sampandquant(xsig,4,td,ts);
subplot(212);
sfig2=plot(t,xsig,'k',t,sqh_out2(1:Lsig),'b
'); set (sfig2,'Linewidth',2);
title('Signal and its 4 level PCM signal')
xlabel('time (sec.)');
%Reconstruction of pcm signal
Lfft=2^ceil( log2(Lsig)+1);
Fmax=1/(2*td);
Faxis=linspace(-Fmax,Fmax,Lfft);
SQHl=fftshift(fft(sqh_outl,Lfft));
SQH2=fftshift(fft(sqh_out2,Lfft));
% Now use LPF to filter the two PCM
signals BW=10 ; %Bandwidth is no larger
than lO Hz . H_lpf=zeros(1,Lfft);
H_lpf(Lfft/2-BW:Lfft/2+BW- 1)=1; %ideal LPF
Sl_recv=SQHl.*H_lpf ; % ideal fil ter ing
s_recvl=real(ifft(fftshift(Sl_recv))); % recons tructed f-
domain recvl = s_recvl(1:Lsig) ; % recons truc ted t-domain
S2_recv= SQH2.*H_lpf; % ideal fil tering
s_recv2 =real(ifft(fftshift(S2_recv))); % recons tructed f-
domain s_recv2 = s_recv2 (1:Lsig) ; % reconstructed t-domain
figure (2)
subplot(211);
sfig3=plot(t,xsig,'b-',t,s_recvl(1:Lsig),'b-.');
legend ( ' original ', ' recovered ')
set ( sfig3,'Linewidth',2) ;
title ('Signal and filtered 16-level PCM signal
') xlabel ( ' time( sec . ) ' );
subplot (212);
sfig4=plot(t,xsig,' b-',t,s_recv2(1:Lsig),' b-. ' )
; legend ( ' original ', ' recovered ')
set ( sfig4,'Linewidth',2) ;
title ( ' Signal and filtered 4-level PCM
signal ') xlabel ( ' time(sec.) ' ) ;
function
[s_out,sq_out,sqh_out1,Delta,SQNR]=sampandquant(sig_in,L,t
d,ts)
% L number of uniform quasantization l evels
% sig_in - input signal vector
% td original signal sampling period of sig_in
% s_out = sampled output
% sq_out = sampled and quantized output
% sqh_out = sapmled, quantized and hold output
% delta = quantization interval
% SQNR = actual signal to quantization noise
ratio if(rem((ts/td),1)==0)
nfac=round(ts/td) ;
p_zoh=ones(1,nfac) ;
s_out=downsample(sig_in,nfac)
;
sig_pmax=max(s_out);
sig_nmax=min(s_out);
Delta=(sig_pmax-
sig_nmax)/L;
q_level=sig_nmax+Delta/2 : Delta : sig_pmax-Delta/2 ; %
define Q- levels
L_sig=length(s_out); % find signal length
sigp=(s_out-sig_nmax)/Delta+1/2; % convert into 1/2 to L+ l/2
range qindex=round(sigp); % round to 1,2,..... L levels
qindex=min(qindex,L); % eleminate L+l as a rare possibility
sq_out=q_level(qindex); % use index vector to generate output
SQNR=20*log10(norm(s_out)/norm(s_out-sq_out)); %actual SQNR
value s_out=upsample(s_out,nfac);
sqh_out1=kron(sq_out,p_zoh);
sq_out=upsample(sq_out,nfac);
else
warning ( ' Error ! ts / td is not an integer ! ');
s_out=[ ]; sq_out=[ ]; sqh_out=[ ]; Delta=[ ];
SQNR=[ ]; end
end

WAVEFORMS:
OBSERVATION:
The 16-level quantization has recovered the signal better than the 4 level
quantization with low QSNR. The output demodulated is matching with the
input signal hence PCM is studied for 2 different levels of quantization.
2) DPCM

ALGORITHM/PRINCIPLE: The basic principle in DPCM is it takes the


differences between the samples quantize it with fewer bits. This technique is
designed to take sample to sample redundancy in a typical waveform if the part
of
the redundant information is known it is possible to predict the future values
due to
correlation between the samples. It also follows the same principle of PCM
addition to it we have a prediction filter which is a feedback and this gets
compared with sampled input.

CODE:

clc
clear all
close all
t=0:0.01:1
; a = 2;
fc = 2;
xsig =
a*sin(2*pi*fc*t);
subplot(6,1,1);
plot(xsig)
title('x(t) = a.*sin(2*pi*fc*t)');
subplot(6,1,2);
q = stem(xsig);
l =zeros(1,100,'int32');
m
=zeros(1,100,'int32');
for N=1:length(t)
m(N)=xsig(N);
end
for N=2:99
if m(N-
1)<m(N)
l(N)=1;
elseif m(N-1)==m(N)
l(N)=0; else
l(N)=-1;
en
d
en
d
title('Sampled Signal');
[index,quants] = quantiz(xsig, [0:0.5:4],[0:0.5:4.5]);
subplot(6,1,3);
stairs(quants
);
subplot(6,1,4
); stairs(l)
title('DPCM Quantized
Signal'); z1 =
dec2bin(abs(l));
z2 = bin2dec(z1);
subplot(6,1,5)
stem(z2);
title('Dequantized
Signal') [b,a] =
butter(2,0.03,'low'); k =
filter(b,a,l);
subplot(6,1,6);
plot(k)
title('Reconstructed Signal');

WAVEFORM:

OBSERVATION:
The input signal is sampled and quantized and at the receiver end it is recovered
which is almost same as the input signal showing good recovering efficiency.
AIM: Obtain Characteristic curve for A Law and Mu Law

CODE:

clc;
clear all;
close all;

% Signal generation
sig = exp(-
4:0.1:4);
V = max(sig);

% Quantization parameters
partition = linspace(0, V, 2^6 - 1); % Partition defines
boundaries codebook = linspace(0, V, 2^6); % Codebook defines
quantization levels

% Quantization of the original signal


[index, qsig] = quantiz(sig, partition,
codebook); distortion = sum((qsig - sig).^2) /
length(sig);

% Mu-law parameters and


processing mu = 255; % mu-law
parameter
csig_compressed = compand(sig, mu, V, 'mu/compressor');
[index_compressed, quants] = quantiz(csig_compressed, partition,
codebook);
csig_expanded = compand(quants, mu, V, 'mu/expander');
distortion2 = sum((csig_expanded - sig).^2) /
length(sig);

% Display distortions for mu-law


disp(['Distortion (original to quantized): ',
num2str(distortion)]); disp(['Distortion (mu-law expanded): ',
num2str(distortion2)]);

% Plot for mu-


law figure(1);
plot(1:length(sig), [sig' qsig' csig_expanded'], 'LineWidth',
1.5); title('Comparison Between Original, Quantized, and
Expanded Signals using mu-law');
xlabel('Interval');
ylabel('Amplitude');
legend('Original', 'Quantized',
'Expanded'); grid on;
axis([0 length(sig) 0 max(sig)]);

% A-law parameters and


processing A = 87.6; % A-law
parameter
csig_compressed_mu = compand(sig, A, V, 'A/compressor');
[index_compressed_mu, quant] =
quantiz(csig_compressed_mu, partition, codebook);
csig_expanded_mu = compand(quant, A, V, 'A/expander');
distortion3 = sum((csig_expanded_mu - sig).^2) /
length(sig);

% Display distortions for A-law


disp(['Distortion (original to quantized): ',
num2str(distortion)]); disp(['Distortion (A-law expanded): ',
num2str(distortion3)]);

% Plot for A-law


figure(2);
plot(1:length(sig), [sig' qsig' csig_expanded_mu'],
'LineWidth', 1.5);
title('Comparison Between Original, Quantized, and Expanded
Signals using A-law');
xlabel('Interval');
ylabel('Amplitude');
legend('Original', 'Quantized',
'Expanded'); grid on;
axis([0 length(sig) 0 max(sig)]);

OBSERVATION:

The original quantized and expanded signals of the A law and Mu law are studied
and observed the a law showed good efficiency in the recovering of the signal.
WAVEFORMS:
2. SIMULATION OF TECHNIQUES LEARNT IN MODULE-2:

DIFFERENT MODULATIONS

1) BASK

ALGORITHM: The binary amplitude shift keying works as follows the


amplitude of the carrier is switched between two different levels keeping the
frequency and constant if cos(t) is the carrier wave for logic 1 the BASK output
is cos(t) for logic 0 of data BASK output is 0.

CODE:

clc;
clear all;
close all;
x=[ 1 0 0 1 1 0 1]; % Binary
Information bp=.000001; % bit period
disp(' Binary information at Trans mitter :');
disp(x);
%XX representation of transmitting binary information as
digital signal XXX
bit=[];
for
n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t1=bp/100:bp/100:100*length(x)*(bp/
100); subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('transmitting information as digital signal');
% Binary-ASK modulation %
A1=10; % Amplitude of carrier signal for information
1 A2=5; % Amplitude of carrier signal for
information 0 br=1/bp; % bit rate
f=br*10; % carrier
frequency
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for
(i=1:1:length(x))
if (x(i)==1)
y=A1*cos(2*pi*f*t2
); else
y=A2*cos(2*pi*f*t2
); end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)')
;
title('waveform for binary ASK modulation coresponding binary
information');
% Binary ASK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y=cos(2*pi*f*t); % carrier
siignal mm=y.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z=trapz(t4,mm) % intregation
zz=round((2*z/bp))
if(zz>7.5) % logic level =
(A1+A2)/2=7.5 a=1;
els
e
a=0
;
end
mn=[mn a];
end
disp(' Binary information at Reciver
:'); disp(mn);
% Representation of binary information as digital signal
which achived
%after ASK demodulation
bit=[];
for
n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t4=bp/100:bp/100:100*length(mn)*(bp/
100); subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('recived information as digital signal after binary ASK
demodulation');

WAVEFORM:

OBSERVATION: The BASK waveform is obtained as same as the algorithm


cos(t) for logic 1 but .5cos(t) for logic 0 and then the BASK waveform is
demodulated the recovered waveform is same as the input waveform.
BPSK:
ALGORITHM: In BPSK the phase of the carrier is shifted between two values
say 0 degree and 180 degree but the amplitude and the frequency of the PSK
waveform remains constant for any binary sequence.

CODE:

clc;
clear all;
close all;
x=[ 1 0 0 1 1 0 1]; % Binary
Information bp=.000001; % bit period
disp(' Binary information at Transmitter :');
disp(x);
%XX representation of transmitting binary information as
digital signal XXX
bit=[];
for
n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t1=bp/100:bp/100:100*length(x)*(bp/
100); subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('transmitting information as digital signal');
% Binary-PSK modulation
A=5; % Amplitude of carrier signal
br=1/bp; % bit rate
f=br*2; % carrier
frequency
t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for
(i=1:1:length(x))
if (x(i)==1)
y=A*cos(2*pi*f*t2)
; else
y=A*cos(2*pi*f*t2+pi); %A*cos(2*pi*f*t+pi) means -
A*cos(2*pi*f*t) end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)')
;
title('waveform for binary PSK modulation coresponding binary
information');
% Binary PSK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y=cos(2*pi*f*t); % carrier
siignal mm=y.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z=trapz(t4,mm) % intregation
zz=round((2*z/bp))
if(zz>0) % logic level = (A+A)/2=0
%becouse A*cos(2*pi*f*t+pi) means -
A*cos(2*pi*f*t) a=1;
els
e
a=0
;
end
mn=[mn a];
end
disp(' Binary information at Reciver
:'); disp(mn);
% Representation of binary information as digital signal
which achived
%after PSK demodulation
bit=[];
for
n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t4=bp/100:bp/100:100*length(mn)*(bp/100);
subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('recived information as digital signal after binary PSK
demodulation');

WAVEFORM:

OBSERVATION: We can see that for every change in the bit state there is a
phase shift in there PSK modulated wave the PSK wave is again demodulated
and the obtained output is matching with the input information signal.

QPSK:
ALGORITHM: The Quadrature Phase Shift Keying is similar to BPSK but
instead of 1 phase shift here we are having 4 possible phase shifts that are 0 90
180 270 or it can also be in the form of 45 125 225 and 315. The Quadrature
shit occurs for every di-bits (2 bits).

CODE:

clc;
clear all;
close all;
data=[0 0 1 1 0 1 1 0 1 1 1 0]; % information
figure(1)
stem(data, 'linewidth',3), grid on;
title(' Information before Transmiting
'); axis([ 0 12 0 1.5]);
data_NZR=2*data-1; % Data Represented at NZR form for
QPSK modulation
s_p_data=reshape(data_NZR,2,length(data)/2); % S/P convertion
of data
br=10.^6; %Let us transmission bit rate
1000000 f=br; % minimum carrier frequency
T=1/br; % bit duration
t=T/99:T/99:T; % Time vector for one bit
information y=[];
y_in=[];
y_qd=[];
d=zeros(1,length(data)/2)
; for i=1:length(data)/2
p=data(2*i);
imp=data(2*i - 1);
y1=s_p_data(1,i)*cos(2*pi*f*t); % inphase component
y2=s_p_data(2,i)*sin(2*pi*f*t) ;% Quadrature component
y_in=[y_in y1]; % inphase signal vector
y_qd=[y_qd y2]; %quadrature signal
vector y=[y y1+y2]; % modulated signal
vector
if (imp == 0) && (p == 0)
d(i)=exp(j*pi/4);%45
degrees end
if (imp == 1)&&(p == 0)
d(i)=exp(j*3*pi/4);%135
degrees end
if (imp == 1)&&(p == 1)
d(i)=exp(j*5*pi/4);%225
degrees end
if (imp == 0)&&(p == 1)
d(i)=exp(j*7*pi/4);%315
degrees end
end
Tx_sig=y; % transmitting signal after
modulation qpsk=d;
tt=T/99:T/99:
(T*length(data))/2; figure(2)
subplot(3,1,1);
plot(tt,y_in,'linewidth',3), grid on;
title(' wave form for inphase component in QPSK modulation ');
xlabel('time(sec)');
ylabel(' amplitude(volt0');
subplot(3,1,2);
plot(tt,y_qd,'linewidth',3), grid on;
title(' wave form for Quadrature component in QPSK modulation
'); xlabel('time(sec)');
ylabel(' amplitude(volt0');
subplot(3,1,3);
plot(tt,Tx_sig,'r','linewidth',3), grid on;
title('QPSK modulated signal (sum of inphase and Quadrature
phase signal)');
xlabel('time(sec)');
ylabel('
amplitude(volt0');
figure(3);
plot(d,'o');%plot constellation without
noise axis([-2 2 -2 2]);
grid on;
xlabel('real'); ylabel('imag');
title('QPSK constellation');

OBSERVATION: From the input waveform the in phase and quadrature phase
waveform is generated from which the QPSK modulated signal is generated the
obtained constellation diagram is matching with the theoretical diagram.

WAVEFORM:
2) BFSK
ALGORITHM: In BFSK the frequency of the carrier wave is changed with the
binary data such that say f2 for logic 1 and f1 for logic 0 keeping the amplitude
and the phase constant.

CODE:

clc;
clear all;
x=[ 1 0 0 1 1 0 1]; % Binary
Information bp=.000001; % bit period
disp(' Binary information at Trans mitter :');
disp(x);
% representation of transmitting binary information as
digital signal
bit=[];
for
n=1:1:length(x)
if x(n)==1;
se=ones(1,100);
else x(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t1=bp/100:bp/100:100*length(x)*(bp/
100); subplot(3,1,1);
plot(t1,bit,'lineWidth',2.5);grid on;
axis([ 0 bp*length(x) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('transmitting information as digital signal');
% Binary-FSK modulation
A=5; % Amplitude of carrier signal
br=1/bp; % bit rate
f1=br*8; % carrier frequency for information
as 1 f2=br*2; % carrier frequency for
information as 0 t2=bp/99:bp/99:bp;
ss=length(t2);
m=[];
for
(i=1:1:length(x))
if (x(i)==1)
y=A*cos(2*pi*f1*t2
);
else
y=A*cos(2*pi*f2*t2
); end
m=[m y];
end
t3=bp/99:bp/99:bp*length(x);
subplot(3,1,2);
plot(t3,m);
xlabel('time(sec)');
ylabel('amplitude(volt)')
;
title('waveform for binary FSK modulation corresponding binary
information');
% Binary FSK demodulation
mn=[];
for n=ss:ss:length(m)
t=bp/99:bp/99:bp;
y1=cos(2*pi*f1*t); % carrier signal for
information 1 y2=cos(2*pi*f2*t); % carrier
signal for information 0 mm=y1.*m((n-(ss-1)):n);
mmm=y2.*m((n-(ss-1)):n);
t4=bp/99:bp/99:bp;
z1=trapz(t4,mm) %
integration z2=trapz(t4,mmm)
% intregation
zz1=round(2*z1/bp)
zz2= round(2*z2/bp)
if(zz1>A/2) % logic lavel= (0+A)/2 or (A+0)/2 or 2.5 (
in this case)
a=1;
else(zz2>A/
2) a=0;
end
mn=[mn a];
end
disp(' Binary information at Reciver
:'); disp(mn);
% Representation of binary information as digital signal
which achived
%after demodulation
bit=[];
for
n=1:length(mn);
if mn(n)==1;
se=ones(1,100);
else mn(n)==0;
se=zeros(1,100);
end
bit=[bit
se]; end
t4=bp/100:bp/100:100*length(mn)*(bp/
100); subplot(3,1,3)
plot(t4,bit,'LineWidth',2.5);grid on;
axis([ 0 bp*length(mn) -.5 1.5]);
ylabel('amplitude(volt)');
xlabel(' time(sec)');
title('recived information as digital signal after binary FSK
demodulation');

WAVEFORMS:

OBSERVATION: As described in the algorithm the frequency chose for logic


1 was 8GHz and or logic 0 is 2GHz the obtained FSK waveform is matching
with the theoretical assumed waveform the demodulation of the signal gives the
output which is matching with the input.
AIM: Demonstrate BER performance of digital communication system
employing BASK, BPSK, QPSK, BFSK modulation scheme you have studied
and show the constellation plot . (MATLAB/SIMULINK). Assume AWGN
channel.

1) BASK

CODE:

clc;
clear all;
close all;
num_bit=1000;%number of bit data=randi(1,num_bit);
%random bit generation (1 or 0) SNRdB=0:10; % SNR
in dB
SNR=10.^(SNRdB/10);
for(k=1:length(SNRdB))%BER (error/bit) calculation for different
SNR
%
y=awgn(complex(data),SNRdB(k)
); error=0;
R=0;
M=[]
;
for(c=1:1:num_bit)
if (y(c)>.5&&data(c)==0)||(y(c)<.5&&data(c)==1)%logic
acording to BASK
error=error+1
; M=[M
~data(c)];
else
M=[M
data(c)]; end
end
error=error/num_bit; %Calculate
error/bit m(k)=error;
end
figure;
semilogy(SNRdB,m,'o','linewidth',2.5),grid on,hold on;
BER_th=(1/2)*erfc(.5*sqrt(SNR));
semilogy(SNRdB,BER_th,'r','linewidth',2.5),grid on,hold
on; title(' curve for Bit Error Rate verses SNR for
Binary ASK modulation');
xlabel(' SNR(dB)');
ylabel('BER');
legend('simulation','theorytical
') axis([0 10 10^-5 1]);

WAVEFORM:

OBSERVATION: The theoretical bit error rate of ASK and the SNR for the
binary ASK modulated wave is compared and the values are matching.
2) BPSK

CODE:

clear; close all; clc; tic;


%number of bits
bit_number=1000;
%generating random bits
data=randn(1,bit_number)>0.5;
%generating BPSK
signal
bpsk_data=2*data-1;
%generating noise with zero mean and var. equal to 1.
noise=1/sqrt(2)*(randn(1,bit_number)+1i*randn(1,bit_number));
mean(abs(noise.^2)) %test the power of the noise
SNR=0:9; %set SNR in dB
snr_lin=10.^(SNR/10); %calculate linear snr from dB SNR.
y=zeros(length(SNR),bit_number);
%multiply sqrt of snr to signal and add
noise: for i=1:length(SNR)
y(i,:)=real(sqrt(snr_lin(i))*bpsk_data+nois
e); end
%reciever and ber count
err=zeros(length(SNR),bit_number);
Err=zeros(10,2); for i=1:length(SNR)
for
j=1:bit_number
if y(i,j)>=0
y(i,j)=1;
else
y(i,j)=0;
end
end
err(i,:)=abs(y(i,:)-data);
Err(i,:)=size(find(err(i,:))
); end
%calculating BER
ber=zeros(length(SNR),1);
for i=1:length(SNR)
ber(i)=Err(i,2)/bit_numbe
r; end
figure;
%theoretical BER calculation
theoryBer =
0.5*erfc(sqrt(snr_lin));
semilogy(SNR,ber,'b*-','linewidth',1); grid on; hold on;
semilogy(SNR,theoryBer,'r+-','linewidth',1); grid on;
xlabel('Eb/N0'); ylabel('BER');
legend('Simulation','Theory') ;

WAVEFORM:

OBSERVATION: The theoretical bit error rate of BPSK and the SNR for the
binary PSK modulated wave is compared and the values are matching.
3) QPSK

CODE:

clear
all;
close
all;
l=10000;
snrdb=1:1:10;
snrlin=10.^(snrdb/10);
for snrdb=1:1:10
si=2*(round(rand(1,l))-0.5);
sq=2*(round(rand(1,l))-
0.5); s=si+j*sq;
w=awgn(s,snrdb,'measured')
; r=w;
si_=sign(real(r));
sq_=sign(imag(r));
ber1=(l-
sum(si==si_))/l;
ber2=(l-
sum(sq==sq_))/l;
ber(snrdb)=mean([ber1
ber2]); end
%semilogy(snrdb, ber,'o-')
snrdb=1:1:10;
snrlin=10.^(snrdb./10);
tber=0.5.*erfc(sqrt(snrlin));
semilogy(snrdb,ber,'-bo',snrdb,tber,'-mh')
title('QPSK with awgn');
xlabel('Signal to noise
ratio'); ylabel('Bit error
rate');
legend('simulation','theoretical
') grid on;

OBSERVATION: The theoretical bit error rate of QPSK and the SNR for
QPSK modulated wave is compared and the values are matching.
WAVEFORM:
4) BFSK

CODE:

clear
all;
close
all; N =
1000;
m = randi([0,1],1,N);
%% BFSK
Modulation for
i=1:N
if m(i)==0
x(i)=sqrt(-
1); else
x(i)=1;
end

end
figure;
scatterplot(x);
%% AWGN Channel and Reception
ber_sim=[];
ber_th=[];
for EbN0dB=0:1:15
EbN0=10^(EbN0dB/10);
sigma=sqrt(1/(EbN0));
n=(1/sqrt(2))*[randn(1,N)+sqrt(-
1)*randn(1,N)];
%Received Signal
%r=x+sigma.*n; %AWGN Channel
r=awgn(complex(x),EbN0dB);
scatterplot(r);
% Randn generates gaussian distribution of noise
% Decision device
m_cap=(real(r)>imag(r));
% BER calculation
noe=sum(m~=m_cap); %Returns the count of where m is not equal
to m_cap
% returns the number of operations
ber_sim1=noe/N;
ber_sim=[ber_sim ber_sim1];
ber_th1=0.5*erfc(sqrt(EbN0/2));
ber_th=[ber_th ber_th1];
end
%% Plot BER vs EbN0 Actual
EbN0dB=0:1:15;
figure;
semilogy(EbN0dB,ber_sim,'r*-',EbN0dB,ber_sim,'bo
-'); xlabel('EbN0 (dB)');
ylabel('BER');
legend('Simulation','Theoritical
'); grid on;

WAVEFORM:
OBSERVATION: The theoretical bit error rate of BFSK and the SNR for the
binary FSK modulated wave is compared and the values are matching.
Direct Sequence Spread Signal:

Algorithm: The algorithm is as follows the input bit sequence is first multiplied
by a randomly generated pseudo noise sequence using shift registers with
feedback then this obtained signal is modulated using the BPSK modulator here
the bit rate of the PSN should always be greater than the bit rate of the input
signal.

CODE:

clc;
close all;
clear all;
b=input('Enter The input Bits : ');
ln=length(b);
% Converting bit 0 to
-1 for i=1:ln
if
b(i)==0
b(i)=-1;
en
d
end
% Generating the bit sequence with each bit 8 samples
long k=1;
for i=1:ln
for j=1:8
bb(k)=b(i)
; j=j+1;
k=k+1;
end
i=i+1;
end
len=length(bb);
subplot(2,1,1);
stairs(bb,'linewidth',2); axis([0 len -2 3]);
title('ORIGINAL BIT SEQUENCE b(t)');
% Generating the pseudo random bit pattern for spreading
pr_sig=round(rand(1,len));
for i=1:len
if
pr_sig(i)==0
pr_sig(i)=-1;
end
end
subplot(2,1,2)
;
stairs(pr_sig,'linewidth',2); axis([0 len -2
3]); title('PSEUDORANDOM BIT SEQUENCE
pr_sig(t)');
% Multiplying bit sequence with Pseudorandom
Sequence for i=1:len
bbs(i)=bb(i).*pr_sig(i);
end
% Modulating the hopped
signal dsss=[];
t=0:1/10:2*pi
; c1=cos(t);
c2=cos(t+pi);
for k=1:len
if
bbs(1,k)==-1
dsss=[dsss
c1]; else
dsss=[dsss
c2]; end
end
figure,
subplot(2,1,1);
stairs(bbs,'linewidth',2); axis([0 len -2 3]);
title('MULTIPLIER OUTPUT SEQUENCE
b(t)*pr_sig(t)'); subplot(2,1,2);
plot(dsss);
title(' DS-SS SIGNAL...');

WAVEFORM:
OBSERVATION: The binary data is multiplied with the pseudo noise signal
and the obtained output is multiplied with the carrier wave to get the output DS-
SS signal as shown in the diagram.
Frequency Hop Spread Spectrum:
Algorithm: There are 2 types of frequency hop fast frequency hop and slow
frequency hop spectrum in fast frequency hop for every bit of data multiple
hops are done where as in slow frequency for every hop multiple bits are taken
So we are generating random carrier frequency which gets added to the original
carrier frequency of the binary data we are implementing mixture of fast
frequency hop and slow frequency hops so for every bit we are adding multiple
hops or multiple data for one hop to the base carrier frequency and the output is
obtained which is then demodulated to recover the original message signal

CODE:

clear
;
clc;

% Generation of bit pattern


s = round(rand(1, 25)); % Generating 25 random
bits signal = [];
carrier = [];
t = [0:2 * pi / 119:2 * pi]; % Time vector for one cycle of
carrier

for k = 1:25
if s(k) == 0
% Creating 120 samples of -1 for
bit 0 sig = -ones(1, 120);
else
% Creating 120 samples of 1 for bit
1 sig = ones(1, 120);
end
c = cos(t); % Carrier
wave carrier = [carrier
c]; signal = [signal
en sig];
d

% Plot the original bit sequence


subplot(4, 1, 1);
plot(signal, 'LineWidth', 1.5);
axis([-100 3100 -1.5 1.5]);
title('\bf\it Original Bit
Sequence'); xlabel('Samples');
ylabel('Amplitude');

% BPSK Modulation of the signal


bpsk_sig = signal .* carrier; % Modulating the
signal subplot(4, 1, 2);
plot(bpsk_sig, 'LineWidth', 1.5);
axis([-100 3100 -1.5 1.5]);
title('\bf\it BPSK Modulated
Signal'); xlabel('Samples');
ylabel('Amplitude');

% Preparation of 6 new carrier


frequencies t1 = [0:2 * pi / 9:2 *
pi];
t2 = [0:2 * pi / 19:2 *
pi]; t3 = [0:2 * pi / 29:2
* pi]; t4 = [0:2 * pi /
39:2 * pi]; t5 = [0:2 * pi
/ 59:2 * pi]; t6 = [0:2 *
pi / 119:2 * pi];

% Replicating to match signal


cycles length
c1 = 1, 12);
repmat(cos(t1),
c2 = 1, 6);
repmat(cos(t2),
c3 = 1, 4);
repmat(cos(t3),
c4 = 1, 3);
repmat(cos(t4),
c5 = 1, 2);
repmat(cos(t5),
c6 = cos(t6);

% Random frequency hops to form a spread signal


spread_signal = [];
for n = 1:25
c = randi([1 6]); % Random selection of one carrier
switch c
case 1
spread_signal = [spread_signal
c1]; case 2
spread_signal = [spread_signal
c2]; case 3
spread_signal = [spread_signal
c3]; case 4
spread_signal = [spread_signal
c4]; case 5
spread_signal = [spread_signal
c5]; case 6
spread_signal = [spread_signal c6];
end
end

% Plot the spread signal with 6 frequencies


subplot(4, 1, 3);
plot([1:3000], spread_signal, 'LineWidth',
1.5); axis([-100 3100 -1.5 1.5]);
title('\bf\it Spread Signal with 6
Frequencies'); xlabel('Samples');
ylabel('Amplitude');
% Spreading BPSK signal into a wider band
freq_hopped_sig = bpsk_sig .* spread_signal;
subplot(4, 1, 4);
plot([1:3000], freq_hopped_sig, 'LineWidth',
1.5); axis([-100 3100 -1.5 1.5]);
title('\bf\it Frequency Hopped Spread Spectrum Signal');
xlabel('Samples');
ylabel('Amplitude');

% Expressing the FFTs


figure;
subplot(2, 1, 1);
plot([1:3000], freq_hopped_sig, 'LineWidth',
1.5); axis([-100 3100 -1.5 1.5]);
title('\bf\it Frequency Hopped Spread Spectrum Signal');
xlabel('Samples');
ylabel('Amplitude');

subplot(2, 1, 2);
fft_result = abs(fft(freq_hopped_sig));
plot(fft_result, 'LineWidth', 1.5);
title('\bf\it FFT of Frequency Hopped
Signal'); xlabel('Frequency Index');
ylabel('Amplitude');

WAVEFORMS:
OBSERVATIONS: the data signal is pulse modulated then multiplied with the
randomly generated carrier frequency as described in the algorithm the FFT of
the obtained spreaded frequency indicates a low power wideband noise signal to
an unintended receiver
HUFFMAN CODING:
ALGORITHM: We arrange the given probabilities into descending order and
then logic one and 0 are given to the smallest probability and the adjacent small
probability then those two are added and sorted such that if there are
probabilities similar to the added then added will be placed in the highest and
this continues till all the numbers are added to get 1.

CODE:
clc;
close all;
clear all;

% Define the probabilities for each


symbol probabilities = [0.4, 0.2, 0.2,
0.1, 0.1];
probabilities = probabilities / sum(probabilities); % Normalize
probabilities

% Initialize the codewords, set_contents, and


set_probabilities for index = 1:length(probabilities)
codewords{index} = []; % Empty
codewords initially
set_contents{index} = index; % Set containing the
index set_probabilities(index) = probabilities(index); %
Set
probabilities
end

% While there is more than one set, combine the least probable
sets while length(set_contents) > 1
% Sort the sets based on probabilities (ascending
order) [temp, sorted_indices] =
sort(set_probabilities);
zero_set = set_contents{sorted_indices(1)}; %
Smallest set zero_probability =
set_probabilities(sorted_indices(1));

for codeword_index = 1:length(zero_set)


codewords{zero_set(codeword_index)} =
[codewords{zero_set(codeword_index)}, 1]; % Add 1 to
codeword end

one_set = set_contents{sorted_indices(2)};% Next


se
t smallest one_probability =

set_probabilities(sorted_indices(2));

for codeword_index = 1:length(one_set)


codewords{one_set(codeword_index)} =
[codewords{one_set(codeword_index)}, 0]; % Add 0 to
codeword end
% Remove the merged sets
set_contents(sorted_indices(1:2)) = [];
set_probabilities(sorted_indices(1:2)) = [];

% Combine the two sets


set_contents{end+1} = [zero_set,
one_set];
set_probabilities(end+1) = zero_probability +
one_probability; %
Combined
probability end

% Display codewords (in reverse order, as codes are built


from bottom to top)
disp('Huffman Codewords:');
for index =
1:length(codewords)
disp(['Symbol ', num2str(index), '
(Probability: ', num2str(probabilities(index)), ')
--> Codeword: ', num2str(codewords{index}(end:-
1:1))]);
end

% Calculate the symbol entropy


entropy = sum(probabilities .* log2(1 ./ probabilities));

% Calculate the average Huffman codeword length


av_length = 0;
for index = 1:length(codewords)
av_length = av_length + probabilities(index) *
length(codewords{index});
end

% Display the results


disp(['The symbol entropy is: ',
num2str(entropy)]); disp(['The average Huffman
codeword length is: ', num2str(av_length)]);
disp(['The efficiency is: ', num2str(entropy / av_length)]);

OUTPUT:
NTERPRETATION: The obtained output and the codes for the symbols is
matching with the calculated output using the algorithm
SHANNON FANO CODING:
ALGORITHM: In this method we are again arranging the probabilities into
descending order then we are creating 2 sets called zero sets and one sets such
that the sum of the probabilities in each set will be almost equal or must be such
that the difference between them must be minimum amongst all combinations.
This is done until every set has only one element

CODE:
% MATLAB code for Shannon-Fano
coding clc;
close all;
clear all;

% Set the probability input


probability_vec = [0.4 0.2 0.2 0.1
0.1];
probability_vec = probability_vec /
sum(probability_vec);

% Normalize probabilities

% Initialization of
variables sta = [];
ar = [];
coded = [];
other = [];
first = 0;
final =
length(probability_vec); sta
= [first final];
% Algorithmic logic for Shannon-Fano coding
for f = 1:length(probability_vec) % Loop for every
step other = [];
ar = [];
for h = 1:(length(sta) - 1) % Loop for each sub-
group first = sta(h) + 1;
final = sta(h +
1); if (first >=
final)
other = [other; 2]; % When only one element in a sub-
grou
p continue;
en
d
asum = 0;
difmat = [];
for i = first:final % Loop for finding difference
vector asum = asum + probability_vec(i);
resum = 0;
for j = i + 1:final
resum = resum + probability_vec(j);
end
dif = abs(asum - resum);
difmat = [difmat dif];
end
small =
min(difmat);
k = 1;
for i = first:final % Loop for finding index of
min difference
if (small ==
difmat(k)) break;
end
k = k + 1;
end
index = i;
ar = [ar index]; % Storing index in temporary stack
ind = (index + 1) - first; % Calculating number of
zeros remind = final - index; % And ones for each
sub-group other = [other; zeros(ind, 1);
en ones(remind, 1)];
d
sta = [ar sta]; % Creating final stack for each
step sta = sort(sta);
coded = [coded other]; % Creating final codeword matrix
if (length(sta) > length(probability_vec)) % Break when all
subgroups have one element
break;
end
end
% Display codewords
codewords = cell(1, length(probability_vec)); %
Initialize codewords cell array
len = [];
for i =
1:length(probability_vec)
word = [];
for j = 1:f % 'f' contains max number of bits among codes
if (coded(i, j) == 2) % Break when invalid number
(i.e. 2) is reached
break;
end
word = [word coded(i, j)];
end
len = [len length(word)]; fprintf('\
nSymbol %d code - ', i); disp(word);
codewords{i} = word;

en
d
% Calculation of entropy and average word
length ent = 0;
av_length = 0;
for i = 1:length(probability_vec)
ent = ent + (probability_vec(i) * log2(1 /
probability_vec(i)));
av_length = av_length + len(i) * probability_vec(i);
end

% Calculation of efficiency
eff = (ent / av_length) *
100;

% Calculation of variance
variance_val = 0;
for ind = 1:length(codewords)
variance_val = variance_val + probability_vec(ind) *
(length(codewords{ind}) - av_length)^2;
end

disp(['The symbol entropy is: ', num2str(ent)]);


disp(['The average Shannon-Fano codeword length is:
', num2str(av_length)]);
disp(['The efficiency is: ', num2str(eff)]);
disp(['The variance is: ',
num2str(variance_val)]);

OUTPUT:

INTERPRETATION:
The output obtained from the matlab is matching with the output traced using the
algorithm as mentioned above hence the data has been coded successfully

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy