0% found this document useful (0 votes)
14 views18 pages

CS Lab

The document discusses several digital modulation techniques including frequency modulation, frequency shift keying, amplitude shift keying, phase shift keying, quadrature amplitude shift keying, and differential phase shift keying. Code examples and plots are provided to demonstrate the modulation and demodulation processes for each technique.
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)
14 views18 pages

CS Lab

The document discusses several digital modulation techniques including frequency modulation, frequency shift keying, amplitude shift keying, phase shift keying, quadrature amplitude shift keying, and differential phase shift keying. Code examples and plots are provided to demonstrate the modulation and demodulation processes for each technique.
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/ 18

1.

Frequency Modulation & Demodulation


clc;
clear all;
close all;
Fs=8000;
Fc=300;
Fm=50;
dev=100;
Vc=input('Enter the voltage Vm= ');
Vm=input('Enter the voltage Vc= ');
t=[0:0.1*Fs]/Fs;
x=Vm*sin(2*pi*t*Fm);
a=Vc*sin(2*pi*Fc*t);
y=fmmod(x,Fc,Fs,dev);
z=fmdemod(y,Fc,Fs,dev);
subplot(4,1,1);
plot(t,x);
title('Message signal');
subplot(4,1,2);
plot(t,a);
title('Carrier signal');
subplot(4,1,3);
plot(t,y);
title('Fm modulated Output');
subplot(4,1,4);
plot(t,z);
title('Fm demodulated Output');

2 . Frequency Shift Keying


%FSK
clc;
clear all;
close all;
x=input('enter the binary input');
L=length(x);
for i=1:1:L
m((i-1)*100+1:i*100)=x(i);
end
figure;
subplot(4,2,1);
plot(m);
xlabel('time');
ylabel('amplitude');
title('modulating signal');
f=100;
t=0:(1/f):(L-(1/f));
f1=10;
f2=5;
c1=sin(2*pi*f1*t);
y1=m.*c1;
subplot(4,2,2);
plot(t,y1);
xlabel('time');
ylabel('amplitude');
for j=1:L
if x(j)==1;
x(j)=0;
else
x(j)=1;
end
m1((j-1)*100+1:j*100)=x(j);
end
c2=sin(2*pi*f2*t);
y2=m1.*c2;
subplot(4,2,3);
plot(t,y2);
xlabel('time');
ylabel('amplitude');
y=y1+y2;
subplot(4,2,4);
plot(t,y);
xlabel('time');
ylabel('amplitude');
title('FSK modulated output');
r=randn(size(y));
f=y+r;
subplot(4,2,5);
plot(f);
xlabel('time');
ylabel('amplitude');
title('noise added FSK signal');
L1=length(f);
t=0:0.01:0.99;
r1=sin(2*pi*f1*t);
r1=fliplr(r1);
L2=length(r1);
L3=L1+L2-1;
u=fft(f,L3);
v=fft(r1,L3);
k1=u.*v;
k11=ifft(k1,L3);
r2=sin(2*pi*f2*t);
r2=fliplr(r2);
w=fft(r2,L3);
k2=u.*w;
k22=ifft(k2,L3);
k=k11-k22;
subplot(4,2,6);
plot(k);
xlabel('time');
ylabel('amplitude');
title('correlated signal');
for z=1:L
t(z)=k(z*100);
if t(z)>0
m1(z)=1;
else
m1(z)=0;
end
end
for i=1:1:L
s((i-1)*100+1:1:i*100)=m1(i);
end
subplot(4,2,7);
plot(s);
xlabel('time');
ylabel('amplitude');
title('demodulated signal')

3.Amplitude Shift Keying


%ASK
clc;
clear all;
close all;
x=input('enter the binary input');
L=length(x);
for i=1:1:L
m((i-1)*100+1:i*100)=x(i);
end
figure;
subplot(5,1,1);
plot(m);
xlabel('time');
ylabel('amplitude');
title('modulating signal');
f=100;
t=0:(1/f):(L-(1/f));
f1=10;
c1=sin(2*pi*f1*t);
y1=m.*c1;
subplot(5,1,2);
plot(t,y1);
xlabel('time');
ylabel('amplitude');
title('modulated signal');
r=randn(size(y1));
f=y1+r;
subplot(5,1,3);
plot(f);
xlabel('time');
ylabel('amplitude');
title('noise added ASK signal');
t1=0:0.1:0.99;
r1=sin(2*pi*t1);
r2=fliplr(r1);
L=length(f)+length(r2)-1;
d1=fft(f,L);
d2=fft(r2,L);
d=d1.*d2;
p=ifft(d,L);
subplot(5,1,4);
plot(p);
xlabel('time');
ylabel('amplitude');
title('correlated signal');
for z=1:length(x)
t(z)=p(z*100);
%t(z)=y(z*100);
if t(z)>0
%if t(z)<0
m1(z)=1;
else
m1(z)=0;
end
end
for i=1:1:length(x)
s((i-1)*100+1:1:i*100)=m1(i);
end
subplot(5,1,5);
plot(s);
xlabel('time');
ylabel('amplitude');
title('demodulated signal')

