附录A Turbo码仿真系统Matlab源代码 -...

19
附录 A Turbo码仿真系统MaHab源代码 附录ATurbo码仿真系统Matlab源代码 'Ilirbo_ system主程序休 clear all diaryturbo_logmap.txt dec_alg = input(’Please enter the decoding algorithm.(0:Log-MAP, 1:SOVA) default0 ’): ifisempty(dec_alg) decal g二0; end ifdec_ alg==0 window-flag = input(’please decide technology(1/2:single/double)default1 ifisempty(window_ flag) window_ flag=2; end sliding window wind_ size=input(’please input the window size.default: 100 ifisempty(wind_ size) windsize=100; end end !_total=input(,Pleaseentertheframesize(=info+tail,default: 400) ’); ifisempty(Ltotal) Ltotal=400; % infomationbitsplustailbits end g=input(’Please entercodegenerator:(default:g=[111;101]) ifisempty(g) g= 1 1 1; 101]: 10 1;1111 ]: 1111:1000 11; (9) - J n f l e L K :一ze 61

Transcript of 附录A Turbo码仿真系统Matlab源代码 -...

Page 1: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统MaHab源代码

附录A Turbo码仿真系统Matlab源代码

'Ilirbo_ system主程序休clear all

diary turbo_logmap.txtdec_alg = input(’Please enter the decoding algorithm. (0:Log-MAP, 1:SOVA)

default 0 ’):

if isempty(dec_alg) decal g二0;

end

