2014-12-04 3 views
3

wav 파일의 주파수 스펙트럼을 플로팅하려하지만 주파수 스펙트럼이 항상 다음과 같은 시간 도메인 신호와 일치하는 것처럼 보입니다.파이썬을 사용하여 wav 파일에서 fft 플로팅

import matplotlib.pyplot as plt 
import numpy as np 


def plot(data): 
    plt.plot(data, color='steelblue') 
    plt.figure() 
    plt.show() 

rate, wav_data = wavfile.read("audio_self/on/on.wav") 
plot(wav_data) 
plot(np.abs(np.fft.fft(wav_data))) 

내가 잘못하고 있나?

+0

나머지 데이터가 보이지 않게 크기가 조정되는 큰 제 1 스파이크가있을 수 있습니다. FFT를 다른 색상으로 플로팅 해보십시오. –

+1

'plotWav'를 호출했지만'plot'을 정의했음을 주목하십시오. 이 외에도 코드가 작동해야합니다. 또한 오디오 파일은 모노 여야합니다. –

답변

2

스테레오 트랙을 왼쪽 및 오른쪽 채널로 분리하고 각각 별도의 그래프를 사용하려면 Frank Zalkow가 말한 것처럼 트랙을 모노로 설정하지 않으면 훨씬 더 정확한 읽기가됩니다. 스테레오 트랙을 왼쪽 및 오른쪽 채널로 분리하는 방법입니다.

""" 
Plot 
""" 
#Plots a stereo .wav file 
#Decibels on the y-axis 
#Frequency Hz on the x-axis 

import matplotlib.pyplot as plt 
import numpy as np 

from pylab import* 
from scipy.io import wavfile 


def plot(file_name): 

    sampFreq, snd = wavfile.read(file_name) 

    snd = snd/(2.**15) #convert sound array to float pt. values 

    s1 = snd[:,0] #left channel 

    s2 = snd[:,1] #right channel 

    n = len(s1) 
    p = fft(s1) # take the fourier transform of left channel 

    m = len(s2) 
    p2 = fft(s2) # take the fourier transform of right channel 

    nUniquePts = ceil((n+1)/2.0) 
    p = p[0:nUniquePts] 
    p = abs(p) 

    mUniquePts = ceil((m+1)/2.0) 
    p2 = p2[0:mUniquePts] 
    p2 = abs(p2) 

''' 
Left Channel 
''' 
    p = p/float(n) # scale by the number of points so that 
      # the magnitude does not depend on the length 
      # of the signal or on its sampling frequency 
    p = p**2 # square it to get the power 




# multiply by two (see technical document for details) 
# odd nfft excludes Nyquist point 
    if n % 2 > 0: # we've got odd number of points fft 
     p[1:len(p)] = p[1:len(p)] * 2 
    else: 
     p[1:len(p) -1] = p[1:len(p) - 1] * 2 # we've got even number of points fft 

    freqArray = arange(0, nUniquePts, 1.0) * (sampFreq/n); 
    plt.plot(freqArray/1000, 10*log10(p), color='k') 
    plt.xlabel('LeftChannel_Frequency (kHz)') 
    plt.ylabel('LeftChannel_Power (dB)') 
    plt.show() 

''' 
Right Channel 
''' 
    p2 = p2/float(m) # scale by the number of points so that 
      # the magnitude does not depend on the length 
      # of the signal or on its sampling frequency 
    p2 = p2**2 # square it to get the power 




# multiply by two (see technical document for details) 
# odd nfft excludes Nyquist point 
    if m % 2 > 0: # we've got odd number of points fft 
     p2[1:len(p2)] = p2[1:len(p2)] * 2 
    else: 
     p2[1:len(p2) -1] = p2[1:len(p2) - 1] * 2 # we've got even number of points fft 

    freqArray2 = arange(0, mUniquePts, 1.0) * (sampFreq/m); 
    plt.plot(freqArray2/1000, 10*log10(p2), color='k') 
    plt.xlabel('RightChannel_Frequency (kHz)') 
    plt.ylabel('RightChannel_Power (dB)') 
    plt.show() 

이 정보가 도움이되기를 바랍니다.

관련 문제