我是靠谱客的博主 暴躁板栗,这篇文章主要介绍Python绘制wav文件频谱图,现在分享给大家,希望可以做个参考。

import wave
import struct,numpy
from scipy import *
from pylab import *
 
def Plot_fft_freq_chart(filename,plot=False):
	wavefile = wave.open(filename, 'r') # open for writing
	nchannels = wavefile.getnchannels()
	sample_width = wavefile.getsampwidth()
	framerate = wavefile.getframerate()
	numframes = wavefile.getnframes()
	  
	print("channel",nchannels)
	print("sample_width",sample_width)
	print("framerate",framerate)
	print("numframes",numframes)
	  
	y = numpy.zeros(numframes)
	  
	for i in range(numframes):
		val = wavefile.readframes(1)
		left = val[0:2]
		#right = val[2:4]
		v = struct.unpack('h', left )[0]
		y[i] = v
	  
	Fs = framerate
	try:
		data, freqs, bins, im = specgram(y, NFFT=1024, Fs=Fs, noverlap=900)
		mm=data[127]
		mm=10. * np.log10(mm+1e-4)
 
	except Exception as e:
		print("error is: ",e)
		return -50
 
	freq1khz_value=mean(mm)
	print(freq1khz_value)
	if plot:
		show()
	return freq1khz_value
 
 
filename1="test2/music_high.wav"
Plot_fft_freq_chart(filename1,True)

最后

以上就是暴躁板栗最近收集整理的关于Python绘制wav文件频谱图的全部内容,更多相关Python绘制wav文件频谱图内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(117)

评论列表共有 0 条评论

立即
投稿
返回
顶部