4.Phase Shift Keying


%psk
clc;
clear all;
close all;
x=input('enter the binary sequence');
n=length(x);
x(x==0)=-1;
t=0.01:0.01:n;
c=2*sin(2*pi*t);
for i=1:1:n
m((i-1)*100+1:i*100)=x(i);
end
y=c.*m;
subplot(3,2,1);
plot(t,m);
xlabel('time');
ylabel('amplitude');
title('digital input signal');
subplot(3,2,2);
plot(t,c);
xlabel('time');
ylabel('amplitude');
title('sinusoidal signal');
subplot(3,2,3);
plot(t,y);
xlabel('time');
ylabel('amplitude');
title('PSK modulated signal');
r=randn(1,length(y));
k=y+r;
subplot(3,2,4);
plot(t,k);
xlabel('time');
ylabel('amplitude');
title('noise added PSK signal');
t1=0:0.1:0.99;
r1=sin(2*pi*t1);
r2=fliplr(r1);
L=length(k)+length(r2)-1;
d1=fft(k,L);
d2=fft(r2,L);
d=d1.*d2;
p=ifft(d,L);
subplot(3,2,5);
plot(p);
xlabel('time');
ylabel('amplitude');
title('ccorrelated signal');
for j=1:length(x)
q(i)=p(100*j);
%q(i)=y(100*j);
if q(i)>0
%if q(i)<0
m1(j)=1;
else
m1(j)=0;
end
end
for i=1:1:n
s((i-1)*100+1:1:i*100)=m1(i);
end
subplot(3,2,6);
plot(s);
xlabel('time');
ylabel('amplitude');
title('demodulated signal');

5.Quadrature Amplitude Shift Keying


clc;
clear all;
close all;
data=[1 0 0 0 1 1 0 1 1 0 1 0 1 0 1 1];
figure(1);
stem(data,'linewidth',3);
grid on;
title('information before transmitting');
axis([0 11 0 1.5]);
data_NRZ=2*data-1;
s_p_data=reshape(data_NRZ,2,length(data)/2);
br=10.^6;
f=br;
T=1/br;
t=T/99:T/99:T;
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);
Y2=s_p_data(2,i)*sin(2*pi*f*t);
Y_in=[Y_in Y1];
Y_qd=[Y_qd Y2];
Y=[Y,Y1+Y2];
if (imp==0) && (p==0)
d(i)=exp(j*pi/4);
end
if (imp==1) && (p==0)
d(i)=exp(j*3*pi/4);
end
if (imp==1) && (p==1)
d(i)=exp(j*5*pi/4);
end
if (imp==0) && (p==1)
d(i)=exp(j*7*pi/4);
end
end
Tx_sig=Y;
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('waveform for inphase component in QPSK modulation');
xlabel('time');
ylabel('amplitude(volt)');
subplot(3,1,2);
plot(tt,Y_qd,'linewidth',3);
grid on;
title('waveform for quadrative component in QPSK modulation');
xlabel('time');
ylabel('amplitude(volt)');
subplot(3,1,3);
plot(tt,Tx_sig,'r','linewidth',3);
grid on;
title('QPSK modulated signal');
xlabel('time');
ylabel('amplitude(volt)');
figure(4);
subplot(1,1,1);
plot(d,'o');
axis([-2 2 -2 2]);
grid on;
xlabel('real');
ylabel('QPSK constellation');
title('QPSK constellation');
%qpsk demodulation
Rx_data=[];
Rx_sig=Tx_sig;
for (i=1:1:length(data)/2)
z_in=Rx_sig((i-1)*length(t)+1:i*length(t)).*cos(2*pi*f*t);
z_in_intg=(trapz(t,z_in))*(2/T);
if (z_in_intg>0)
Rx_in_data=1;
else
Rx_in_data=0;
end
z_qd=Rx_sig((i-1)*length(t)+1:i*length(t)).*sin(2*pi*f*t);
z_qd_intg=(trapz(t,z_qd))*(2/T);
if (z_qd_intg>0)
Rx_qd_data=1;
else
Rx_qd_data=0;
end
Rx_data=[Rx_data Rx_in_data Rx_qd_data];
end
figure(3);
stem(Rx_data,'linewidth',3);
title('information after receiving');
axis([0 11 0 1.5]);
grid on;

