import numpy as np import os from scipy.io import wavfile WAV_FILE_NAME = 'my_audio.wav' lowcut = 1200.0 highcut = 1300.0 FRAME_RATE = 16000 def butter_bandpass(lowcut, highcut, fs, order=5): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') return b, a def butter_bandpass_filter(data, lowcut, highcut, fs, order=5): b, a = butter_bandpass(lowcut, highcut, fs, order=order) y = lfilter(b, a, data) return y def bandpass_filter(buffer): return butter_bandpass_filter(buffer, lowcut, highcut, FRAME_RATE, order=6) samplerate, data = wavfile.read(os.path.join(record_path, WAV_FILE_NAME)) assert samplerate == FRAME_RATE filtered = np.apply_along_axis(bandpass_filter, 0, data).astype('int16') wavfile.write(os.path.join(record_path, f'filtered_{WAV_FILE_NAME}'), samplerate, filtered)
Mã sau là để tạo bộ lọc thông qua băng tần từ đây: //scipy.github.io/old-wiki/pages/cookbook/butterworthbandpassPass
from scipy.signal import butter, lfilter def butter_bandpass(lowcut, highcut, fs, order=5): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') return b, a def butter_bandpass_filter(data, lowcut, highcut, fs, order=5): b, a = butter_bandpass(lowcut, highcut, fs, order=order) y = lfilter(b, a, data) return y if __name__ == "__main__": import numpy as np import matplotlib.pyplot as plt from scipy.signal import freqz # Sample rate and desired cutoff frequencies (in Hz). fs = 5000.0 lowcut = 500.0 highcut = 1250.0 # Plot the frequency response for a few different orders. plt.figure(1) plt.clf() for order in [3, 6, 9]: b, a = butter_bandpass(lowcut, highcut, fs, order=order) w, h = freqz(b, a, worN=2000) plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order) plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)], '--', label='sqrt(0.5)') plt.xlabel('Frequency (Hz)') plt.ylabel('Gain') plt.grid(True) plt.legend(loc='best') # Filter a noisy signal. T = 0.05 nsamples = T * fs t = np.linspace(0, T, nsamples, endpoint=False) a = 0.02 f0 = 600.0 x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t)) x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1) x += a * np.cos(2 * np.pi * f0 * t + .11) x += 0.03 * np.cos(2 * np.pi * 2000 * t) plt.figure(2) plt.clf() plt.plot(t, x, label='Noisy signal') y = butter_bandpass_filter(x, lowcut, highcut, fs, order=6) plt.plot(t, y, label='Filtered signal (%g Hz)' % f0) plt.xlabel('time (seconds)') plt.hlines([-a, a], 0, T, linestyles='--') plt.grid(True) plt.axis('tight') plt.legend(loc='upper left') plt.show()Xem nếu điều này giúp bạn nguyên nhân.
Bạn có thể chỉ định các tần số mong muốn ở đây:
# Sample rate and desired cutoff frequencies (in Hz). fs = 5000.0 lowcut = 500.0 highcut = 1250.0Bộ lọc thông thấp
Một bộ lọc thông thấp truyền tín hiệu thấp hơn tần số cắt cụ thể và làm giảm tín hiệu cao hơn tần số cắt.low-pass filter passes signals that are lower than a specific cutoff frequency and diminishes signals that are higher than the cutoff frequency.
Nói cách khác, bộ lọc thông thấp truyền tín hiệu có tần số thấp hơn tần số cắt, giữ một giá trị nhất định được chỉ định bởi người dùng. Tất cả các tín hiệu có tần số nhiều hơn tần số cắt được loại bỏ.
Một bộ lọc thông thấp sẽ loại bỏ các tần số cao nhất khỏi tín hiệu âm thanh của chúng tôi.
Bộ lọc thông cao
Một bộ lọc thông cao đối diện với bộ lọc thông thấp. Nó truyền tín hiệu với tần số cao hơn tần số cắt nhất định và loại bỏ các tín hiệu có tần số thấp hơn tần số cắt.high-pass filter is the opposite of a low-pass filter. It transmits signals with a frequency higher than a certain cutoff frequency and removes signals with a frequency lower than the cutoff frequency.
Một bộ lọc thông cao sẽ loại bỏ các tần số thấp nhất khỏi tín hiệu âm thanh của chúng tôi.
Một bộ lọc thông dải
Một bộ lọc thông dải có thể được hình thành bằng cách xếp tầng bộ lọc thông cao và bộ lọc thông thấp.band-pass filter can be formed by cascading a high-pass filter and a low-pass filter.
Nói cách khác, bộ lọc thông dải là bộ lọc truyền tần số trong phạm vi và từ chối tần số bên ngoài phạm vi đó.
Một bộ lọc thông dải loại bỏ cả tần số cao nhất và thấp nhất khỏi tín hiệu âm thanh của chúng tôi.
Một bộ lọc từ chối dải
Một bộ lọc từ chối dải là sự kết hợp song song của các bộ lọc thông thấp và thông cao.band-reject filter is a parallel combination of low-pass and high-pass filters.
Nói một cách đơn giản, nó từ chối tần số giữa giới hạn thấp hơn và giới hạn cao hơn và truyền các tần số khác.
Người đóng góp
Bản quyền © 2022 Giáo dục, Inc. Tất cả quyền được bảo lưu