100% found this document useful (1 vote)
173 views

Matlab

The document discusses MATLAB code simulations for digital modulation schemes BPSK, QPSK, and 8-QAM using different pulse shaping filters, including rectangular pulse shaping (RPS) and square root raised cosine (SQRC) filtering. The code simulates the modulation and demodulation of bits to symbols and back, transmission over an AWGN channel, and calculation of bit error rate (BER) performance. Key aspects covered include Gray mapping, upsampling, convolution with pulse shaping filters, adding noise, matched filtering, and evaluating BER versus theoretical curves.

Uploaded by

zampradeep
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
173 views

Matlab

The document discusses MATLAB code simulations for digital modulation schemes BPSK, QPSK, and 8-QAM using different pulse shaping filters, including rectangular pulse shaping (RPS) and square root raised cosine (SQRC) filtering. The code simulates the modulation and demodulation of bits to symbols and back, transmission over an AWGN channel, and calculation of bit error rate (BER) performance. Key aspects covered include Gray mapping, upsampling, convolution with pulse shaping filters, adding noise, matched filtering, and evaluating BER versus theoretical curves.

Uploaded by

zampradeep
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 26

modulation BPSK, QPSK,8 QAM,Square Root Raised Cosine (SQRC), and demodulation

this blog about digital communication, how to simulate code matlab for BPSK, QPS
K and 8 QAM, then apply it to Rectangular pulse shaping (RPS) then simulate code
matlab for Square Root Raised Cosine (SQRC) filter as pulse shaping filter and
matched filter, and apply it to the system, and we found minimum number of coeff
icient that the loss did not exceed 0.5 db ,then we evaluate the coded performan
ce of BPSK and QPSK and 8-QAM with SQRC pulse shaping.
Tuesday, 28 May 2013
Code MATLAB, Simulated, QPSK, BPSK, 8 QAM, coded, uncoded, SQRC,
simulated
BPSK MATLAB code
clc;
clear all;
bits=1000000;
data=randint(1,bits)>0.5;
ebno=0:10;
BER=zeros(1,length(ebno));
for i=1:length(ebno)
%---Transmitter--------%mapping of bits into symbols
symb=2.*data-1;
%----Filter
psf=ones(1,1);
M=length(psf);
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits);
%Passing the symbols from PSF
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebnos=10.^(ebno(i)/10);
n_var=1/sqrt(2.*ebnos);
rx_symb=tx_symb+n_var*randn(1,length(tx_symb));
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(M:M:length(rx_match));
rx=rx(1:1:bits);
recv_bits=(sign(rx)+1)./2;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));

errors=size(errors,2);
BER(i)=errors/bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with Rectangular Pulse Shaping for BPSK');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure
semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for BPSK')
legend('simulation','theory')
grid on
QPSK MATLAB code
clc
clear all
bits=1000000;
data=randint(1,bits)>0.5;
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:10;
BER=zeros(1,length(ebno));
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/2;
I=zeros(1,col);
Q=I;
I=data(1:2:bits-1);
Q=data(2:2:bits);
I= -2.*I+1;
Q= -2.*Q+1;
symb=I+j.*Q;
%----Filter
psf=ones(1,1);
%---M=length(psf);
for i=1:length(ebno)
% inserting zeros between the bits

% w.r.t number of coefficients of


% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/2);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/2);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
npsd=10.^(ebno(i)/10);
n_var=1/sqrt(2.*npsd);
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(M:M:length(rx_match));
rx=rx(1:1:bits/2);
recv_bits=zeros(1,bits);
%demapping
k=1;
for ii=1:bits/2
recv_bits(k)= -( sign( real( rx(ii) ) ) -1)/2;
recv_bits(k+1)=-( sign( imag( rx(ii) ) ) -1)/2;
k=k+2;
end
%sign( real( rx )
%sign( imag( rx )
%data
%tx_symb
%rx_symb

)
)

