Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views34 pages

Digital Communication Lab Report

Digital communication

Uploaded by

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

Digital Communication Lab Report

Digital communication

Uploaded by

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

1 clc;

2 clear all;
3
4 F = 10; % frequency of signal [Hz]
5 Fs = 2.5*F; % sampling rate [Hz]
6 Ts = 1/Fs;
7
8 tc = 0:0.001:5/F; % CT axis
9 xc = cos(2*pi*F*tc); % CT signal
10
11 td = 0:Ts:5/F; % DT axis
12 xd = cos(2*pi*F*td); % DT signal
13
14 grid on
15 subplot(311)
16 plot(tc,xc,'r')
17 xlabel('Time axis');
18 ylabel('Amplitude');
19 title('Carrier wave');
20
21 subplot(312)
22 stem(td,xd,'b')
23 xlabel('Time axis');
24 ylabel('Amplitude')
25 title('Sampled wave');
26
27 xr = zeros(size(tc))%all zero vector
28 N = length(td);
29 for t = 1:length(tc)
30 for n = 0:N-1
31 xr(t) = xr(t) + xd(n+1)*sin(pi*(tc(t)-n*Ts)/Ts)./(pi*(tc(t)-n*Ts)/Ts);
32 end
33 end
34
35 subplot(313)
36 plot(tc,xc,'r')
37 hold on;
38 plot(tc,xr,'g')
39 xlabel('Time [sec]')
40 ylabel('Amplitude')
41 title('Reconstructed wave');
42
43 print("-fillpage","bestfit","-landscape","exp1.pdf");
Carrier wave
1

0.5
Amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5
Time axis

Sampled wave
1

0.5
Amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6
Time axis

Reconstructed wave
1.5
1
Amplitude

