You are on page 1of 20

COMMUNICATION LAB

EXPERIMENT2
ASK, FSK AND PSK
MODULATION AND
DEMODULATION

1
1
2 %----------AMPLITUDE SHIFT KEYING--------%
3
4 clear all;
5 close all;
6 %-----------------Generating the Carrier ------------
7
8 fc=1000; % carrier frequency i Hz
9 amp=10;
10 amp2=4 % carrier amplitude
11 t=0:0.1:100;
12 s=amp*cos(2*pi*fc*t/length(t));
13 subplot(4,1,1);
14 plot(t,s);
15 xlabel("Time");
16 ylabel("Amplitude");
17 title("Carrier");
18 grid on;
19
20 %-------------- Generating the Data Stream------------
21
22 fm=100; % bit rate
23 b=[1 0 1 1 0 1 1]; %input bits
24 N = length(b);
25 Tb = 1/fm;
26 nb = 100;
27 digit = [];
28 for n = 1:1:N
29 if b(n) == 1;
30 sig = ones(1,nb);
31 else b(n) == 0;
32 sig = zeros(1,nb);
33 end
34 digit = [digit sig];
35 display(digit);
36 end
37 t1 = Tb/nb:Tb/nb:nb*N*(Tb/nb);
38 subplot(4,1,2);
39
40 plot(t1,digit,'r','Linewidth',2);
41 xlabel("Time");
42 ylabel("Amplitude");
43 title("Binary input");
44 grid on;
45 % ------------------- ASK Modulation ----------------
46
47 msignal=[];
48 t2 = Tb/nb:Tb/nb:Tb; % Signal time
49 for (i = 1:1:N)
50 if (b(i) == 1)
51 y = amp*cos(2*pi*fc*t2); % Modulation signal with carrier signal 1
52 else
53 y = amp2*cos(2*pi*fc*t2); % Modulation signal with carrier signal 2
54 end
55 msignal = [msignal y];
56 end
57 t3 = Tb/nb:Tb/nb:Tb*N; % time period
58 subplot(4,1,3);
59 plot(t3,msignal);
60
61 xlabel('Time');
62 ylabel('Amplitude');
63 title('ASK Modulated Signal');
64 grid on;
65
66 % ------------------- ASK Demodulation ----------------
67
68 h=1;
69 x = msignal;
70 y = h.*x
71 demod=[];
72 s = length(t2);
73
74 for n = s:s:length(y)
75 t4 = Tb/nb:Tb/nb:Tb; % Time period
76 c = cos(2*pi*fc*t4); % Carrier signal
77 mm = c.*y((n-(s-1)):n); % Convolution
78 t5 = Tb/nb:Tb/nb:Tb;
79 z = trapz(t5,mm); % Intregation
80 rz = round((2*z/Tb));
81 Ac = ((amp + amp2)/2); % Average of carrier amplitudes
82 if(rz > Ac) % Logical condition
83 a = 1;
84 else
85 a = 0;
86 end
87 demod = [demod a];
88 end
89 % ********** Representing demodulated information as digital signal **********
90
91 digit = [];
92 for n = 1:length(demod);
93 if demod(n) == 1;
94 sig = ones(1,nb);
95 else demod(n) == 0;
96 sig = zeros(1,nb);
97 end
98 digit = [digit sig];
99 end
100 t5 = Tb/nb:Tb/nb:nb*length(demod)*(Tb/nb); % Time period
101 subplot(4,1,4)
102 plot(t5,digit,'LineWidth',2.5);grid on;
103 axis([0 Tb*length(demod) -0.5 1.5]);
104 xlabel('Time');
105 ylabel('Amplitude');
106 title('ASK Demodulated Signal');
107 grid on;
Carrier
10
Amplitude

5
0
-5
-10
0 20 40 60 80 100
Time
Binary input
1
Amplitude

0.8
0.6
0.4
0.2
0
0 0.02 0.04 0.06 0.08
Time
ASK Modulated Signal
10
Amplitude

