You are on page 1of 10

%OFDM Code

%codi ng used: Convol ut i onal codi ng


%Si ngl e f r ame si ze: 96 bi t s
%Tot al no. of Fr ames: 100
%Modul at i on: 16- QAM
%No. of Pi l ot s: 4
%Cyl i c Ext ensi on: 25%( 16)

cl ose al l
cl ear al l
cl c


%%
%Gener at i ng and codi ng dat a
t _dat a=r andi nt ( 9600, 1) ' ;
x=1;
si =1; %f or BER r ows
%%
f or d=1: 100;
dat a=t _dat a( x: x+95) ;
x=x+96;
k=3;
n=6;
s1=si ze( dat a, 2) ; %Si ze of i nput mat r i x
j =s1/ k;

%%
%Convol ut i onal l y encodi ng dat a
const l en=7;
codegen = [ 171 133] ; %Pol ynomi al
t r el l i s = pol y2t r el l i s( const l en, codegen) ;
codedat a = convenc( dat a, t r el l i s) ;



%%
%I nt er l eavi ng coded dat a

s2=si ze( codedat a, 2) ;
j =s2/ 4;
mat r i x=r eshape( codedat a, j , 4) ;

i nt l vddat a = mat i nt r l v( mat r i x' , 2, 2) ' ; %I nt er l eave.
i nt l vddat a=i nt l vddat a' ;


%%
%Bi nar y t o deci mal conver si on

dec=bi 2de( i nt l vddat a' , ' l ef t - msb' ) ;


%%
%16- QAM Modul at i on

M=16;
y = qammod( dec, M) ;
%scat t er pl ot ( y) ;


%%
%Pi l ot i nser t i on

l endat a=l engt h( y) ;
pi l t =3+3j ;
nof pi t s=4;

k=1;

f or i =( 1: 13: 52)

pi l t _dat a1( i ) =pi l t ;

f or j =( i +1: i +12) ;
pi l t _dat a1( j ) =y( k) ;
k=k+1;
end
end

pi l t _dat a1=pi l t _dat a1' ; %si ze of pi l t _dat a =52
pi l t _dat a( 1: 52) =pi l t _dat a1( 1: 52) ; %upsi zi ng t o 64
pi l t _dat a( 13: 64) =pi l t _dat a1( 1: 52) ; %upsi zi ng t o 64

f or i =1: 52

pi l t _dat a( i +6) =pi l t _dat a1( i ) ;

end


%%
%I FFT

i f f t _si g=i f f t ( pi l t _dat a' , 64) ;


%%
%Addi ng Cycl i c Ext ensi on

cext _dat a=zer os( 80, 1) ;
cext _dat a( 1: 16) =i f f t _si g( 49: 64) ;
f or i =1: 64

cext _dat a( i +16) =i f f t _si g( i ) ;

end


%%
%Channel

%SNR

o=1;
f or snr =0: 2: 50

of dm_si g=awgn( cext _dat a, snr , ' measur ed' ) ; %Addi ng whi t e Gaussi an Noi se
%f i gur e;
%i ndex=1: 80;
%pl ot ( i ndex, cext _dat a, ' b' , i ndex, of dm_si g, ' r ' ) ; %pl ot bot h si gnal s
%l egend( ' Or i gi nal Si gnal t o be Tr ansmi t t ed' , ' Si gnal wi t h AWGN' ) ;


%%
% RECEI VER
%%
%Removi ng Cycl i c Ext ensi on

f or i =1: 64

r xed_si g( i ) =of dm_si g( i +16) ;

end


%%
%FFT

f f _si g=f f t ( r xed_si g, 64) ;

%%
%Pi l ot Synch%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


f or i =1: 52

synched_si g1( i ) =f f _si g( i +6) ;

end

k=1;

f or i =( 1: 13: 52)

f or j =( i +1: i +12) ;
synched_si g( k) =synched_si g1( j ) ;
k=k+1;
end
end

%scat t er pl ot ( synched_si g)


%%
%Demodul at i on
dem_dat a= qamdemod( synched_si g, 16) ;


%%
%Deci mal t o bi nar y conver si on

bi n=de2bi ( dem_dat a' , ' l ef t - msb' ) ;
bi n=bi n' ;


%%
%De- I nt er l eavi ng


dei nt l vddat a = mat dei nt r l v( bi n, 2, 2) ; %De- I nt er l eave
dei nt l vddat a=dei nt l vddat a' ;
dei nt l vddat a=dei nt l vddat a( : ) ' ;




%%
%Decodi ng dat a
n=6;
k=3;
decodedat a =vi t dec( dei nt l vddat a, t r el l i s, 5, ' t r unc' , ' har d' ) ; %decodi ng
dat ausi ng vet er bi decoder
r xed_dat a=decodedat a;

%%
%Cal cul at i ng BER
r xed_dat a=r xed_dat a( : ) ' ;
er r or s=0;


c=xor ( dat a, r xed_dat a) ;
er r or s=nnz( c) ;

%f or i =1: l engt h( dat a)
%
%
% i f r xed_dat a( i ) ~=dat a( i ) ;
% er r or s=er r or s+1;
%
% end
%end


BER( si , o) =er r or s/ l engt h( dat a) ;
o=o+1;

end %SNR l oop ends her e
si =si +1;
end %mai n dat a l oop

%%
%Ti me aver agi ng f or opt i mumr esul t s

f or col =1: 25; %%%change i f SNR l oop Changed
ber ( 1, col ) =0;
f or r ow=1: 100;


ber ( 1, col ) =ber ( 1, col ) +BER( r ow, col ) ;
end
end
ber =ber . / 100;

%%
f i gur e
i =0: 2: 48;
semi l ogy( i , ber ) ;
t i t l e( ' BER vs SNR' ) ;
yl abel ( ' BER' ) ;
xl abel ( ' SNR ( dB) ' ) ;
gr i d on

%DVB- T 2K Tr ansmi ssi on
%The avai l abl e bandwi dt h i s 8 MHz
%2K i s i nt ended f or mobi l e ser vi ces
cl c
cl ear al l ;
cl ose al l ;
%DVB- T Par amet er s
Tu=224e- 6; %usef ul OFDM symbol per i od
T=Tu/ 2048; %baseband el ement ar y per i od
G=0; %choi ce of 1/ 4, 1/ 8, 1/ 16, and 1/ 32
del t a=G*Tu; %guar d band dur at i on
Ts=del t a+Tu; %t ot al OFDM symbol per i od
Kmax=1705; %number of subcar r i er s
Kmi n=0;
FS=4096; %I FFT/ FFT l engt h
q=10; %car r i er per i od t o el ement ar y per i od r at i o
f c=q*1/ T; %car r i er f r equency
Rs=4*f c; %si mul at i on per i od
t =0: 1/ Rs: Tu;
%Dat a gener at or ( A)
M=Kmax+1;
r and( ' st at e' , 0) ;
a=- 1+2*r ound( r and( M, 1) ) . ' +i *( - 1+2*r ound( r and( M, 1) ) ) . ' ;
A=l engt h( a) ;
i nf o=zer os( FS, 1) ;
i nf o( 1: ( A/ 2) ) = [ a( 1: ( A/ 2) ) . ' ] ; %Zer o paddi ng
i nf o( ( FS- ( ( A/ 2) - 1) ) : FS) = [ a( ( ( A/ 2) +1) : A) . ' ] ;
%Subcar r i er s gener at i on ( B)
car r i er s=FS. *i f f t ( i nf o, FS) ;
t t =0: T/ 2: Tu;
f i gur e( 1) ;
subpl ot ( 211) ;
st em( t t ( 1: 20) , r eal ( car r i er s( 1: 20) ) ) ;
subpl ot ( 212) ;
st em( t t ( 1: 20) , i mag( car r i er s( 1: 20) ) ) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 2) ;
f =( 2/ T) *( 1: ( FS) ) / ( FS) ;
subpl ot ( 211) ;
pl ot ( f , abs( f f t ( car r i er s, FS) ) / FS) ;
subpl ot ( 212) ;
pwel ch( car r i er s, [ ] , [ ] , [ ] , 2/ T) ;
%D/ A si mul at i on
L = l engt h( car r i er s) ;
chi ps = [ car r i er s. ' ; zer os( ( 2*q) - 1, L) ] ;
p=1/ Rs: 1/ Rs: T/ 2;
g=ones( l engt h( p) , 1) ; %pul se shape
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 3) ;
st em( p, g) ;
dummy=conv( g, chi ps( : ) ) ;
u=[ dummy( 1: l engt h( t ) ) ] ; %( C)
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 4) ;
subpl ot ( 211) ;
pl ot ( t ( 1: 400) , r eal ( u( 1: 400) ) ) ;
subpl ot ( 212) ;
pl ot ( t ( 1: 400) , i mag( u( 1: 400) ) ) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 5) ;
f f =( Rs) *( 1: ( q*FS) ) / ( q*FS) ;
subpl ot ( 211) ;
pl ot ( f f , abs( f f t ( u, q*FS) ) / FS) ;
subpl ot ( 212) ;
pwel ch( u, [ ] , [ ] , [ ] , Rs) ;
[ b, a] = but t er ( 13, 1/ 20) ; %r econst r uct i on f i l t er
[ H, F] = FREQZ( b, a, FS, Rs) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 6) ;
pl ot ( F, 20*l og10( abs( H) ) ) ;
uof t = f i l t er ( b, a, u) ; %baseband si gnal ( D)
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 7) ;
subpl ot ( 211) ;
pl ot ( t ( 80: 480) , r eal ( uof t ( 80: 480) ) ) ;
subpl ot ( 212) ;
pl ot ( t ( 80: 480) , i mag( uof t ( 80: 480) ) ) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 8) ;
subpl ot ( 211) ;
pl ot ( f f , abs( f f t ( uof t , q*FS) ) / FS) ;
subpl ot ( 212) ;
pwel ch( uof t , [ ] , [ ] , [ ] , Rs) ;
%Upconver t er
s_t i l de=( uof t . ' ) . *exp( 1i *2*pi *f c*t ) ;
s=r eal ( s_t i l de) ; %passband si gnal ( E)
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 9) ;
pl ot ( t ( 80: 480) , s( 80: 480) ) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;
f i gur e( 10) ;
subpl ot ( 211) ;
%pl ot ( f f , abs( f f t ( ( ( r eal ( uof t ) . ' ) . *cos( 2*pi *f c*t ) ) , q*FS) ) / FS) ;
%pl ot ( f f , abs( f f t ( ( ( i mag( uof t ) . ' ) . *si n( 2*pi *f c*t ) ) , q*FS) ) / FS) ;
pl ot ( f f , abs( f f t ( s, q*FS) ) / FS) ;
subpl ot ( 212) ;
%pwel ch( ( ( r eal ( uof t ) . ' ) . *cos( 2*pi *f c*t ) ) , [ ] , [ ] , [ ] , Rs) ;
%pwel ch( ( ( i mag( uof t ) . ' ) . *si n( 2*pi *f c*t ) ) , [ ] , [ ] , [ ] , Rs) ;
pwel ch( s, [ ] , [ ] , [ ] , Rs) ;
t i t l e( ' TRANSMI TI ON' ) ;
gr i d on;


%DVB- T 2K Recept i on
cl c
cl ear al l ;
cl ose al l ;
Tu=224e- 6; %usef ul OFDM symbol per i od
T=Tu/ 2048; %baseband el ement ar y per i od
G=0; %choi ce of 1/ 4, 1/ 8, 1/ 16, and 1/ 32
del t a=G*Tu; %guar d band dur at i on
Ts=del t a+Tu; %t ot al OFDM symbol per i od
Kmax=1705; %number of subcar r i er s
Kmi n=0;
FS=4096; %I FFT/ FFT l engt h
q=10; %car r i er per i od t o el ement ar y per i od r at i o
f c=q*1/ T; %car r i er f r equency
Rs=4*f c; %si mul at i on per i od
t =0: 1/ Rs: Tu;
t t =0: T/ 2: Tu;
%Dat a gener at or
sM=2;
[ x, y] = meshgr i d( ( - sM+1) : 2: ( sM- 1) , ( - sM+1) : 2: ( sM- 1) ) ;
al phabet = x( : ) + 1i *y( : ) ;
N=Kmax+1;
r and( ' st at e' , 0) ;
a=- 1+2*r ound( r and( N, 1) ) . ' +i *( - 1+2*r ound( r and( N, 1) ) ) . ' ;
A=l engt h( a) ;
i nf o=zer os( FS, 1) ;
i nf o( 1: ( A/ 2) ) = [ a( 1: ( A/ 2) ) . ' ] ;
i nf o( ( FS- ( ( A/ 2) - 1) ) : FS) = [ a( ( ( A/ 2) +1) : A) . ' ] ;
car r i er s=FS. *i f f t ( i nf o, FS) ;
%Upconver t er
L = l engt h( car r i er s) ;
chi ps = [ car r i er s. ' ; zer os( ( 2*q) - 1, L) ] ;
p=1/ Rs: 1/ Rs: T/ 2;
g=ones( l engt h( p) , 1) ;
dummy=conv( g, chi ps( : ) ) ;
u=[ dummy; zer os( 46, 1) ] ;
[ b, aa] = but t er ( 13, 1/ 20) ;
uof t = f i l t er ( b, aa, u) ;
del ay=64; %Reconst r uct i on f i l t er del ay
s_t i l de=( uof t ( del ay+( 1: l engt h( t ) ) ) . ' ) . *exp( 1i *2*pi *f c*t ) ;
s=r eal ( s_t i l de) ;
%OFDM RECEPTI ON
%Downconver si on
r _t i l de=exp( - 1i *2*pi *f c*t ) . *s; %( F)
f i gur e( 1) ;
subpl ot ( 211) ;
pl ot ( t , r eal ( r _t i l de) ) ;
axi s( [ 0e- 7 12e- 7 - 60 60] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 1) ;
subpl ot ( 212) ;
pl ot ( t , i mag( r _t i l de) ) ;
axi s( [ 0e- 7 12e- 7 - 100 150] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 2) ;
f f =( Rs) *( 1: ( q*FS) ) / ( q*FS) ;
subpl ot ( 211) ;
pl ot ( f f , abs( f f t ( r _t i l de, q*FS) ) / FS) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 2) ;
subpl ot ( 212) ;
pwel ch( r _t i l de, [ ] , [ ] , [ ] , Rs) ;
%Car r i er suppr essi on
[ B, AA] = but t er ( 3, 1/ 2) ;
r _i nf o=2*f i l t er ( B, AA, r _t i l de) ; %Baseband si gnal cont i nuous- t i me ( G)
f i gur e( 3) ;
subpl ot ( 211) ;
pl ot ( t , r eal ( r _i nf o) ) ;
axi s( [ 0 12e- 7 - 60 60] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 3) ;
subpl ot ( 212) ;
pl ot ( t , i mag( r _i nf o) ) ;
axi s( [ 0 12e- 7 - 100 150] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 4) ;
f =( 2/ T) *( 1: ( FS) ) / ( FS) ;
subpl ot ( 211) ;
pl ot ( f f , abs( f f t ( r _i nf o, q*FS) ) / FS) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
subpl ot ( 212) ;
pwel ch( r _i nf o, [ ] , [ ] , [ ] , Rs) ;
%Sampl i ng
r _dat a=r eal ( r _i nf o( 1: ( 2*q) : l engt h( t ) ) ) . . . %Baseband si gnal , di scr et et i me
+1i *i mag( r _i nf o( 1: ( 2*q) : l engt h( t ) ) ) ; %( H)
f i gur e( 5) ;
subpl ot ( 211) ;
st em( t t ( 1: 20) , ( r eal ( r _dat a( 1: 20) ) ) ) ;
axi s( [ 0 12e- 7 - 60 60] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 5) ;
subpl ot ( 212) ;
st em( t t ( 1: 20) , ( i mag( r _dat a( 1: 20) ) ) ) ;
axi s( [ 0 12e- 7 - 100 150] ) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 6) ;
f =( 2/ T) *( 1: ( FS) ) / ( FS) ;
subpl ot ( 211) ;
pl ot ( f , abs( f f t ( r _dat a, FS) ) / FS) ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
subpl ot ( 212) ;
pwel ch( r _dat a, [ ] , [ ] , [ ] , 2/ T) ;
%FFT
i nf o_2N=( 1/ FS) . *f f t ( r _dat a, FS) ; %( I )
i nf o_h=[ i nf o_2N( 1: A/ 2) i nf o_2N( ( FS- ( ( A/ 2) - 1) ) : FS) ] ;
%Sl i ci ng
f or k=1: N,
a_hat ( k) =al phabet ( ( i nf o_h( k) - al phabet ) ==mi n( i nf o_h( k) - al phabet ) ) ; %
end;
f i gur e( 7)
pl ot ( i nf o_h( ( 1: A) ) , ' . k' ) ;
t i t l e( ' i nf o- h Recei ved Const el l at i on' )
axi s squar e;
axi s equal ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
f i gur e( 8)
pl ot ( a_hat ( ( 1: A) ) , ' or ' ) ;
t i t l e( ' a_hat 4- QAM' )
axi s squar e;
axi s equal ;
t i t l e( ' RECEPTI ON' ) ;
gr i d on;
axi s( [ - 1. 5 1. 5 - 1. 5 1. 5] ) ;

You might also like