Awell00 commited on
Commit
0345aaf
·
1 Parent(s): 85c9881

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -54
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
- # 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
 
 
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