Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Matlab code for Rayleigh Fading Channel

Status
Not open for further replies.

reddy2

Newbie level 5
Joined
Mar 29, 2006
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,342
rayleigh fading channel matlab code

Can some one help me with matlab code for rayleigh fading channel .

Thank you
 

matlab code for rayleigh distribution

If you have communication blockset in simulink, you can try "Multipath Rayleigh Fading Channels" in it.
 
average fade duration matlab

Here is a matlab code:

You can mention the number of taps and corresponding power:

Code:
    tap_delay=[0 10 20 30 40 50];
    tap_power_db=[-3.9234   -5.2288   -6.9897  -13.0103  -15.2288  -16.9897];
    L=length(tap_delay);
    tap_power_lin=10.^(tap_power_db/10);
    temp=randn(1,L)+j*randn(1,L);
     for k=1:L
           
  h(k)=sqrt(tap_power_lin(k)/2).*real(temp(k))+j*sqrt(tap_power_lin(k)/2).*imag(temp(k));       
     end
 
Last edited by a moderator:
rayleigh distribution matlab

Code:
% Calculating average fade duration and plotting envelope of Rayleigh distribution for specified value of fm and ro %
%************************** ****************************************************************************************%
close all
clear all
clc

N=256;    %Number of frequency samples
M=8192;   %Number of time samples 

% Required parameters for INPUT: fm and row (r0)

fm=input('ENTER THE VALUE OF fm [20 Hz, 200Hz]:')
r0=input('ENTER THE VALUE OF r0 [1,0.1,0.01]:')

y=1;
Afd_p=0;                   % Average fade duration; practical value
Nr_p=0;                    % Number of Zero-crossing level per second
Rrms_p=0;                  % Practically calculated R-rms value

while(y<=1)
    
    delta_f=2*fm/N;        % Frequency resolution
    delta_t=N/(M-1)/2/fm;  % Time resolution

%*************************   NOTE   **********************************%
% "If N=M-1, then the time resolution delta_t=1/2*fm, which may not be
% small so take M >> N. When M > N, we need to pad with zero values
% before taking IFFT."


X1(1)=randn(1);            % Generating Gaussain Random with N(0,1)
X1=X1(1);
Y1(1)=randn(1);
Y1=Y1(1);  

for m=2:(N/2)+1
    X1(m)=randn(1);
    X2(m)=randn(1);
    Y1(m)=randn(1);
    Y2(m)=randn(1);
    X(m)=X1(m)+i*X2(m);
    Y(m)=Y1(m)+i*Y2(m);
end

for m=1:(N/2)+1
    X(M-m+1)=conj(X(m+1));
    Y(M-m+1)=conj(Y(m+1));
end

% Sample Se(f) Spectrum

for jj=1:N/2               
SeF(jj)=1.5/(pi*fm*(sqrt(1-((jj-1)*delta_f/fm)^2))); 
end

% Calculating Edge Value by extending the slope prior to passband edge to edge

SeF((N/2)+1)=SeF(N/2)+SeF(N/2)-SeF((N/2)-1); 

for m=1:N/2
SeF(M-m+1)=SeF(m+1);
end

for m=1:M
    X_shaped(m)=X(m)*sqrt(SeF(m));
    Y_shaped(m)=Y(m)*sqrt(SeF(m));
end

X_component=real(ifft(X_shaped));   % Only considering the real part
Y_component=real(ifft(Y_shaped));

%************* Find R-rms value and envelope of Rayleigh Distribution ***********%

R=sqrt(X_component.^2+Y_component.^2);
r=20*log10(R);

rms=sqrt(mean(R.^2));
Rrms=20*log10(rms);
level=20*log10(r0*rms);
R=r-Rrms;

figure
plot(1:8192,R,'r')
xlabel ('Time Samples, M=8192');
ylabel ('Instantaneous Power dB');
title ('Figure(1):Rayleigh fading signal for Specified fm & r0 ');

%  Calculating (Practically) Number of Zero Level Crossing and Average Fade Duration  %

h=1;
c=0;
C1=0;
NUM=0;
  while h<=M
  if r(h)<=level
      i=h;
      while i<=M
          if r(i)>=level
              NUM=NUM+1;
              break;
          end
          i=i+1;
      end
      c=i-h;
      C1=C1+c;
      h=i-1;
  end
  h=h+1;
end    

Afd_p=Afd_p+(C1/NUM)*delta_t;
Nr_p=Nr_p+NUM*delta_f;
Rrms_p=Rrms_p+Rrms;
y=y+1;
end

