You are on page 1of 8

# Scanned by CamScanner

EE16A Homework 12

In [18]: %pylab inline
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import sys

## In [26]: ## RUN THIS FUNCTION BEFORE YOU START THIS PROBLEM

## This function will generate the gold code associated with the satellite ID using line
ar shift registers
## The satellite_ID can be any integer between 1 and 24
def Gold_code_satellite(satellite_ID):
codelength = 1023
registerlength = 10

## # Defining the MLS for G1 generator

register1 = -1*np.ones(registerlength)
MLS1 = np.zeros(codelength)
for i in range(codelength):
MLS1[i] = register1[9]
modulo = register1[2]*register1[9]
register1 = np.roll(register1,1)
register1[0] = modulo

## # Defining the MLS for G2 generator

register2 = -1*np.ones(registerlength)
MLS2 = np.zeros(codelength)
for j in range(codelength):
MLS2[j] = register2[9]
modulo = register2[1]*register2[2]*register2[5]*register2[7]*register2[8]*regist
er2[9]
register2 = np.roll(register2,1)
register2[0] = modulo

delay = np.array([5,6,7,8,17,18,139,140,141,251,252,254,255,256,257,258,469,470,471,
472,473,474,509,512,513,514,515,516,859,860,861,862])
G1_out = MLS1;
shamt = delay[satellite_ID - 1]
G2_out = np.roll(MLS2,shamt)

## CA_code = G1_out * G2_out

return CA_code

Part (a)
In [38]: def array_correlation(array1,array2):
""" This function should return two array or a matrix with one row corresponding t
o
the offset and other to the correlation value
"""
## Use np.correlate with "FULL". Check out the helper page for it

## offset = np.array([float(i - len(array1)) for i in range(len(array1)*2)])

cor = np.correlate(array1,array2, "full")
return np.array([offset, cor])

plt.plot(auto10[1])
print('High auto correlation')

Part (b)

## In [41]: cross1013 = array_correlation(Gold_code_satellite(10),Gold_code_satellite(13))

plt.plot(cross1013[1])
print('low cross correlation difference')

## low cross correlation difference

Part (c)
In [29]: def integernoise_generator(length_of_noise):
noise_array = np.random.randint(2,size = length_of_noise)
noise_array = 2*noise_array - np.ones(size(noise_array))
return noise_array

random = integernoise_generator(1023)
cross10random = array_correlation(Gold_code_satellite(10),random)
plt.plot(list(zip(cross10random[1], cross1013[1])))
print('low cross correlation. can identify satellites with anything except itself.')

Like part b, this cross-correlation is also low. It means that we can uniquely identify
each satellite with anything except itself.

Part (d)

## In [30]: def gaussiannoise_generator(length_of_noise):

noise_array = np.random.normal(0, 1, length_of_noise)
return noise_array

random = gaussiannoise_generator(1023)
cross = array_correlation(Gold_code_satellite(10), random)
plt.plot(list(zip(cross[1], cross1013[1])))

## Out[30]: [<matplotlib.lines.Line2D at 0x24a238f9588>,

<matplotlib.lines.Line2D at 0x24a238f96d8>]
Part (e)

## In [49]: data1 = np.load("data1.npy")

satellites = []
for i in range(24):
cross = array_correlation( data1, Gold_code_satellite(i))[1]
if np.max(cross) > 800:
satellites.append(i)
plt.plot(cross)

print("satellites are",satellites)

## satellites are [4, 7, 13, 19]

Part (f)
satellites = []
for i in range(24):
cross = array_correlation( data2, Gold_code_satellite(i))[1]
if np.max(cross) > 800:
satellites.append(i)
plt.plot(cross)

## print("present satellite is",satellites[0])

print('the sequence is [1 -1 -1 -1 1]')

present satellite is 3
the sequence is [1 -1 -1 -1 1]

Part (g)

## In [48]: data3 = np.load("data3.npy")

satellites = []
for i in range(24):
cross = array_correlation(data3, Gold_code_satellite(i))[1]
if np.max(cross) > 800:
satellites.append(i)
plt.plot(cross)

## print("satellites present are",satellites)

print('delays are 1000ms for one satellite and 1500ms for the other')

