Category Archives: Fundamentals

The basic building blocks.

MATLAB vs Python Computational Speed

Windows Edition
Windows 8.1 Pro

System
Processor Intel(R) Core(TM) i7-5500U CPU @ 2.4GHz
Installed Memory 8.00 GB
System Type 64 Bit Operating System, x64 Based Processor

Integrated Development Environment (IDE)
Enthought Canopy
Version 2.1.3.3542 (32 bit)

Operation Time in sec (MATLAB) Time in sec (PYTHON)
10 million uniform random variable generation 0.10 0.15
10 million normal random variable generation 0.13 0.40
for loop counting up to 100 million 0.40 11.60
Comparing two vectors of length 10 million each 0.39 0.55
Plotting a histogram of 10 million values 0.89 0.76
Plotting a scatter plot of 1 million values 0.30 0.23
Bit error rate calculation of BPSK for 10 values of SNR 2.49 4.51

Although Python is a bit slower than MATLAB for most of the cases but the real difference is in implementation of “for loop” where the speed of MATLAB is 29x that of Python. Another surprising result was that the plot functions for Python were somewhat faster than MATLAB.

BPSK Bit Error Rate Calculation Using Python

Have you ever thought about how life would be without MATLAB. As it turns out there are free and open source options such as Python. We have so far restricted ourselves to MATLAB in this blog but now we venture out to find out what are the other options. Given below is a most basic Python code that calculates the Bit Error Rate of Binary Phase Shift Keying (BPSK). Compare this to our MATLAB implementation earlier [BPSK BER].

There are various IDEs available for writing your code but I have used Enthought Canopy Editor (32 bit) which is free to download and is also quite easy to use [download here]. So as it turns out that there is life beyond MATLAB. In fact there are several advantages of using Python over MATLAB which we will discuss later in another post. Lastly please note the indentation in the code below as there is no “end” statement in a “for loop” in Python.

from numpy import sqrt
from numpy.random import rand, randn
import matplotlib.pyplot as plt
  
N = 5000000
EbNodB_range = range(0,11)
itr = len(EbNodB_range)
ber = [None]*itr

for n in range (0, itr): 
 
    EbNodB = EbNodB_range[n]   
    EbNo=10.0**(EbNodB/10.0)
    x = 2 * (rand(N) >= 0.5) - 1
    noise_std = 1/sqrt(2*EbNo)
    y = x + noise_std * randn(N)
    y_d = 2 * (y >= 0) - 1
    errors = (x != y_d).sum()
    ber[n] = 1.0 * errors / N
    
    print "EbNodB:", EbNodB
    print "Error bits:", errors
    print "Error probability:", ber[n] 
        
plt.plot(EbNodB_range, ber, 'bo', EbNodB_range, ber, 'k')
plt.axis([0, 10, 1e-6, 0.1])
plt.xscale('linear')
plt.yscale('log')
plt.xlabel('EbNo(dB)')
plt.ylabel('BER')
plt.grid(True)
plt.title('BPSK Modulation')
plt.show()
BPSK Bit Error Rate
BPSK Bit Error Rate

MATLAB vs PYTHON A COMPARISON

Knife Edge Diffraction Model

What is Diffraction

Diffraction is a phenomenon where electromagnetic waves (such as light waves) bend around corners to reach places which are otherwise not reachable i.e. not in the line of sight. In technical jargon such regions are also called shadowed regions (the term again drawn from the physics of light). This phenomenon can be explained by Huygen’s principle which states that “as a plane wave propagates in a particular direction each new point along the wavefront is a source of secondary waves”. This can be understood by looking at the following figure. However one peculiarity of this principle is that it is unable to explain why the new point source transmits only in the forward direction.

Image result for diffraction

Diffraction is Difficult to Model

The electromagnetic field in the shadowed region can be calculated by combining vectorially the contributions of all of these secondary sources, which is not an easy task. Secondly, the geometry is usually much more complicated than shown in the above figure. For example consider a telecom tower transmitting electromagnetic waves from a rooftop and a pedestrian using a mobile phone at street level. The EM waves usually reach the receiver at street level after more than one diffraction (not to mention multiple reflections). However, an approximation that works well in most cases is called knife edge diffraction, which assumes a single sharp edge (an edge with a thickness much smaller than the wavelength) separates the transmitter and receiver.