%************ Theoretical calculation of  Number of Zero Level Crossing (Nr) and Average Fade Duration ************* %

Nr_theoretical=sqrt(2*pi)*fm*r0*exp(-r0^2); 

z1=exp(r0^2)-1;
z2=r0*fm*sqrt(2*pi);  
Average_fade_duration_theoretical =z1/z2;

rowdb=10*log10(r0) ;
Rrms_theoretical=Rrms+rowdb;

%*********************** Displayiing Calculated values  ************************ %

Nr_practical=(Nr_p);
Nr_practical
Nr_theoretical
Average_fade_duration_Practical=(Afd_p);
Average_fade_duration_Practical
Average_fade_duration_theoretical =z1/z2
Rrms_Practical=Rrms_p
Rrms_theoretical
 
Last edited by a moderator:
Re: rayleigh distribution matlab

ahmedseu said:
% Calculating average fade duration and plotting envelope of Rayleigh distribution for specified value of fm and ro %
%************************** ****************************************************************************************%
close all
clear all
clc

N=256; %Number of frequency samples
M=8192; %Number of time samples

% Required parameters for INPUT: fm and row (r0)

fm=input('ENTER THE VALUE OF fm [20 Hz, 200Hz]:')
r0=input('ENTER THE VALUE OF r0 [1,0.1,0.01]:')

y=1;
Afd_p=0; % Average fade duration; practical value
Nr_p=0; % Number of Zero-crossing level per second
Rrms_p=0; % Practically calculated R-rms value

while(y<=1)

delta_f=2*fm/N; % Frequency resolution
delta_t=N/(M-1)/2/fm; % Time resolution

%************************* NOTE **********************************%
% "If N=M-1, then the time resolution delta_t=1/2*fm, which may not be
% small so take M >> N. When M > N, we need to pad with zero values
% before taking IFFT."


X1(1)=randn(1); % Generating Gaussain Random with N(0,1)
X1=X1(1);
Y1(1)=randn(1);
Y1=Y1(1);

for m=2:(N/2)+1
X1(m)=randn(1);
X2(m)=randn(1);
Y1(m)=randn(1);
Y2(m)=randn(1);
X(m)=X1(m)+i*X2(m);
Y(m)=Y1(m)+i*Y2(m);
end

for m=1:(N/2)+1
X(M-m+1)=conj(X(m+1));
Y(M-m+1)=conj(Y(m+1));
end

% Sample Se(f) Spectrum

for jj=1:N/2
SeF(jj)=1.5/(pi*fm*(sqrt(1-((jj-1)*delta_f/fm)^2)));
end

% Calculating Edge Value by extending the slope prior to passband edge to edge

SeF((N/2)+1)=SeF(N/2)+SeF(N/2)-SeF((N/2)-1);

for m=1:N/2
SeF(M-m+1)=SeF(m+1);
end

for m=1:M
X_shaped(m)=X(m)*sqrt(SeF(m));
Y_shaped(m)=Y(m)*sqrt(SeF(m));
end

X_component=real(ifft(X_shaped)); % Only considering the real part
Y_component=real(ifft(Y_shaped));

%************* Find R-rms value and envelope of Rayleigh Distribution ***********%

R=sqrt(X_component.^2+Y_component.^2);
r=20*log10(R);

rms=sqrt(mean(R.^2));
Rrms=20*log10(rms);
level=20*log10(r0*rms);
R=r-Rrms;

figure
plot(1:8192,R,'r')
xlabel ('Time Samples, M=8192');
ylabel ('Instantaneous Power dB');
title ('Figure(1):Rayleigh fading signal for Specified fm & r0 ');

% Calculating (Practically) Number of Zero Level Crossing and Average Fade Duration %

h=1;
c=0;
C1=0;
NUM=0;
while h<=M
if r(h)<=level
i=h;
while i<=M
if r(i)>=level
NUM=NUM+1;
break;
end
i=i+1;
end
c=i-h;
C1=C1+c;
h=i-1;
end
h=h+1;
end

Afd_p=Afd_p+(C1/NUM)*delta_t;
Nr_p=Nr_p+NUM*delta_f;
Rrms_p=Rrms_p+Rrms;
y=y+1;
end

%************ Theoretical calculation of Number of Zero Level Crossing (Nr) and Average Fade Duration ************* %

Nr_theoretical=sqrt(2*pi)*fm*r0*exp(-r0^2);

