Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.