%recv_bits
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));
errors=size(errors,2);
BER(i)=errors/bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with Rectangular Pulse Shaping for QPSK');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for QPSK')
legend('Simulation','Theory')
grid on
8 QAM MATLAB CODE
clc
clear all
bits=3000000;
data=randint(1,bits)>0.5;
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:10;
BER=zeros(1,length(ebno));
thr=BER;
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/3;
I=zeros(1,col);
Q=I;
k=1;
for i=1:3:length(data)
if(data(i:i+2)==[0 0 0])
I(k)=1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 0 1])
I(k)=3;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 1 0])
I(k)=-1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 1 1])
I(k)=-3;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[1 0 0])
I(k)=1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 0 1])
I(k)=3;
Q(k)=-1;

k=k+1;
elseif(data(i:i+2)==[1 1 0])
I(k)=-1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 1 1])
I(k)=-3;
Q(k)=-1;
k=k+1;
end
end
symb=I+j*Q;
%real(symb)
%imag(symb)
%----Filter
psf=ones(1,1);
Es=sum(psf.^2);
eb=Es/3;
eb=2;
%---M=length(psf);
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/3);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/3);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10.^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(M:M:length(rx_match));
rx=rx(1:1:bits/3);
recv_bits=zeros(1,bits);
%demapping
k=1;
for n=1:bits/3
I=real(rx(n));
Q=imag(rx(n));
if (I > 0) && (I < 2) && (Q > 0)
recv_bits(k:k+2)=[0 0 0];

elseif (I > 0) && (I < 2) && (Q < 0)


recv_bits(k:k+2)=[1 0 0];
elseif (I > 2) && (Q >0)
recv_bits(k:k+2)=[0 0 1];
elseif (I > 2) && (Q < 0)
recv_bits(k:k+2)=[1 0 1];
elseif (I < 0) && (I > -2) && (Q > 0)
recv_bits(k:k+2)=[0 1 0];
elseif (I < 0) && (I > -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 0];
elseif (I < -2) && (Q > 0)
recv_bits(k:k+2)=[0 1 1];
elseif (I < -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 1];
end
k=k+3;
end
tx_symb;
rx_symb;
data;
recv_bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));
errors=size(errors,2);
BER(i)=errors/bits;
ebno_lin=(10^(ebno(i)/10))
thr(i)=(5/12)*erfc(sqrt(ebno_lin/2));
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with Rectangular Pulse Shaping for 8QAM');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure
semilogy(ebno,BER,'b.-');
hold on
%ebno2=(10.^(ebno/10));
%thr=(5/12).*erfc(sqrt((10.^(ebno/10))./2));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for 8-QAM')
legend('Simulation','Theory')
grid on

BPSK With SQRC MATLAB code :


clc;
clear all;
bits=1000000;
data=randint(1,bits)>0.5;
ebno=0:11;
BER=zeros(1,length(ebno));

%---Transmitter--------%mapping of bits into symbols


symb=2.*data-1;
%----Filter
interval=3;
T=4;
num_coff=2*interval*T+1
%num_coff=10;
hn=zeros(1,num_coff);
beta=0.2;
PI=22/7;
k=1;
for n=-interval : 1/T : interval
if(n==0)
hn(k)=1-beta+4*beta/PI;
elseif( n==1/(4*beta) || n==-1/(4*beta) )
hn(k)=beta*cos( 0.25*PI*(1-1/beta) ) - (2*beta/PI)* cos( 0.25
*PI*(1+1/beta) );
else
hn(k)=( sin(PI*n*(1-beta)) + 4*beta*n*cos(PI*n*(1+beta)) )/(
PI*n*(1-16*beta^2*n^2));
end
k=k+1;
end
n=-interval : 1/T : interval ;
%hn=ones(1,1)
psf=hn(1:1:length(n));
psf=psf/sqrt(sum(psf.^2));
%psf=ones(1,1);
figure
stem(n,psf)
%xxxxxxxx
%Energy of bit
Es=sum(psf.^2);
eb=Es;
%xxxxxxxxxxxxxxxxxxx
%M=length(psf);
M=4;
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits);

upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits);
%Passing the symbols from PSF
tx_symb=conv(upsamp2,psf);
%tx_symb=tx_symb(1:length(upsamp2));
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)));
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(num_coff:T:length(rx_match)-num_coff);
rx=rx(1:1:bits);
recv_bits=(sign(rx)+1)./2;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));
errors=size(errors,2);
BER(i)=errors/bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with SQRC Pulse Shaping for BPSK');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

data;
upsamp2;
recv_bits;
%stem(tx_symb)
figure
semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')

title('Simulated Vs Theoritical Bit Error Rate of BPSK With SQRC')


legend('Simulation','Theory')
grid on
QPSK With SQRC MATLAB code:
clc
clear all
bits=1000000;
data=randint(1,bits)>0.5;
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:11;
BER=zeros(1,length(ebno));
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/2;
I=zeros(1,col);
Q=I;
I=data(1:2:bits-1);
Q=data(2:2:bits);
I= -2.*I+1;
Q= -2.*Q+1;
symb=I+j.*Q;
%----Filter
interval=3;
T=4;
num_coff=2*interval*T+1
%num_coff=1;
hn=zeros(1,num_coff);
beta=0.2;
PI=22/7;
k=1;
for n=-interval : 1/T : interval
if(n==0)
hn(k)=1-beta+4*beta/PI;
elseif( n==1/(4*beta) || n==-1/(4*beta) )
hn(k)=beta*cos( 0.25*PI*(1-1/beta) ) - (2*beta/PI)* cos( 0.25
*PI*(1+1/beta) );
else
hn(k)=( sin(PI*n*(1-beta)) + 4*beta*n*cos(PI*n*(1+beta)) )/(
PI*n*(1-16*beta^2*n^2));
end
k=k+1;
end
n=-interval : 1/T : interval ;
%hn=ones(1,1)
psf=hn(1:1:length(n));
psf=psf/sqrt(sum(psf.^2));
%psf=ones(1,1);
figure

stem(n,psf)
%xxxxxxxx
%Energy of bit
Es=sum(psf.^2);
eb=Es;

%---M=4;
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/2);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/2);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(num_coff:T:length(rx_match)-num_coff);
%rx=rx(1:1:bits/2);
recv_bits=zeros(1,bits);
%demapping
k=1;
for ii=1:bits/2
recv_bits(k)= -( sign( real( rx(ii) ) ) -1)/2;
recv_bits(k+1)=-( sign( imag( rx(ii) ) ) -1)/2;
k=k+2;
end
%sign( real( rx )
%sign( imag( rx )
%data
%tx_symb
%rx_symb

)
)

%recv_bits
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));
errors=size(errors,2);
BER(i)=errors/bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx

end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with SQRC Pulse Shaping for QPSK');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for QPSK with SQRC')
legend('simulation','theory')
grid on
8 QAM With SQRC MATLAB code
clc
clear all
bits=3000000;
data=randint(1,bits)>0.5;
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:.5:14.5;
BER=zeros(1,length(ebno));
thr=BER;
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/3;
I=zeros(1,col);
Q=I;
k=1;
for i=1:3:length(data)
if(data(i:i+2)==[0 0 0])
I(k)=1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 0 1])
I(k)=3;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 1 0])