## satellites present are [5, 20]

delays are 1000ms for one satellite and 1500ms for the other
Question 3: Image Analysis
In [18]: def plot_circle(a, d, e):
is_circle = d**2 + e**2 - 4*a > 0
assert is_circle, "Not a circle"

XLIM_LO = -1
XLIM_HI = 3
YLIM_LO = -2
YLIM_HI = 2
X_COUNT = 400
Y_COUNT = 400
x = np.linspace(XLIM_LO, XLIM_HI, X_COUNT)
y = np.linspace(YLIM_LO, YLIM_HI, Y_COUNT)
x, y = np.meshgrid(x, y)
f = lambda x,y: a*(x**2 + y**2) + d*x + e*y
c1 = plt.contour(x, y, f(x,y), [1], colors='r')
plt.axis('scaled')
plt.xlabel('x')
plt.ylabel('y')
plt.title(r'\${:.2f}(x^2 + y^2) {:+.2f}x {:+.2f}y\$'.format(a,d,e))

## In [19]: def plot_ellipse(a, b, c, d, e):

is_ellipse = b**2 - 4*a*c < 0
assert is_ellipse, "Not an ellipse"
XLIM_LO = -1
XLIM_HI = 3
YLIM_LO = -2
YLIM_HI = 2
X_COUNT = 400
Y_COUNT = 400
x = np.linspace(XLIM_LO, XLIM_HI, X_COUNT)
y = np.linspace(YLIM_LO, YLIM_HI, Y_COUNT)
x, y = np.meshgrid(x, y)
f = lambda x,y: a*x**2 + b*x*y + c*y**2 + d*x + e*y
c1 = plt.contour(x, y, f(x,y), [1], colors='r')
plt.axis('scaled')
plt.xlabel('x')
plt.ylabel('y')
plt.title(r'\${:.2f}x^2 {:+.2f}xy {:+.2f}y^2 {:+.2f}x {:+.2f}y\$'.format(a,b,c,d,e))

## In [20]: # Here is an example of plot_ellipse.

# This plots (x-1)**2 + (y-1)**2 = 1,
# which is a circle centered at (1,1).
plt.figure(figsize=(5,5))
plot_ellipse(1, 0, 1, -1, -1)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-20-2cb2095ac3fb> in <module>()
2 # This plots (x-1)**2 + (y-1)**2 = 1,
3 # which is a circle centered at (1,1).
----> 4 plt.figure(figsize=(5,5))
5 plot_ellipse(1, 0, 1, -1, -1)

## NameError: name 'plt' is not defined

You may find plt.scatter (http://matplotlib.org/api/pyplot_api.html) useful for plotting the points.
Part (c)

## In [28]: points = [(.3,-.69),(.5,.87),(.9,-.86),(1,.88),(1.2,-.82),(1.5,.64),(1.8,0)]

def make_A(pair):
x, y = pair[0], pair[1]
return np.array([(x*x) + (y*y), x, y])
A_circle, b_circle = [], []
for i in range(len(points)):
A_circle.append(make_A(points[i]))
b_circle.append(1)
A_circle, b_circle = np.array(A_circle), np.array(b_circle)
x_circle = np.dot(np.dot(np.linalg.inv(np.dot(A_circle.T,A_circle)),A_circle.T), b_c
ircle)
print('Coefficients of best-fit circle: ', x_circle)
error_circle = np.linalg.norm(np.dot(A_circle,x_circle) - b_circle)
print("Circle's |e|/N = ",error_circle/len(points))
plt.figure(figsize=(6,6))
plot_circle(4.873, -7.893, -0.227)

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-28-e2ec20382486> in <module>()
5 A_circle, b_circle = [], []
6 for i in range(len(points)):
----> 7 A_circle.append(make_A(points[i]))
8 b_circle.append(1)
9 A_circle, b_circle = np.array(A_circle), np.array(b_circle)

<ipython-input-28-e2ec20382486> in make_A(pair)
2 def make_A(pair):
3 x, y = pair[0], pair[1]
----> 4 return np.array([(x*x) + (y*y), x, y])
5 A_circle, b_circle = [], []
6 for i in range(len(points)):

Part (d)