Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -59,29 +59,75 @@ def record(audio):
|
|
59 |
# -----------------Filter----------------- #
|
60 |
|
61 |
def butter_bandpass(sr, order=5):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
nyquist = 0.5 * sr
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
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----------------- #
|
|
|
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 with a 500 Hz offset
|
76 |
+
low = (low_frequency - 500) / nyquist
|
77 |
+
high = (high_frequency + 500) / nyquist
|
78 |
+
|
79 |
+
# Design the Butterworth bandpass filter
|
80 |
coef = butter(order, [low, high], btype='band')
|
81 |
+
|
82 |
+
# Extract the filter coefficients
|
83 |
b = coef[0]
|
84 |
a = coef[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 |
+
# Define the input and output file paths
|
119 |
input_file = 'recorded.wav'
|
120 |
output_file = 'output_filtered_receiver.wav'
|
121 |
|
122 |
+
# Read the audio data from the input file
|
123 |
sr, data = read(input_file)
|
124 |
|
125 |
+
# Apply the bandpass filter to the audio data
|
126 |
filtered_data = butter_bandpass_filter(data, sr)
|
127 |
+
|
128 |
+
# Write the filtered data to the output file
|
129 |
write(output_file, sr, np.int16(filtered_data))
|
130 |
+
|
131 |
return "Filtered Audio Generated"
|
132 |
|
133 |
# -----------------Frame----------------- #
|