forked from pranavsingh007/VLABS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASK.py
More file actions
235 lines (188 loc) · 6.52 KB
/
Copy pathASK.py
File metadata and controls
235 lines (188 loc) · 6.52 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
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
import matplotlib
matplotlib.use('Agg',force=True)
import matplotlib.pyplot as plt
import numpy as num
from matplotlib.figure import Figure
import base64
from io import BytesIO
import io
import base64
t=num.arange(0,1,0.001)
def ASK_Simulate1(A,F1,F2,user_input):
if user_input == 'Binary':
print("Binary Inputs")
# Binary data inputs
binary_data=[]
F2 = int(F2)
s = str(F2)
for i in range(0,len(s)):
binary_data.append(int(s[i]))
# Create time axis
# Convert binary data to a square wave
square_wave = num.repeat(binary_data, 2)
square_wave = num.concatenate(([square_wave[0]], square_wave))
t1 = num.linspace(0, len(binary_data), len(square_wave), endpoint=False)
t2=num.arange(0,len(binary_data),0.001)
x=A*num.sin(2*num.pi*F1*t2)#Carrier Sine wave
u=[]#Message signal
b=[0.2,0.4,0.6,0.8,1.0]
# fig = Figure()
# ax = fig.subplots()
# ax.plot(t2,x)
# # Save it to a temporary buffer.
# buf = BytesIO()
# fig.savefig(buf, format="png")
# # Embed the result in the html output.
# data = base64.b64encode(buf.getbuffer()).decode("ascii")
# print(data)
# return f"<img src='data:image/png;base64,{data}'/>"
img = io.BytesIO()
plt.plot(t2,x)
plt.savefig(img, format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
return (plot_url)
# plt.plot(t2,x);
# plt.xlabel('Time');
# plt.ylabel('Amplitude');
# plt.title('Carrier');
# plt.grid(True)
# plt.show()
else:
x=A*num.sin(2*num.pi*F1*t)#Carrier Sine wave
u=[]#Message signal
b=[0.2,0.4,0.6,0.8,1.0]
plt.plot(t,x);
plt.xlabel('Time');
plt.ylabel('Amplitude');
plt.title('Carrier');
plt.grid(True)
plt.show()
return 'I will dance dance dance with my hands hands hands above my head'
def ASK_Simulate2(A,F1,F2,user_input):
if user_input == 'Binary':
# Binary data inputs
binary_data=[]
s = str(F2)
for i in range(0,len(s)):
binary_data.append(int(s[i]))
# Convert binary data to a square wave
square_wave = num.repeat(binary_data, 2)
square_wave = num.concatenate(([square_wave[0]], square_wave))
# Create time axis
t1 = num.linspace(0, len(binary_data), len(square_wave), endpoint=False)
t2=num.arange(0,len(binary_data),0.001)
squares = list(square_wave)
temp = 0
waves = []
for i in range(len(t1)):
times=num.arange(temp,t1[i],0.001)
for j in range(len(times)):
waves.append(squares[i])
temp = t1[i]
t2=num.arange(0,temp,0.001)
# Adjust the length of waves to match the length of t2
waves = waves[:len(t2)]
# Plot square wave
plt.plot(t2, waves, drawstyle='steps-post')
plt.ylim(-0.1, 1.1)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()
else:
#Plot square wave
x=A*num.sin(2*num.pi*F1*t)#Carrier Sine wave
u=[]#Message signal
b=[0.2,0.4,0.6,0.8,1.0]
s=1
for i in t:
if(i==b[0]):
b.pop(0)
if(s==0):
s=1
else:
s=0
u.append(s)
v=[]#Sine wave multiplied with square wave
for i in range(len(t)):
v.append(A*num.sin(2*num.pi*F1*t[i])*u[i])
plt.plot(t,u)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Square wave Pulses')
plt.grid(True)
plt.show()
return 'I will dance dance dance with my hands hands hands above my head'
def ASK_Simulate3(A, F1, F2, user_input):
if user_input == 'Binary':
# Binary data inputs
binary_data = []
s = str(F2)
for i in range(0, len(s)):
binary_data.append(int(s[i]))
# Convert binary data to a square wave
square_wave = num.repeat(binary_data, 2)
square_wave = num.concatenate(([square_wave[0]], square_wave))
t1 = num.linspace(0, len(binary_data), len(square_wave), endpoint=False)
t2 = num.arange(0, len(binary_data), 0.001)
# Create waves for each square pulse
squares = list(square_wave)
temp = 0
waves = []
for i in range(len(t1)):
times = num.arange(temp, t1[i], 0.001)
for j in range(len(times)):
waves.append(squares[i])
temp = t1[i]
t2 = num.arange(0, temp, 0.001)
# Adjust the length of waves to match the length of t2
waves = waves[:len(t2)]
# Multiply the carrier signal with square wave
v = A * num.sin(2 * num.pi * F1 * t2) * waves
x=A*num.sin(2*num.pi*F1*t2)#Carrier Sine wave
u=[]#Message signal
b=[0.2,0.4,0.6,0.8,1.0]
# fig , axs = plt.subplots(3,1,figsize=(50,100))
# fig.subplots_adjust(hspace=0.7,wspace=0.7)
# # Plot the ASK signal
img = io.BytesIO()
# axs[0].plot(t2, waves, drawstyle='steps-post')
# axs[0].set_title('Amplitude')
# axs[0].set_xlabel('yo')
# axs[0].set_ylabel('Hi')
# axs[1].plot(t2, v)
# axs[1].set_title('Sqquare Plot')
# axs[1].set_xlabel('yo')
# axs[1].set_ylabel('Hi')
# axs[2].plot(t2,x)
# axs[2].set_title('ASK WavePlot')
# axs[2].set_xlabel('yo')
# axs[2].set_ylabel('Hi')
plt.grid(True)
# plt.show()
plt.plot(t2,v)
plt.savefig(img, format='png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
return (plot_url)
else:
# Generate message signal
t = num.arange(0, 1, 0.001)
u = []
b = [0.2, 0.4, 0.6, 0.8, 1.0]
s = 1
for i in t:
if len(b) > 0 and i == b[0]:
b.pop(0)
s = 1 - s
u.append(s)
# Multiply the carrier signal with message signal
v = A * num.sin(2 * num.pi * F1 * t) * u
# Plot the ASK signal
plt.plot(t, v)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('ASK Signal')
plt.grid(True)
plt.show()
return 'I will dance dance dance with my hands hands hands above my head'