Awell00 commited on
Commit
1c4573f
·
1 Parent(s): c1f84db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -48
app.py CHANGED
@@ -13,13 +13,16 @@ import reedsolo
13
  import wavio
14
  from scipy.signal import butter, lfilter
15
 
 
 
16
  low_frequency = 18000
17
- high_frequency = 19000
18
  bit_duration = 0.007
19
  sample_rate = 44100
20
  amplitude_scaling_factor = 10.0
21
 
22
- #-----------------Record-----------------#
 
23
 
24
  def record(audio):
25
  try:
@@ -30,10 +33,11 @@ def record(audio):
30
  except Exception as e:
31
  return f"Error: {e}"
32
 
33
- #-----------------Filter-----------------#
34
 
35
- def butter_bandpass(lowcut, highcut, fs, order=5):
36
- nyquist = 0.5 * fs
 
 
37
  low = lowcut / nyquist
38
  high = highcut / nyquist
39
  coef = butter(order, [low, high], btype='band')
@@ -41,44 +45,45 @@ def butter_bandpass(lowcut, highcut, fs, order=5):
41
  a = coef[1]
42
  return b, a
43
 
44
- def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
45
- b, a = butter_bandpass(lowcut, highcut, fs, order=order)
 
46
  y = lfilter(b, a, data)
47
  return y
48
 
 
49
  def main():
50
  input_file = 'recorded.wav'
51
  output_file = 'output_filtered_receiver.wav'
52
  lowcut = 17500
53
  highcut = 19500
54
 
55
- fs, data = read(input_file)
56
 
57
- filtered_data = butter_bandpass_filter(data, lowcut, highcut, 44100)
58
- write(output_file, fs, np.int16(filtered_data))
59
  return "Filtered Audio Generated"
60
 
61
- #-----------------Frame-----------------#
62
 
63
- def calculate_snr(data, start, end, target_frequency, sample_rate):
64
 
 
65
  segment = data[start:end]
66
  spectrum = np.fft.fft(segment)
67
  frequencies = np.fft.fftfreq(len(spectrum), 1 / sample_rate)
68
  target_index = np.abs(frequencies - target_frequency).argmin()
69
  amplitude = np.abs(spectrum[target_index])
70
 
71
- noise_segment = data[100:1000+len(segment)]
72
  noise_spectrum = np.fft.fft(noise_segment)
73
  noise_amplitude = np.abs(noise_spectrum[target_index])
74
 
75
  snr = 10 * np.log10(amplitude / noise_amplitude)
76
  return snr
77
-
78
- filename = 'output_filtered_receiver.wav'
79
 
80
  def frame_analyse(filename):
81
- fs, y = read(filename)
82
 
83
  first_part_start = 0
84
  first_part_end = len(y) // 2
@@ -86,13 +91,13 @@ def frame_analyse(filename):
86
  second_part_start = len(y) // 2
87
  second_part_end = len(y)
88
 
89
- nperseg = 256
90
- noverlap = 128
91
 
92
- f, t, Sxx = signal.spectrogram(y, fs, nperseg=nperseg, noverlap=noverlap)
93
 
94
  plt.figure()
95
- plt.pcolormesh(t, f, Sxx, shading="gouraud")
96
  plt.xlabel("Time [s]")
97
  plt.ylabel("Frequency [Hz]")
98
  plt.title("Spectrogram of the signal")
@@ -102,37 +107,40 @@ def frame_analyse(filename):
102
 
103
  f_idx = np.argmin(np.abs(f - f0))
104
 
105
- thresholds_start = calculate_snr(y, first_part_start, first_part_end, low_frequency, sample_rate)
106
- thresholds_end = calculate_snr(y, second_part_start, second_part_end, high_frequency, sample_rate)
107
 
108
- t_idx_start = np.argmax(Sxx[f_idx] > thresholds_start)
109
 
110
  t_start = t[t_idx_start]
111
 
112
  t_idx_end = t_idx_start
113
- while t_idx_end < len(t) and np.max(Sxx[f_idx, t_idx_end:]) > thresholds_end:
114
  t_idx_end += 1
115
 
116
  t_end = t[t_idx_end]
117
 
118
  return t_start, t_end
119
 
120
- #-----------------Receiver-----------------#
121
 
