-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoverlap.py
More file actions
181 lines (162 loc) · 4.21 KB
/
overlap.py
File metadata and controls
181 lines (162 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def givep(f,l):
temp=f/l
if(f%l==0):
P=temp+1
else:
P=temp+2
return P
def returnx(l,p,n,mat1):
x=[[0 for row in range(0,int(n))] for col in range(0,int(p))]
z=int(0)
lenmat1=len(mat1)
for row in range(0,p-1):
value=0
for col in range(0,n):
if(value<l):
if(z<lenmat1):
x[row][col]=mat1[z]
value=value+1
z=z+1
else:
x[row][col]=0
return x
def findy(p,n,l,x,mat2):
sfinal=[]
h=len(mat2)
run=n-h
for z in range(1,run+1):
mat2.append(0)
y=[[0 for row in range(0,int(n))] for col in range(0,int(p))]
temp=[]
for i in range(0,p):
for j in range(0,n):
temp.append(x[i][j])
sfinal=convol(temp,mat2)
for j in range(0,n):
y[i][j]=sfinal[j]
del sfinal[:]
del temp[:]
return y
def inputmat(z):
mat=[]
for v in range(1,z+1):
q=int(input('Enter %d element : '%(v)))
mat.append(q)
return mat
def check_diff(mat1,mat2):
x=len(mat1)
h=len(mat2)
temp=[]
if(x>h):
run=x-h
for z in range(1,run+1):
mat2.append(0)
temp=convol(mat1,mat2)
return mat1,mat2
else:
run=h-x
for z in range(1,run+1):
mat1.append(0)
temp=convol(mat2,mat1)
return mat2,mat1
def convol(large,small):
lenlarge=len(large)
templist=[0]*lenlarge
temp=0
yn=[]
#print('Last Element is : %d'%(large[lenlarge-1]))
#convol=[]
convol=[[0 for row in range(0,lenlarge)] for col in range(0,lenlarge)]
for r in range(0,lenlarge):
if(r>0):
first=large[0]
last=large[lenlarge-1]
for q in range(0,lenlarge):
if(q>0 & q<lenlarge):
templist[q]=large[q-1]
convol[r][q]=templist[q]
else:
templist[0]=last
convol[r][q]=templist[q]
for p in range(0,lenlarge):
large[p]=templist[p]
else:
for p in range(0,lenlarge):
convol[r][p]=large[p]
yn=matmul(convol,small)
return yn
def matmul(convol1,small):
final=[]
lenlarge=len(convol1)
for i in range(0,lenlarge):
total=0
for j in range(0,lenlarge):
total2=convol1[j][i]*small[j]
total=total+total2
final.append(total)
return final
def giveans(yn,n,p,l):
ans=[]
var=0
temp=[]
for i in range(0,p):
for j in range(0,n):
if(j<l):
if not temp:
var=yn[i][j]
ans.append(var)
else:
var=yn[i][j]
var=var+temp[0]
ans.append(var)
del temp[0]
else:
temp.append(yn[i][j])
a=len(ans)
del ans[a-1]
return ans
def printvalue(p,n,mat,var):
printit=[]
values=1
for i in range(0,p):
print('%s%d : '%(var,values))
for j in range(0,n):
printit.append(mat[i][j])
print(printit)
del printit[:]
values+=1
def main():
print('\n'*5)
mat1=[]
large=[]
small=[]
x=[]
yn=[]
ans=[]
f=int(input('Enter the length of the x(n) matrix : '))
mat1=inputmat(f)
m=int(input('Enter the length of the h(n) matrix : '))
mat2=inputmat(m)
l=int(input('Enter the value of l : '))
n=l+m-1
p=int(givep(f,l))
print('The value of p is : %d '%(p))
print('The value of n is : %d '%(n))
print('\nx(n) : ')
print(mat1)
print ('h(n) : ')
print(mat2)
x=returnx(l,p,n,mat1)
print('\nThe Different values of X(inputs) : ')
variable='X'
printvalue(p,n,x,variable)
variable='Y'
yn=findy(p,n,l,x,mat2)
print('\nThe Different values of Y(Output) : ')
printvalue(p,n,yn,variable)
ans=giveans(yn,n,p,l)
print('\n****** Final answer of overlap add method ******* : ')
print(ans)
if __name__=="__main__":
main()
print('\n**** Programmed By : AFZAL JUNEJA *****')