计算wav文件的FFT时遇到的问题
Problems faced during calculating FFT of wav files
我一直在寻找 .wav 文件的 FFT 变换。我的初始程序是(对于 AMplitude - TIme Plot)
data_dir = 'C:/Users/asus/Desktop/Song_Test/Split/Done1.wav'
audio1, sfreq = lr.load(data_dir)
len(audio1), sfreq
Duration = len(audio1)/sfreq
print(Duration, " seconds")
time = np.arange(0, len(audio1)) / sfreq
fig, ax = plt.subplots()
ax.plot(time, audio1)
ax.set(xlabel='Time (s)', ylabel='Sound Amplitude')
plt.show()
这是我到目前为止编写的函数。
import scipy
def fft_plot(audio, sampling_rate):
n = int(len(audio))
T = 1 / sampling_rate
yf = scipy.fft.fft(audio)
print(n, T)
xf = np.linspace(0.0, 1.0/(2.0*T), n/2.0)
fig, ax = plt.subplot()
ax.plot(xf, 2.0/n * np.abs(yf[:n//2]))
plt.grid()
plt.xlabel("Freq")
plt.ylabel("Magnitude")
return plt.show()
我调用这个模块的那一刻,使用fft_plot(audio1, sfreq)
弹出如下错误
Traceback (most recent call last):
File "C:\Users\asus\anaconda3\envs\untitled\lib\site-packages\numpy\core\function_base.py", line 117, in linspace
num = operator.index(num)
TypeError: 'float' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/asus/PycharmProjects/untitled/Librosa_level2.py", line 92, in <module>
fft_plot(audio1, sfreq)
File "C:/Users/asus/PycharmProjects/untitled/Librosa_level2.py", line 59, in fft_plot
xf = np.linspace(0.0, 1.0/(2.0*T), n//2.0)
File "<__array_function__ internals>", line 6, in linspace
File "C:\Users\asus\anaconda3\envs\untitled\lib\site-packages\numpy\core\function_base.py", line 121, in linspace
.format(type(num)))
TypeError: object of type <class 'float'> cannot be safely interpreted as an integer.
我该如何解决这个浮动问题,请帮助我?
第三个参数:
xf = np.linspace(0.0, 1.0/(2.0*T), n/2.0)
即n / 2.0
应该是一个整数:
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
查看 docs 了解详情。
您的 n
是一个整数,但是当您除以 2.0
时,您可以得到分数(实数)。在 Python 术语(以及绝大多数其他编程语言)中,如果将整数除以浮点数,您将始终得到一个浮点数。
解决方案
确保传递的是偶数,例如:
if n % 2 == 0:
pass # Even
else:
n -= 1
我一直在寻找 .wav 文件的 FFT 变换。我的初始程序是(对于 AMplitude - TIme Plot)
data_dir = 'C:/Users/asus/Desktop/Song_Test/Split/Done1.wav'
audio1, sfreq = lr.load(data_dir)
len(audio1), sfreq
Duration = len(audio1)/sfreq
print(Duration, " seconds")
time = np.arange(0, len(audio1)) / sfreq
fig, ax = plt.subplots()
ax.plot(time, audio1)
ax.set(xlabel='Time (s)', ylabel='Sound Amplitude')
plt.show()
这是我到目前为止编写的函数。
import scipy
def fft_plot(audio, sampling_rate):
n = int(len(audio))
T = 1 / sampling_rate
yf = scipy.fft.fft(audio)
print(n, T)
xf = np.linspace(0.0, 1.0/(2.0*T), n/2.0)
fig, ax = plt.subplot()
ax.plot(xf, 2.0/n * np.abs(yf[:n//2]))
plt.grid()
plt.xlabel("Freq")
plt.ylabel("Magnitude")
return plt.show()
我调用这个模块的那一刻,使用fft_plot(audio1, sfreq)
弹出如下错误
Traceback (most recent call last):
File "C:\Users\asus\anaconda3\envs\untitled\lib\site-packages\numpy\core\function_base.py", line 117, in linspace
num = operator.index(num)
TypeError: 'float' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/asus/PycharmProjects/untitled/Librosa_level2.py", line 92, in <module>
fft_plot(audio1, sfreq)
File "C:/Users/asus/PycharmProjects/untitled/Librosa_level2.py", line 59, in fft_plot
xf = np.linspace(0.0, 1.0/(2.0*T), n//2.0)
File "<__array_function__ internals>", line 6, in linspace
File "C:\Users\asus\anaconda3\envs\untitled\lib\site-packages\numpy\core\function_base.py", line 121, in linspace
.format(type(num)))
TypeError: object of type <class 'float'> cannot be safely interpreted as an integer.
我该如何解决这个浮动问题,请帮助我?
第三个参数:
xf = np.linspace(0.0, 1.0/(2.0*T), n/2.0)
即n / 2.0
应该是一个整数:
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
查看 docs 了解详情。
您的 n
是一个整数,但是当您除以 2.0
时,您可以得到分数(实数)。在 Python 术语(以及绝大多数其他编程语言)中,如果将整数除以浮点数,您将始终得到一个浮点数。
解决方案
确保传递的是偶数,例如:
if n % 2 == 0:
pass # Even
else:
n -= 1