6. Quadrature Amplitude Modulation


%QAM
clc;
clear all;
close all;
M=16;
fprintf('\n\n\n');
Ld=log2(M);
ds=ceil(Ld);
dif=ds-Ld;
if (dif~=0)
error('the value of M is only acceptable if log2(M) is an integer');
end
nbit=16
msg=round(rand(nbit,1));
disp('binary information at transmitter');
disp(msg);
fprintf('\n\n');
x=msg;
bp=0.000001;
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);
figure(1);
subplot(3,1,1);
plot(t1,bit);
grid on;
axis ([0 bp*length(x) -0.5 1.5]);
xlabel('time');
ylabel('amplitude');
title('transmitting information as digital signal');
msg_reshape=reshape(msg,log2(M),nbit/log2(M));
disp('information are reshaped for converting symbolic form');
disp(msg_reshape);
for j=1:1:nbit/log2(M)
for i=1:1:log2(M)
a(j,i)=num2str(msg_reshape(j,i));
end
end
as=bin2dec(a);
ass=as;
figure(1);
subplot(3,1,2);
stem(ass);
title('serial symbol for m-array qam at transmitter');
xlabel('n');
ylabel('magnitude');
disp('symbolic form information for m-array qam');
disp(ass);
fprintf('\n\n');
x1=(0:M-1);
p=qammod(ass,M);
scatterplot(p);
sym=(0:1:M-1);
pp=qammod(sym,M);
scatterplot(pp);
grid on;
title('constellation diagram for m-array qam');
RR=real(p);
II=imag(p);
Sp=bp*2;
Sr=1/Sp;
f=Sr*2;
t=Sp/100:Sp/100:Sp;
ss=length(t);
m=[];
for k=1:1:length(RR);
Yr=RR(k)*cos(2*pi*f*t);
yim=II(k)*sin(2*pi*f*t);
Y=Yr+yim;
m=[m Y];
end
tt=Sp/100:Sp/100:Sp*length(RR);
figure(1);
subplot(3,1,3);
plot(tt,m);
title('waveform for m_array qam according to symbolic information');
xlabel('time');
ylabel('amplitude');
%qam demodulation
m1=[];
m2=[];
for n=ss:ss:length(m)
t=Sp/100:Sp/100:Sp;
y1=cos(2*pi*f*t);
y2=sin(2*pi*f*t);
mm1=y1.*m((n-(ss-1)):n);
mm2=y2.*m((n-(ss-1)):n);
z1=trapz (t,mm1);
z2=trapz (t,mm2);
zz1=round (2*z1/Sp);
zz2=round (2*z2/Sp);
m1=[m1 zz1];
m2=[m2 zz2];
end
%demapping m-array
clear i;
clear j;
for k=1:1:length(m1)
gt(k)=m1(k)+m2(k);
end
gt;
ax=qamdemod(gt,M);
figure(2);
subplot(2,1,1);
stem(ax);
title('re-obtain symbol m_array qam demodulation');
xlabel('n discrete time');
ylabel('magnitude');
disp('re-obtain symbol after m_array qam demodulation');
bi_in=dec2bin(ax);
[row col]=size(bi_in);
p=1;
for i=1:1:row
for j=1:1:col
re_bi_in(p) =str2num (bi_in(i,j));
p=p+1;
end
end
disp('reobtain information after m_array qam demodulation');
disp(re_bi_in);
X=re_bi_in;
bp=0.000001;
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);
figure(2);
subplot(2,1,2);
plot(t1,bit);
grid on;
axis([0 bp*length(x) -0.5 1.5]);
xlabel('time');
ylabel('amplitude');
title('receiving information as digital signal after m_array
demodulation');

7.Differential Phase Shift Keying


clc;
clear all;
close all;
x=input('enter the binary sequence');
n=length(x);
e(1)=1;
for i=1:1:n;
e(i+1)=xor(x(i),e(i));
e1(i)=e(i+1);
if e1(i)==1;
e(i+1)=0;
else if e1(i)==0;
e(i+1)=1;
end
end
end
p=e;
p(p==0)=-1;
disp(e);
for i=1:1:n
m2((i-1)*100+1:i*100)=x(i);
end
t=0.01:0.01:n;
subplot(3,2,1);
plot(t,m2);
t=0.01:0.01:n+1;
c=2*sin(2*pi*t);
for i=1:1:n+1
m((i-1)*100+1:i*100)=p(i);
end
y=c.*m;
xlabel('time');
ylabel('amplitude');
title('digital input signal');
subplot(3,2,2);
plot(t,c);
xlabel('time');
ylabel('amplitude');
title('sinusoidal signal');
subplot(3,2,3);
plot(t,y);
xlabel('time');
ylabel('amplitude');
title('DPSK modulated signal');
r=randn(1,length(y));
k=y+r;
subplot(3,2,4);
plot(t,k);
xlabel('time');
ylabel('amplitude');
title('noise added DPSK signal');
t1=0:0.1:0.99;
r1=sin(2*pi*t1);
r2=fliplr(r1);
L=length(k)+length(r2)-1;
d1=fft(k,L);
d2=fft(r2,L);
d=d1.*d2;
p=ifft(d,L);
subplot(3,2,5);
plot(p);
xlabel('time');
ylabel('amplitude');
title('correlated signal');
r=[];
for j=1:length(x)+1
q(j)=p(100*j);
%q(j)=y(100*j);
if q(j)>0
%if q(j)<0
r(j)=1;
else
r(j)=0;
end
end
for i=1:1:n;
q(i)=xor(e(i),e(i+1));
if q(i)==1;
m1(i)=0;
else if q(i)==0;
m1(i)=1;
end
end
end
disp(m1);
for i=1:1:n;
s((i-1)*100+1:1:i*100)=m1(i);
end
subplot(3,2,6);
plot(s);
xlabel('time');
ylabel('amplitude');
title('demodulated signal');

8.Error Control Coding – Hamming


clc;
clear all;
close all;
g=input('Enter the matrix');
disp('The Generated matrix is : ');
disp(g);
[n,k]=size(transpose(g));
disp('The order of linear block code for generated matrix');
disp('The code word length is : ');
disp(n);
disp('The parity bit length is : ');
disp(k);
for i=1:2^k
for j=1:k
if rem(i-1,2^(-j+k+1))>=2^(-j+k)
u(i,j)=1;
else
u(i,j)=0;
end
end
end
disp('The possible Message bits are : ');
disp('c0,c1,c2,c3');
disp(u);
disp('The possible code words are : ');
disp('b0,b1,b2,c0,c1,c2,c3 hamming distance : ');
c=rem(u*g,2);
d_min1=sum((c(1:2^k,:))');
d_min2=d_min1';
s=[c d_min2];
disp(s);
disp('The minimum hamming distance d_min for block codes :');
d_min1=min(sum((c(2:2^k,:))'));
disp(d_min1);
p=g(:,k+1:end);
h=[transpose(p),eye(n-k)];
disp('The H matrix is');
disp(h);
ht=transpose(h);
disp('The H Transpose matrix is :');
disp(ht);
r=input('Enter the received code vectors');
disp('syndrome of code vector is :');
e1=rem(r*ht,2);
disp(e1)
for i=1:1:size(ht)
if (eq(ht(i,1:n-k),e1))
r(i)=1-r(i);
break;
else
i=0;
end
end
if i==0
disp('The code word is Error less :')
else
disp('The error is in bit :')
disp(i);
disp('The corrected code word is :')
end
disp(r);

9 . Cyclic Codes
clc;
clear all;
close all;
x=input('Enter the data : ');
x1=length(x);
if x1==4
y=[1 0 1 1];
else if x1==8
y=[1 0 0 0 0 0 1 1 1];
else if x1==10
y=[1 1 0 0 0 1 1 0 1 0 1];
else if x1==16
y=[1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1];
else if x1==32
y=[1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0
1 1 0 1 1 1];
end
end
end
end
end
x2=length(y);
disp('The Generator is : ');
disp(y);
z=[x zeros(1,x2-1)];
z1=length(z);
y1=x2;
i=1;
rem=z;
for y2=y1:1:z1
k=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc(j)=xor(rem(j),y(k));
k=k+1;
else
crc(j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc(j);
end
i=i+1;
end
j=1;
for i=x1+1:1:z1;
r(j)=crc(i);
j=j+1;
end
disp('The CRC is : ');
disp(r);
a=[x r];
disp('The Code Word is : ');
disp(a);
%crc decoder
p=input('Enter the Received code word : ');
p2=length(p);
p1=p2/2;
if p1==3
y=[1 0 1 1];
else if p1==8
y=[1 0 0 0 0 0 1 1 1];
else if p1==10
y=[1 1 0 0 0 1 1 0 1 0 1];
else if p1==16
y=[1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1];
else if p1==32
y=[1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 1 0
1 1 0 1 1 1];
end
end
end
end
end
x2=length(y);
y1=x2;
i=1;
rem=p;
for y2=y1:1:p2
k=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc(j)=xor(rem(j),y(k));
k=k+1;
else
crc(j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc(j);
end
i=i+1;
end
j=1;
x1=p2-x2+1;
for i=x1+1:1:p2;
r(j)=crc(i);
j=j+1;
end
disp('The Syndrome is :');
disp(r);
if r==0
disp('code has received successfully');
break;
else
disp('Error has been occured');
end
i1=1;
for ia=1:1:x1;
g1=[];
if ia==1
g1=[ones(1,1) zeros(1,p2-ia)];
else
g1=[zeros(1,ia-1) ones(1,1) zeros(1,p2-ia)];
end
rem=[];
rem=g1;
i=1;
for y21=x1:1:p2;
k1=1;
for j=i:1:y1+i-1;
if rem(i)==1
crc1(i,j)=xor(rem(j),y(k1));
k1=k1+1;
else
crc1(i,j)=xor(rem(j),0);
end
end
for j=i:1:y1+i-1;
rem(j)=crc1(i,j);
end
i=i+1;
end
j=1;
for i=x1+1:1:p2;
ps(i1,j)=rem(i);
j=j+1;
end
i1=i1+1;
end
disp('The parity bits are :');
disp(ps);
h=[transpose(ps),eye(p2-x1)];
disp('The H matrix is');
disp(h);
ht=transpose(h);
disp('The H Transpose matrix is :');
disp(ht);
for i=1:1:size(ht)
if (eq(ht(i,1:p2-x1),r))
p(i)=1-p(i);
break;
end
end
disp('The error is in bit :')
disp(i);
disp('The corrected code word is :')
disp(p);

10 . Convolutional Codes
clc;
clear all;
close all;
x=input('Enter the Message : ');
g1=[1 1 1];
g2=[1 0 1];
a=conv(g1,x);
a1=length(a);
for i=1:1:a1;
if rem(a(i),2)==0
a(i)=0;
else
a(i)=1;
end
end
b=conv(g2,x);
b1=length(b);
for i=1:1:b1;
if rem(b(i),2)==0
b(i)=0;
else
b(i)=1;
end
end
j=1;
for i=1:2:a1+b1-1;
y(i:i+1)=[a(j),b(j)];
j=j+1;
end
disp('Encoded Data : ');
disp(y);
%Decode
a=[0 0];
b=[0 1];
c=[1 0];
d=[1 1];
x1=a;
l=1;m=1;n=1;hamm=0;
y=input('Enter the received Data : ');
for i=1:2:length(y)-1;
if x1==a;
for j=1:1:2;
g(j)=xor(a(j),y(l));
l=l+1;
h(j)=xor(d(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=a;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=b;
end
else if x1==b;
for j=1:1:2;
g(j)=xor(c(j),y(l));
l=l+1;
h(j)=xor(b(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=c;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=d;
end
else if x1==c;
for j=1:1:2;
g(j)=xor(d(j),y(l));
l=l+1;
h(j)=xor(a(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=a;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=b;
end
else if x1==d;
for j=1:1:2;
g(j)=xor(b(j),y(l));
l=l+1;
h(j)=xor(d(j),y(m));
m=m+1;
end
e=sum(g(1:2));
e1=e+hamm;
f=sum(h(1:2));
f1=f+hamm;
if e1<f1
hamm=e1;
z(n)=0;
n=n+1;
x1=c;
else
hamm=f1;
z(n)=1;
n=n+1;
x1=d;
end
end
end
end
end
end
s=length(z)-length(g1)+1;
for i=1:1:s;
z1(i)=z(i);
end
disp('Decoded Data :');
disp(z1);

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