Awell00 commited on
Commit
e9ef9ac
·
1 Parent(s): d3bc4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -25
app.py CHANGED
@@ -36,21 +36,21 @@ def butter_bandpass(sr, order=5):
36
  Returns:
37
  tuple: The filter coefficients `b` and `a`.
38
  """
39
- try:
40
  # Calculate the Nyquist frequency
41
  nyquist = 0.5 * sr
42
-
43
  # Normalize the cutoff frequencies
44
  low = low_frequency / nyquist
45
  high = high_frequency / nyquist
46
-
47
  # Design the Butterworth bandpass filter
48
  coefficient = butter(order, [low, high], btype='band')
49
-
50
  # Extract the filter coefficients
51
  b = coefficient[0]
52
  a = coefficient[1]
53
-
54
  return b, a
55
  except Exception as e:
56
  # If an error occurs, return an error message
@@ -72,10 +72,10 @@ def butter_bandpass_filter(data, sr, order=5):
72
  try:
73
  # Get the filter coefficients
74
  b, a = butter_bandpass(sr, order=order)
75
-
76
  # Apply the filter to the data
77
  y = lfilter(b, a, data)
78
-
79
  return y
80
  except Exception as e:
81
  # If an error occurs, return an error message
@@ -260,13 +260,13 @@ def dominant_frequency(signal_value):
260
  try:
261
  # Perform a Fast Fourier Transform on the signal
262
  yf = fft(signal_value)
263
-
264
  # Generate the frequencies corresponding to the FFT coefficients
265
  xf = np.linspace(0.0, sample_rate / 2.0, len(signal_value) // 2)
266
-
267
  # Find the peaks in the absolute values of the FFT coefficients
268
  peaks, _ = find_peaks(np.abs(yf[0:len(signal_value) // 2]))
269
-
270
  # Return the frequency corresponding to the peak with the highest amplitude
271
  return xf[peaks[np.argmax(np.abs(yf[0:len(signal_value) // 2][peaks]))]]
272
  except Exception as e:
@@ -287,9 +287,9 @@ def binary_to_text(binary):
287
  try:
288
  # Convert each 8-bit binary number to a character and join them together
289
  return ''.join(chr(int(binary[i:i + 8], 2)) for i in range(0, len(binary), 8))
290
- except Exception as e:
291
  # If an error occurs, return an error message
292
- return f"Except: {e}"
293
 
294
 
295
  def decode_rs(binary_string, ecc_bytes):
@@ -306,20 +306,20 @@ def decode_rs(binary_string, ecc_bytes):
306
  try:
307
  # Convert the binary string to a bytearray
308
  byte_data = bytearray(int(binary_string[i:i + 8], 2) for i in range(0, len(binary_string), 8))
309
-
310
  # Initialize a Reed-Solomon codec
311
  rs = reedsolo.RSCodec(ecc_bytes)
312
-
313
  # Decode the bytearray
314
  corrected_data_tuple = rs.decode(byte_data)
315
  corrected_data = corrected_data_tuple[0]
316
-
317
  # Remove trailing null bytes
318
  corrected_data = corrected_data.rstrip(b'\x00')
319
-
320
  # Convert the bytearray back to a binary string
321
  corrected_binary_string = ''.join(format(byte, '08b') for byte in corrected_data)
322
-
323
  return corrected_binary_string
324
  except Exception as e:
325
  # If an error occurs, return an error message
@@ -336,7 +336,7 @@ def manchester_decoding(binary_string):
336
  Returns:
337
  str: The decoded binary string.
338
  """
339
- try:
340
  decoded_string = ''
341
  for i in tqdm(range(0, len(binary_string), 2), desc="Decoding"):
342
  if i + 1 < len(binary_string):
@@ -366,15 +366,15 @@ def signal_to_binary_between_times(filename):
366
  try:
367
  # Get the start and end times of the signal of interest
368
  start_time, end_time = frame_analyse(filename)
369
-
370
  # Read the audio file
371
  sr, data = read(filename)
372
-
373
  # Calculate the start and end samples of the signal of interest
374
  start_sample = int((start_time - 0.007) * sr)
375
  end_sample = int((end_time - 0.007) * sr)
376
  binary_string = ''
377
-
378
  # Convert each sample to a binary digit
379
  for i in tqdm(range(start_sample, end_sample, int(sr * bit_duration))):
380
  signal_value = data[i:i + int(sr * bit_duration)]
@@ -383,7 +383,7 @@ def signal_to_binary_between_times(filename):
383
  binary_string += '0'
384
  else:
385
  binary_string += '1'
386
-
387
  # Find the start and end indices of the binary string
388
  index_start = binary_string.find("1000001")
389
  substrings = ["0111110", "011110"]
@@ -393,13 +393,13 @@ def signal_to_binary_between_times(filename):
393
  if index != -1:
394
  index_end = index