I(k)=-1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 1 1])
I(k)=-3;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[1 0 0])
I(k)=1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 0 1])
I(k)=3;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 1 0])
I(k)=-1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 1 1])
I(k)=-3;
Q(k)=-1;
k=k+1;
end
end
symb=I+j*Q;
%real(symb)
%imag(symb)
%----Filter
interval=3;
T=4;
num_coff=2*interval*T+1
%num_coff=1;
hn=zeros(1,num_coff);
beta=0.2;
PI=22/7;
k=1;
for n=-interval : 1/T : interval
if(n==0)
hn(k)=1-beta+4*beta/PI;
elseif( n==1/(4*beta) || n==-1/(4*beta) )
hn(k)=beta*cos( 0.25*PI*(1-1/beta) ) - (2*beta/PI)* cos( 0.25
*PI*(1+1/beta) );
else
hn(k)=( sin(PI*n*(1-beta)) + 4*beta*n*cos(PI*n*(1+beta)) )/(
PI*n*(1-16*beta^2*n^2));
end
k=k+1;
end
n=-interval : 1/T : interval ;
%hn=ones(1,1)
psf=hn(1:1:length(n));

%psf=ones(1,1);
psf=psf./sqrt((sum(psf.^2)));
Es=sum(psf.^2);
eb=Es;
%we need to remove this
eb=2;
%xxxxxxxxxxxxxxx
%---M=4;
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/3);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/3);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10.^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(num_coff:T:length(rx_match)-num_coff);
%rx=rx(1:1:bits/3);
recv_bits=zeros(1,bits);
%demapping
k=1;
for n=1:bits/3
I=real(rx(n));
Q=imag(rx(n));
if (I > 0) && (I < 2) && (Q > 0)
recv_bits(k:k+2)=[0 0 0];
elseif (I > 0) && (I < 2) && (Q < 0)
recv_bits(k:k+2)=[1 0 0];
elseif (I > 2) && (Q >0)
recv_bits(k:k+2)=[0 0 1];
elseif (I > 2) && (Q < 0)
recv_bits(k:k+2)=[1 0 1];
elseif (I < 0) && (I > -2) && (Q > 0)
recv_bits(k:k+2)=[0 1 0];
elseif (I < 0) && (I > -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 0];
elseif (I < -2) && (Q > 0)
recv_bits(k:k+2)=[0 1 1];
elseif (I < -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 1];

end
k=k+3;
end
tx_symb;
rx_symb;
data;
recv_bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(recv_bits,data));
errors=size(errors,2);
BER(i)=errors/bits;
ebno_lin=(10^(ebno(i)/10))
thr(i)=(5/12)*erfc(sqrt(ebno_lin/2));
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal with SQRC Pulse Shaping for 8QAM');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

semilogy(ebno,BER,'b.-');
hold on
%ebno2=(10.^(ebno/10));
%thr=(5/12).*erfc(sqrt((10.^(ebno/10))./2));
semilogy(ebno,thr,'rx-');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for 8-QAM with SQRC')
legend('simulation','theory')
grid on

BPSK Coded matlab code


clc;
clear all;
bits=4000000;
gen_data=randint(1,bits)>0.5;
uncoded_data = gen_data;
bits=bits*7/4;
%coding
n=7;
k=4;
P=[0 1 1;1 0 1;1 1 0;1 1 1];
G=[P eye(4)];

H=[eye(n-k) P.'];
Ht=H.';
e=[zeros(1,7);diag(ones(1,7))];
synd_table=[ mod(e*Ht,2) e];
U=zeros(1,length(gen_data)*7/4);
kk=1;
for ii=1:4:length(gen_data)
U(kk:kk+6)=mod(gen_data(ii:ii+3)*G,2);
kk=kk+7;
end
%xxxxxxxx
%coded data
data=U;
%xxxxx
ebno=0:10;
BER=zeros(1,length(ebno));
for i=1:length(ebno)
%---Transmitter--------%mapping of bits into symbols
symb=2.*data-1;
%----Filter
psf=ones(1,1);
M=length(psf);
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits);
%Passing the symbols from PSF
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
eb=1.6;
ebno2=10.^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+ n_var*randn(1,length(tx_symb)) ;
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(M:M:length(rx_match));
rx=rx(1:1:bits);
recv_bits=(sign(rx)+1)./2;