0.5
0
-0.5
-1
-1.5
0 0.1 0.2 0.3 0.4 0.5
Time [sec]
1 %pcm-pulse code modulation
2 clc;
3 close all;
4 clear all;
5
6 n=input('Enter for n-bit PCM system: '); %encoded bit;
7 n1=input('Enter sampling frequency: '); %sampling frequency
8 L=2^n; %quantization levels
9
10 %%Anlog signal and its sampled form
11 Vmax=4;
12 x=0:pi/n1:4*pi;
13 InputSignal=Vmax*sin(x);
14
15 subplot (3,1,1);
16 grid on
17 plot(InputSignal); %Analog signal
18 title('Analog Signal');
19
20 subplot (3,1,2);
21 stem (InputSignal); % Sampled Signal grid on;
22 grid on;
23 title('sampled signal');
24
25 %%Quantization process
26 Vmin=-Vmax;
27 Stepsize=(Vmax-Vmin)/L; %differene btw each quantization level
28 Partition=Vmin:Stepsize:Vmax; %quantization levels-partione
29 codebook=Vmin-(Stepsize/2):Stepsize:Vmax+(Stepsize/2); %quantization vales_as final outpu
30
31 [ind,q]=quantiz(InputSignal,Partition,codebook);
32 %Quantization process
33 NonZeroInd=find(ind~=0);
34 ind(NonZeroInd)=ind(NonZeroInd)-1;
35
36 BelowVmin=find(q==Vmin-(Stepsize/2));
37 q(BelowVmin)=Vmin+(Stepsize/2);
38
39 %AboveVmax=find(q==Vmax+(Stepsize/2);
40
41 subplot(3,1,3);
42 stem(q);
43 grid on; %display the quantization values
44 title('Quantized Signal');
45
46 %Encoding process
47 figure
48 TransmittedSig=de2bi(ind,3,'left-msb'); %encode the quantization level
49 %by default de2bi give right msb
50 %Y=TransmittedSig'
51 encode=reshape(TransmittedSig',[1,size(TransmittedSig,1)*size(TransmittedSig,2)])
52
53 subplot(2,1,1);
54 grid on;
55 stairs(encode) % display the serial code bit stream
56 axis([0 100 -2 3]) ;
57 title('TransmittedSig sign3al');
58
59 %% Demodulation of pcm signal
60
61 RecevedCode=reshape(encode,n,length(encode)/n);
62 %Again convert the serialcode into frame of 1 byte
63 decode_sig=bi2de(RecevedCode',3,'left-msb');
64 %binary to decimal conversion
65 q=(Stepsize*decode_sig); %convert into voltage values
66 q=q+(Vmin+(Stepsize/2)); %above gives a DC shifted version of Actual signal
67 %Thus it is necessary to bring it to zero levels
68
69 subplot(2,1,2);
70 grid on;
71 plot(q); %plot demodulation signal
72 title('Demodulated Siganl');
73
74 print("-fillpage","bestfit","-landscape","exp1.pdf");
Analog Signal
4

-2

-4
0 20 40 60 80 100

sampled signal
4

-2

-4
0 20 40 60 80 100

Quantized Signal
4

-2

-4
0 20 40 60 80 100
TransmittedSig sign3al
3

-1

-2
0 20 40 60 80 100

Demodulated Siganl
10

-2

-4
0 20 40 60 80 100
1 %ASK-amplitude shift keyed
2 clc %for clearing the command window
3 close all %for closing all the window except command window
4 clear all %for deleting all the variables from the memory
5
6 fc=input('Enter the freq of Sine Wave carrier:');
7 fp=input('Enter the freq of Periodic Binary pulse (Message):');
8 amp=input('Enter the amplitude (For Carrier & Binary Pulse Message):');
9
10 t=0:0.001:1; % For setting the sampling interval
11 c=amp.*sin(2*pi*fc*t); % For Generating Carrier Sine wave
12
13 subplot(3,1,1) %For Plotting The Carrier wave
14 plot(t,c)
15 xlabel('Time')
16 ylabel('Amplitude')
17 title('Carrier Wave')
18 m=1*square(2*pi*fp*t)+1; %For Generating Square wave message
19
20 subplot(3,1,2) %For Plotting The Square Binary Pulse (Message)
21 plot(t,m)
22 xlabel('Time')
23 ylabel('Amplitude')
24 title('Binary Message Pulses')
25
26 w=c.*m; % The Shift Keyed Wave
27
28 subplot(3,1,3) %For Plotting The Amplitude Shift Keyed Wave
29 plot(t,w)
30 xlabel('Time')
31 ylabel('Amplitude')
32 title('Amplitide Shift Keyed Signal')
33
34 print("-fillpage","bestfit","-landscape","exp1.pdf");
Carrier Wave
2

Amplitude 1

-1

-2
0 0.2 0.4 0.6 0.8 1
Time

Binary Message Pulses


2

1.5
Amplitude

0.5

0
0 0.2 0.4 0.6 0.8 1
Time

Amplitide Shift Keyed Signal


4

2
Amplitude

-2

-4
0 0.2 0.4 0.6 0.8 1
Time
1 %ASK using linecoding
2 clc;
3 close all;
4 clear all;
5 bits = [1 0 1 0 1 0 0 1 1 0 1];
6 bitrate = 1; % bits per second
7
8 T = length(bits)/bitrate; %total time
9 n = 200; %no of sample per bit
10 N = n*length(bits); %total no of sample
11 dt = T/N; %time taken by 1 sample
12 t = 0:dt:T;
13 x = zeros(1,length(t));
14
15
16 %% Unipolar NRZ
17 for i = 0:length(bits)-1
18 if bits(i+1) == 1
19 x(i*n+1:(i+1)*n) = 1;
20 else
21 x(i*n+1:(i+1)*n) = 0;
22 end
23 end
24
25 fc=input('Enter the freq of Sine Wave carrier:');
26 fp=input('Enter the freq of Periodic Binary pulse (Message):');
27 amp=input('Enter the amplitude (For Carrier & Binary Pulse Message):');
28
29 c=amp.*sin(2*pi*fc*t);
30 subplot(3,1,1) %
31 plot(t,c)
32 xlabel('Time')
33 ylabel('Amplitude')
34 title('Carrier Wave')
35
36 subplot(3,1,2)
37 plot(t,x+1,'LineWidth',4)
38 xlabel('Time')
39 ylabel('Amplitude')
40 title('Binary Message Pulses')
41
42 w=c.*(x+1);
43 subplot(3,1,3)
44 plot(t,w)
45 xlabel('Time')
46 ylabel('Amplitude')
47 title('Amplitide Shift Keyed Signal')
48
49 print("-fillpage","bestfit","-landscape","exp1.pdf");
Carrier Wave
2

Amplitude 1

-1

-2
0 2 4 6 8 10 12
Time

Binary Message Pulses


2

1.8
Amplitude

1.6

1.4

1.2

1
0 2 4 6 8 10 12
Time

Amplitide Shift Keyed Signal


4

2
Amplitude

-2

-4
0 2 4 6 8 10 12
Time
1 %FSK-Frequency shift keyed
2 clc;
3 close all
4 clear all
5
6 fc1=input('Enter the freq of 1st Sine Wave carrier:');
7 fc2=input('Enter the freq of 2nd Sine Wave carrier:');
8 fp=input('Enter the freq of Periodic Binary pulse (Message):');
9 amp=input('Enter the amplitude (For Both Carrier & Binary Pulse Message):');
10
11 amp=amp/2;
12 t=0:0.001:1; % For setting the sampling interval
13
14 c1=amp.*sin(2*pi*fc1*t);% For Generating 1st Carrier Sine wave
15 c2=amp.*sin(2*pi*fc2*t);% For Generating 2nd Carrier Sine wave
16
17 subplot(4,1,1); %For Plotting The Carrier wave
18 plot(t,c1)
19 xlabel('Time')
20 ylabel('Amplitude')
21 title('Carrier 1 Wave')
22
23 subplot(4,1,2) %For Plotting The Carrier wave
24 plot(t,c2)
25 xlabel('Time')
26 ylabel('Amplitude')
27 title('Carrier 2 Wave')
28
29 m=amp.*square(2*pi*fp*t)+amp;%For Generating Square wave message
30
31 subplot(4,1,3) %For Plotting The Square Binary Pulse (Message)
32 plot(t,m,'LineWidth',3)
33 xlabel('Time')
34 ylabel('Amplitude')
35 title('Binary Message Pulses')
36
37 for i=0:1000 %here we are generating the modulated wave
38 if m(i+1)==0
39 mm(i+1)=c2(i+1);
40 else
41 mm(i+1)=c1(i+1);
42 end
43 end
44
45 subplot(4,1,4) %For Plotting The Modulated wave
46 plot(t,mm)
47 xlabel('Time')
48 ylabel('Amplitude')
49 title('Modulated Wave')
50
51 print("-fillpage","bestfit","-landscape","exp1.pdf");
Carrier 1 Wave
1
Amplitude 0.5
0
-0.5
-1
0 0.2 0.4 0.6 0.8 1
Time

Carrier 2 Wave
1
Amplitude

0.5
0
-0.5
-1
0 0.2 0.4 0.6 0.8 1
Time

Binary Message Pulses


2
Amplitude

1.5
1
0.5
0
0 0.2 0.4 0.6 0.8 1
Time

Modulated Wave
1
Amplitude

0.5
0
-0.5
-1
0 0.2 0.4 0.6 0.8 1
Time
1 %psk-phase shift keyed
2 clc
3 close all
4 clear all
5
6 t=0:.001:1; % For setting the sampling interval
7
8 fc=input('Enter frequency of Carrier Sine wave: ');
9 fm=input('Enter Message frequency : ');
10 amp=input('Enter Carrier & Message Amplitude(Assuming Both Equal):');
11
12 c=amp.*sin(2*pi*fc*t);% Generating Carrier Sine
13
14 subplot(3,1,1) %For Plotting The Carrier wave
15 plot(t,c)
16 xlabel('Time')
17 ylabel('Amplitude')
18 title('Carrier')
19
20 m=square(2*pi*fm*t);% For Plotting Message signal
21
22 subplot(3,1,2)
23 plot(t,m,'LineWidth',4)
24 xlabel('time')
25 ylabel('ampmplitude')
26 title('Message Signal')
27
28 x=c.*m; % Sine wave multiplied with square wave in order to generate PSK
29
30 subplot(3,1,3) % For Plotting PSK (Phase Shift Keyed) signal
31 plot(t,x)
32 xlabel('t')
33 ylabel('y')
34 title('PSK')
35
36 print("-fillpage","bestfit","-landscape","exp1.pdf");
Carrier
2

Amplitude 1

-1

-2
0 0.2 0.4 0.6 0.8 1
Time

Message Signal
1
ampmplitude

0.5

-0.5

-1
0 0.2 0.4 0.6 0.8 1
time

PSK
2

0
y

-1

-2
0 0.2 0.4 0.6 0.8 1
t
1 %delta modulation
2 clc;
3 clear all;
4 close all;
5
6 fm=1;
7 fs=20*fm;
8 A=1;
9
10 t=0:1/fs:2;
11
12 sig=sin(2*pi*fm*t);
13
14 d=0.8*(2*pi*fm*A/fs);
15
16 for i=1:length(sig)
17 if i==1
18 diffsig(i)=sig(i)-0;
19 qsig(i)=d*sign(diffsig(i));
20 xcap(i)=qsig(i);
21 else diffsig(i)=sig(i)-xcap(i-1);
22 qsig(i)=d*sign(diffsig(i));
23 xcap(i)=qsig(i)+xcap(i-1);
24 end
25 end
26 %recons
27 for i=1:length(sig)
28 if i==1
29 xcapr(i)=qsig(i);
30 else
31 xcapr(i)=qsig(i)+xcapr(i-1);
32 end
33 end
34 plot(t,sig);
35 hold on;
36 stairs(t,xcapr);
37
38 print("-fillpage","bestfit","-landscape","exp1.pdf");
1.5

0.5

-0.5

-1

-1.5
0 0.5 1 1.5 2
1 %differential pulse code modulation
2 clc;
3 clear all;
4 close all;
5
6 sig=[0 0.3 0.6 1 1.3 1.5 1.3 1 0.6 0.3 0 -0.3 -0.6 -1 -1.3 -1.5 -1.3 -1 -0.6 -0.3 0];
7 L=length(sig);
8 n=0:L-1;
9 subplot(221);
10 stem(n,sig,'r');
11 xlabel('length');
12 ylabel('sampled value');
13 title('message signal');
14
15 delaysig=[0 sig(1:end-1)];
16
17
18 diffsig=sig-delaysig;
19 subplot(222);
20 stem(n,diffsig,'g*');
21 xlabel('length');
22 ylabel('sampled value');
23 title('difference signal before(green) and after(blue) quantisation');
24
25 %quantization
26 QSIG=[];
27 for i=1:L
28 if diffsig(i)>0.3
29 Qsig=0.5;
30 elseif diffsig(i)>=0.1
31 Qsig=0.25;
32 elseif diffsig(i)>=0
33 Qsig=0.1;
34 elseif diffsig(i)<-0.3
35 Qsig=-0.5;
36 elseif diffsig(i)<-0.1
37 Qsig=-0.25;
38 elseif diffsig(i)<=0
39 Qsig=-0.1;
40 end
41 QSIG=[QSIG Qsig];
42 end
43 hold on;
44 stem(n,QSIG,'b');
45
46 subplot(223);
47 stem(n,QSIG,'b');
48 xlabel('length');
49 ylabel('quantised value');
50 title('signal after quantisation');
51
52 pred=0; %predicted eroor
53 xcap=[];
54 for j=1:L
55 xcapint=pred+QSIG(j);
56 xcap=[xcap xcapint];
57 pred=xcapint;
58 end
59
60 subplot(224);
61 stem(n,xcap,'k*');
62 xlabel('length');
63 ylabel('sampled value');
64 title('reconstructed signal(black) and original signal(red)');
65 hold on;
66 stem(n,sig,'r');
67
68 print("-fillpage","bestfit","-landscape","exp1.pdf");
message signal difference signal before(green) and after(blue) quantisation
1.5 0.8

0.6
1
0.4
sampled value

sampled value
0.5
0.2

0 0

-0.2
-0.5
-0.4
-1
-0.6

-1.5 -0.8
0 5 10 15 20 0 5 10 15 20
length length

signal after quantisation reconstructed signal(black) and original signal(red)


0.8 2

0.6

0.4 1
quantised value

sampled value
0.2

0 0

-0.2

-0.4 -1

-0.6

-0.8 -2
0 5 10 15 20 0 5 10 15 20
length length
1 %dpcm by another method
2 clc;
3 clear all;
4
5 fm=1;
6 fs=20*fm;
7 A=2;
8 t=0:1/fs:2;
9 sig=sin(2*pi*fm*t);
10
11 for i=1:length(sig)
12 if i==1
13 diffsig(i)=sig(i)-0;
14 qsig(i)=round(diffsig(i));
15 xcap(i)=qsig(i);
16 else diffsig(i)=sig(i)-xcap(i-1);
17 qsig(i)=round(diffsig(i));
18 xcap(i)=qsig(i)+xcap(i-1);
19 end
20 end
21
22 for i=1:length(sig)
23 if i==1
24 xcapr(i)=qsig(i);
25 else
26 xcapr(i)=qsig(i)+xcapr(i-1);
27 end
28 end
29 plot(t,xcapr);
30 hold on;
31 plot(t,sig);
32
33 print("-fillpage","bestfit","-landscape","exp1.pdf");
1

0.5

-0.5

-1
0 0.5 1 1.5 2
1 %linecoding
2 clc;
3 clear all;
4 close all;
5
6 bits = [ 1 0 1 0 0 0 1 1 0];
7 bitrate = 1;
8
9 T = length(bits)/bitrate; #total time taken
10 n = 200; #no of sample points per bit
11 N = n*length(bits); # total bit samples
12 dt = T/N;
13 t = 0:dt:T;
14 x = zeros(1,length(t));
15
16
17 %%%%%%%%%% UNIPOLAR NON RETURN TO ZERO %%%%%%%%%%%%%%%%%%%
18 for i = 0:length(bits)-1
19 if bits(i+1) == 1
20 x(i*n+1:(i+1)*n) = 1;
21 else
22 x(i*n+1:(i+1)*n) = 0;
23 end
24 end
25 subplot(411);
26 plot(t,x,'LineWidth',2);
27 xlabel('length');
28 ylabel('value');
29 title('unipolar NRZ');
30 axis([0 10 -0.1 1.1]);
31 %%%%%%%%% UNIPOLAR RETURN TO ZERO %%%%%%%%%%%%%%%%%%%%%%%%%%
32 for i = 0:length(bits)-1
33 if bits(i+1) == 1
34 x(i*n+1:(i+0.5)*n) = 1;
35 x((i+0.5)*n+1:(i+1)*n) = 0;
36 else
37 x(i*n+1:(i+1)*n) = 0;
38 x((i+0.5)*n+1:(i+1)*n) = 0;
39 end
40 end
41 subplot(412);
42 plot(t,x,'LineWidth',2);
43 xlabel('length');
44 ylabel('value');
45 title('unipolar RZ');
46 axis([0 10 -0.1 1.1]);
47 %%%%%%%%% POLAR RETURN TO ZERO %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 for i = 0:length(bits) - 1
49 if bits(i+1)==1
50 x(i*n+1:(i+0.5)*n) = 1;
51 x((i+0.5)*n+1:(i+1)*n) = 0;
52 else
53 x(i*n+1:(i+0.5)*n) = -1;
54 x((i+0.5)*n+1:(i+1)*n) = 0;
55 end
56 end
57 subplot(413);
58 plot(t,x,'LineWidth',2);
59 xlabel('length');
60 ylabel('value');
61 title('polar RZ');
62 axis([0 10 -1.1 1.1]);
63 %%%%%%%%%%%% MANCHESTER CODING%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 for i = 0:length(bits)-1
65 if bits(i+1)==1
66 x(i*n+1:(i+0.5)*n) = 1;
67 x((i+0.5)*n+1:(i+1)*n) = -1;
68 else
69 x(i*n+1:(i+0.5)*n) = -1;
70 x((i+0.5)*n+1:(i+1)*n) = 1;
71 end
72 end
73 subplot(414);
74 plot(t,x,'LineWidth',2);
75 xlabel('length');
76 ylabel('value');
77 title('manchester coding');
78 axis([0 10 -1.1 1.1]);
79
80 print("-fillpage","bestfit","-landscape","exp1.pdf");
unipolar NRZ

1
value 0.8
0.6
0.4
0.2
0
0 2 4 6 8 10
length

unipolar RZ

1
0.8
value

0.6
0.4
0.2
0
0 2 4 6 8 10
length

polar RZ
1
0.5
value

0
-0.5
-1
0 2 4 6 8 10
length

manchester coding
1
0.5
value

0
-0.5
-1
0 2 4 6 8 10
length

You might also like