Awell00 commited on
Commit
a22641b
·
1 Parent(s): 693f6e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -41
app.py CHANGED
@@ -23,29 +23,6 @@ sample_rate = 44100
23
  amplitude_scaling_factor = 10.0
24
 
25
 
26
- import numpy as np
27
- from scipy.io.wavfile import write
28
- from scipy.signal import find_peaks
29
- from scipy.fft import fft
30
- from tqdm import tqdm
31
- import time
32
- import matplotlib.pyplot as plt
33
- from scipy.io.wavfile import read
34
- from scipy import signal
35
- import gradio as gr
36
- import reedsolo
37
- import wavio
38
- from scipy.signal import butter, lfilter
39
-
40
- # ---------------Parameters--------------- #
41
-
42
- low_frequency = 18000
43
- high_frequency = 19000
44
- bit_duration = 0.007
45
- sample_rate = 44100
46
- amplitude_scaling_factor = 10.0
47
-
48
-
49
  # -----------------Record----------------- #
50
 
51
  def record(audio):
@@ -81,34 +58,77 @@ def record(audio):
81
 
82
  # -----------------Filter----------------- #
83
 
84
- def butter_bandpass(lowcut, highcut, sr, order=5):
 
 
 
 
 
 
 
 
 
 
 
85
  nyquist = 0.5 * sr
86
- low = lowcut / nyquist
87
- high = highcut / nyquist
88
- coef = butter(order, [low, high], btype='band')
89
- b = coef[0]
90
- a = coef[1]
 
 
 
 
 
 
 
91
  return b, a
92
 
93
 
94
- def butter_bandpass_filter(data, lowcut, highcut, sr, order=5):
95
- b, a = butter_bandpass(lowcut, highcut, sr, order=order)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  y = lfilter(b, a, data)
 
97
  return y
98
 
99
 
100
- def main():
101
- input_file = 'recorded.wav'
102
- output_file = 'output_filtered_receiver.wav'
103
- lowcut = 17500
104
- highcut = 19500
 
 
 
 
 
 
 
 
 
105
 
106
- sr, data = read(input_file)
 
107
 
108
- filtered_data = butter_bandpass_filter(data, lowcut, highcut, sr)
109
- write(output_file, sr, np.int16(filtered_data))
110
- return "Filtered Audio Generated"
111
-
112
 
113
  # -----------------Frame----------------- #
114
 
 
23
  amplitude_scaling_factor = 10.0
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # -----------------Record----------------- #
27
 
28
  def record(audio):
 
58
 
59
  # -----------------Filter----------------- #
60
 
61
+ def butter_bandpass(sr, order=5):
62
+ """
63
+ This function designs a Butterworth bandpass filter.
64
+
65
+ Parameters:
66
+ sr (int): The sample rate of the audio.
67
+ order (int): The order of the filter.
68
+
69
+ Returns:
70
+ tuple: The filter coefficients `b` and `a`.
71
+ """
72
+ # Calculate the Nyquist frequency
73
  nyquist = 0.5 * sr
74
+
75
+ # Normalize the cutoff frequencies
76
+ low = low_frequency / nyquist
77
+ high = high_frequency / nyquist
78
+
79
+ # Design the Butterworth bandpass filter
80
+ coefficient = butter(order, [low, high], btype='band')
81
+
82
+ # Extract the filter coefficients
83
+ b = coefficient[0]
84
+ a = coefficient[1]
85
+
86
  return b, a
87
 
88
 
89
+ def butter_bandpass_filter(data, sr, order=5):
90
+ """
91
+ This function applies the Butterworth bandpass filter to a given data.
92
+
93
+ Parameters:
94
+ data (array): The audio data to be filtered.
95
+ sr (int): The sample rate of the audio.
96
+ order (int): The order of the filter.
97
+
98
+ Returns:
99
+ array: The filtered audio data.
100
+ """
101
+ # Get the filter coefficients
102
+ b, a = butter_bandpass(sr, order=order)
103
+
104
+ # Apply the filter to the data
105
  y = lfilter(b, a, data)
106
+
107
  return y
108
 
109
 
110
+ def filtered():
111
+ """
112
+ This function reads an audio file, applies the bandpass filter to the audio data,
113
+ and then writes the filtered data to an output file.
114
+
115
+ Returns:
116
+ str: A success message if the audio is filtered correctly, otherwise an error message.
117
+ """
118
+ try:
119
+ # Read the audio data from the input file
120
+ sr, data = read(input_file)
121
+
122
+ # Apply the bandpass filter to the audio data
123
+ filtered_data = butter_bandpass_filter(data, sr)
124
 
125
+ # Write the filtered data to the output file
126
+ write(output_file, sr, np.int16(filtered_data))
127
 
128
+ return "Filtered Audio Generated"
129
+ except Exception as e:
130
+ # If an error occurs, return an error message
131
+ return f"Error: {str(e)}"
132
 
133
  # -----------------Frame----------------- #
134