5
0
-5
-10
0 0.02 0.04 0.06 0.08
Time
ASK Demodulated Signal
1.5
Amplitude

1
0.5
0
-0.5
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07
Time
1 %------PHASE SHIFT KEYING-------%
2 clc;
3 clear all;
4 close all;
5 x=[ 0 1 0 0 1 0 1]; % Binary input
6 bp=.000001; % period of pulse
7
8 bit=[];
9 for n=1:1:length(x)
10 if x(n)==1;
11 se=ones(1,100);
12 else x(n)==0;
13 se=zeros(1,100);
14 end
15 bit=[bit se];
16 end
17 t1=bp/100:bp/100:100*length(x)*(bp/100);
18 subplot(3,1,1);
19 plot(t1,bit,'lineWidth',2.5);grid on;
20 axis([ 0 bp*length(x) -.5 1.5]);
21 ylabel('amplitude(volt)');
22 xlabel(' time(sec)');
23 title(' binary input data');
24
25 %-----modulation----%
26 A=5;% Amplitude of carrier signal
27 br=1/bp; % bit rate
28 f=br*2; % carrier frequency
29 t2=bp/99:bp/99:bp;
30 ss=length(t2);
31 m=[];
32 for (i=1:1:length(x))
33 if (x(i)==1)
34 y=A*cos(2*pi*f*t2);
35 else
36 y=A*cos(2*pi*f*t2+pi);
37 end
38 m=[m y];
39 end
40 t3=bp/99:bp/99:bp*length(x);
41 subplot(3,1,2);
42 plot(t3,m,'r');
43 xlabel('time(sec)');
44 ylabel('amplitude(volt)');
45 title('PSK modulation');
46 %-----demodulation-----%
47 mn=[];
48 for n=ss:ss:length(m)
49 t=bp/99:bp/99:bp;
50 y=cos(2*pi*f*t); % carrier signal
51 mm=y.*m((n-(ss-1)):n);
52 t4=bp/99:bp/99:bp;
53 z=trapz(t4,mm) % intregation
54 zz=round((2*z/bp))
55 if(zz>0) % logic level = (A+A)/2=0
56
57 a=1;
58 else
59 a=0;
60 end
61 mn=[mn a];
62 end
63
64 %digital signal after demodulation
65 bit=[];
66 for n=1:length(mn);
67 if mn(n)==1;
68 se=ones(1,100);
69 else mn(n)==0;
70 se=zeros(1,100);
71 end
72 bit=[bit se];
73 end
74 t4=bp/100:bp/100:100*length(mn)*(bp/100);
75 subplot(3,1,3)
76 plot(t4,bit,'LineWidth',2.5);grid on;
77 axis([ 0 bp*length(mn) -.5 1.5]);
78 ylabel('amplitude');
79 xlabel(' time');
80 title('received binary data');
81
binary input data
1.5
amplitude(volt)

1
0.5
0
-0.5
0 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06
time(sec)
PSK modulation
6
amplitude(volt)

4
2
0
-2
-4
-6
0 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06
time(sec)
received binary data
1.5
amplitude

1
0.5
0
-0.5
0 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06
time
1 %------------FREQUENCY SHIFT KEYING--------%
2
3 clc
4 close all
5 clear all
6 fc1=20; % carrier frequency 1
7 fc2=60; % carrier frequency 2
8 fp= 5;% input frequency
9 amp=5; % amplitude
10 amp=amp/2;
11 t=0:0.001:1;
12 c1=amp.*sin(2*pi*fc1*t);% 1st Carrier Sine wave
13 c2=amp.*sin(2*pi*fc2*t);% 2nd Carrier Sine wave
14 subplot(4,1,1);
15 plot(t,c1)
16 xlabel('Time')
17 ylabel('Amplitude')
18 title('Carrier1')
19 subplot(4,1,2)
20 plot(t,c2)
21 xlabel('Time')
22 ylabel('Amplitude')
23 title('Carrier2')
24 m=amp.*square(2*pi*fp*t)+amp;% message
25 subplot(4,1,3) % Binary Pulse
26 plot(t,m)
27 xlabel('Time')
28 ylabel('Amplitude')
29 title('Binary Message Pulses')
30 for i=0:1000
31 if m(i+1)==0
32 mm(i+1)=c2(i+1);
33 else
34 mm(i+1)=c1(i+1);
35 end
36 end
37 subplot(4,1,4)
38 plot(t,mm)
39 xlabel('Time')
40 ylabel('Amplitude')
41 title('Modulated Wave')
Carrier1
3
Amplitude