z1=exp(r0^2)-1;
z2=r0*fm*sqrt(2*pi);
Average_fade_duration_theoretical =z1/z2;

rowdb=10*log10(r0) ;
Rrms_theoretical=Rrms+rowdb;

%*********************** Displayiing Calculated values ************************ %

Nr_practical=(Nr_p);
Nr_practical
Nr_theoretical
Average_fade_duration_Practical=(Afd_p);
Average_fade_duration_Practical
Average_fade_duration_theoretical =z1/z2
Rrms_Practical=Rrms_p
Rrms_theoretical


Can somebody provide me brief explanation and further references for understanding the above code?
 

Hello people,

does it consider omnidirectional or directional antennas? Can anyone provide me with something in this frame (mulitpath vs directional antennas)?

Thanks in advance.

Lupin
 
here is the code...relatively easy one

Code:
function [c t]=Rayleigh_Calculation(fd)
So=16;										%Number of paths
Fs=1000;
Ts=1/Fs;
tstart=0;
tend=2;
t=[tstart:Ts:tend];
wm=2*pi*fd;								%Maximum shift
fm=wm/(2*pi);							%Doppler shift
S=2*(2*So+1);
Xco=(1.414*cos(wm*t));
Xso=0;
sum1=0;
sum2=0;
for n=1:So
   A(n)=(2*pi*n)/S;					%Azimuthal angles
   wn(n)=wm*cos(A(n));
   O(n)=(pi*n)/(So+1);
  sum1=sum1+(cos(O(n)).*cos(wn(n)*t));
     sum2=sum2+(cos(wn(n)*t).*sin(O(n)));
end
Xc=2*sum1+Xco;
Xs=2*sum2+Xso;
c=(1/sqrt(2*So+1))*(Xc+j*Xs);

it considers jakes model for implementation
 
Last edited by a moderator:
i did this task but it shows one problem and that is..
function [c t]=Rayleigh_Calculation(fd);
|
Error: Function definitions are not permitted at the prompt or in
scripts.

what is this and how i solve this problem ..pls help me
 

Code:
%=============================== fading channel===============================
function [fading]=corr_fading(total)
%Function for generating fading Rayleigh using Jakes model.
o_start=rand(1,1)*10;%First point of random observation.
alpha=pi/4;
N0=8;%low frequency oscillator equal omega_n.
N=(2*N0+1)*2;
sample=total; %Length of data .
n=[1:N0]';
beta_n=n*pi/(N0+1);
%Light velocity.
c=3*10^8;
%carrier frequency.
fc=5*10^9;
%Symbol period.
T=64*10^(-5);
%User velocity.
v=30;
%angle of Antenna .
theta=0;
%Doppler frequency.
v_c=v*(1000/3600);
%v_c=900;
omega_d=2*pi*v_c*fc/c*cos(theta);%Doppler Shift.
omega_n=omega_d*cos(2*pi*n/N);
delta_t=T*1000;
%Observation time.
t=[o_start+delta_t:delta_t:o_start+delta_t*sample];
for k=1:N0
    temp_x_c(k,:)=cos(beta_n(k))*cos(omega_n(k)*t);
    temp_x_s(k,:)=sin(beta_n(k))*cos(omega_n(k)*t);
end
x_c=(sum(temp_x_c)*2+(2*sqrt(2)*cos(alpha)*cos(omega_d*t)))/(N0);
x_s=(sum(temp_x_s)*2+(2*sqrt(2)*sin(alpha)*cos(omega_d*t)))/(N0+1);
fading=sqrt((x_c.^2+(x_s.^2)*i)/2); 
figure(3);
plot([1:sample]*T,10*log10(abs(fading))), axis([0 sample*T -20 10])
title(['Rayleigh Fading channel modelling using Jakes, f_d=',num2str(v_c),'Hz, T_s=',num2str(T),'s']);
xlabel('time[s]'), 
ylabel('Magnitude[dB]')


or you can use that function

