Ask Your Question
1

What is the method for altering the frequency range of a wav file from 20Hz to 2KHz using FFT in Python3?

asked 2022-08-25 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-12 19:00:00 +0000

david gravatar image

Here is the Python code for altering the frequency range of a wav file from 20Hz to 2KHz using FFT:

import numpy as np
import scipy.io.wavfile as wav

# Read the input wav file
fs, data = wav.read('input.wav')

# Compute the FFT of the audio signal
fft_out = np.fft.fft(data)

# Compute the frequency range of the FFT output
freqs = np.fft.fftfreq(len(fft_out))

# Compute the indexes of the frequency range of interest
idxs = np.where((freqs >= 20) & (freqs <= 2000))[0]

# Create a new array and copy the selected frequencies to it
new_fft_out = np.zeros(len(fft_out), dtype=np.complex)
new_fft_out[idxs] = fft_out[idxs]

# Compute the inverse FFT of the modified signal
new_data = np.fft.ifft(new_fft_out)

# Write the output to a new wav file
wav.write('output.wav', fs, np.int16(new_data.real))

Note that the code assumes the input wav file has a single channel. If the file has multiple channels, you need to process each channel separately.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-25 11:00:00 +0000

Seen: 7 times

Last updated: Feb 12 '22