-
Notifications
You must be signed in to change notification settings - Fork 1
/
SS_Project.py
263 lines (215 loc) · 6.55 KB
/
SS_Project.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
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 25 19:24:17 2019
@author: anshu
"""
""" This code is presented to Prof. Dr. Hilmi Dajani for partial fulfillment of the requirements for the course ELG7172/EACJ [W]– Topics in Signal Processing I. """
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from scipy import random
plt.close('all')
"""Generating a Small subthreshold signal (Sine Wave)"""
A=2 # amplitude
f=50 # frequency
T=1/f #time period
fs=100*f #sampling freq
Ts=1/fs
cycles=5
t=np.arange(0,cycles*T,Ts) #defining t
signal = A*np.sin(2*pi*f*t) #defining the signal
plt.subplot(2,1,1)
plt.plot(signal) #plotting the signal
plt.title('Sine wave / Subthreshold signal')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
plt.grid(True)
plt.show()
signal_power = signal**2
signal_power_db = 10*np.log10(signal_power)
""" Generating a system with a threshold """
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (signal[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
plt.subplot(2,1,1)
plt.plot(detected_signal, 'red')
plt.title('System output')
plt.xlabel('Input signal')
plt.ylabel('Output')
plt.grid(True)
plt.show()
"""Generating random White Gaussian Noise, adding with the noise and then using it as an input to our system"""
"""1st noise level"""
noise_power = 1.5
mean=0
noise = np.random.normal(mean, np.sqrt(noise_power), len(signal_power))
plt.plot(noise)
plt.title('White Gaussian Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
plt.grid(True)
plt.show()
output = signal + noise
plt.subplot(2,1,1)
plt.plot(output, 'orange')
plt.title('Output Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
ax = plt.gca()
ax.set_yticks(np.arange(-5, 5.1, 2.5))
plt.grid(True)
plt.show()
output_power = output**2
output_power_db= 10* np.log10(output_power)
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (output[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
plt.subplot(2,1,1)
plt.plot(detected_signal, 'red')
plt.title('System output')
plt.xlabel('Input signal')
plt.ylabel('Output')
plt.grid(True)
plt.show()
"""2nd noise level"""
noise_power = 2
mean=0
noise = np.random.normal(mean, np.sqrt(noise_power), len(signal_power))
plt.plot(noise)
plt.title('White Gaussian Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
plt.grid(True)
plt.show()
output = signal + noise
plt.subplot(2,1,1)
plt.plot(output, 'orange')
plt.title('Output Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
ax = plt.gca()
ax.set_yticks(np.arange(-5, 5.1, 2.5))
plt.grid(True)
plt.show()
output_power = output**2
output_power_db= 10* np.log10(output_power)
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (output[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
plt.subplot(2,1,1)
plt.plot(detected_signal, 'red')
plt.title('System output')
plt.xlabel('Input signal')
plt.ylabel('Output')
plt.grid(True)
plt.show()
"""3rd noise level"""
noise_power = 3
mean=0
noise = np.random.normal(mean, np.sqrt(noise_power), len(signal_power))
plt.plot(noise)
plt.title('White Gaussian Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
plt.grid(True)
plt.show()
output = signal + noise
plt.subplot(2,1,1)
plt.plot(output, 'orange')
plt.title('Output Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
ax = plt.gca()
ax.set_yticks(np.arange(-5, 5.1, 2.5))
plt.grid(True)
plt.show()
output_power = output**2
output_power_db= 10* np.log10(output_power)
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (output[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
plt.subplot(2,1,1)
plt.plot(detected_signal, 'red')
plt.title('System output')
plt.xlabel('Input signal')
plt.ylabel('Output')
plt.grid(True)
plt.show()
"""4th noise level"""
noise_power = 4
mean=0
noise = np.random.normal(mean, np.sqrt(noise_power), len(signal_power))
plt.plot(noise)
plt.title('White Gaussian Noise')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
plt.grid(True)
plt.show()
output = signal + noise
plt.subplot(2,1,1)
plt.plot(output, 'orange')
plt.title('Output Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amp (v)')
ax = plt.gca()
ax.set_yticks(np.arange(-5, 5.1, 2.5))
plt.grid(True)
plt.show()
output_power = output**2
output_power_db= 10* np.log10(output_power)
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (output[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
plt.subplot(2,1,1)
plt.plot(detected_signal, 'red')
plt.title('System output')
plt.xlabel('Input signal')
plt.ylabel('Output')
plt.grid(True)
plt.show()
""" Generating 200 noise levels and adding to subthreshold signal to use as an input to our system and then find relevant plot of SNR """
noise_power = 1
snr_array=np.zeros(200)
noise_power_array=np.zeros(200)
for j in range (200):
noise_power_array[j]=noise_power;
mean=0
noise = np.random.normal(mean, np.sqrt(noise_power), len(signal_power))
output = signal + noise
output_power = output**2
output_power_db= 10* np.log10(output_power)
detected_signal = np.zeros(500,np.int16)
detected_signal_amp = np.zeros(500,np.int16)
for i in range(500):
if (output[i] > 2.5):
detected_signal[i]=1;
detected_signal_amp[i]=output[i]-2.5
detected_signal_power=detected_signal**2;
avg_noise_power = np.mean(noise_power)
avg_detected_signal_power = np.mean(detected_signal_power)
snr_detected=avg_detected_signal_power / avg_noise_power
snr_detected_db = 10* np.log10(snr_detected)
snr_array[j]=snr_detected_db;
print('SNR for this detected signal =' , snr_detected_db)
noise_power=noise_power+.01;
plt.plot(noise_power_array,snr_array, 'black')
plt.title('SNR vs Noise')
plt.xlabel('Increasing Noise')
plt.ylabel('SNR (db)')
plt.show()
""" End of code """