anta
Member level 1
Hi all. I am doing ofdm for my diploma thesis and i am facing some problems with rayleigh fading channel....Apparently there's a mistake in my code that i can't find...Any suggestion will be helpful...thank you in advance
Here's my code:
%% DEFINE PARAMETERS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%
M=4;%size of signal constellation
L=1024; % length of data
cp=32;%cyclic prefix
% maximum value of SNR to be simulated (in dB)
max_SNR =25;
%maximum number of iterations
max_ITER =1000;
bits_in_error= zeros(1,max_SNR);
symbols_in_error=zeros(1,max_SNR);
% generate impulse response h of the channel
% independent Rayleigh fading
h=1.*randn(1,1);
%Rayleigh factor
%follows gaussian distribution with mean zero and
%standard deviation 1
%h2=1.*randn(1,K+1);
%energy of the rayleigh factor
%Eh2=sum(abs(h1).^2)/(L);
% h1=heaviside(0);
%h=h2.*h1;
% find frequency response
H=fft(h,L);
% clear figure from previous runs
clf;
%%%%%%%%%%%%%%%%%%%%%%%%%
%% OFDM TRANSMITTER %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%
for iter=1:max_ITER;
%%Modulation
%Modulate using 16-QAM.
%generates random symbols
x=randint(1,L,M);
%qam modulation
y=qammod(x,M);
%normalization according to M-QAM signal constellation
z=y*(1/2)*sqrt(6/(M-1));
%signal energy
Ez=sum(abs(z).^2)/L;
%%IFFT
Z = ifft(z);
%signal energy after the ifft
EZ=sum(abs((Z).^2))/L;
%apply the fourier transform factor
Z1 = sqrt(L)*Z;
%signal energy
EZ1=sum(abs((Z1).^2))/L;
%cyclic prefix
%apply cyclic prefix
Z2 = [Z1(L-cp+1:L) Z1];
%energy of the new signal(with cyclic prefix)
EZ2=sum(abs((Z2.^2))/(L+cp));
%transmitted signal after rayleigh factor applied
Y=Z2*h;
%%%%%%%%%%%%%%%%%%%%%%
%% GAUSSIAN CHANNEL %%
% %%
%%%%%%%%%%%%%%%%%%%%%%
%given SNR in dB
for SNR_dB=1:1:max_SNR
% convert snr
snr=10^(SNR_dB/10);
%find the noise for every SNR
n=sqrt(1/(2*snr))*(randn(1,(L+cp))+j*randn(1,(L+cp)));
%noise energy
En=sum(abs(n.^2))/L;
%find y_noisy for every SNR
ynoisy=Y+n;
%ynoisy=0;
%find the signal energy
Eynoisy=sum(abs(ynoisy.^2))/(L+cp);
%undo cyclic prefix
ynoisy_cut=ynoisy(cp+1L+cp));
%ynoisy_cut(without cyclic prefix) energy
Eynoisy_cut=sum(abs(ynoisy_cut.^2))/L;
%signal normalization
Y1=ynoisy_cut./sqrt(L);
%fast fourier transform
Y2=fft(Y1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% OFDM RECEIVER %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DEMODULATION %%
%% CHECK y_demod SHOULD BE THE SAME WITH THE TRANSMITTED x when there is no noise applied %%%
%% CHECK y_demod WHEN GAUSSIAN NOISE IS APPLIED%%%%%
%%% undo the rayleigh factor
Y3=Y2./H;
%%% M-QAm normalization
y4=ynoisy_cut*2/sqrt(6/(M-1));
%%%demodulated signal when noise is applied
y_demod=qamdemod(y4,M);
%%difference between transmitted and received signal when noise is applied
y_dif=abs(y_demod-x);
%%%% convert to binary%%%%%%
x_binary=dec2bin(x);
y_demod_binary=dec2bin(y_demod);
%%the difference should be zero when the channel is zero
y_binary=sum(abs(dec2bin(x(1:L))-dec2bin(y_demod(1:L))),2);
%find the total bits in error
total_bits_in_error = sum(y_binary);
%find total symbols in error
total_symbols_in_error=sum(y_dif);
%bits in error for every SNR
bits_in_error(SNR_dB) = bits_in_error(SNR_dB) + total_bits_in_error;
%symbols in error for every SNR
symbols_in_error(SNR_dB)=symbols_in_error(SNR_dB)+total_symbols_in_error;
%%binary difference
%%the difference should be zero when the channel is zero
y_binary_total= sum(sum(dec2bin(x(1:L))-dec2bin(y_demod(1:L)),1),2)/L;
%%shows the binary difference between the symbols due to the noise%%
y_dif_binary=y_demod_binary-x_binary;
end
%end for iter=1:max_ITER
end
%%find bit error rate
BER = bits_in_error/(log2(M)*L)/max_ITER;
%%find symbol error rate
SER=symbols_in_error/(M*L)/max_ITER;
%%BER plot
semilogy(1:max_SNR,BER);
Here's my code:
%% DEFINE PARAMETERS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%
M=4;%size of signal constellation
L=1024; % length of data
cp=32;%cyclic prefix
% maximum value of SNR to be simulated (in dB)
max_SNR =25;
%maximum number of iterations
max_ITER =1000;
bits_in_error= zeros(1,max_SNR);
symbols_in_error=zeros(1,max_SNR);
% generate impulse response h of the channel
% independent Rayleigh fading
h=1.*randn(1,1);
%Rayleigh factor
%follows gaussian distribution with mean zero and
%standard deviation 1
%h2=1.*randn(1,K+1);
%energy of the rayleigh factor
%Eh2=sum(abs(h1).^2)/(L);
% h1=heaviside(0);
%h=h2.*h1;
% find frequency response
H=fft(h,L);
% clear figure from previous runs
clf;
%%%%%%%%%%%%%%%%%%%%%%%%%
%% OFDM TRANSMITTER %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%
for iter=1:max_ITER;
%%Modulation
%Modulate using 16-QAM.
%generates random symbols
x=randint(1,L,M);
%qam modulation
y=qammod(x,M);
%normalization according to M-QAM signal constellation
z=y*(1/2)*sqrt(6/(M-1));
%signal energy
Ez=sum(abs(z).^2)/L;
%%IFFT
Z = ifft(z);
%signal energy after the ifft
EZ=sum(abs((Z).^2))/L;
%apply the fourier transform factor
Z1 = sqrt(L)*Z;
%signal energy
EZ1=sum(abs((Z1).^2))/L;
%cyclic prefix
%apply cyclic prefix
Z2 = [Z1(L-cp+1:L) Z1];
%energy of the new signal(with cyclic prefix)
EZ2=sum(abs((Z2.^2))/(L+cp));
%transmitted signal after rayleigh factor applied
Y=Z2*h;
%%%%%%%%%%%%%%%%%%%%%%
%% GAUSSIAN CHANNEL %%
% %%
%%%%%%%%%%%%%%%%%%%%%%
%given SNR in dB
for SNR_dB=1:1:max_SNR
% convert snr
snr=10^(SNR_dB/10);
%find the noise for every SNR
n=sqrt(1/(2*snr))*(randn(1,(L+cp))+j*randn(1,(L+cp)));
%noise energy
En=sum(abs(n.^2))/L;
%find y_noisy for every SNR
ynoisy=Y+n;
%ynoisy=0;
%find the signal energy
Eynoisy=sum(abs(ynoisy.^2))/(L+cp);
%undo cyclic prefix
ynoisy_cut=ynoisy(cp+1L+cp));
%ynoisy_cut(without cyclic prefix) energy
Eynoisy_cut=sum(abs(ynoisy_cut.^2))/L;
%signal normalization
Y1=ynoisy_cut./sqrt(L);
%fast fourier transform
Y2=fft(Y1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% OFDM RECEIVER %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DEMODULATION %%
%% CHECK y_demod SHOULD BE THE SAME WITH THE TRANSMITTED x when there is no noise applied %%%
%% CHECK y_demod WHEN GAUSSIAN NOISE IS APPLIED%%%%%
%%% undo the rayleigh factor
Y3=Y2./H;
%%% M-QAm normalization
y4=ynoisy_cut*2/sqrt(6/(M-1));
%%%demodulated signal when noise is applied
y_demod=qamdemod(y4,M);
%%difference between transmitted and received signal when noise is applied
y_dif=abs(y_demod-x);
%%%% convert to binary%%%%%%
x_binary=dec2bin(x);
y_demod_binary=dec2bin(y_demod);
%%the difference should be zero when the channel is zero
y_binary=sum(abs(dec2bin(x(1:L))-dec2bin(y_demod(1:L))),2);
%find the total bits in error
total_bits_in_error = sum(y_binary);
%find total symbols in error
total_symbols_in_error=sum(y_dif);
%bits in error for every SNR
bits_in_error(SNR_dB) = bits_in_error(SNR_dB) + total_bits_in_error;
%symbols in error for every SNR
symbols_in_error(SNR_dB)=symbols_in_error(SNR_dB)+total_symbols_in_error;
%%binary difference
%%the difference should be zero when the channel is zero
y_binary_total= sum(sum(dec2bin(x(1:L))-dec2bin(y_demod(1:L)),1),2)/L;
%%shows the binary difference between the symbols due to the noise%%
y_dif_binary=y_demod_binary-x_binary;
end
%end for iter=1:max_ITER
end
%%find bit error rate
BER = bits_in_error/(log2(M)*L)/max_ITER;
%%find symbol error rate
SER=symbols_in_error/(M*L)/max_ITER;
%%BER plot
semilogy(1:max_SNR,BER);