-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial 2.py
314 lines (151 loc) · 6 KB
/
Tutorial 2.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 23:55:32 2023
@author: ARIJIT
"""
# PYTHON TUTORIAL 30AUG2023
#############################################################
# Main topics to remember/learn in Python :
#
# 1. Data types/ Data structure : list, array, int, float, string, tuple, dict, plot
# 2. Control flow : Branching : if-else, iteration : forloop, while etc
# 3. Functions/class : built-in functions, functions from imported module, User Defined Functions - def, lambda
#
# 4. Interaction among 1, 2, and 3
#############################################################
# https://pynative.com/python-object-oriented-programming-oop-exercise/
# Class Exercises 01
# Write a Python program to create a Vehicle class with max_speed and mileage instance attributes.
class Vehicle:
def __init__(self, ms, mlg, clr="White"):
self.max_speed = ms
self.milage = mlg
self.color = clr
polo = Vehicle(240, 20)
polo.max_speed
polo.milage
# Class Exercise 02
# Create a Vehicle class without any variables and methods
class Vehicle:
pass
# Class Ex 03
# Create a child class Bus that will inherit all of the variables and methods of the Vehicle class
class Vehicle:
def __init__(self, ms, mlg):
self.max_speed = ms
self.milage = mlg
class Bus(Vehicle):
def odo(self, km):
self.total_km = km
polo_bus = Bus(100, 10)
polo_bus.odo(1000)
volo_bus = Bus(200, 20)
# Q - Define a property that must have the same value for every class instance (object)
class Vehicle:
colour = "White" # Hard coding the colour into the class definition
def __init__(self, ms, mlg):
self.max_speed = ms
self.milage = mlg
bus = Vehicle(10, 2)
bus2 = Vehicle(100, 22)
# Q Program to write factorial of a number.
# n ! = n * n-1 * n-2 * ... * 1
def facto(n):
# default_num = n
prod = 1
for x in range(n):
y = x + 1
prod = prod * y
return prod
# Clock 29AUG2023 (By Rehana SULTANA)
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
import math
class classVCO:
def __init__(self,freq,Ts):
self.freq = freq
self.Ts = Ts
self.maxCountVal = self.freq * self.Ts # Length of the period of the signal
self.countVal = self.maxCountVal
self.noise = 0
def VCOstep(self):
c = self.countVal
m = self.maxCountVal
# trigger graph logic
if c == 0.5*m:
edgeFlag = -1
elif c == m:
edgeFlag = 1
else:
edgeFlag = 0
# Square graph logic
if(c > 0.5*m):
squareGraph = 1
else:
squareGraph = 0
# Decrement of countval by 1
self.countVal = self.countVal - 1
# self.noise = self.noise + np.random.uniform(low=5, high=10, size=1)[0]
# Resetting step (AFTER ONE CYCLY)
if self.countVal == 0: # this where a new cycle starts
# random_noise = 0
random_noise = math.floor(np.random.normal(5,5,1)[0])
self.maxCountVal = self.maxCountVal + random_noise
self.countVal = self.maxCountVal # Resetting the countval to max value so that the cycle restarts
# random_noise = 0
# random_noise = math.floor(np.random.normal(0,5,1)[0])
# self.countVal = m + random_noise # Resetting the countval to max value
# self.noise = self.noise + np.random.normal(loc=0, scale=0.5, size=1)[0] #Accumulating noise
# self.noise = self.noise + np.random.uniform(low=0, high=10,size=1)[0] #Accumulating noise
return edgeFlag, squareGraph
s1 = classVCO(10,1) #Initializing classVCO
tt = []
VCOout = []
count = []
max_count_val = []
for i in range(0,1000,1):
# tt.append(i)
temp_countval = s1.countVal
temp_noise = s1.noise
temp_max_count_val = s1.maxCountVal
temp = s1.VCOstep()
VCOout.append(temp[1]) # Taking the square graph
count.append(temp_countval)
max_count_val.append(temp_max_count_val)
tt.append(i)
# print("Time, countval , VCOout : ",i , ", ", temp_countval, ",", temp)
# plt.plot(tt, VCOout, marker='o')
plt.step(tt, VCOout)
# plt.xlim(100, 300)
# plt.stem(tt, count) #marker='o')
plt.show()
# Period Count function
def period_count(signal):
'''
Parameters
----------
signal : Sequence of 1's and 0's
Returns
-------
T : number of complete cycles/period in the sequence
'''
period_count = 0
previous_value = None
list_of_cycle_lenghts = []
cycle_length = 0
for binary_value in signal:
if previous_value is None:
previous_value = binary_value #Setting the initial state of previous_value
if previous_value == 0 and binary_value == 1 :
period_count = period_count + 1 # Transition from 0 to 1 indicates a complete cycle
list_of_cycle_lenghts.append(cycle_length + 1)
cycle_length = 0
else:
cycle_length = cycle_length + 1
previous_value = binary_value # Storing the previous value at the end of the for loop
return period_count, list_of_cycle_lenghts
t = period_count(VCOout)
t[0]
t[1]
np.std(t[1])