小智 56
Python提供了几个api来相当快地完成这个.我从这个链接下载sheep-bleats wav文件.您可以将其保存在桌面上以及cd终端内.python提示中的这些行应该足够了:(省略>>>)
import matplotlib.pyplot as plt
from scipy.fftpack import fft
from scipy.io import wavfile # get the api
fs, data = wavfile.read('test.wav') # load the data
a = data.T[0] # this is a two channel soundtrack, I get the first track
b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
c = fft(b) # calculate fourier transform (complex numbers list)
d = len(c)/2 # you only need half of the fft list (real signal symmetry)
plt.plot(abs(c[:(d-1)]),'r')
plt.show()
Run Code Online (Sandbox Code Playgroud)
以下是输入信号的图表:

这是频谱

要获得正确的输出,您必须将其转换xlabel为频谱图的频率.
k = arange(len(data))
T = len(data)/fs # where fs is the sampling frequency
frqLabel = k/T
Run Code Online (Sandbox Code Playgroud)
如果你必须处理一堆文件,你可以将它作为一个函数来实现:把这些行放在test2.py:
import matplotlib.pyplot as plt
from scipy.io import wavfile # get the api
from scipy.fftpack import fft
from pylab import *
def f(filename):
fs, data = wavfile.read(filename) # load the data
a = data.T[0] # this is a two channel soundtrack, I get the first track
b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
c = fft(b) # create a list of complex number
d = len(c)/2 # you only need half of the fft list
plt.plot(abs(c[:(d-1)]),'r')
savefig(filename+'.png',bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
说,我test.wav和test2.wav在当前工作目录,在下面的命令python提示界面是足够:进口TEST2地图(test2.f,["test.wav","test2.wav"])
假设您有100个这样的文件并且您不想单独键入它们的名称,则需要glob包:
import glob
import test2
files = glob.glob('./*.wav')
for ele in files:
f(ele)
quit()
Run Code Online (Sandbox Code Playgroud)
getparams如果.wav文件不是同一位,则需要在test2.f中添加.
您可以使用以下代码进行转换:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import scipy.io.wavfile as wavfile
import scipy
import scipy.fftpack
import numpy as np
from matplotlib import pyplot as plt
fs_rate, signal = wavfile.read("output.wav")
print ("Frequency sampling", fs_rate)
l_audio = len(signal.shape)
print ("Channels", l_audio)
if l_audio == 2:
signal = signal.sum(axis=1) / 2
N = signal.shape[0]
print ("Complete Samplings N", N)
secs = N / float(fs_rate)
print ("secs", secs)
Ts = 1.0/fs_rate # sampling interval in time
print ("Timestep between samples Ts", Ts)
t = scipy.arange(0, secs, Ts) # time vector as scipy arange field / numpy.ndarray
FFT = abs(scipy.fft(signal))
FFT_side = FFT[range(N/2)] # one side FFT range
freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])
fft_freqs = np.array(freqs)
freqs_side = freqs[range(N/2)] # one side frequency range
fft_freqs_side = np.array(freqs_side)
plt.subplot(311)
p1 = plt.plot(t, signal, "g") # plotting the signal
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(312)
p2 = plt.plot(freqs, FFT, "r") # plotting the complete fft spectrum
plt.xlabel('Frequency (Hz)')
plt.ylabel('Count dbl-sided')
plt.subplot(313)
p3 = plt.plot(freqs_side, abs(FFT_side), "b") # plotting the positive fft spectrum
plt.xlabel('Frequency (Hz)')
plt.ylabel('Count single-sided')
plt.show()
Run Code Online (Sandbox Code Playgroud)