Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Architectures and Algorithms For DSP Systems (Crl702) : Centre For Applied Research in Electronics Iit Delhi

The document is a lab report submitted by Group 10 students Vidhan vashishtha and Madhu Sudan Sharma for the course Architectures and Algorithms for DSP Systems (CRL702). The report contains code and algorithms to solve two objectives: 1) Reverse an array without using another array, and 2) Compute the cross-correlation and auto-correlation of two NumPy arrays using built-in functions, shift-multiply method, and matrix multiplication method. For objective 1, the code uses NumPy functions to reverse the elements of an input array. For objective 2, the three correlation methods are implemented and their outputs are compared.

Uploaded by

Raj Aryan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Architectures and Algorithms For DSP Systems (Crl702) : Centre For Applied Research in Electronics Iit Delhi

The document is a lab report submitted by Group 10 students Vidhan vashishtha and Madhu Sudan Sharma for the course Architectures and Algorithms for DSP Systems (CRL702). The report contains code and algorithms to solve two objectives: 1) Reverse an array without using another array, and 2) Compute the cross-correlation and auto-correlation of two NumPy arrays using built-in functions, shift-multiply method, and matrix multiplication method. For objective 1, the code uses NumPy functions to reverse the elements of an input array. For objective 2, the three correlation methods are implemented and their outputs are compared.

Uploaded by

Raj Aryan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CENTRE FOR APPLIED RESEARCH IN ELECTRONICS

IIT DELHI

ARCHITECTURES AND ALGORITHMS FOR DSP SYSTEMS (CRL702)

Lab Report on ASSIGNMENT 1

Submitted by: GROUP 10

ENTRY NUMBER NAME


2020CRF2590 Vidhan vashishtha
2020CRF2580 Madhu Sudan Sharma
OBJECTIVE:- 1
Input an array and reverse its elements without using another array. For example, if input is [1 2
3 4 5], your output should be [5 4 3 2 1]. Your code should work for any arbitrary length of array.

NumPy Library
In this question we use NumPy library of python. NumPy stands for Numerical Python. NumPy has
functions related to data manipulation related to array, matrices. It also has functions for arrays, linear
algebra, Fourier transform, and matrices. NumPy was created in 2005 by Travis Oliphant. It is an open-
source project, and we can use it freely.
NumPy provides array objects which is different from traditional Lists. Arrays are very faster then Lists.
Because NumPy uses continuous memory locations to store the data. SO NumPy is very famous in field
of data science because of its speed and resources.
Array object in NumPy is called ‘ndarray’.

Code
import numpy as np

n = int(input("Enter number of elements of 1st array : "))


a=np.zeros(n);
print("Enter the values of 1st input array : ")

for i in range(0, n): # iterating till the range


b = int(input())
a[i]=b # adding the element

print("1st Array is : ", a) # original array

c=np.size(a) # size of array


a=np.append(a,a) # append 'a' with 'a'

for i in range(0,c): # for loop to reverse the array


a[c-1-i]=a[i+c]

a=a[:c:1]
print("reversed array is: ", a)

ALGORITHM
1. import numpy as np

here we firstly import NumPy library as NP and use the NP for NumPy reference.

2. a=np.array([1,2,3,4,5])
print("original array is: ", a)
c=np.size(a)

here we enter a array. We use np.array so it use array object of NumPy library . And then we print
the original array by using print function. Here we using size function of the NumPy library to get
the size of the array.

3. a=np.append(a,a)

here we use the append function to make the a matrix size double. Now the ‘a’ matrix became
double the size of previous array.

4. for i in range (0,c)


a[c-1-i]=a[i+c]

here we use the for loop and assign the right half elements to the reverse order. Using this
operation the right half element get reversed and assign to the left half elements.

5. a=a[:c:1]
print("reversed array is: ", a)
here we first assign the first half elements to the a array. Rest right half elements gets discarded.
Then we print this matrix ‘a’.