recvd_coded_bits=recv_bits;
%decoding
corrected_coded_bits=zeros(1,length(recvd_coded_bits));
uncoded_bits=zeros(1,length(gen_data));
length(gen_data)
c=1;
for kkk=1:7:length(recvd_coded_bits)
S=mod(recvd_coded_bits(kkk:kkk+6)*Ht,2);
for iii=1:8
if S==[synd_table(iii,1) synd_table(iii,2) synd_table(iii,3)]
ed=[synd_table(iii,4) synd_table(iii,5) synd_table(iii,6) ...
synd_table(iii,7) synd_table(iii,8) synd_table(iii,9) ...
synd_table(iii,10) ];
end
end
corrected_coded_bits(kkk:kkk+6)=xor(recvd_coded_bits(kkk:kkk+6),ed);
uncoded_bits(c:c+3)=corrected_coded_bits(kkk+3:kkk+6);
c=c+4;
end
corrected_coded_bits;
uncoded_bits;
length(uncoded_bits);
length(gen_data);
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
%---SIMULATED BIT ERROR RATE---errors=find(xor(uncoded_bits,gen_data));
errors=size(errors,2);
BER(i)=errors/length(gen_data);

Pc(i)=0.5*erfc(sqrt((4/7)*10.^(ebno(i)/10)));
PC2(i)=0;
for ii=2:7
PC2(i)=PC2(i)+ii*nchoosek(7,ii)*Pc(i)^ii*(1-Pc(i))^(7-ii);
end
Pb_th(i)=PC2(i)/7;

%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal for Coded BPSK with SQRC');

xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

uncoded_data;
S;
ed;
corrected_coded_bits;
uncoded_bits;
semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
hold on
semilogy(ebno,Pb_th,'-ok');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate for Coded BPSK')
legend('Simulation','Theory Uncoded','Theory Coded')
grid on
QPSK with coded matlab code:
clc
clear all
bits=4000000;
gen_data=randint(1,bits)>0.5;
uncoded_data = gen_data;
bits=bits*7/4;
%coding
n=7;
k=4;
P=[0 1 1;1 0 1;1 1 0;1 1 1];
G=[P eye(4)];
H=[eye(n-k) P.'];
Ht=H.';
e=[zeros(1,7);diag(ones(1,7))];
synd_table=[ mod(e*Ht,2) e];
U=zeros(1,length(gen_data)*7/4);
kk=1;
for ii=1:4:length(gen_data)
U(kk:kk+6)=mod(gen_data(ii:ii+3)*G,2);
kk=kk+7;
end
%xxxxxxxx
%coded data
data=U;

%xxxxx
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:10;
BER=zeros(1,length(ebno));
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/2;
I=zeros(1,col);
Q=I;
I=data(1:2:bits-1);
Q=data(2:2:bits);
I= -2.*I+1;
Q= -2.*Q+1;
symb=I+j.*Q;
%----Filter
interval=3;
T=4;
num_coff=2*interval*T+1
%num_coff=1;
hn=zeros(1,num_coff);
beta=0.2;
PI=22/7;
k=1;
for n=-interval : 1/T : interval
if(n==0)
hn(k)=1-beta+4*beta/PI;
elseif( n==1/(4*beta) || n==-1/(4*beta) )
hn(k)=beta*cos( 0.25*PI*(1-1/beta) ) - (2*beta/PI)* cos( 0.25
*PI*(1+1/beta) );
else
hn(k)=( sin(PI*n*(1-beta)) + 4*beta*n*cos(PI*n*(1+beta)) )/(
PI*n*(1-16*beta^2*n^2));
end
k=k+1;
end
n=-interval : 1/T : interval ;
%hn=ones(1,1)
psf=hn(1:1:length(n));
psf=psf/sqrt(sum(psf.^2));
%psf=ones(1,1);
%

figure
stem(n,psf)
%xxxxxxxx
%Energy of bit
Es=sum(psf.^2);
eb=Es;
eb=1.55

%---M=4;
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/2);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/2);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(num_coff:T:length(rx_match)-num_coff);
%rx=rx(1:1:bits/2);
recv_bits=zeros(1,bits);
%demapping
k=1;
for ii=1:bits/2
recv_bits(k)= -( sign( real( rx(ii) ) ) -1)/2;
recv_bits(k+1)=-( sign( imag( rx(ii) ) ) -1)/2;
k=k+2;
end
recvd_coded_bits=recv_bits;
%decoding
corrected_coded_bits=zeros(1,length(recvd_coded_bits));
uncoded_bits=zeros(1,length(gen_data));
length(gen_data)
c=1;
for kkk=1:7:length(recvd_coded_bits)
S=mod(recvd_coded_bits(kkk:kkk+6)*Ht,2);
for iii=1:8
if S==[synd_table(iii,1) synd_table(iii,2) synd_table(iii,3)]
ed=[synd_table(iii,4) synd_table(iii,5) synd_table(iii,6) ...
synd_table(iii,7) synd_table(iii,8) synd_table(iii,9) ...
synd_table(iii,10) ];
end
end
corrected_coded_bits(kkk:kkk+6)=xor(recvd_coded_bits(kkk:kkk+6),ed);