122
- def dominant_frequency(signal, sample_rate=44100):
123
- yf = fft(signal)
124
- xf = np.linspace(0.0, sample_rate / 2.0, len(signal) // 2)
125
- peaks, _ = find_peaks(np.abs(yf[0:len(signal) // 2]))
126
- return xf[peaks[np.argmax(np.abs(yf[0:len(signal) // 2][peaks]))]]
 
 
 
127
 
128
  def binary_to_text(binary):
129
  try:
130
- return ''.join(chr(int(binary[i:i + 8], 2)) for i in range(0, len(binary), 8))
131
  except Exception as e:
132
- return f"Except: {e}"
 
133
 
134
  def decode_rs(binary_string, ecc_bytes):
135
- byte_data = bytearray(int(binary_string[i:i+8], 2) for i in range(0, len(binary_string), 8))
136
  rs = reedsolo.RSCodec(ecc_bytes)
137
  corrected_data_tuple = rs.decode(byte_data)
138
  corrected_data = corrected_data_tuple[0]
@@ -143,6 +151,7 @@ def decode_rs(binary_string, ecc_bytes):
143
 
144
  return corrected_binary_string
145
 
 
146
  def manchester_decoding(binary_string):
147
  decoded_string = ''
148
  for i in tqdm(range(0, len(binary_string), 2), desc="Decoding"):
@@ -156,20 +165,21 @@ def manchester_decoding(binary_string):
156
  return None
157
  return decoded_string
158
 
 
159
  def signal_to_binary_between_times(filename):
160
  start_time, end_time = frame_analyse(filename)
161
 
162
- sample_rate, data = read(filename)
163
 
164
- start_sample = int((start_time - 0.007) * sample_rate)
165
- end_sample = int((end_time - 0.007) * sample_rate)
166
  binary_string = ''
167
 
168
  start_analyse_time = time.time()
169
 
170
- for i in tqdm(range(start_sample, end_sample, int(sample_rate * bit_duration))):
171
- signal = data[i:i + int(sample_rate * bit_duration)]
172
- frequency = dominant_frequency(signal, sample_rate)
173
  if np.abs(frequency - low_frequency) < np.abs(frequency - high_frequency):
174
  binary_string += '0'
175
  else:
@@ -186,13 +196,12 @@ def signal_to_binary_between_times(filename):
186
  break
187
 
188
  print("Binary String:", binary_string)
189
- binary_string_decoded = manchester_decoding(binary_string[index_start+7:index_end])
190
 
191
  decoded_binary_string = decode_rs(binary_string_decoded, 20)
192
 
193
- return decoded_binary_string
194
 
195
- #-----------------Interface-----------------#
196
 
197
  def receive():
198
  try:
@@ -201,14 +210,17 @@ def receive():
201
  except Exception as e:
202
  return f"Error: {e}"
203
 
 
 
 
204
  with gr.Blocks() as demo:
205
  input_audio = gr.Audio(sources=["upload"])
206
- output = gr.Textbox(label="Record Sound")
207
- btn = gr.Button(value="Convert")
208
- btn.click(fn=record, inputs=input_audio, outputs=output)
209
 
210
- output_third = gr.Textbox(label="Received Text")
211
- btn_3 = gr.Button(value="Received Text")
212
- btn_3.click(fn=receive, outputs=output_third)
213
 
214
  demo.launch()
 
13
  import wavio
14
  from scipy.signal import butter, lfilter
15
 
16
+ # ---------------Parameters--------------- #
17
+
18
  low_frequency = 18000
19
+ high_frequency = 19000
20
  bit_duration = 0.007
21
  sample_rate = 44100
22
  amplitude_scaling_factor = 10.0
23
 
24
+
25
+ # -----------------Record----------------- #
26
 
27
  def record(audio):
28
  try:
 
33
  except Exception as e:
34
  return f"Error: {e}"
35
 
 
36
 
37
+ # -----------------Filter----------------- #
38
+
39
+ def butter_bandpass(lowcut, highcut, sr, order=5):
40
+ nyquist = 0.5 * sr
41
  low = lowcut / nyquist
42
  high = highcut / nyquist
43
  coef = butter(order, [low, high], btype='band')
 
45
  a = coef[1]
46
  return b, a
47
 
48
+
49
+ def butter_bandpass_filter(data, lowcut, highcut, sr, order=5):
50
+ b, a = butter_bandpass(lowcut, highcut, sr, order=order)
51
  y = lfilter(b, a, data)
52
  return y
53
 
54
+
55
  def main():
56
  input_file = 'recorded.wav'
57
  output_file = 'output_filtered_receiver.wav'
58
  lowcut = 17500
59
  highcut = 19500
60
 
61
+ sr, data = read(input_file)
62
 
63
+ filtered_data = butter_bandpass_filter(data, lowcut, highcut, sr)
64
+ write(output_file, sr, np.int16(filtered_data))
65
  return "Filtered Audio Generated"
66
 
 
67
 
68
+ # -----------------Frame----------------- #
69
 
70
+ def calculate_snr(data, start, end, target_frequency):
71
  segment = data[start:end]
72
  spectrum = np.fft.fft(segment)
73
  frequencies = np.fft.fftfreq(len(spectrum), 1 / sample_rate)
74
  target_index = np.abs(frequencies - target_frequency).argmin()
75
  amplitude = np.abs(spectrum[target_index])
76
 
77
+ noise_segment = data[100:1000 + len(segment)]
78
  noise_spectrum = np.fft.fft(noise_segment)
79
  noise_amplitude = np.abs(noise_spectrum[target_index])
80
 
81
  snr = 10 * np.log10(amplitude / noise_amplitude)
82
  return snr
83
+
 
84
 
85
  def frame_analyse(filename):
86
+ sr, y = read(filename)
87
 
88
  first_part_start = 0
89
  first_part_end = len(y) // 2
 
91
  second_part_start = len(y) // 2
92
  second_part_end = len(y)
93
 
94
+ segment_length = 256
95
+ overlap_size = 128
96
 
97
+ f, t, sxx = signal.spectrogram(y, sr, nperseg=segment_length, noverlap=overlap_size)
98
 
99
  plt.figure()
100
+ plt.pcolormesh(t, f, sxx, shading="gouraud")
101
  plt.xlabel("Time [s]")
102
  plt.ylabel("Frequency [Hz]")
103
  plt.title("Spectrogram of the signal")
 
107
 
108
  f_idx = np.argmin(np.abs(f - f0))
109
 
110
+ thresholds_start = calculate_snr(y, first_part_start, first_part_end, low_frequency)
111
+ thresholds_end = calculate_snr(y, second_part_start, second_part_end, high_frequency)
112
 
113
+ t_idx_start = np.argmax(sxx[f_idx] > thresholds_start)
114
 
115
  t_start = t[t_idx_start]
116
 
117
  t_idx_end = t_idx_start
118
+ while t_idx_end < len(t) and np.max(sxx[f_idx, t_idx_end:]) > thresholds_end:
119
  t_idx_end += 1
120
 
121
  t_end = t[t_idx_end]
122
 
123
  return t_start, t_end
124
 
 
125
 
126
+ # -----------------Receiver----------------- #
127
+
128
+ def dominant_frequency(signal_value):
129
+ yf = fft(signal_value)
130
+ xf = np.linspace(0.0, sample_rate / 2.0, len(signal_value) // 2)
131
+ peaks, _ = find_peaks(np.abs(yf[0:len(signal_value) // 2]))
132
+ return xf[peaks[np.argmax(np.abs(yf[0:len(signal_value) // 2][peaks]))]]
133
+
134
 
135
  def binary_to_text(binary):
136
  try:
137
+ return ''.join(chr(int(binary[i:i + 8], 2)) for i in range(0, len(binary), 8))
138
  except Exception as e:
139
+ return f"Except: {e}"
140
+
141
 
142
  def decode_rs(binary_string, ecc_bytes):
143
+ byte_data = bytearray(int(binary_string[i:i + 8], 2) for i in range(0, len(binary_string), 8))
144
  rs = reedsolo.RSCodec(ecc_bytes)
145
  corrected_data_tuple = rs.decode(byte_data)
146
  corrected_data = corrected_data_tuple[0]
 
151
 
152
  return corrected_binary_string
153
 
154
+
155
  def manchester_decoding(binary_string):
156
  decoded_string = ''
157
  for i in tqdm(range(0, len(binary_string), 2), desc="Decoding"):
 
165
  return None
166
  return decoded_string
167
 
168
+
169
  def signal_to_binary_between_times(filename):
170
  start_time, end_time = frame_analyse(filename)
171
 
172
+ sr, data = read(filename)
173
 
174
+ start_sample = int((start_time - 0.007) * sr)
175
+ end_sample = int((end_time - 0.007) * sr)
176
  binary_string = ''
177
 
178
  start_analyse_time = time.time()
179
 
180
+ for i in tqdm(range(start_sample, end_sample, int(sr * bit_duration))):
181
+ signal_value = data[i:i + int(sr * bit_duration)]
182
+ frequency = dominant_frequency(signal_value)
183
  if np.abs(frequency - low_frequency) < np.abs(frequency - high_frequency):
184
  binary_string += '0'
185
  else:
 
196
  break
197
 
198
  print("Binary String:", binary_string)
199
+ binary_string_decoded = manchester_decoding(binary_string[index_start + 7:index_end])
200
 
201
  decoded_binary_string = decode_rs(binary_string_decoded, 20)
202
 
203
+ return decoded_binary_string
204
 
 
205
 
206
  def receive():
207
  try:
 
210
  except Exception as e:
211
  return f"Error: {e}"
212
 
213
+
214
+ # -----------------Interface----------------- #
215
+
216
  with gr.Blocks() as demo:
217
  input_audio = gr.Audio(sources=["upload"])
218
+ output_text = gr.Textbox(label="Record Sound")
219
+ btn_convert = gr.Button(value="Convert")
220
+ btn_convert.click(fn=record, inputs=input_audio, outputs=output_text)
221
 
222
+ output_convert = gr.Textbox(label="Received Text")
223
+ btn_receive = gr.Button(value="Received Text")
224
+ btn_receive.click(fn=receive, outputs=output_convert)
225
 
226
  demo.launch()