卷積碼編譯碼程序_第1頁(yè)
卷積碼編譯碼程序_第2頁(yè)
卷積碼編譯碼程序_第3頁(yè)
卷積碼編譯碼程序_第4頁(yè)
卷積碼編譯碼程序_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、這是我搜集整理的一個(gè)關(guān)于卷積編碼和Viterbi譯碼的Matlab程序,現(xiàn)在把它們放在這里,希望對(duì)需要的人有些幫助。卷積編碼程序:function output, len_tal = cnv_encd(secrettext, encodetext)g = 0 0 1 0 0 1 0 0; 0 0 0 0 0 0 0 1; 1 0 0 0 0 0 0 1; 0 1 0 0 1 1 0 1;k0 = 1;% 讀入文本文件并計(jì)算文件長(zhǎng)度f(wàn)rr = fopen(secrettext, r);msg, len = fread(frr, ubit1);msg = msg;% check to see if

2、 extra zero padding is necessaryif rem(length(msg), k0) 0 msg = msg, zeros(size(1:k0-rem(length(msg),k0);endn = length(msg)/k0; % 把輸入比特按k0分組,n為所得的組數(shù)。% check the size of matrix gif rem(size(g, 2), k0) 0 error(Error, g is not of the right size.);end% determine L and n0L = size(g, 2)/k0;n0 = size(g, 1)

3、;% add extra zeros,以保證編碼器是從全0開(kāi)始,并回到全0狀態(tài)。u = zeros(size(1:(L-1)*k0), msg, zeros(size(1:(L-1)*k0);% generate uu, a matrix whose columns are the contents of conv. encoder at% various clock cycles.u1 = u(L*k0: -1 :1);for i = 1:n+L-2 u1 = u1, u(i+L)*k0:-1:i*k0+1);enduu = reshape(u1, L*k0, n+L-1);% determ

4、ine the outputoutput = reshape(rem(g*uu, 2), 1, n0*(L+n-1);len_tal = n0*(L + n - 1);% write the output to the encodetextresult = fopen(encodetext, w);for i = 1:n0*(L+n -1) fwrite(result, output(i), bit1);endfclose(result);Viterbi譯碼程序:function decoder_output, survivor_state, cumulated_metric = viterb

5、i(channel_output, decodetext)ticG= 0 0 1 0 0 1 0 0; 0 0 0 0 0 0 0 1; 1 0 0 0 0 0 0 1; 0 1 0 0 1 1 0 1;k = 1;frr = fopen(channel_output, r);msg, len = fread(frr, ubit1);channel_output = msg;n = size(G,1);% check the sizesif rem(size(G, 2), k) = 0 error(channel_output not of the right size);endL = siz

6、e(G, 2)/k;number_of_states = 2(L-1)*k);% generate state transition matrix, output matrix, and input matrixfor j = 0:number_of_states - 1 for i = 0:2k-1 next_state, memory_contents = nxt_stat(j,i, L, k); input(j+1, next_state + 1) = i; branch_output = rem(memory_contents*G, 2); nextstate(j+1, i+1) =

7、next_state; output(j+1, i+1) = bin2deci(branch_output); endend% add the extra zero, ensure the length of channel_output is integral% times to n.if rem(len, n)0 channel_output = channel_output, zeros(size(n-rem(len, n):-1:1);endstate_metric = zeros(number_of_states, 2);depth_of_trellis = length(chann

8、el_output)/n;channel_output_matrix = reshape(channel_output, n, depth_of_trellis);survivor_state = zeros(number_of_states, depth_of_trellis + 1);% start decoding of non-tail channel outputsfor i = 1:depth_of_trellis-L+1 flag = zeros(1, number_of_states); if i state_metric(j+1,1). + branch_metric) |

9、flag(nextstate(j+1, l+1) + 1) = 0) state_metric(nextstate(j+1,l+1)+1, 2) = state_metric(j+1, 1) + branch_metric; suvivor_state(nextstate(j+1,l+1) + 1, i+1) = j; flag(nextstate(j+1, l+1) + 1) = 1; end end end state_metric = state_metric(:, 2:-1:1);end% start decoding of the tail channel-outputsfor i

10、= depth_of_trellis - L + 2:depth_of_trellis flag = zeros(1, number_of_states); last_stop = number_of_states/(2(i - depth_of_trellis+L-2)*k); for j = 0:last_stop - 1 branch_metric = 0; binary_output = deci2bin(output(j + 1, 1), n); for r = 1:n branch_metric = branch_metric + metric(channel_output_mat

11、rix(r, i), binary_output(r); end if(state_metric(nextstate(j+1, l+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; suvivor_state(nextstate(j+1,1) + 1, i+1) = j; flag(nextstate(j+1, 1) + 1) =

12、 1; end end state_metric = state_metric(:, 2:-1:1);end% generate the decode output from the optimal pathstate_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) = suv

13、ivor_state(state_sequence(1, depth_of_trellis+2-i). +1), depth_of_trellis - i+2);enddecoder_output_matrix = zeros(k, depth_of_trellis -L+1);for i = 1:depth_of_trellis - L + 1 dec_output_deci = input(state_sequence(1, i)+1, state_sequence(1, i+1)+1); dec_output_bin = deci2bin(dec_output_deci, k); dec

14、oder_output_matrix(:,i) = dec_output_bin(k:-1:1);enddecoder_output = reshape(decoder_output_matrix, 1, k*(depth_of_trellis-L+1);cumulated_metric = state_metric(1, 1);% write the output to the encodetextresult = fopen(decodetext, w);for i = 1:k*(depth_of_trellis-L+1) fwrite(result, decoder_output(i),

15、 bit1);endfclose(result);toc%*%function distance = metric(x,y)if x = y distance = 0;else distance = 1;end%*%function next_state, memory_contents = nxt_stat(current_state, input, L, k)binary_state = deci2bin(current_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_bina

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論