uncoded_bits(c:c+3)=corrected_coded_bits(kkk+3:kkk+6);
c=c+4;
end
corrected_coded_bits;
uncoded_bits;
length(uncoded_bits);
length(gen_data);

%---SIMULATED BIT ERROR RATE---errors=find(xor(uncoded_bits,gen_data));


errors=size(errors,2);
BER(i)=errors/length(gen_data);
Pc(i)=0.5*erfc(sqrt((4/7)*10.^(ebno(i)/10)));
PC2(i)=0;
for ii=2:7
PC2(i)=PC2(i)+ii*nchoosek(7,ii)*Pc(i)^ii*(1-Pc(i))^(7-ii);
end
Pb_th(i)=PC2(i)/7;

%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal for Coded QPSK with SQRC');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure

semilogy(ebno,BER,'b.-');
hold on
thr=0.5*erfc(sqrt(10.^(ebno/10)));
semilogy(ebno,thr,'rx-');
hold on
semilogy(ebno,Pb_th,'-ok');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate (Coded QPSK)')
legend('Simulation','Theory Uncoded','Theory Coded')

grid on

8 QAM Coded matlab code:


clc
clear all
bits=3000000;
gen_data=randint(1,bits)>0.5;
uncoded_data = gen_data;
bits=bits*7/4;
%coding
n=7;
k=4;
P=[0 1 1;1 0 1;1 1 0;1 1 1];
G=[P eye(4)];
H=[eye(n-k) P.'];
Ht=H.';
e=[zeros(1,7);diag(ones(1,7))];
synd_table=[ mod(e*Ht,2) e];
U=zeros(1,length(gen_data)*7/4);
kk=1;
for ii=1:4:length(gen_data)
U(kk:kk+6)=mod(gen_data(ii:ii+3)*G,2);
kk=kk+7;
end
%xxxxxxxx
%coded data
data=U;
%xxxxx
%---debugging--%data=[1 1 1]
%xxxxxxxxxx
ebno=0:14;
BER=zeros(1,length(ebno));
thr=BER;
%---Transmitter--------%Gray mapping of bits into symbols
col=length(data)/3;
I=zeros(1,col);
Q=I;
k=1;
for i=1:3:length(data)
if(data(i:i+2)==[0 0 0])
I(k)=1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 0 1])
I(k)=3;
Q(k)=1;