Results
After execution, this program into “Pycharm” software. We get the following results.
Enter number of elements of array : 4
Enter the values of input array :
1
2
3
4
Original Array is : [1 2 3 4]
reversed array is: [4 3 2 1]
we can see that these results are correct. And the python program is working fine.
OBJECTIVE :- 2
Input two NumPy arrays of arbitrary length and compute their cross-correlation. Also find auto-
correlation of those two input arrays. Your code should work for any arbitrary length of array. Also,
implement correlation using matrix multiplication method.

Correlation
In signal processing, cross-correlation is a measure of similarity of two series as a function of the
displacement of one relative to the other. This is also known as a sliding dot product or sliding inner-
product. It is commonly used for searching a long signal for a shorter, known feature. The cross-
correlation is similar in nature to the correlation of two functions.
In an autocorrelation, which is the cross-correlation of a signal with itself, there will always be a peak at
a lag of zero, and its size will be the signal energy.

Code
import numpy as np
# creating an empty list
arr_1 = []
arr_2 = []

# number of elemetns as input


n = int(input("Enter number of elements of 1st array : "))
print ("Enter the values of 1st input array : ")
# iterating till the range
for i in range(0, n):
a = int(input())

arr_1.append(a) # adding the element


ar_1 = np.asarray(arr_1)
print ("1st Array is : ", ar_1)

# number of elemetns as input


n = int(input("Enter number of elements of 2nd array : "))
print ("Enter the values of 2nd input array : ")
# iterating till the range
for i in range(0, n):
a = int(input())

arr_2.append(a) # adding the element


ar_2 = np.asarray(arr_2)
print ("2nd array is: ", ar_2)

#USING BUILT-IN FUNCTION