Knife Edge Model

The path loss due to diffraction in the knife edge model is controlled by the Fresnel Diffraction Parameter which measures how deep the receiver is within the shadowed region. A negative value for the parameter shows that the obstruction is below the line of sight and if the value is below -1 there is hardly any loss. A value of 0 (zero) means that the transmitter, receiver and tip of the obstruction are all in line and the Electric Field Strength is reduced by half or the power is reduced to one fourth of the value without the obstruction i.e. a loss of 6dB.  As the value of the Fresnel Diffraction Parameter increases on the positive side the path loss rapidly increases reaching a value of 27 dB for a parameter value of 5. Sometimes the exact calculation is not needed and only an approximate calculation, as proposed by Lee in 1985, is sufficient.

Fresnel Diffraction Parameter (v) is defined as:

v=h√(2(d1+d2)/(λ d1 d2))

where

d1 is the distance between the transmitter and the obstruction along the line of sight

d2 is the distance between the receiver and the obstruction along the line of sight

h is the height of the obstruction above the line of sight

and λ is the wavelength

The electrical length of the path difference between a diffracted ray and a LOS ray is equal to φ=(π/2)(v²) and the normalized electric field produced at the receiver, relative to the LOS path is e-jφ. Performing a summation of all the exponentials above the obstruction (from v to positive infinity) gives us the Fresnel Integral, F(v).

Knife Edge Diffraction Model Using Huygens Principle

Plot of Diffraction Loss

Diffraction Loss Using Knife-Edge Model

The MATLAB codes used to generate the above plots are given below (approximate method followed by the exact method). Feel free to use them in your simulations and if you have a question drop us a comment.

MATLAB Code for Approximate Calculation of Diffraction Loss
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Calculation of the path loss based on the value of
% Fresnel Diffraction Parameter as proposed by Lee
% Lee W C Y Mobile Communications Engineering 1985
% Copyright www.raymaps.com %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
v=-5:0.01:5;
for n=1:length(v)
if v(n) <= -1
G(n)=0;
elseif v(n) <= 0
G(n)=20*log10(0.5-0.62*v(n));
elseif v(n) <= 1
G(n)=20*log10(0.5*exp(-0.95*v(n)));
elseif v(n) <= 2.4
G(n)=20*log10(0.4-sqrt(0.1184-(0.38-0.1*v(n))^2));
else
G(n)=20*log10(0.225/v(n));
end
end
plot(v, G, 'b')
xlabel('Fresnel Diffraction Parameter')
ylabel('Diffraction Loss (dB)')
MATLAB Code for Exact Calculation of Diffraction Loss
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Exact calculation of the path loss (in dB)
% based on Fresnel Diffraction Parameter (v)
% T S Rappaport Wireless Communications P&P
% Copyright www.raymaps.com
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
close all
v=-5:0.01:5;
for n=1:length(v)
v_vector=v(n):0.01:v(n)+100;
F(n)=((1+1i)/2)*sum(exp((-1i*pi*(v_vector).^2)/2));
end
F=abs(F)/(abs(F(1)));
plot(v, 20*log10(F),'r')
xlabel('Fresnel Diffraction Parameter')
ylabel('Diffraction Loss (dB)')

We have used the following equations in the exact calculation of the Diffraction Loss [1] above. We did not want to scare you with the math so have saved it for the end.

Also please checkout this interesting video explaining the phenomenon of diffraction.

[1] http://www.waves.utoronto.ca

Reflection vs Scattering in Ray-Tracing

In ray-tracing simulations one is faced with a complexity at points of intersection between rays and objects. How to handle the reflection, should it be specular or scattered. In this post we consider the two extreme cases; pure reflection vs scattering.

For this we consider that in the case of specular reflection the power of the ray is reduced by 3dB (that is R=0.5) and the ray continues as it would if there was no interaction with the object (off course direction would be changed with angle of reflection being equal to angle of incidence).

In the case of scattered ray we assume that the point of interaction between the ray and the object is a point source from where the rays are regenerated. Therefore there is a rapid drop in the E-field strength as the ray propagates away from the point of interaction.

clear all
close all

Eo=1;
r1=1:100;
r2=101:200;
r=[r1 r2];
R=0.5;

Er1=Eo./r1;
Er2=R*Eo./r2;
Er=[Er1 Er2];

