python - Spectrogram of a wave file -
i trying obtain spectrogram of wav
file in python. gives error:
'module' object has no attribute 'spectrogram'.
here code :
import scipy.io.wavfile scipy.io.wavfile import read scipy import signal sr_value, x_value = scipy.io.wavfile.read("test.wav") f, t, sxx= signal.spectrogram(x_value,sr_value)
is there way obtain spectrogram of wav
file?
using scipy.fftpack
can plot fft
contents spectrogram.
** based on old posting **
sample code below.
"""plots time in ms vs amplitude in db of input wav signal """ import numpy import matplotlib.pyplot plt import pylab scipy.io import wavfile scipy.fftpack import fft myaudio = "audio.wav" #read file , sampling freq [ 44100 hz ] , sound object samplingfreq, mysound = wavfile.read(myaudio) #check if wave file 16bit or 32 bit. 24bit not supported mysounddatatype = mysound.dtype #we can convert our sound array floating point values ranging -1 1 follows mysound = mysound / (2.**15) #check sample points , sound channel duel channel(5060, 2) or (5060, ) mono channel mysoundshape = mysound.shape samplepoints = float(mysound.shape[0]) #get duration of sound file signalduration = mysound.shape[0] / samplingfreq #if 2 channels, select 1 channel mysoundonechannel = mysound[:,0] #plotting tone # can represent sound plotting pressure values against time axis. #create array of sample point in 1 dimension timearray = numpy.arange(0, samplepoints, 1) # timearray = timearray / samplingfreq #scale milliseconds timearray = timearray * 1000 #plot tone plt.plot(timearray, mysoundonechannel, color='g') plt.xlabel('time (ms)') plt.ylabel('amplitude') plt.show() #plot frequency content #we can frquency amplitude , time using fft , fast fourier transform algorithm #get length of mysound object array mysoundlength = len(mysound) #take fourier transformation on given sample point #fftarray = fft(mysound) fftarray = fft(mysoundonechannel) numuniquepoints = numpy.ceil((mysoundlength + 1) / 2.0) fftarray = fftarray[0:numuniquepoints] #fft contains both magnitude , phase , given in complex numbers in real + imaginary parts (a + ib) format. #by taking absolute value , real part fftarray = abs(fftarray) #scale fft array length of sample points magnitude not depend on #the length of signal or on sampling frequency fftarray = fftarray / float(mysoundlength) #fft has both positive , negative information. square positive fftarray = fftarray **2 #multiply 2 (research why?) #odd nfft excludes nyquist point if mysoundlength % 2 > 0: #we've got odd number of points in fft fftarray[1:len(fftarray)] = fftarray[1:len(fftarray)] * 2 else: #we've got number of points in fft fftarray[1:len(fftarray) -1] = fftarray[1:len(fftarray) -1] * 2 freqarray = numpy.arange(0, numuniquepoints, 1.0) * (samplingfreq / mysoundlength); #plot frequency plt.plot(freqarray/1000, 10 * numpy.log10 (fftarray), color='b') plt.xlabel('frequency (khz)') plt.ylabel('power (db)') plt.show() #get list of element in frequency array #print freqarray.dtype.type freqarraylength = len(freqarray) print "freqarraylength =", freqarraylength numpy.savetxt("freqdata.txt", freqarray, fmt='%6.2f') #print fftarray information print "fftarray length =", len(fftarray) numpy.savetxt("fftdata.txt", fftarray)
Comments
Post a Comment