Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -59,76 +59,30 @@ def record(audio):
|
|
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 |
-
|
80 |
-
|
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 |
-
|
113 |
-
and then writes the filtered data to an output file.
|
114 |
|
115 |
-
|
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 |
-
|
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 |
|
|
|
59 |
# -----------------Filter----------------- #
|
60 |
|
61 |
def butter_bandpass(sr, order=5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
nyquist = 0.5 * sr
|
|
|
|
|
63 |
low = low_frequency / nyquist
|
64 |
high = high_frequency / nyquist
|
65 |
+
coef = butter(order, [low, high], btype='band')
|
66 |
+
b = coef[0]
|
67 |
+
a = coef[1]
|
|
|
|
|
|
|
|
|
|
|
68 |
return b, a
|
69 |
|
70 |
|
71 |
def butter_bandpass_filter(data, sr, order=5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
b, a = butter_bandpass(sr, order=order)
|
|
|
|
|
73 |
y = lfilter(b, a, data)
|
|
|
74 |
return y
|
75 |
|
76 |
|
77 |
def filtered():
|
78 |
+
input_file = 'recorded.wav'
|
79 |
+
output_file = 'output_filtered_receiver.wav'
|
|
|
80 |
|
81 |
+
sr, data = read(input_file)
|
|
|
|
|
|
|
|
|
|
|
82 |
|
|
|
83 |
filtered_data = butter_bandpass_filter(data, sr)
|
84 |
+
write(output_file, sr, np.int16(filtered_data))
|
85 |
+
return "Filtered Audio Generated"
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# -----------------Frame----------------- #
|
88 |
|