2
1
0
-1
-2
-3
0 0.2 0.4 0.6 0.8 1
Time
Carrier2
3
Amplitude

2
1
0
-1
-2
-3
0 0.2 0.4 0.6 0.8 1
Time
Binary Message Pulses
5
Amplitude

4
3
2
1
0
0 0.2 0.4 0.6 0.8 1
Time
Modulated Wave
3
Amplitude

2
1
0
-1
-2
-3
0 0.2 0.4 0.6 0.8 1
Time
DSBSC MODULATION AND
DEMODULATION

10
1 function amplitude = dsbsc
2
3 fm =50; % message signal frequency
4 fc =100; %carrier frequency
5 Am = 5; %amplitude of message signal
6 Ac = 10; %amplitude of carrier
7
8 Tm = 1/fm;
9 Tc = 1/fc;
10
11 t1 = 0:Tm/999:6*Tm;
12
13 message_signal = Am*sin(2*pi*fm*t1);
14 subplot(3,1,1)
15 plot(t1, message_signal, 'r');
16 grid();
17 title('Message signal');
18
19 carrier_signal = Ac*sin(2*pi*fc*t1);
20 subplot(3,1,2)
21 plot(t1, carrier_signal, 'b');
22 grid();
23 title('Carrier Signal');
24
25 amplitude = message_signal.*carrier_signal;
26 subplot(3,1,3)
27 plot(t1,amplitude, 'g');
28 grid();
29 title('DSBSC');
Message signal
6
4
2
0
-2
-4
-6
0 0.02 0.04 0.06 0.08 0.1 0.12

Carrier Signal
10
5
0
-5
-10
0 0.02 0.04 0.06 0.08 0.1 0.12

DSBSC
40
20
0
-20
-40
0 0.02 0.04 0.06 0.08 0.1 0.12
PAM, PPM AND PWM

13
1
2 %----PULSE AMPLITUDE MODULATION-------
3
4 close all;
5 clear all;
6
7 fc = 50; %carrier frequency
8 fm = 3; % message frequency
9 fs = 1000; % sampling frequency
10
11 t = 1;
12 n = [0:1/fs:t];
13 n = n(1:end - 1);
14 duty = 20;
15 s = square(2*pi*fc*n,duty);
16 s(find(s<0)) = 0;
17 msg = sin(2*pi*fm*n);
18 period_samp = length(n)/fc;
19 ind = [1:period_samp:length(n)];
20 on_samp = ceil(period_samp * duty/100);
21 pam = zeros(1,length(n));
22 for i = 1 : length(ind)
23 pam(ind(i):ind(i) + on_samp) = msg(ind(i));
24 end
25
26 subplot(3,1,1);
27 plot(n,s,'r');
28 ylim([-0.2 1.2]);
29 xlabel('Time');
30 ylabel('Amplitude');
31 title('CARRIER');
32 subplot(3,1,2);
33 plot(n,msg);
34 ylim([-1.2 1.2]);
35 xlabel('Time');
36 ylabel('Amplitude');
37 title('MESSAGE');
38 subplot(3,1,3);
39 plot(n,pam);
40 ylim([-1.2 1.2]);
41 xlabel('Time');
42 ylabel('Amplitude');
43 title('PAM');
44
CARRIER

1
Amplitude