if you have bits data, ie: bits_data=[10100010101001010101010]
for fading value you may declare H=corr_fading(length(bits_data);
and you can multiply the data with fading value fading_data=bits_data*H;
you get it

good luck
 
Last edited by a moderator:

Tap means the coefficient of the delay spread of each path of the signal



tap describe the velocity of the signal
 

i have written a code for ofdm over Rayleigh Fading Channel. but i gives an error on the line written with red color.


Code:
clear all;
clc;

% OFDM Parameters

N=256;              % Total number of Subcarriers
ofdmBW=2.5e6;       % OFDM Bandwitdh
deltaf=ofdmBW/256;  %data on each subcarriers
Tfft=1/deltaf;      % ifft period
Tgi=Tfft/4;         %guard interval duration duration of cyclic prefix 1/4rth portion of ofdm symbols
Tsignal=Tgi+Tfft;   %total duration of BPSK-OFDM symbols=guard time+ifft period
Ncp=N*Tgi/Tfft;     %number of symbols allocated to cyclic prefix
nbitspersymbol=N;   % For BPSK, the number of Bits/Symbol is the same as N

% Simulation Parameters

samples=10;        %Number of the OFDM Symbols to transmit
EbNOdB= 0:20;    % Bit to noise ratio
EsNOdB = EbNOdB + 10*log10(N/N) + 10*log10(N/(Ncp+N)); % Symbol to Noise Ratio

SimulatedBER = zeros(1,length(EsNOdB));             %Place Holder for Simulated BER
TheoreticalBER = zeros(1,length(EsNOdB)); %Place Holder for Theoretical BER

% Monte Carlo Simulation
for i=1:length(EsNOdB),
    
    %Transmitter
    
    
data=randint(1,N*samples);              % Generating Random Data
A=reshape(data,samples,N);              % Serial to Parallel
s=2*A-1;                                % BPSK Modulation (1=1 & 0=-1)
d_ifft=ifft(s);                         % Applying IFFT
ofdm_signal=[d_ifft(:,N-Ncp+1:N) d_ifft]; % Cyclic Prefix

[rows cols]=size(ofdm_signal);
    
    %Parallel to serial conversion for transmission
    ofdm_signal=reshape(ofdm_signal.',1,rows*cols);

    
    
     %-----------------------------------------------
    %Channel Modeling 
    %-----------------------------------------------
    %Zero mean unit variance AWGN channel
     nTap = 10;
    ht = 1/sqrt(2)*1/sqrt(nTap)*(randn(samples,nTap)).';
    %%convolution of each symbol with the random channel
   for j = 1:samples;
      [COLOR="#FF0000"]xht(j,:) = conv(ht(j,:),ofdm_signal(j,:));[/COLOR]
   end
    ofdm_signal = xht;
    noise=1/sqrt(2)*(randn(1,rows*cols));%%%+1i*randn(1,rows*cols));% i components i wont add as its BPSK signal%%))_
    %Tdata= sqrt((N+Ncp)/N)*ofdm_signal + 10^(-EsNOdB(i)/20)*noise;
   
% %     Tdata= sqrt((N+Ncp)/N)*ofdm_signal + 10^(-EsNOdB(i)/20)*noise;
   %Receiver
    RecDataP=reshape(ofdm_signal.',(N+Ncp),(samples+nTap-1)).'; % Serial to Parallel Conversion

% Removing Cyclic Prefix
RecDataP=RecDataP(:,Ncp+1:(N+Ncp));

% FFT

R_Freq=fft(RecDataP);

%BPSK Demodulation
 R_Freq1= reshape(R_Freq,1,samples*N);
R_Freq1(R_Freq1>0) = +1;
R_Freq1(R_Freq1<0) = 0;


    s_cap=R_Freq1;
    
    
   
    
    
RecSymbols=reshape(R_Freq1,1,N*samples);     % Parallel to Serial Conversion
numErrors=sum(xor(RecSymbols,data)); % Count Number of Errors
   

    SimulatedBER(i)=numErrors/(samples*N);
   TheoreticalBER(i)=(1/2)*erfc(sqrt(10.^(EbNOdB(i)/10)));
end
 % Results
    figure
    plot(EbNOdB,SimulatedBER,'r-o');
    hold on;
    plot(EbNOdB,TheoreticalBER,'k*');
    grid on; title('BER Vs SNR(db) for OFDM with BPSK Modulation Over AWGN Channel');
    xlabel ('Eb/No (dB) - Samples = 1000');ylabel ('BER');legend ('simulated','theoretical');
 
Last edited by a moderator:

hey i need help writing assumptions for a rayleigh flat fading program. can someone help me please?
 

Hi ,

if we have channel impulse response = [0.4544 -0.0036 0.0065 -0.0107 0.0169 -0.0267] , how i can extract the path gain from the channel impulse response.
 

HI,

If i have a QPSK than what would be the modification in the code above. please help
 

hi priya, were u able to get the code..if u did i would be grateful if u cud send to me...
cheers
 

sorry i m still waiting for the reply...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top