Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import scipy.io.wavfile as wav
- from scipy.signal import butter, sosfilt
- def filter_frequency(input_file, output_file, freq_to_remove, order=5):
- """
- Filters a specific frequency from a WAV file.
- Args:
- input_file (str): Path to the input WAV file.
- output_file (str): Path to save the filtered WAV file.
- freq_to_remove (float): Frequency to remove in Hz.
- order (int): Order of the Butterworth filter (default: 5).
- """
- # Read the WAV file
- fs, data = wav.read(input_file)
- # Design a bandstop filter
- nyquist = 0.5 * fs
- low = (freq_to_remove - 5) / nyquist
- high = (freq_to_remove + 5) / nyquist
- sos = butter(order, [low, high], btype='bandstop', analog=False, output='sos')
- # Apply the filter
- filtered_data = sosfilt(sos, data)
- # Write the filtered data to a new WAV file
- wav.write(output_file, fs, filtered_data.astype(np.int16))
- if __name__ == "__main__":
- input_file = "your_audio_file.wav"
- output_file = "filtered_audio.wav"
- freq_to_remove = 60 # Example: remove 60 Hz hum
- filter_frequency(input_file, output_file, freq_to_remove)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement