我需要獲取一個嘈雜的 .wav 音頻檔案并過濾掉所有的噪音。我必須使用傅里葉變換來做到這一點。經過幾天的研究和實驗,我終于做了一個作業功能,問題是它沒有按我的意愿作業。這是我制作的功能:
# Audio signal processing
from scipy.io.wavfile import read, write
import matplotlib.pyplot as plt
import numpy as np
from scipy.fft import fft, fftfreq, ifft
def AudioSignalProcessing(audio):
# Import the .wav format audio into two variables:
# sampling (int)
# audio signal (numpy array)
sampling, signal = read(audio)
# time duration of the audio
length = signal.shape[0] / sampling
# x axis based on the time duration
time = np.linspace(0., length, signal.shape[0])
# show original signal
plt.plot(time, signal)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Original signal")
plt.show()
# apply Fourier transform and normalize
transform = abs(fft(signal))
transform = transform/np.linalg.norm(transform)
# obtain frequencies
xf = fftfreq(transform.size, 1/sampling)
# show transformed signal (frequencies domain)
plt.plot(xf, transform)
plt.xlabel("Frecuency (Hz)")
plt.ylabel("Amplitude")
plt.title("Frequency domain signal")
plt.show()
# filter the transformed signal to a 40% of its maximum amplitude
threshold = np.amax(transform)*0.4
filtered = transform[np.where(transform > threshold)]
xf_filtered = xf[np.where(transform > threshold)]
# show filtered transformed signal
plt.plot(xf_filtered, filtered)
plt.xlabel("Frecuency (Hz)")
plt.ylabel("Amplitude")
plt.title("FILTERED time domain signal")
plt.show()
# transform the signal back to the time domain
filtrada = ifft(signal)
# show original signal filtered
plt.plot(time, filtrada)
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Filtered signal")
plt.show()
# convert audio signal to .wav format audio
# write(audio.replace(".wav", " filtrado.wav"), sampling, filtrada.astype(signal.dtype))
return None
AudioSignalProcessing("audio.wav")
這是輸出圖:
原始信號
轉換后的信號
濾波后的變換信號
過濾后的音頻信號
過濾后的頻率看起來不像我認為的那樣,在將過濾后的信號轉換回音頻后,它聽起來一點也不好。此外,我嘗試過使用不同的音頻,但會發生相同的濾波器失真。
uj5u.com熱心網友回復:
我建議在https://dsp.stackexchange.com/上詢問詳細的信號處理問題。
看起來您只想保留那些至少在最大分量的 40% 范圍內的頻率分量。如果是這樣的話:
保持 DFT 的復雜形式,否則你將無法轉換回來;所以
abs從行中洗掉transform = abs(fft(signal))。不要
np.where用來“保持”你想要的頻率;相反,將變換幅度低于閾值的位置設定為 0;就像是transform[abs(transform) < 0.4 * max(abs(transform))] = 0最后,將逆 DFT 應用于這個改變的變換;您已將其應用于
signal(請參見第 1 行filtrata = ifft(signal))。(在繪制 filtrada 時,您可能會收到有關丟棄虛值的警告。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443162.html
上一篇:大向量的外積超出記憶體