395
  break
396
-
397
  print("Binary String:", binary_string)
398
  binary_string_decoded = manchester_decoding(binary_string[index_start + 7:index_end])
399
-
400
  # Decode the binary string
401
  decoded_binary_string = decode_rs(binary_string_decoded, 20)
402
-
403
  return decoded_binary_string
404
  except Exception as e:
405
  # If an error occurs, return an error message
 
36
  Returns:
37
  tuple: The filter coefficients `b` and `a`.
38
  """
39
+ try:
40
  # Calculate the Nyquist frequency
41
  nyquist = 0.5 * sr
42
+
43
  # Normalize the cutoff frequencies
44
  low = low_frequency / nyquist
45
  high = high_frequency / nyquist
46
+
47
  # Design the Butterworth bandpass filter
48
  coefficient = butter(order, [low, high], btype='band')
49
+
50
  # Extract the filter coefficients
51
  b = coefficient[0]
52
  a = coefficient[1]
53
+
54
  return b, a
55
  except Exception as e:
56
  # If an error occurs, return an error message
 
72
  try:
73
  # Get the filter coefficients
74
  b, a = butter_bandpass(sr, order=order)
75
+
76
  # Apply the filter to the data
77
  y = lfilter(b, a, data)
78
+
79
  return y
80
  except Exception as e:
81
  # If an error occurs, return an error message
 
260
  try:
261
  # Perform a Fast Fourier Transform on the signal
262
  yf = fft(signal_value)
263
+
264
  # Generate the frequencies corresponding to the FFT coefficients
265
  xf = np.linspace(0.0, sample_rate / 2.0, len(signal_value) // 2)
266
+
267
  # Find the peaks in the absolute values of the FFT coefficients
268
  peaks, _ = find_peaks(np.abs(yf[0:len(signal_value) // 2]))
269
+
270
  # Return the frequency corresponding to the peak with the highest amplitude
271
  return xf[peaks[np.argmax(np.abs(yf[0:len(signal_value) // 2][peaks]))]]
272
  except Exception as e:
 
287
  try:
288
  # Convert each 8-bit binary number to a character and join them together
289
  return ''.join(chr(int(binary[i:i + 8], 2)) for i in range(0, len(binary), 8))
290
+ except ValueError:
291
  # If an error occurs, return an error message
292
+ return f"Error: Invalid binary number '{binary}'"
293
 
294
 
295
  def decode_rs(binary_string, ecc_bytes):
 
306
  try:
307
  # Convert the binary string to a bytearray
308
  byte_data = bytearray(int(binary_string[i:i + 8], 2) for i in range(0, len(binary_string), 8))
309
+
310
  # Initialize a Reed-Solomon codec
311
  rs = reedsolo.RSCodec(ecc_bytes)
312
+
313
  # Decode the bytearray
314
  corrected_data_tuple = rs.decode(byte_data)
315
  corrected_data = corrected_data_tuple[0]
316
+
317
  # Remove trailing null bytes
318
  corrected_data = corrected_data.rstrip(b'\x00')
319
+
320
  # Convert the bytearray back to a binary string
321
  corrected_binary_string = ''.join(format(byte, '08b') for byte in corrected_data)
322
+
323
  return corrected_binary_string
324
  except Exception as e:
325
  # If an error occurs, return an error message
 
336
  Returns:
337
  str: The decoded binary string.
338
  """
339
+ try:
340
  decoded_string = ''
341
  for i in tqdm(range(0, len(binary_string), 2), desc="Decoding"):
342
  if i + 1 < len(binary_string):
 
366
  try:
367
  # Get the start and end times of the signal of interest
368
  start_time, end_time = frame_analyse(filename)
369
+
370
  # Read the audio file
371
  sr, data = read(filename)
372
+
373
  # Calculate the start and end samples of the signal of interest
374
  start_sample = int((start_time - 0.007) * sr)
375
  end_sample = int((end_time - 0.007) * sr)
376
  binary_string = ''
377
+
378
  # Convert each sample to a binary digit
379
  for i in tqdm(range(start_sample, end_sample, int(sr * bit_duration))):
380
  signal_value = data[i:i + int(sr * bit_duration)]
 
383
  binary_string += '0'
384
  else:
385
  binary_string += '1'
386
+
387
  # Find the start and end indices of the binary string
388
  index_start = binary_string.find("1000001")
389
  substrings = ["0111110", "011110"]
 
393
  if index != -1:
394
  index_end = index
395
  break
396
+
397
  print("Binary String:", binary_string)
398
  binary_string_decoded = manchester_decoding(binary_string[index_start + 7:index_end])
399
+
400
  # Decode the binary string
401
  decoded_binary_string = decode_rs(binary_string_decoded, 20)
402
+
403
  return decoded_binary_string
404
  except Exception as e:
405
  # If an error occurs, return an error message