PAM汇报——
%需要设置的参数: % path:图像路径 % Rs:发送信号的波特率 % EsN0:平均符号能量与高斯白噪声功率的比值(单位:dB) clear;close all; path = 'cameraman.tif'; Rs = 1000; EsN0 = 8; %image to binary sequence img = imread(path); [img_seq,height,width] = img2seq(img); %Hamming hamming_seq = hamming(img_seq); %mod & awgn & demod [send_seq,]=pam4_send(img_seq,Rs,EsN0); rev_seq=pam4_rev(send_seq,Rs); %Hamming [send_seq_ham,] = pam4_send(hamming_seq,Rs,EsN0); rev_seq_ham = pam4_rev(send_seq_ham,Rs); %DeHamming rev_seq_deham = dehamming(rev_seq_ham); %performance evaluation num_errors = sum(img_seq ~= rev_seq); BER = num_errors / length(img_seq); %Hamming num_errors_ham = sum(img_seq ~= rev_seq_deham(1:height*width*8)); BER_ham = num_errors_ham / length(img_seq); %binary sequence to image recover_image = seq2img(rev_seq,height,width); figure(1) % subplot(1,2,1); imshow(recover_image); title(sprintf('Es/N0=%ddB Recovered Image BER=%.2f%%',EsN0,BER*100)); %Hamming recover_image_ham = seq2img(rev_seq_deham,height,width); figure(2); % subplot(1,2,2); imshow(recover_image_ham); title(sprintf('Es/N0=%ddB Recovered Image -Hamming BER=%.2f%%',EsN0,BER_ham*100))
%需要设置的参数: % path:图像路径 % Rs:发送信号的波特率 % EsN0:平均符号能量与高斯白噪声功率的比值(单位:dB) close all;clear; path = 'cameraman.tif'; Rs = 1000; % image to binary sequence img = imread(path); [img_seq,height,width] = img2seq(img); %Hamming hamming_seq = hamming(img_seq); %SpecialChannel %img_seq rev_seq = img_seq; sector_num = ceil(length(rev_seq)/16); rev_seq = [rev_seq,zeros(sector_num*16-length(rev_seq))]; for index = 1:sector_num random_index = randi([1,16]); rev_seq((index-1)*16+random_index)=~rev_seq((index-1)*16+random_index); end num_errors_sp1 = sum(rev_seq ~= img_seq); BER_SpecialChannel1 = num_errors_sp1 / length(img_seq); %Hamming hamming_seq_sp = hamming_seq; jie = length(hamming_seq)/16; for index = 1:jie random_index = randi([1,16]); hamming_seq_sp((index-1)*16+random_index)=~hamming_seq_sp((index-1)*16+random_index); end num_errors_sp2 = sum(hamming_seq ~= hamming_seq_sp); BER_SpecialChannel2 = num_errors_sp2 / length(hamming_seq); %DeHamming rev_seq_deham = dehamming(hamming_seq); rev_seq_deham_sp = dehamming(hamming_seq_sp); %performance evaluation num_errors = sum(img_seq ~= rev_seq(1:height*width*8)); BER = num_errors / length(img_seq); %Hamming No noise num_errors_ham = sum(img_seq ~= rev_seq_deham(1:height*width*8)); BER_ham = num_errors_ham / length(img_seq); %Hamming Special Channel num_errors_ham_sp = sum(img_seq ~= rev_seq_deham_sp(1:height*width*8)); BER_ham_sp = num_errors_ham_sp / length(img_seq); %binary sequence to image recover_image = seq2img(rev_seq,height,width); figure(1); imshow(recover_image); title(sprintf('Recovered Image BER=%.4f',BER)) %Hamming No noise recover_image_ham = seq2img(rev_seq_deham,height,width); figure(2); imshow(recover_image_ham); title('Recovered Image -Hamming') %Hamming Special Channel recover_image_ham_sp = seq2img(rev_seq_deham_sp,height,width); figure(3); imshow(recover_image_ham_sp); title(sprintf('Recovered Image -Hamming BER=%.4f',BER_ham_sp))
%函数说明 % 功能:错误位置判断 %参数说明 % 输入参数 % seq:16位二进制序列 % 输出参数 % answer:发生错误的位置(若为0则说明无错误或0000位出现错误,若为1说明第2比特出现错误) function [answer] = check(seq) answer = 0; if mod(sum(seq(2:2:16)),2)==1 answer = answer + 1; end if mod(sum(seq(3:4:15))+sum(seq(4:4:16)),2)==1 answer = answer + 2; end if mod(sum(seq(5:8))+sum(seq(13:16)),2)==1 answer = answer + 4; end if mod(sum(seq(9:16)),2)==1 answer = answer + 8; end
%函数说明 % 功能:(15,11)汉明编码收端 %参数说明 % 输入参数 % rev_seq:接收到的二进制序列 % 输出参数 % cor_seq:纠错后的二进制序列 function [cor_se] = dehamming(rev_seq) sector_num = length(rev_seq)/16; cor_se = zeros(1,sector_num*11); for i = 1:sector_num index_old = (i-1)*16; index_new = (i-1)*11; wrong_bit = check(rev_seq(index_old+1:index_old+16)); if wrong_bit rev_seq(index_old+wrong_bit+1) = ~rev_seq(index_old+wrong_bit+1); end cor_se(index_new+1) = rev_seq(index_old+4); cor_se(index_new+2:index_new+4) = rev_seq(index_old+6:index_old+8); cor_se(index_new+5:index_new+11) = rev_seq(index_old+10:index_old+16); end
%函数说明 % 功能:(15,11)汉明编码发端 %参数说明 % 输入参数 % original_seq:原始二进制序列 % 输出参数 % hamming_seq:纠错位后的二进制序列 function [hamming_seq] = hamming(original_seq) len = length(original_seq); sector_num = ceil(len/11); len_pad = sector_num*11; pad_seq = [original_seq,zeros(1,len_pad-len)]; hamming_seq = zeros(1,sector_num*16); for i = 1:sector_num index_old = (i-1)*11; index_new = (i-1)*16; hamming_seq(index_new+4) = pad_seq(index_old+1); hamming_seq(index_new+6:index_new+8) = pad_seq(index_old+2:index_old+4); hamming_seq(index_new+10:index_new+16) = pad_seq(index_old+5:index_old+11); hamming_seq(index_new+2) = mod(pad_seq(index_old+1)+pad_seq(index_old+2)+pad_seq(index_old+4)+pad_seq(index_old+5)+pad_seq(index_old+7)++pad_seq(index_old+9)++pad_seq(index_old+11),2); hamming_seq(index_new+3) = mod(pad_seq(index_old+1)+pad_seq(index_old+3)+pad_seq(index_old+4)+pad_seq(index_old+6)+pad_seq(index_old+7)+pad_seq(index_old+10)+pad_seq(index_old+11),2); hamming_seq(index_new+5) = mod(sum(pad_seq(index_old+2:index_old+4))+sum(pad_seq(index_old+8:index_old+11)),2); hamming_seq(index_new+9) = mod(sum(pad_seq(index_old+5:index_old+11)),2); hamming_seq(index_new+1) = mod(sum(hamming_seq(index_new+1:index_new+16)),2); end
%函数说明 % 功能:将图像转换为二进制序列 %参数说明 % 输入参数 % img:图像 % 输出参数 % image_seq:二进制序列 % height:图像的高度 % width:图像的宽度 function [image_seq,height,width] = img2seq(img) img_bin = dec2bin(img(:))'; image_seq = double(char(img_bin(:)')) - '0'; [height,width] = size(img);
%函数说明 % 功能:将二进制序列转化为图像 %参数说明 % 输入参数 % rev_seq:二进制序列 % height:图像的高度 % width:图像的宽度 % 输出参数 % rev_image:图像 function [recover_image] = seq2img(rev_seq,height,width) new_length = height*width; new_array = zeros(1, new_length); for i = 1:new_length start_index = (i - 1) * 8 + 1; end_index = i * 8; new_array(i) = sum(rev_seq(start_index:end_index) .* (2 .^ [7 6 5 4 3 2 1 0])); end recover_image = uint8(reshape(new_array,height,width));
Work Hard
But do not forget to enjoy life😀
本文来自博客园,作者:YuhangLiuCE,转载请注明原文链接:https://www.cnblogs.com/YuhangLiuCE/p/17867517.html