print("\nMETHOD :1 (USING BUILT-IN FUNCTON) ")
def Built_in_Function(ar_1,ar_2):
c = np.correlate(ar_1, ar_2, "full") #built in function
return c
print("Cross Correlation using Built In Function:
",Built_in_Function(ar_1,ar_2))
print("Auto Correlation of 1st using Built In Function:
",Built_in_Function(ar_1,ar_1))
print("Auto Correlation of 2nd using Built In Function:
",Built_in_Function(ar_2,ar_2))

#SHIFT AND MULTIPLICATION METHOD


print("\nMETHOD :2 (USING SHIFT AND MULTIPLICATION METHOD) ")
size_ar_1 = np.size(ar_1)
size_ar_2 = np.size(ar_2)
def Shift_and_Multiplication_Correlation(ar_1,ar_2,size_ar_1,size_ar_2):
t = size_ar_1 + size_ar_2 - 1 # size of resultant correlation
#there used padding of zeros to make size equal to resutat correlation
pad_2 = abs(size_ar_1) - 1
pad_1 = abs(size_ar_2) - 1
r_2 = np.array([0] * pad_2)
r_1 = np.array([0] * pad_1)
a_2 = np.append(r_2, ar_2)
a_1 = np.append(ar_1, r_1)
cor = np.zeros(t)
for i in range(0, t):
sum = 0;
o = 0;
u = i + 1;
for j in range(0, u):
o = (a_1[j] * a_2[j - i - 1]); #multipliationo shifted
elements
sum = sum + o;

cor[i] = sum; #storing value


return cor

print("Cross Correlation using Shift and Multiplication Method ",


Shift_and_Multiplication_Correlation(ar_1,ar_2,size_ar_1,size_ar_2))
print("Auto Correlation of 1st array using Shift and Multiplication Method
", Shift_and_Multiplication_Correlation(ar_1,ar_1,size_ar_1,size_ar_1))
print("Auto Correlation of 2nd array using Shift and Multiplication Method
", Shift_and_Multiplication_Correlation(ar_2,ar_2,size_ar_2,size_ar_2))

#MATRIX MULTIPLICATION METHOD


print("\nMETHOD :3 (USING MATRIX MULTIPLICATON METHOD ) ")
def Matrix_Multiplication_Correlation(ar_1,ar_2,size_ar_1,size_ar_2):
mat1 = np.matrix(ar_1)
mat2 = np.matrix(ar_2[::-1]) #reverse the matrix
n = np.transpose(mat2) #transpose the matrix
t=size_ar_1+size_ar_2-1; #size of resultan correlation
# This will return multiplication
res = n @ mat1
matrix_mul = np.zeros(t)
for i in range(0, t):
sum = 0;
for j in range(0, size_ar_2):
for k in range(0, size_ar_1):
o = 0;
if (i == j + k): #select element when i is equal to j+k
o = res[j, k];
sum = sum + o;
matrix_mul[i] = sum; #storing value

return matrix_mul

print("Cross Correlation using Matrix Multiplication Method ",


Matrix_Multiplication_Correlation(ar_1,ar_2,size_ar_1,size_ar_2))

print("Auto Correlation of 1st array using Matrix Multiplication Method ",


Matrix_Multiplication_Correlation(ar_1,ar_1,size_ar_1,size_ar_1))

print("Auto Correlation of 2nd array using Matrix Multiplication Method ",


Matrix_Multiplication_Correlation(ar_2,ar_2,size_ar_2,size_ar_2))

Algorithm
We used 3 method to calculate the cross correlation and auto correlation of two arrays ,these methods are-
1. Using built in command “np.correlate” of Numpy library
2. Using Shift and Multiplication method
3. Using Matrix Multiplication Method

• Shift and Multiplication Method :


First we take both input array array1 and array2 from the user. Then, we add backward and forward
padding of zeros in array1 and array2 respectively to make the size of both arrays to be equal to
(size[array1] + size[array2] -1). After that we create a array of similar length to store the value of the
correlation. In next step, we used Two For loop , one loop for the shifting and other for the multiplication
of the elements of array and at last we store the summation of these multiplication values into the
correlation array. The above method is more clear by below pictorial representation-
Lets two array are-
array1 =[a b c] and array2=[g f e]
modified arrays after padding are-
array1=[a b c 0 0] and array2=[0 0 g f e]
then cross correlation is
and for autocorrelation both array1 and array2 is equal.

• Matrix Multiplication Method :


First we take both input array array1 and array2 from the user. Then, we take the transpose of the
second array and after that we multiply the array2 and array1 from which we get a matrix of M*N where
M and N are the element of array2 and array1 respectively. After that we create a array of similar length
to store the value of the correlation. In next step, we used Two For loop , one loop for the shifting and
other for the multiplication of the elements of array and at last we store the summation of these
multiplication values into the correlation array. The above method is more clear by below pictorial
representation-
Lets twoLet we have two array a_1=[a b c] and a_2=[g f e] respectively whose cross-correlation we have
to calculate. The cross correlation we can easily calculate by writing matrix in below represented form

So, Cross-Correlation matrix =[ea fa+eb ga+fb+ec gb+fc gc]


Auto-correlation matrix of 1st array = [ac ab+bc aa+bb+cc ab+bc ac] and
Auto-correlation matrix of 2nd array = [ge gf+fg gg+ff+ee gf+fg ge]
Results
Enter number of elements of 1st array : 4
Enter the values of 1st input array :
1
2
3
4
1st Array is : [1 2 3 4]
Enter number of elements of 2nd array : 3
Enter the values of 2nd input array :
6
7
8
2nd array is: [6 7 8]

METHOD :1 (USING BUILT-IN FUNCTON)


Cross Correlation using Built In Function: [ 8 23 44 65 46 24]
Auto Correlation of 1st using Built In Function: [ 4 11 20 30 20 11 4]
Auto Correlation of 2nd using Built In Function: [ 48 98 149 98 48]

METHOD :2 (USING SHIFT AND MULTIPLICATION METHOD)


Cross Correlation using Shift and Multiplication Method [ 8. 23. 44. 65. 46. 24.]
Auto Correlation of 1st array using Shift and Multiplication Method [ 4. 11. 20. 30. 20. 11. 4.]
Auto Correlation of 2nd array using Shift and Multiplication Method [ 48. 98. 149. 98. 48.]

METHOD :3 (USING MATRIX MULTIPLICATON METHOD )


Cross Correlation using Matrix Multiplication Method [ 8. 23. 44. 65. 46. 24.]
Auto Correlation of 1st array using Matrix Multiplication Method [ 4. 11. 20. 30. 20. 11. 4.]
Auto Correlation of 2nd array using Matrix Multiplication Method [ 48. 98. 149. 98. 48.]

You might also like