k=k+1;
elseif(data(i:i+2)==[0 1 0])
I(k)=-1;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[0 1 1])
I(k)=-3;
Q(k)=1;
k=k+1;
elseif(data(i:i+2)==[1 0 0])
I(k)=1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 0 1])
I(k)=3;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 1 0])
I(k)=-1;
Q(k)=-1;
k=k+1;
elseif(data(i:i+2)==[1 1 1])
I(k)=-3;
Q(k)=-1;
k=k+1;
end
end
symb=I+j*Q;
%real(symb)
%imag(symb)
%----Filter
interval=3;
T=4;
num_coff=2*interval*T+1
%num_coff=1;
hn=zeros(1,num_coff);
beta=0.2;
PI=22/7;
k=1;
for n=-interval : 1/T : interval
if(n==0)
hn(k)=1-beta+4*beta/PI;
elseif( n==1/(4*beta) || n==-1/(4*beta) )
hn(k)=beta*cos( 0.25*PI*(1-1/beta) ) - (2*beta/PI)* cos( 0.25
*PI*(1+1/beta) );
else
hn(k)=( sin(PI*n*(1-beta)) + 4*beta*n*cos(PI*n*(1+beta)) )/(
PI*n*(1-16*beta^2*n^2));
end
k=k+1;
end
n=-interval : 1/T : interval ;
%hn=ones(1,1)

psf=hn(1:1:length(n));
%psf=ones(1,1);
psf=psf./sqrt((sum(psf.^2)));
Es=sum(psf.^2);
eb=Es;
%we need to remove this
eb=2.9;
%xxxxxxxxxxxxxxx
%---M=4;
for i=1:length(ebno)
% inserting zeros between the bits
% w.r.t number of coefficients of
% PSF to pass the bit stream from the PSF
z=zeros(M-1,bits/3);
upsamp=[symb;z];
upsamp2=reshape(upsamp,1,(M)*bits/3);
%Passing the symbols from PSF
%tx_symb=conv(real(upsamp2),psf)+j*conv(imag(upsamp2),psf);
tx_symb=conv(upsamp2,psf);
%--------CHANNEL----------%Random noise generation and addition to the signal
ebno2=10.^(ebno(i)/10);
%no=eb/ebno2;
%n_var=sqrt(no/2);
n_var=sqrt(eb/(2*ebno2));
rx_symb=tx_symb+(n_var*randn(1,length(tx_symb)) +j*n_var*randn(1,length(tx_
symb)) );
%xxxxxxxxxxxxxxxxxxxxxxxxxx
%-------RECEIVER----------rx_match=conv(rx_symb,psf);
rx=rx_match(num_coff:T:length(rx_match)-num_coff);
%rx=rx(1:1:bits/3);
recv_bits=zeros(1,bits);
%demapping
k=1;
for n=1:bits/3
I=real(rx(n));
Q=imag(rx(n));
if (I > 0) && (I < 2) && (Q > 0)
recv_bits(k:k+2)=[0 0 0];
elseif (I > 0) && (I < 2) && (Q < 0)
recv_bits(k:k+2)=[1 0 0];
elseif (I > 2) && (Q >0)
recv_bits(k:k+2)=[0 0 1];
elseif (I > 2) && (Q < 0)
recv_bits(k:k+2)=[1 0 1];
elseif (I < 0) && (I > -2) && (Q > 0)
recv_bits(k:k+2)=[0 1 0];
elseif (I < 0) && (I > -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 0];
elseif (I < -2) && (Q > 0)

