《matlab卷积码程序(7页).doc》由会员分享,可在线阅读,更多相关《matlab卷积码程序(7页).doc(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-matlab卷积码程序-第 7 页1、卷积码编码function output=cnv_encd(input)%output=cnv_encd(g,k0,input) 卷积码编码函数%g 生成矩阵%k0 输入码长%input 输入信源序列%output 输出卷积编码序列g=1 1 1;1 0 1;编码矩阵k0=1;input=1 1 0 1;if rem(length(input),k0)0 input=input,zeros(size(1:k0-rem(length(input),k0);endn=length(input)/k0;if rem(size(g,2),k0)0 error(E
2、rror,g is not of the right size.)endli=size(g,2)/k0;n0=size(g,1);u=zeros(size(1:(li-1)*k0),input,zeros(size(1:(li-1)*k0);u1=u(li*k0:-1:1);for i=1:n+li-2 u1=u1,u(i+li)*k0:-1:i*k0+1);enduu=reshape(u1,li*k0,n+li-1);output=reshape(rem(g*uu,2),1,n0*(n+li-1);2、Viterbi译码程序1)function y=bin2deci(x)l=length(x
3、);y=(l-1:-1:0);y=2.y;y=x*y;2)function y=deci2bin(x,l)y=zeros(1,l);i=1;while x=0 & i=l y(i)=rem(x,2); x=(x-y(i)/2; i=i+1;endy=y(l:-1:1);3)function distance=metric(x,y)if x=y distance=0;else distance=1;end4)function next_state,memory_contents=nxt_stat(current_state,input,L,k)binary_state=deci2bin(curr
4、ent_state,k*(L-1);binary_input=deci2bin(input,k);next_state_binary=binary_input,binary_state(1:(L-2)*k);next_state=bin2deci(next_state_binary);memory_contents=binary_input,binary_state;5)function decoder_output,survivor_state,cumulated_metric=viterbi(channel,snr_db)G=1 1 1;1 0 1; % G 卷积编码矩阵,如(2,1,3)
5、卷积码生成矩阵1 1 1;1 0 1,可以根据自己的需要输入编码矩阵k=1; % k 信息源输入端口数 k=1channel=1 1 0 1 0 1 0 0 1 0 1 1 ; %信源编码snr_db=6;%信噪比,可以通过调节信噪比大小观察viterbi译码的性能%bpsk调制channel_output=bpsk(channel,snr_db);%调用bpsk函数,得到信道编码n=size(G,1); % n 编码输出端口数量,(2,1,3)中n=2if rem(size(G,2),k)=0 %当G列数不是k的整数倍时 error(Size of G and k do not agree)
6、 %发出出错信息endif rem(size(channel_output,2),n)=0 %当输出量元素个数不是输出端口的整数倍时 error(channel output not of the right size)endN=size(G,2)/k; %得出移位数,即寄存器的个数M=2k;number_of_states=2(k*(N-1); %状态数for j=0:number_of_states-1 %j表示当前寄存器组的状态因为状态是从零 %开始的,所以循环从0到number_of_states-1 for m=0:M-1 %m为从k个输入端的信号组成的状态,总的状 %态数为2k,所
7、以循环从0到2k-1 %nxt_stat完成从当前的状态和输入的矢量得出下寄存器组的一个状态 next_state,memory_contents=nxt_stat(j,m,N,k);%调用nxt_stat函数 input(j+1,next_state+1)=m; branch_output=rem(memory_contents*G,2); nextstate(j+1,m+1)=next_state; output(j+1,m+1)=bin2deci(branch_output); endend%state_metric数组用于记录译码过程在每状态时的汉明距离%state_metric大小为
8、number_of_states 2,(:,1)当前%状态位置的汉明距离,为确定值,而(:,2)为当前状态加输入%得到的下一个状态汉明距离,为临时值state_metric=zeros(number_of_states,2);depth_of_trellis=length(channel_output)/n;channel_output_matrix=reshape(channel_output,n,depth_of_trellis);survivor_state=zeros(number_of_states,depth_of_trellis+1);for i=1:depth_of_trell
9、is-N+1 flag=zeros(1,number_of_states); if(istate_metric(j+1,1)+branch_metric) | flag(nextstate(j+1,m+1)+1)=0 ) state_metric(nextstate(j+1,m+1)+1,2)=state_metric(j+1,1)+branch_metric; survivor_state(nextstate(j+1,m+1)+1,i+1)=j; flag(nextstate(j+1,m+1)+1)=1; end end end state_metric=state_metric(:,2:-
10、1:1);endfor i=depth_of_trellis-N+2:depth_of_trellis flag=zeros(1,number_of_states); % 状态数从number_of_statesnumber_of_states/2.21 %程序说明同上,只不过输入矢量只为0 last_stop=number_of_states/(2(k*(i-depth_of_trellis+N-2); for j=0:last_stop-1 branch_metric=0; binary_output=deci2bin(output(j+1,1),n); for ll=1:n branch
11、_metric=branch_metric+metric(channel_output_matrix(ll,i),binary_output(ll); end if( (state_metric(nextstate(j+1,1)+1,2)state_metric(j+1,1)+branch_metric) | flag(nextstate(j+1,1)+1)=0 ) state_metric(nextstate(j+1,1)+1,2)=state_metric(j+1,1)+branch_metric; survivor_state(nextstate(j+1,1)+1,i+1)=j; fla
12、g(nextstate(j+1,1)+1)=1; end end state_metric=state_metric(:,2:-1:1);end%从最佳路径中产生解码%译码过程可从数组survivor_state的最后一个位置向前逐级译码state_sequence=zeros(1,depth_of_trellis+1);state_sequence(1,depth_of_trellis)=survivor_state(1,depth_of_trellis+1);for i=1:depth_of_trellis state_sequence(1,depth_of_trellis-i+1)=su
13、rvivor_state(state_sequence(1,depth_of_trellis+2-i)+1),depth_of_trellis-i+2);enddecoder_output_matrix=zeros(k,depth_of_trellis-N+1);for i=1:depth_of_trellis-N+1 %根据数组input的定义来得出从当前状态到下一个状态的输入信号矢量 dec_output_deci=input(state_sequence(1,i)+1,state_sequence(1,i+1)+1); dec_output_bin=deci2bin(dec_output
14、_deci,k); %将一次译码存入译码输出矩阵decoder_output_matrix相应的位置 decoder_output_matrix(:,i)=dec_output_bin(k:-1:1);enddecoder_output=reshape(decoder_output_matrix,1,k*(depth_of_trellis-N+1);cumulated_metric=state_metric(1,1);3、卷积码译码误码性能分析clear all;clc;cycl = 50;snr_db = 0:1:10;% 输入信息msg = randint(1,1024);ber0 = z
15、eros(cycl,length(snr_db);ber1 = zeros(cycl,length(snr_db);ber2 = zeros(cycl,length(snr_db);% Trellisestrel = poly2trellis(3,5 7); %Define trellis for rate 1/2 code.for n = 1:cyclfor x = 1:length(snr_db)% Code wordscode = convenc(msg,trel); % Encode.% Interleaverstate = 20;inter = randintrlv(code,sta
16、te);% BPSK 调制s0 = sign(msg - 0.5); s1 = sign(inter-0.5); s2 = sign(code-0.5);% AWGN Channeladd_noise0=awgn(s0,snr_db(x),measured);add_noise1=awgn(s1,snr_db(x),measured);add_noise2=awgn(s2,snr_db(x),measured);% Deinterleaver with noise for soft decodingdeinter_noise = randdeintrlv(add_noise1,state);%
17、 解调r_0 = 0.5*sign(add_noise0) + 0.5;r_1 = 0.5*sign(add_noise1) + 0.5; r_2 = 0.5*sign(add_noise2) + 0.5; % Deinterleaverdeinter_1 = randdeintrlv(r_1,state);% Traceback lengthtblen = 5;% vitdec 硬判决decoded1 = vitdec(deinter_1,trel,tblen,cont,hard);% vitdec 软判决y,qcode = quantiz(deinter_noise,-.75 -.5 -.
18、25 0 .25 .5 .75,7:-1:0);decoded2 = vitdec(qcode,trel,tblen,cont,soft,3);% 比较误码率num0,rat0 = biterr(r_0,msg);num1,rat1 = biterr(double(decoded1(tblen+1:end),msg(1:end-tblen);num2,rat2 = biterr(double(decoded2(tblen+1:end),msg(1:end-tblen);ber0(n,x) = rat0;ber1(n,x) = rat1;ber2(n,x) = rat2;endend ber0 = mean(ber0);ber1 = mean(ber1);ber2 = mean(ber2);semilogy(snr_db,ber0,b-o,snr_db,ber1,r-s,snr_db,ber2,k-p);xlabel(SNR (dB);ylabel(BER);legend(Uncoded,Hard Coded,Soft Coded);title(Performance of convolutional code with rate 1/2);