if dec_ alg==0 window-flag = input(’please decide

technology(1/2:single /double) default 1 if isempty(window_ flag)

window_ flag=2; end

sliding window

wind_ size=input(’please input the window size.default: 100if isempty(wind_ size)

wind size=100;

end

end

!_total=input(,Please enter the frame size(=info+tail, default:400) ’);

if isempty(L total)L total=400; %infomation bits plus tail bits

end

g=input(’Please enter code generator:(default:g=[111;101])

if isempty(g)g = 1 1 1;

101]:

1 0 1;1 1 1 1 ]:1 1 1 1:1 0 0 0 11;

(9)

﹁一

一一

一-

介入

J目

n

口︹

fl

e

阳愉少

份兀

L

K :一ze61

Page 2: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Mat{ab源代妈

nslates 二

puncture二

0 ’):

2 m

input(' Please choose punctured/unpunctured (0/1):default

if isempty(puneture)puncture=0;

end

rate=1/(2+puncture);

%Fading amplitude; a=l in AWGN channela 二 1:

niter

3

=input(’Please enter number of iterations for each frame: default,);

if isempty(niter)niter = 3

e门d

EbNOdb二input(’Please enter Eb/NO in dB:default [2.0]if -isempty(EbNOdb)

EbNOdb=[2. 0l;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

fprintf (' \n\n

if dec_ alg =

fprintf('else

fprintf ('end

\n

0

Log-MAP decoder \n

二二=SOVA decoder二二=\n

fprintf (' Frame size=%6d\n',L_ total):fprintf (' code generator: \n');for i=1:n

for j=1:K

fprintf(end

fprintf (' \n'

%6d',g(i, J))

end

if puncture==0

fprintf(' Punctured, code rate=else

fprintf (' Unpunctured, code rateend

fprintf(' iteration number二 %6d\nfprintf(' Eb/NO (dB)=’);for i=1:length(EbNOdb)

1/2 \n'

=1/3\n'

niter);

62

Page 3: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Matlab源代码

fprintf('%10.2f',EbNOdb(i))end

fprintf(' \n \n\n

fprintf('++++\n');

++Please be patient. Wait a whileto get the result.十 +

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%4

for nEN=I:length(EbNOdb) en = 10-(EbNOdb(nEN)八0); %L -c二4*a*en*rate;%reliability value of the channel

L_ c=4*a*en;

%sigma=1/sgrt(2*rate*en): %standard deviation of AWGN noise sigma=l/sgrt(2*en); errs(nEN,l:niter)=zeros (1, niter); nferr(nEN,l:niter)=zeros(l,niter): nframe=0;

while nframe<=250

nframe =一nframe+1;

%rand ('state',0);

x=round(rand(l, L total-m)): %info. bits %rand ('state',6); [temp, alpha]=sort(rand(1,L_total)); %random

interleaver mapping

en_ output=encoderm( x, g, alpha, puncture);%encoder output(+1/一1)

%randn ('state',16); r=en.output+sigma*randn(1,L_total*(2+puncture));%received

bits

yk=demultiplex(r,alpha,puncture):%demultiplex to get inputfor decoder 1 and 2

%Scale the received bits

rec_ s=0.5*L c*yk; % Initialize extrinsic information

L_ e(1:L_ total)二zeros(1,L_total);

L_ all=zeros(l,L_ total); for iter=l:niter

%Decoder one

La (alpha)=L e;%a priori info. if dec_ alg二二0

%L all=logmapo(rec_ s(1,:),g, L a, 1);%completeinfo.

if window_ flag==1

L_ all=singlewindow(wind size,rec_ s(1 g, L a, L all, 1);

63

Page 4: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统iSPSIab源代码

else

L_ all=doublewindow(wind_size,rec_ s(1,: end

else

La 11=sova0(rec_ s(1, end

L_e二L_ all一2.0*rec_ s(1, 1i nfo.

,g, L_ a, 1,_ all, 1);

g, L_ a, 1);%complete info.

:2:2*L total) I a %extrinsic

%Decoder two

L_ a=址e (alpha);%a priori info.if dec_ alg==0 %L all=logmapo(rec_ s(2,:),g,1. a,2 %complete

info.

if window_ flag=-1

I,_all=singlewindow(wind_size,rec_s(2,:),g, La,比all, 2) else

l-all=doublewindow(wind_ size,rec_s(2,:),g, La, La l l, 2); end

else

L all=sova0(rec_s(2,:),g, La, 2);%complete info. end

L e=L all一2.0*rec_ s(2,1:2:2*L_ total)一La;extrinsic info.

xhat(alpha)=(sign(L all)+1)/2;err

If

(iter)二length(find(xhat(1:L-total-m)-=x));err(iter)>O nferr(nEN, iter)=nferr(nEN, iter)+1

end %nframe

end %iter

errs(nEN,l:niter)=errs(nEN,l:niter)+err(l:niter);

if' rem(nframe, 3)二=0 ber(nEN,l:niter)=errs(nEN,l:niter)/nframe/(址total-m); %Frame error rate

fer(nEN,l:niter)=nferr(nEN,l:niter)/nframe;

%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%

%Display intermediate results in processfprintf('**************Eb/NO=%5.2f db **************\n

EbNOdb(nEN));fprintf('Frame size 二 %d, rate 1/%d. \n l. total

64

Page 5: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

IV#录A Turbo码仿真系统Maliab源代码

2+puncture); fprintf('%d frames transmitted, %d frames in

nframe, nferr(nEN, niter)): fprintf('Bit Error Rate (from iteration 1 to

%d) : \n',niter): for i=l:niter

fprintf (' %8. 4e ’,ber(nEN,i)); end

fprintf (' \n'); fprintf ('Frame Error Rate (from iteration 1 to

%d) : \n',niter); for i=l:niter

fprintf (' %8. 4e ’,fer(nEN, i)): end

fprintf (' \n'):

error. \n

iteration

iteration

fprintf('***********************************************\n\n,); %Save intermediate results

马照挑叭禺黔汉出胎淤改欣令禺叹}沁今踢叹佚叼淡肠%黔撇浅%%%叼{浅叭1}溉%%哪跳

save turbo_ sys_ demo EbNOdb ber fer end

end %while

end MEN

diary off

网格图样程序模块1Yellisunction [next out, next state, last_ out, last-state]=trellis(g)

%set up the trellis

%g given in binary

code generator g

form. e.g. g=仁111;101

%next_ out(i,1:2):input=0, state=

%next_ out(i,3:4):input二1, state二

%next_ state(i,l):=1,⋯ 2'm

%next_ state(i,2):%last out (i, 1:2):

input=0, state二

%last_ out(i,3:4):input=1, state二

%last_ state(i, 1):=0:

%last_ state(i,2):

trellis next夕ut

i;next_ out(i, j)trellis next out

l:

(systematic bit; parity bit) when

= -1 or 1

(systematic bit; parity bit) when

next state when input 0, state二1;next state(i,i)

next state when input二1, s

trellis

i:last

last out

out (i, j)last out

(systematic

tate 二

bit;parity bit) when

= 一1 or 1

n

十L

本.

e

l

。1

﹄曰

卜日

.n

W(systematic bit;parity bit)

previous state that comes to statei when info.

previous state that comes to state i when info

65

Page 6: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Matlab源代码

二 1

J

g

.

Q自

[n, K]m 二 K :一zemax state =

%set up next_ out and next_ state matrices for systematic codefor state=l:max state

state vector=bin state( state-1, m);

%when receive a 0

dk=0;

a -k=rem( g(1, :)*[0 state_ vector]’,2);[out-0, state夕〕=encode_ bit(g, a _k, statevector)

out_ 0(1)=0;

%when receive a 1

dk=1;-

a-k二rem( g(1,:)*仁1 state vector[out-l, state-11=encode_ bit(g,

out_ 1(1)=1;next_ out(state,:)=2*[out_ 0 outnext state(state,:) =

(int_ state(state_ l)+1)〕;end

%find out which two previous states

last-state=zeros(max_ state,2);for bit=0:l

2);

state vector)

仁(int_ state(state_ 0)+1)

come to present state

for state=1:max state

last_ state(next_ state(state,bit+l), bit+l)=state; last out(next_ state(state, bit+l), bit*2+1:bit*2+2)⋯ =next_ out(state, bit*2+l:bit*2+2):

end

end

编码器各程序模块Encoderm模块function en_ output=encoderm( x, g, alpha, puncture)

%uses interleaver map’alpha'%if puncture二1, unpunctured, produces a rate 1/3 output of fixed length

%if puncture二0, punctured, produces a rate 1/2 output%multiplexer chooses odd check bits from RSC1

%and even check bits from RSC2

66

Page 7: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录w Turbo码仿真系统Matlab源代码

%determine the constraint length (K),memory (m)%and number of information bits plus tail bits.

[n,K〕K

size (g);1:

info

total

Iength(x);二l. info+m

m

,.L

eeL

%generatethe codeword corresponding to the 1st RSC coderperfectly terminated;七

x

% end

input

outputl二rsc_ encode (g, input, 1);

%make a

%second

%third

matrix with first

row corresponding石owLO

corresponing to info sequenceRSC #I' s

row corresponding to RSC #2"s

check bits

check bits.

outputl(1:2outputl(2:2

2*L total);

2*L total);

%interleave input to second encoderfor i二I:L total

inputl(1, i)=y(l,alpha(i));end

output2=rsc_ encode(g, inputl(1,1:L_ total),一1y (3,:)=output2(2:2:2*L_ total);

%paralell to serial multiplex to get output vector%puncture=0: rate increase from 1/3 to 1/2;%puncture=1;unpunctured, rate二1/3;

if puncture>0 %unpunctured

total

1:3

了卜

~一

for i二 1:

en_ output (1,3*(i一1) +j)=y(.i, i):

Y

d

O

n

卜卞

e

end

else %punctured into rate 1/2 for i=1:1. total

en_ output(l,n*(i一1) +1)=y(1, i); if rem(i,2) %odd check bits from RSCI

67

Page 8: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

F13-7L A 7Lrbo b fi具 系统 Matlab Y4 ,代妈

en_ output (l, n*i)二y(2, i); else

%even check bits from RSC2

en_ output(l,n*i)=y (3, i): end

end

end

%antipodal modulation: +1/一1en_ output=2*en_output一ones(size(en_ output))

RS C encode

function y

函数模块

=rsc_ encode(g, x, terminated)

%encodes a block of

%convolutional code

%returns the output

data

with

in y

x (0/1)with a recursive

generator

(0/1)vectors in g,

systematic

and

%if terminated>0, the trellis

% if terminated<0. it is left

is perfectly terminatedunterminated

%determine the constraint length (K)%and number of information bits.

[n, K7=size(g);m=K一1:

if terminated>0

memory (m) and rate (1/n)

L info=

L total=

else

L total=

L info=

end

length (x);L info+m;

length (x);L total一 m

%initialize the state vector

state=zeros (1, m);

%generate the codewordfor i=1:L total

if terminated<0{(terminated>0&i<=L_ info)

d_k=x(1, i): elseif terminated>0&DL info

%terminate the trellis

d_k二rem( g(1,2:K)*state',2); end

a_ k=rem( g(1,:)*[d _k state]',2);

68

Page 9: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Matlab源代码

仁output_ bits, state〕二encode_ 6i t (g, a -k, state); %since systematic, first output is input bit output_ bits(1,1)二d -k; y (n*0一I) +1:n*i)二Output_ bits;

end

encode少it程序模块function [output, state」二encode少it(g, i叩ut, state)%Copyright 1996 Matthew C. Valenti%MPRG lab, Virginia Tech%for academic use only

%This function takes as an input a to be encoded

%as well as

%the current

% It returns

the coeficients of the

state vector.

single bit

generatorpolynomials and

as output 卜

n encoded databits, where 1/n is therate.

rate is 1/n

the constraint

the amount of

=size(g);

length

memory

m = k-1

%determine the next output bitfor i二I:n

output(i)for

end

i=2

= g(i, 1)*input;:k

=xor(output(i),g(i,j)*state(j-1));

end

state=[input, state(l:m-1)];

int state程序模块function int_ state二int state( state

%converts a row vector of m hits intoa integer (base 10)

[dummy, m]=size( state

vect(i)=2一(m-i)end

69

Page 10: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统MaUab源代码

int state= state*vect ;

bin state程序模块function bin state=bin_ state( int_ state, m%converts an vector of integer into a matrix;

form

%of m bits for the i-th integer

)

the i-throw is the binarv

for j二

for

1:length( int_ state)i二m:一1:1

state (j, m-i+l)=fix(int_ state(j)/(2一〔i一1)));int state(j)=int_ state(j)一state (j, m-i+l) *2一(i一1)

end

end

bin state= state;

译码器各程序模块采用双滑动窗口技术的LOG-MAP分量译码器function L all=doublewindow(wind_size, rec_s, g, l-a, L_ all, ind_ dec)

L total=length(rec_ s)/2;Num=coil(比total加ind_ size):Numl=ceil(Num/2.0);

Num2=Num-Numl;

[n, K]=size (g);m=K-1;

nstates=2一m;

Infty=IeIO;

Alpha (1,1)=0;Alpha (l, 2: nstates) =-Infty*ones (l, nstates-1);Alpha(2:L total+l,l:nstates)二一Infty*ones(L total,nstates);

if ind dec==1

Beta(L_ total+1,1)=0; Beta(L_ total+1,2:nstates)=-Infty*ones(l,nstates-1);

elseif ind dec==2

Beta(L_ total+l,l:nstates)=-m*log(2)*ones(l,nstates)

end

Beta(I:L total,l:nstates)二一Infty*ones(L total,nstates)

%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%

for k=O:Numl

if k二二0 %computer the first sliding window

70

Page 11: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Matlab源代码

Alpha=Alphaf(1, wind_ size+l, recs, g, La, Alpha, nstates);Beta(2*wind_ size+l,l:nstates)=-m*log(2)*ones(l,nstates);

Seta=Betaf (2*wind_ si ze+l, wind_size+l, rec s, g, L,_a, Beta, nstates) elseif (k)0)&(k<Numl) %

Beta( (k十2)*wind_ size+l,l:nstates)二一m*log(2)*ones(l,nstates);

Beta=Betaf((k+2)*wind_size+l, (k+l)*wind_ size+l, rec-s, g, L a, Beta, nstatCS);

Alpha=Alphaf (k*wind-size+l, (k+l) *wind_size+l, recs, g, La, Alpha, nstates):

Beta=Betaf(k*wind_ size+l,(k-1)*wind_ size+l,rec_s,g,L_ a,Beta,nstates)

L_ all=l -allf (k*wind_ size+l, (k-1) *wind_ size+l, rec_ s, g, L a, L all, Alpha,Beta, nstates);

elseif k==Numl

Beta=Betaf (k*wind_ size+l, (k-l)*wind-size+l, rec_ s, g, L_ a, Beta, nstates);

L_ all=L_ allf(k*wind_ size+l, (k-1)*wind_ size+l, rec_s, g, L _a, L all, Alpha,Beta, nstates);

end

end

叫禺%%%%%%%哪禺料概%%饿%%%%%%%%糊钱%%%%%%哪钱%叭浅黔禺

for kk=0 : Num2

if kk==0

Beta=Betaf((L_ total+l),(L_ total+l)-win走size, rec_s, g, L_ a, Beta, nstates):

Alpha((L_total+l)-2*wind_ size,l:nstates)二一。*log (2) *ones (1, nstates);

Alpha=Alphaf((L_ total+l)-2*wind_ size,(L_ total+l)-wind_ size,rec_ s,g,La,Alpha,nstates); elseif (kk>0)&(kk<Num2-1)

Alpha((L_total+l)一(kk+2)*wind-size,l:nstates)=-m*log(2)*ones(l,nstates):

Alpha=Alphaf((L total+l)一(kk+2)*wind_ size,(比total+l)一(kk+1)*wind_ size, rec_ s, g, L_ a, Alpha, nstates);

71

Page 12: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Ma[lab源代码

Beta=Betaf((L_ total+l卜kk*wind_ size,([.-total+l)一(kk十1)*wind-size,recs, g, L a, Beta, nstates);

Alpha=Alphaf((L-totnal+l)-kk*windsize,(L_total+1)一(kk-1)*wind_ size,rec s, g, L-a, Alpha, nstates);

L_ all=L allf((L_ total+l)一(kk-1)*wind-size,(L__total+l)-kk*wind_ size, rec_ s, g, La, L all, Alpha, Beta, nstates);

elseif kk==Num2-1

Beta=Betaf((址total+l)-kk*wind_ size, Numl*wind_ size+l, rec_ s, g, La, Beta

,nstates);

Alpha=Alphaf((L-total+l)-kk*wind_size,(L-total+l)一(kk-1)*wind_ size,rec_ s, g, L_ a, Alpha, nstates);

L_ all=L_ allf((L_ total+l)一(kk-1)*wind-size,(L_ total+l-kk*wind_size),rec_s, g,址a, L_ all, Alpha, Beta, nstates);

elseif kk==Num2

L_ all=L_ allf((L_ total+l)一(kk-1) *wind_ size, Numl*wind_ size+l, rec_ s, g, La, L_ all, Alpha, Beta, nstates);

end

end

采用单滑动窗口技术的LOG-MAP分量译码器

function L all=singlewindow(win之size, rec_ s, g, L__ a, L_ all, ind_ dec)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

L_ total=length(rec_ s)/2;Num=ceil(L total/wind_ size);[n, K卜size(g);m=K-I;

nstates=2'm;

Infty=leIO;

Alpha(1,1)=0;

Alpha(1,2:nstates)=一Infty*ones(l,nstates-1);Alpha(2:L_ total+l,l:nstates)二一Infty*ones(L_ total,nstates);

1)

2:

if ind dec二二1

Beta(L_ total+1, Beta(L_ total+1,

else

Beta(L_ total+l,end

=0;

nstates)=-Infty*ones(l,nstates-1)

l:nstates)=-m*log(2)*ones(l,nstates);

72

Page 13: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Mallab源代码

Beta(1:L_total, L:nstates)=-infty*ones(L_ total,nstates):

%%%%%%%%%%份浅黔冷%%%%黔1%%%%%%黔%%黔浅%叱%%%黔%%既%叭

for k=C:Num

if k==0 %computer the first sliding window

Alpha=Alphaf (1, wind_si ze+l, rec_ s, g, L_ a, Alpha, nstates);Beta(2*wind_ size+l,l:nstates)二一m*log(2)*ones(l,nstates):

Beta=Betaf (2*wind size+l, wind size+l, rec_ s, g, L_ a, Beta, nstates); elseif (k>O)&(k<Num-2) %

Beta((k+2)*wind_ size+l,l:nstates)二一。*log(2)*ones(l,nstates)

Beta=Betaf ((k+2) *wind_ size+l, (k+l) *wind_size+l, rec_s, g, L_ a, Beta, nstates);

Alpha=Alphaf(k*winds):

_size+l, (k+l)*wind_ size+l, rec_ s, g, L a, Alpha, nstate

Beta=Betaf(k*win之size+l, (k-1) *wind_ size+l, rec_ s, g, L_ a, Beta, nstates):

L_ all=L_ allf (k*wind_ size+l, (k-1) *wind-size+l, rec_ s, g, La, L all, Alpha,Beta, nstates);

elseif k==Num-2

Beta=Betaf((L_ total+l),(k+l) *wind_ size+l, rec_ s, g, L_ a, Beta, nstates);

Alpha=Alphaf (k*wind_ size+l, (k+l) *wind_ size+l, rec_ s, g, L a, Alpha, nstates):

Beta=Betaf(k*win走size+l, (k-1) *wind_ size+1, rec_ s, g, L_ a, Beta, nstates);

L_ al l=L_ al if (k*wind_ size+l, (k-1) *wind_ size+l, rec_ s, g, L_ a, L al l, Alpha,Beta, nstates);

elseif k二二Num-1

Alpha=Alphaf (k*wind_ size+l, L total+l, rec s, g, L a, Alpha, nstates);

Beta=Betaf(k*wind_ size+l, (k-1)*wind_ size+l,rec_ s,g,L_ a,Beta,nstates)

L_ all=L_ allf(k*wind_ size, (k-1)*wind_si,ze+l, rec_s, g, L_ a, L_ all, Alpha, Beta, nstates);

elseif k==Num

73

Page 14: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统iSPSIab源代码

[_all=L-al If((l._ total+l)ta, nstates):

(k-1) *wind_ size+1, roc_ s, g, L_ a, L_ all, Alpha, Be

end

end

SO VA分量译码器function L all二sova(rec_ s, g, La, ind_ dec)%This function implememts Soft Output Viterbi Algorithm in trace back

mode

%Input:% rec s: scaled received bits. rec_s(k)二0.5*L_ c (k)*y (k)% L c=4*a*Es/No, reliability value of the channel

% y: received bits

% g: encoder generator matrix in binary form, g(1,:)for feedback,

g (2,:)for feedforward% La: a priori information about the info. bits. Extrinsic info.

from the previous

% component decoder% ind dec: index of the component decoder.

% =1:component decoder 1;The trellis is terminated to all

zero state

% =2: component decoder 2; The trellis is not perfectly

terminated.

%Output:% L all:log(P(x=1{Y))/(P (x=一1{Y))%

%Copyright: Yufei Wu, Nov. 1998%MPRG lab, Virginia Tech

%for academic use only

%Frame size, info.+tail bits

L -total=length(Les a);[n, K]=size (g);m=K一1;

nstates=2一m;

Infty=leIO;

%SOVA window size. Make decision after’delta, delay. Decide bit k when

received bits

%for bit (k+delta) are processed. Trace back from (k+delta) to k

delta=30;

%Set up the trellis defined by g.[next_ out, next_ state, last_ out, last-state]二trelIis(g);

74

Page 15: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统M.Uab源代码

%Initialize path metrics tofor t=1:L total十1

for state=l:nstates

path_ metric(state,t)二 end

end

一Infty

Infty;

%Trace forward to compute all the path metrics

path_ metric(1, 1}二0;for t=1:L total

y=rec_ s(2*t-1:2*t); for state=1:nstates

sym0=last_ out(state,1:2); syml = last_ out(state,3:4); stateO=last_state(state,l):

statel=last_ state (state, 2); MkO=y*sym0’一L_ a (t)/2+path metric(stateO,t) Mkl=y*sym11+L a (t) /2+path metric(statel,t)

if MkO>Mkl

path_ metric(state,t+l)=MkO; Mdiff (state, t+l)二MkO一Mkl pfev_bit(state, t+1)=0;

else

path_ metric(state,t+l)=Mk1; Mdiff (state, t+l)二Mkl一MkO prev_bit(state,t+l)=1;

end

end

end

%For decoder 1, trace back from all zero state,%for decoder two, trace back from the most likelyif ind dec==1

mlstate(L total+l)=1;else

mlstate(L_ total+l)find( path_ metric(:,L_ total+l) =max(path metric(:,end

state

L total+l)));

%Trace back to get the estimated bits, and the most likely pathfor t=L total:一1:1

.5

Page 16: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统Madab源代码

est (t)=prev_ bit(mlstate(t+l),t+1);mlstate(t)=last state(mlstate(t+l),est (t)+1)

end

%Find the minimum delta that corresponds to a compitition path withdifferent info. bit estimation.

%Give the soft output

for t=1:L total

ilr=Infty;

for i=O:delta

if t+i<L total+1

bit=1-est(t+i);

temp_ state=last_ state(mlstate(t+i+l),bit+1);

for j=i一1:一1:0

bit=prev_ bit(temp_ state,t+j+l); temp state=last_ state(temp_ state, bit+1);

end

if bit-=est(t) llr=min( llr,Mdiff(mlstate(t+i+l),t+i+l));

end

end

end

L_ all (t)=(2*est (t)一1)*l1r;end

Alphaf函数模块function A1pha=Alphaf (bottom, top, rec_ s, g, L_ a, Alpha, nstates)

Tnfty=1e10;

仁n, K) =size (g);m=K-1;

nstates=2 m;

[next out,next_ state,last out,last_ state)=trellis(g):for k=bottom+l:top

for state=l:nstates

Gemma=Gemmaf (rec_ s, g, L a, k, state, 1); if(sum(exp(Gemma(1,:)+Alpha(k-1,:)))<1e-300) Alpha(k,state)=-Infty;

else

Alpha(k, state) log( sum( exp( Gemma(1, :)+Alpha(k-1,:))));

end

end

tempmax = max(Alpha(k,:)): Alpha(k,:)二Al pha (k,:)一tempmax;

end

76

Page 17: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Thrbo码伤真系统Madab源代码

Betaf函数模块

function Beta=Betaf (top, bottom, rec_ s, g,址a, Beta, nstates)

Infty=1e10;[n, K]=size (g);m=K-1;nstates=2一m;

[next out,next_ state,last_ out,last_ state卜trelIis(g);

for k=top-l:-l:bottom for state=l:nstates

Gemma=Gemmaf (rec_ s, g, Les a, k, state, 2): if(sum(exp(Gemma(1, :)+Beta(k+l,:)))<le-300) Beta(k, state)= Infty;

else

Beta(k,state)=log(sum(exp(Gemma(l,:)+Beta(k+l,:))));

end

end

tempmax=max(Beta(k,:)); Beta(k, :)=Beta(k, :)-tempmax;

End

Gemmaf函数模块

function Gemma--Gemmaf (rec_ s, g, Lee a, k, state, index一B)

Infty=lelO;[n, K]=size (g):m=K-1:

nstates=2̂m;[next out,riext_ state,last out,last_ state)=trellis(g);Gemma=-Infty*ones(l,nstates);

if index AB==1 % <一

Gemma(l,last_ state(state,l))e, 2)-emendation(0, L a(k-1));

二一rec s(2*k-3)+rec_ s(2*k-2)*last_ out(stat

Gemma(l,last_ state(state,2))二rec_ s(2*k-3)+rec_ s(2*k-2)*last out(state,4)+L a(k-l)-emendation(O,L_ a

(k-1));elseif index AB==2 %一>

Gemma(l,next_ state(state,2)-emendation(O,L a(k));

1))=-rec s(2*k-1)+rec_ s(2*k)*next out(state,

Gemma(l,next_ state(state,2))=rec_ s(2*k-l)+rec_ s(2*k)*next_ out(state,4)+L a(k)-emendation(O,L一 a(k))

77

Page 18: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbu码仿真系统Matlab源代码

End

L allf函数模块function

I-all=L_ alif (top, bottom, rec_ s, g, L_ a, L al l, Alpha, Beta, nstates)Infty=IeIO;

[next_ out,next_ state,last_ out,last_ state]=trellis(g);Gemma=-lnfty*ones(2,nstatos);for k=top-1:一1:bottom

for state=l:nstates

gammaO=-rec_ s(2*k-1)+rec_ s(2*k)*next_ out(state,2)⋯

-emendation(O,L a(k)):gammal=rec_ s(2*k-1)+rec_ s(2*k)*next out(state,4)⋯

+L_ a(k)-emendation(O,L a(k));

tempo(state)=exp(gammaO+Alpha(k,state)+Beta(k+l,next_ state(state,

templ(state)=exp(gammal+Alpha(k,state)+Beta(k+l,next_ state(state,

1)))

2)))

endLall (k)=log(sum(templ))一log(sum(temp0));End

demultlplex函数模块function subr二demultiplex(r, alpha, puncture):%At receiver end, serial to paralle demultiplex to get the code word ofeach

%encoder

%alpha: interleaver mapping

%puncture=0: use puncturing to increase rate to 1/2;

%puncture=1;unpunctured, rate 1/3;

%Frame size, which includes info. bits and tail bits

L_ total=length(r)/(2+puncture):

%Extract the parity bits for both decoders

if puncture二=1 %unpuncturedfor i=1:L total

x-sys(i)for j=1:2

r(3*(i-1)+1);

subr(j,2*i)=r(3*(i一1)+1+j)end

end

else %unpunctured

78

Page 19: 附录A Turbo码仿真系统Matlab源代码 - pudn.comread.pudn.com/downloads128/sourcecode/others/... · 附录A Turbo码仿真系统MaHab源代码 附录A Turbo码仿真系统Matlab源代码

附录A Turbo码仿真系统iSPSIab源代码

for i二1:L_ total

x_ sys(i)二r(2*(i-1)+1); for j二1:2 subr (j, 2*i )=0;

end

if rem(i, 2) >0

subr(1,2*i)=r (2*i); else

subr(2,2*i)二r (2*i): end

end

end

%Extract the systematic bits for both decoders

for j=1:L total%For decoder one

subr(1,2*(j-1)+1)=x_ sys(j);%For decoder two: interleave the systematic bits

subr(2,2*(j-1)+1)“x_ sys(alpha(j)):end

Newton插值校正函数模块function y=emendation(a,x=abs(a-b);if x<6.0

y=max (a, b) +0.6931-0.else

y=max(a,b);end

b)

4583*x+0.1028*x-2-0.0076*x-3: