fft_basics.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:ML 作者: saurabhsuman47 项目源码 文件源码
def generate_plot_fft(wave_filename, max_freq_plot = None):
    sample_rate, X = scipy.io.wavfile.read(wave_filename)
    duration = ((X.shape[0] / sample_rate))
    N = X.shape[0] # number of samples
    T = 1.0 / sample_rate# sample spacing
    Y = sp.fft(X) # calculate fft
    #factor expands the x scale
    factor = 1
    if max_freq_plot is not None:
        factor = 2*max_freq_plot*T
    xf = np.linspace(0.0, 1*factor/(2.0*T), N*factor/2) #get x axis points =  freq for plotting
    plt.plot(xf, 2.0/N * np.abs(Y[0:N*factor/2])) # plot x and y (number of y points should be equal to x)
    plt.grid()
    plt.show()

# without support max freq upto which it should be plotted option, it plots upto sample_rate/2 Hz
# simple to understand, first check this
#def generate_plot_fft(wave_filename):
#    sample_rate, X = scipy.io.wavfile.read(wave_filename)
#    duration = ((X.shape[0] / sample_rate))
#    Y = sp.fftpack.fft(X) # calculate fft
#    N = X.shape[0] # number of samples
#    T = 1.0 / sample_rate# sample spacing
#    xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
#    plt.plot(xf, 2.0/N * np.abs(Y[0:N/2]))
#    plt.grid()
#    plt.show()


# $ sox --null -r 22050 sine_a.wav synth 0.2 sine 400
# $ sox --null -r 22050 sine_b.wav synth 0.2 sine 3000
# $ sox --combine mix --volume 1 sine_b.wav --volume 0.5 sine_a.wav sine_mix.wav
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号