Es1=Eo./r1;
Es2=Es1(end)./(r2-r1(end));
Es=[Es1 Es2];

plot(r,10*log10(Er),'b')
hold on
plot(r,10*log10(Es),'r:')
hold off

legend('Reflection','Scattering')
xlabel('Distance (m)')
ylabel('E-field (V/m)')

Reflected vs Scattered Ray

The simulation code above considers that a ray travels unobstructed for 100 m and at this point comes in contact with an object and is reflected (reflection coefficient of 0.5 is assumed) or scattered. The ray then again travels for another 100 m without coming in contact with an object.

It is observed that there is a rapid decrease in E-field strength of the scattered ray beyond 100 m. This is because the E-field strength at the point of interaction is much lower than that at the source and when this is considered to be a point source, re-generating the rays, the resulting E-field decays quite rapidly.

We assume that the reflection is specular in most of our simulations as this is closer to reality than assuming a fully scattered ray.

How to Find Point of Intersection of Two Lines

Finding the point of intersection of two lines has many important application such as in Ray-Tracing Simulation.  Two lines always intersect at some point unless they are absolutely parallel, like the rails of a railway track. We start with writing the equations of the two lines in slope-intercept form.

y1=b1+m1*x1

y2=b2+m2*x2

straight-lines

Here m1 and m2 are the slopes of the two lines and b1 and b2 are their y-intercepts. At the point of intesection y1=y2, so we have.

b1+m1*x1=b2+m2*x2

But at  the point of intersection x1=x2 as well, so replacing x1 and x2 with x we have.

b1+m1*x=b2+m2*x

or

b1-b2=-x*(m1-m2)

or

x=-(b1-b2)/(m1-m2)

Once the x-component of the point of intersection is found we can easily find the y-component by substituting x in any of the two line equations above.

y=b1+m1*x

In future posts we would like to discuss the cases of intersection of two surfaces and the intersection of two volumes.

Shannon Capacity CDMA vs OFDMA

We have previously discussed Shannon Capacity of CDMA and OFMDA, here we will discuss it again in a bit more detail. Let us assume that we have 20 MHz bandwidth for both the systems which is divided amongst 20 users. For OFDMA we assume that each user gets 1 MHz bandwidth and there are no guard bands or pilot carriers. For CDMA we assume that each user utilizes full 20 MHz bandwidth. We can say that for OFDMA each user has a dedicated channel whereas for CDMA the channel is shared between 20 simultaneous users.

We know that Shannon Capacity is given as

C=B*log2(1+SNR)

or in the case of CDMA

C=B*log2(1+SINR)

where ‘B’ is the bandwidth and SINR is the signal to noise plus interference ratio. For OFDMA the SNR is given as

SNR=Pu/(B*No)

where ‘Pu’ is the signal power of a single user and ‘No’ is the Noise Power Spectral Density. For CDMA the calculation of SINR is a bit more complicated as we have to take into account the Multiple Access Interference. If the total number of users is ‘u’ the SINR is calculated as

SINR=Pu/(B*No+(u-1)*Pu)

The code given below plots the capacity of CDMA and OFDMA as a function of Noise Power Spectral Density ‘No’.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CAPACITY OF CDMA and OFDMA
% u - Number of users
% Pu - Power of a single user
% No - Noise Power Spectral Density
%
% Copyright RAYmaps (www.raymaps.com)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
close all

u=20;
Pu=1;
No=1e-8:1e-8:1e-6;

B=20e6;
C_CDMA=u*B*log2(1+Pu./(B*No+(u-1)*Pu));

B=1e6;
C_OFDMA=u*B*log2(1+Pu./(B*No));

plot(No,C_CDMA/1e6);hold on
plot(No,C_OFDMA/1e6,'r');hold off
xlabel('Noise Power Spectral Density (No)')
ylabel('Capacity (Mbps)')
legend('CDMA','OFDMA')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Shannon Capacity of CDMA and OFDMA
Shannon Capacity of CDMA and OFDMA

We see that the capacity of OFDMA is much more sensitive to noise than CDMA. Within the low noise region the capacity of OFDMA is much better than CDMA but as the noise increases the capacity of the two schemes converges. In fact it was seen that as the noise PSD is further increased the two curves completely overlap each other. Therefore it can be concluded that OFDMA is the preferred technique when we are operating in the high SNR regime.