这里是以前做过的一个matlab仿真作业,年代有点久远,把代码传上来记录一下。当时一起的组员为Rebecca Aktins,对我们帮助很大的tutor为Ali,当时才开始写matlab不久,很多地方都有不足之处,注释都是英文的,但是也是因为才开始写,注释写得非常详细,耐心的话应该很好看懂。
当时没有写子函数分块的概念,都写在一起了。
这个代码完成的功能是:设定EbNo的值,输入一个格式为bmp的图片,可以输出一个经过这个信道后的bmp文件,以及其EbNo vs BER的图像。
代码如下:
%function transmitimage(lena1)
clear
lena1 = imread('lena1.bmp')
%Lets make 3 different 128x128 matrices to represent each colour
red= lena1(:,:,1)
green = lena1(:,:,2)
blue = lena1(:,:,3)
%Now to convert them into single column vectors:
red= red(:)
green = green(:)
blue = blue(:)
%Now convert these into binary; note that we are using de2bi, this
%conversion means the right bit is the most significant.
redbin = de2bi(red)
greenbin = de2bi(green)
bluebin = de2bi(blue)
%redbin, greenbin and bluebin are 16384x8 matrices. We now need to convert
%this into single column vectors
redbin = redbin(:)
greenbin = greenbin(:)
bluebin = bluebin(:)
%Now to append these three colmn vectors to one another ready to transmit
lena1bin =[redbin greenbin bluebin]
lena1bin = lena1bin(:)
%Lets change this from uint8 to double precision for when we add noise
lena1bin = double(lena1bin)
%Lets transmit these bits (add noise) and then convert back into a 128x128x3
%matrice
%we firstly need to create our own noise
%Assume A = 1 and T = 1
EbN0 = 6; % in dB
%Convert EbN0 into decibels
EbN0=10^(EbN0/10)
A=1
T=1
Eb = 1/2
N0 = Eb/EbN0;
sd = sqrt(N0*T/4)
%Now to make a column vector of noise the same size as out transmitted
%column vector
X = normrnd(0,sd,length(lena1bin),1);
%Lets convert our 1's and 0's into the signal we will send; +-AT/2
S = lena1bin -.5;
%Add this noise to the signal we will recieve
R = S + X;
%Now if R>0, the signal will be closer to 0.5 and hence would have most
%likely originally been a 1. Using this same logic, where R<0 the original
%data was most likely a 0.
%Now that we are translating this transmitted signal back into an image,
%the "t" at the end of the variable names represent the transmitted data
lena1bint = R>=0; %Where R>=0 lena1bint = 1. Where R<0 lenabint = 0.
%Now we have a transmitted vector, now to convert back into a 128x128x3
%uint8 matrice:
lena1bint = reshape(lena1bint,131072,3)
redbint = lena1bint(:,1)
greenbint = lena1bint(:,2)
bluebint = lena1bint(:,3)
redbint = reshape(redbint,16384,8);
greenbint = reshape(greenbint,16384,8);
bluebint = reshape(bluebint,16384,8);
redt =bi2de(redbint)
greent = bi2de(greenbint)
bluet = bi2de(bluebint)
redt = reshape(redt,128,128)
greent = reshape(greent,128,128)
bluet = reshape(bluet,128,128)
lena1_BPSK_6(:,:,1) = redt
lena1_BPSK_6(:,:,2) = greent
lena1_BPSK_6(:,:,3) = bluet
lena1_BPSK_6 = uint8(lena1_BPSK_6)
figure(1)
image(lena1_BPSK_6)
%%Yay we have an image!!
%Now lets find the Bit Error Rate vs. EbN0
EbN0 = [1:0.2:10];
EbN0=10.^(EbN0/10);
for i = 1:length(EbN0)
N0 = Eb/EbN0(i);
sd = sqrt(N0*T/4);
it = 20;
%"it" will create numerous measurements and the same EbN0 value to increase
%accuracy when it is graphed
%making a big matric of noise, length(layer) x it
X = normrnd(0,sd,length(lena1bin),it);
%Lets convert our 1's and 0's into the signal we will send; +-AT/2
S = lena1bin -.5;
%repmat makes a matrice where every column = the S vector repeated
S = repmat(S,1,it);
%Add error to our signal we are sending to create the signal we will
%recieve
R = S + X;
%Where R>0, the original data was most likely a 1 and where R<0 the
%original data was most likely closer to a 0.
dem_bits = R>=0;
%Finding the error; seeing where our initial signal we sent differs from
%the signal we recieved
erros = dem_bits ~= lena1bin;
%Finding the ratio of errors in our transmitted signal
BER(i) = sum(sum(erros))/(length(erros)*it);
%We have created this in a for loop so it repeats many times as different
%values of EbN0 to creat a smooth curve when we plot these values on a
%graph
end
%Now to plot out data with a lof scale along the y axis:
EbN0 = 10*log10(EbN0)
figure(2)
semilogy(EbN0,BER)
附上我们当时用的图片:
相应的,还是用同一张图片,一样的思路,MPSK信道仿真代码如下:
%function transmitimage(lena1)
clear
lena1 = imread('lena1.bmp')
%Lets make 3 different 128x128 matrices to represent each colour
red= lena1(:,:,1)
green = lena1(:,:,2)
blue = lena1(:,:,3)
%Now to convert them into single column vectors:
red= red(:)
green = green(:)
blue = blue(:)
%Now convert these into binary; note that we are using de2bi, this
%conversion means the right bit is the most significant.
redbin = de2bi(red)
greenbin = de2bi(green)
bluebin = de2bi(blue)
%redbin, greenbin and bluebin are 16384x8 matrices. We now need to convert
%this into single column vectors
redbin = redbin(:)
greenbin = greenbin(:)
bluebin = bluebin(:)
%Now to append these three colmn vectors to one another ready to transmit
lena1bin =[redbin greenbin bluebin]
lena1bin = lena1bin(:)
%Lets change this from uint8 to double precision for when we add noise
lena1bin = double(lena1bin)
%Lets transmit these bits (add noise) and then convert back into a 128x128x3
%matrice
%we firstly need to create our own noise
%Assume A = 1 and T = 1
EbN0 = 6; % in dB
%Convert EbN0 into decibels
EbN0=10^(EbN0/10)
A=1
T=1
Eb = 1/2
N0 = Eb/EbN0;
sd = sqrt(N0*T/4)
%Now to make a column vector of noise the same size as out transmitted
%column vector
X = normrnd(0,sd,length(lena1bin),1);
%Lets convert our 1's and 0's into the signal we will send; +-AT/2
S = lena1bin -.5;
%Add this noise to the signal we will recieve
R = S + X;
%Now if R>0, the signal will be closer to 0.5 and hence would have most
%likely originally been a 1. Using this same logic, where R<0 the original
%data was most likely a 0.
%Now that we are translating this transmitted signal back into an image,
%the "t" at the end of the variable names represent the transmitted data
lena1bint = R>=0; %Where R>=0 lena1bint = 1. Where R<0 lenabint = 0.
%Now we have a transmitted vector, now to convert back into a 128x128x3
%uint8 matrice:
lena1bint = reshape(lena1bint,131072,3)
redbint = lena1bint(:,1)
greenbint = lena1bint(:,2)
bluebint = lena1bint(:,3)
redbint = reshape(redbint,16384,8);
greenbint = reshape(greenbint,16384,8);
bluebint = reshape(bluebint,16384,8);
redt =bi2de(redbint)
greent = bi2de(greenbint)
bluet = bi2de(bluebint)
redt = reshape(redt,128,128)
greent = reshape(greent,128,128)
bluet = reshape(bluet,128,128)
lena1_BPSK_6(:,:,1) = redt
lena1_BPSK_6(:,:,2) = greent
lena1_BPSK_6(:,:,3) = bluet
lena1_BPSK_6 = uint8(lena1_BPSK_6)
figure(1)
image(lena1_BPSK_6)
%%Yay we have an image!!
%Now lets find the Bit Error Rate vs. EbN0
EbN0 = [1:0.2:10];
EbN0=10.^(EbN0/10);
for i = 1:length(EbN0)
N0 = Eb/EbN0(i);
sd = sqrt(N0*T/4);
it = 20;
%"it" will create numerous measurements and the same EbN0 value to increase
%accuracy when it is graphed
%making a big matric of noise, length(layer) x it
X = normrnd(0,sd,length(lena1bin),it);
%Lets convert our 1's and 0's into the signal we will send; +-AT/2
S = lena1bin -.5;
%repmat makes a matrice where every column = the S vector repeated
S = repmat(S,1,it);
%Add error to our signal we are sending to create the signal we will
%recieve
R = S + X;
%Where R>0, the original data was most likely a 1 and where R<0 the
%original data was most likely closer to a 0.
dem_bits = R>=0;
%Finding the error; seeing where our initial signal we sent differs from
%the signal we recieved
erros = dem_bits ~= lena1bin;
%Finding the ratio of errors in our transmitted signal
BER(i) = sum(sum(erros))/(length(erros)*it);
%We have created this in a for loop so it repeats many times as different
%values of EbN0 to creat a smooth curve when we plot these values on a
%graph
end
%Now to plot out data with a lof scale along the y axis:
EbN0 = 10*log10(EbN0)
figure(2)
semilogy(EbN0,BER)