0.8
0.6
0.4
0.2
0
-0.2
0 0.2 0.4 0.6 0.8 1
Time
MESSAGE
1
Amplitude

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

0.5
0
-0.5
-1
0 0.2 0.4 0.6 0.8 1
Time
1 %----PULSE POSITION MODULATION---%
2
3 clear all;
4 close all;
5 fc = 20; %carrier frequency
6 fm = 2; % message frequency
7 fs = 1000; % sampling frequency
8 t = 1;
9 n = [0:1/fs:t];
10 n = n(1:end - 1);
11 duty = 10;
12 % no. of samples in one square wave period
13 per = fs/fc;
14 % no. of samples in on time
15 on_t = per/duty;
16
17 s = square(2*pi*fc*n,duty);
18 s(find(s<0)) = 0;
19 % message signal
20 m = sin(2*pi*fm*n);
21
22 % Triangular wave
23 A=1.25;
24 c=A.*sawtooth(2*pi*fc*n);%Carrier sawtooth
25
26 %
27 ppm = zeros(1,length(s));
28
29
30 id = find(c > m);
31 idd = diff(id);
32 iddd = find(idd ~= 1);
33 temp(1) = id(1);
34 temp(2:length(iddd)+1) = id(iddd + 1);
35
36 % ppm signal
37 for i = 1:length(temp)
38 ppm(temp(i) : temp(i) + on_t - 1) = 1;
39 end
40
41 subplot(3,1,1);
42 plot(n,m,'LineWidth',2);
43 title('Message Signal');
44 hold on; plot(n,c,'r','LineWidth',2);
45 grid on;
46 subplot(3,1,2);
47 plot(n,s,'LineWidth',2);
48 title('Pulse Train');
49 grid on; ylim([-0.2 1.2]);
50 subplot(3,1,3);
51 plot(n,ppm,'LineWidth',2);
52 title('PPM Signal');
53 grid on;
54 ylim([-0.2 1.2]);
55
56
Message Signal
1.5
1
0.5
0
-0.5
-1
-1.5
0 0.2 0.4 0.6 0.8 1

Pulse Train

1
0.8
0.6
0.4
0.2
0
-0.2
0 0.2 0.4 0.6 0.8 1

PPM Signal

1
0.8
0.6
0.4
0.2
0
-0.2
0 0.2 0.4 0.6 0.8 1
1 clear all;
2
3 %------PULSE WIDTH MODULATION-------%
4
5
6 close all;
7 fm=5; %message frequency
8 fc=50; %carrier frequency
9 A=5; % Amplitude
10 t=0:0.001:2;
11 c=A.*sawtooth(2*pi*fc*t);%Carrier sawtooth
12 subplot(3,1,1);
13 plot(t,c);
14 xlabel('time');
15 ylabel('Amplitude');
16 title('Carrier sawtooth wave');
17 grid on;
18 m=0.75*A.*sin(2*pi*fm*t);%Message amplitude must be less than Sawtooth
19 subplot(3,1,2);
20 plot(t,m,'k');
21 xlabel('Time');
22 ylabel('Amplitude');
23 title('Message Signal');
24 grid on;
25 n=length(c);%Length of carrier sawtooth is stored to ‘n’
26 for i=1:n%Comparing Message and Sawtooth amplitudes
27 if (m(i)>=c(i))
28 pwm(i)=1;
29 else
30 pwm(i)=0;
31 end
32 end
33 subplot(3,1,3);
34 plot(t,pwm,'r');
35 xlabel('Time');
36 ylabel('Amplitude');
37 title('plot of PWM');
38 axis([0 1 0 2]);
Carrier sawtooth wave
6
4
Amplitude

2
0
-2
-4
-6
0 0.5 1 1.5 2
time
Message Signal
4
Amplitude

2
0
-2
-4
0 0.5 1 1.5 2
Time
plot of PWM
2
Amplitude

1.5
1
0.5
0
0 0.2 0.4 0.6 0.8 1
Time

You might also like