recv_bits(k:k+2)=[0 1 1];
elseif (I < -2) && (Q < 0)
recv_bits(k:k+2)=[1 1 1];
end
k=k+3;
end
tx_symb;
rx_symb;
data;
recv_bits;
%xxxxxxxxxxxxxxxxxxxxxxxxxxx
recvd_coded_bits=recv_bits;
%decoded matlab code
corrected_coded_bits=zeros(1,length(recvd_coded_bits));
uncoded_bits=zeros(1,length(gen_data));
length(gen_data)
c=1;
for kkk=1:7:length(recvd_coded_bits)
S=mod(recvd_coded_bits(kkk:kkk+6)*Ht,2);
for iii=1:8
if S==[synd_table(iii,1) synd_table(iii,2) synd_table(iii,3)]
ed=[synd_table(iii,4) synd_table(iii,5) synd_table(iii,6) ...
synd_table(iii,7) synd_table(iii,8) synd_table(iii,9) ...
synd_table(iii,10) ];
end
end
corrected_coded_bits(kkk:kkk+6)=xor(recvd_coded_bits(kkk:kkk+6),ed);
uncoded_bits(c:c+3)=corrected_coded_bits(kkk+3:kkk+6);
c=c+4;
end
corrected_coded_bits;
uncoded_bits;
length(uncoded_bits);
length(gen_data);
%---SIMULATED BIT ERROR RATE---errors=find(xor(uncoded_bits,gen_data));
errors=size(errors,2);
BER(i)=errors/length(gen_data);
ebno_lin=(10^(ebno(i)/10))
thr(i)=(5/12)*erfc(sqrt(ebno_lin/2));
Pc(i)=(5/12)*erfc(sqrt((4/14)*10.^(ebno(i)/10)));
PC2(i)=0;
for ii=2:7
PC2(i)=PC2(i)+ii*nchoosek(7,ii)*Pc(i)^ii*(1-Pc(i))^(7-ii);
end
Pb_th(i)=PC2(i)/7;

%xxxxxxxxxxxxxxxxxxxxxxxxxxx
end
fs=1;
n_pt=2^9;
tx_spec=fft(tx_symb,n_pt);
f= -fs/2:fs/n_pt:fs/2-fs/n_pt;
figure
plot(f,abs(fftshift(tx_spec)));
title('Signal Spectrum for Signal for Coded 8-QAM with SQRC');
xlabel('Frequency [Hz]');
ylabel('x(F)');
figure
semilogy(ebno,BER,'b.-');
hold on
%ebno2=(10.^(ebno/10));
%thr=(5/12).*erfc(sqrt((10.^(ebno/10))./2));
semilogy(ebno,thr,'rx-');
hold on
semilogy(ebno,Pb_th,'-ok');
xlabel('Eb/No (dB)')
ylabel('Bit Error rate')
title('Simulated Vs Theoritical Bit Error Rate of Coded 8QAM')
legend('simulation','Theory Uncoded','Theory Coded')
grid on

Share on facebook
Share on twitter
Share on email
Share on print
More Sharing Services
0
Posted by ahmad jawarneh at Tuesday, May 28, 2013 Email ThisBlogThis!Share to Tw
itterShare to FacebookShare to Pinterest
Labels: 8 QAM, 8 QAM MATLAB code, 8QAM, BER, BPSK, BPSK MATLAB code, code, coded
, coded matlab code, decoded matlab code, ebnos, example, MATLAB, matlab code, m
odulation, program, QPSK, QPSK MATLAB code, SQRC, SQRC MATLAB CODE
Newer Post Home
Subscribe to: Post Comments (Atom)
Search This Blog
Blog Archive
2013 (12)
May (12)
example of simulate code matlab for BPSK, QPSK, 8 ...
1.1 Binary phase shift keying (BPSK)
1.2 Quadrature Phase Shift-Keying (QPSK)
1.3 Quadrature Amplitude Modulation (8 QAM)
2 Bit generation and mapping with SIMULATION MODE...
2.3 Pulse Shaping Filter
2.9 Encoder and Decoder
3.2 8 QAM with Rectangular pulse shaping filter
3.3 Uncoded Performance of BPSK with SQRC
3.6 Coded Performance of BPSK with SQRC
simulate the hamming code with bit error rate

Code MATLAB, Simulated, QPSK, BPSK, 8 QAM, coded, ...


AddThis
Google+ Badge
Simple template. Powered by Blogger.

You might also like