def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ def energy(x): e = np.sum(np.abs(x)**2) return e fs, x = UF.wavread(inputFile) w = get_window(window, M, False) mX, pX = stft.stftAnal(x, w, N, H) y = stft.stftSynth(mX, pX, M, H) n = x - y[:x.size] n2 = x[w.size:-w.size] - y[:x.size][w.size:-w.size] SNR1 = 10 * np.log10(energy(y) / energy(n)) SNR2 = 10 * np.log10(energy(y) / energy(n2)) return SNR1, SNR2
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ ## your code here w = get_window(window, M, False) (fs, x) = UF.wavread(inputFile) (mX, pX) = stft.stftAnal(x, w, N, H) y = stft.stftSynth(mX, pX, M, H) noise = x-y[:x.size] # get energy of x enX1 = np.sum(abs(x)*abs(x)) enNoise = np.sum(abs(noise)*abs(noise)) enX2 = np.sum(abs(x[M:-M])*abs(x[M:-M])) enNoise2 = np.sum(abs(noise[M:-M])*abs(noise[M:-M])) SNR1 = 10*np.log10(enX1/enNoise) SNR2 = 10*np.log10(enX2/enNoise2) return (SNR1, SNR2)
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ ## your code here (fs,x) = UF.wavread(inputFile) w = get_window(window, M) mX, pX = stft.stftAnal(x, w, N, H) y = stft.stftSynth(mX, pX, M, H) n1 = x - y[:x.size] # Get to where signal lies in y: test the dimension of y n2 = x[M:-M] - y[:x.size][M:-M] def calculate_energy(x): e = np.sum(x**2) return e SNR1 = 10*np.log10(calculate_energy(x)/calculate_energy(n1)) SNR2 = 10*np.log10(calculate_energy(x)/calculate_energy(n2)) return (SNR1, SNR2)
def main(inputFile = '../../sounds/piano.wav', window = 'hamming', M = 1024, N = 1024, H = 512): """ analysis/synthesis using the STFT inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (choice of rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size N: fft size (power of two, bigger or equal than M) H: hop size (at least 1/2 of analysis window size to have good overlap-add) """ # read input sound (monophonic with sampling rate of 44100) fs, x = UF.wavread(inputFile) # compute analysis window w = get_window(window, M) # compute the magnitude and phase spectrogram mX, pX = STFT.stftAnal(x, fs, w, N, H) # perform the inverse stft y = STFT.stftSynth(mX, pX, M, H) # output sound file (monophonic with sampling rate of 44100) outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_stft.wav' # write the sound resulting from the inverse stft UF.wavwrite(y, fs, outputFile) return x, fs, mX, pX, y
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ ## your code here def energy(mag): e = np.sum((10 ** (mag / 20)) ** 2) return e (fs, x) = UF.wavread(inputFile) w = get_window(window, M) mX, pX = STFT.stftAnal(x, fs, w, N, H) y = STFT.stftSynth(mX, pX, M, H) n = x - y[:x.size] n2 = x[w.size:-w.size] - y[:x.size][w.size:-w.size] mN, pN = STFT.stftAnal(n, fs, w, N, H) mN2, pN2 = STFT.stftAnal(n2, fs, w, N, H) snr1 = 10 * np.log10(energy(mX) / energy(mN)) snr2 = 10 * np.log10(energy(mX) / energy(mN2)) return snr1, snr2
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ ## your code here def energy(mag): e = np.sum((10**(mag / 20))**2) return e (fs, x) = UF.wavread(inputFile) w = get_window(window, M) mX, pX = STFT.stftAnal(x, fs, w, N, H) y = STFT.stftSynth(mX, pX, M, H) n = x - y[:x.size] n2 = x[w.size:-w.size] - y[:x.size][w.size:-w.size] mN, pN = STFT.stftAnal(n, fs, w, N, H) mN2, pN2 = STFT.stftAnal(n2, fs, w, N, H) snr1 = 10 * np.log10(energy(mX) / energy(mN)) snr2 = 10 * np.log10(energy(mX) / energy(mN2)) return snr1, snr2
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ # Calculate the SNR after synthesis and analysis STFT w = get_window(window, M) # SNR (signal to noise ratio) = 10log10(Energy of signal / Energy of noise) (fs, x) = UF.wavread(inputFile) # Do analysis and synthesis mX, pX = stft.stftAnal(x, w, N, H) y = stft.stftSynth(mX, pX, M, H) # Resizing y so we can calculate energy of noise resized_y = y[:x.size] # Calculating the noise of part 1 and 2 noise1 = x - resized_y noise2 = x[w.size:-w.size] - resized_y[w.size:-w.size] # Analyse both noises mNoise1, pNoise1 = stft.stftAnal(noise1, w, N, H) mNoise2, pNoise2 = stft.stftAnal(noise2, w, N, H) energyInput = energy_computation(mX) energyNoise1 = energy_computation(mNoise1) energyNoise2 = energy_computation(mNoise2) SNR1 = 10 * np.log10(energyInput / energyNoise1) SNR2 = 10 * np.log10(energyInput / energyNoise2) return SNR1, SNR2
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function returns a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ w = get_window(window, M) fs, x = wavread(inputFile) mX, pX = stft.stftAnal(x, fs, w, N, H) y = stft.stftSynth(mX, pX, M, H) y = y[:len(x)] noise = y - x X = abs(fft(x)) Y = abs(fft(noise)) X[X < eps] = eps Y[Y < eps] = eps Es1 = En1 = 0 for k in range(0, len(x)): Es1 = Es1 + X[k] * X[k] En1 = En1 + Y[k] * Y[k] SNR1 = 10 * np.log10(float(Es1) / En1) x2 = x[M:len(x) - M] y2 = y[M:len(y) - M] noise2 = y2 - x2 X2 = abs(fft(x2)) Y2 = abs(fft(noise2)) X2[X2 < eps] = eps Y2[Y2 < eps] = eps Es2 = En2 = 0 for k in range(0, len(x2)): Es2 = Es2 + X2[k] * X2[k] En2 = En2 + Y2[k] * Y2[k] SNR2 = 10 * np.log10(float(Es2) / En2) return (SNR1, SNR2)
def computeSNR(inputFile, window, M, N, H): """ Input: inputFile (string): wav file name including the path window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window length (odd positive integer) N (integer): fft size (power of two, > M) H (integer): hop size for the stft computation Output: The function should return a python tuple of both the SNR values (SNR1, SNR2) SNR1 and SNR2 are floats. """ ## your code here fs, x = UF.wavread(inputFile) #x1 = x #x2 = x[M:-M] w = get_window(window, M) (mX, pX) = stft.stftAnal(x, fs, w, N, H) y = stft.stftSynth(mX, pX, M, H) noise = x - y[:x.size] return (snr(x, noise), snr(x[M:-M], noise[M:-M]))
import time, os, sys sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) import stft as STFT import utilFunctions as UF import matplotlib.pyplot as plt from scipy.signal import hamming (fs, x) = UF.wavread('../../../sounds/piano.wav') w = np.hamming(1024) N = 1024 H = 512 mX, pX = STFT.stftAnal(x, w, N, H) y = STFT.stftSynth(mX, pX, w.size, H) plt.figure(1, figsize=(9.5, 7)) plt.subplot(411) plt.plot(np.arange(x.size)/float(fs), x, 'b') plt.title('x (piano.wav)') plt.axis([0,x.size/float(fs),min(x),max(x)]) plt.subplot(412) numFrames = int(mX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = np.arange(mX[0,:].size)*float(fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(mX)) plt.title('mX, M=1024, N=1024, H=512') plt.autoscale(tight=True)
def computeEngEnv(inputFile, window, M, N, H): """ Inputs: inputFile (string): input sound file (monophonic with sampling rate of 44100) window (string): analysis window type (choice of rectangular, triangular, hanning, hamming, blackman, blackmanharris) M (integer): analysis window size (odd positive integer) N (integer): FFT size (power of 2, such that N > M) H (integer): hop size for the stft computation Output: The function should return a numpy array engEnv with shape Kx2, K = Number of frames containing energy envelop of the signal in decibles (dB) scale engEnv[:,0]: Energy envelope in band 0 < f < 3000 Hz (in dB) engEnv[:,1]: Energy envelope in band 3000 < f < 10000 Hz (in dB) """ ### your code here F1 = 3000 F2 = 10000 (fs, x) = UF.wavread(inputFile) w = get_window(window, M) mX, pX = stft.stftAnal(x, fs, w, N, H) y = stft.stftSynth(mX, pX, M, H) print "length x = " + str(x.size) + " Shape mX = " np.shape(mX) #print mX[0] mX = np.power(10,(mX/20.0)) # de-convert from decibel # Note that mX is an array of DFT frames corresponding to each frame in the entire signal. # Each ROW of mX is a DFT of the particular frame. Toral numebr of rows is the total number of frames. # Find number of bins in the region 0 - 3k, f = k*Fs/N k0 = 0 k1 = np.ceil(F1*N/fs) # Bin number/index of first limit k2 = np.ceil(F2*N/fs) print "Bin 1 =" + str(k1) + " Bin 2 =" + str(k2) R1 = [k0,k1] R2 = [k1,k2] numFrames = int(mX[:,0].size) print "Number of Frames = " + str(numFrames) # Find the energy in the spectrum f > 0 and <3k e1 = np.zeros(numFrames) e2 = np.zeros(numFrames) engEnv = np.zeros((numFrames,2)) for i in range(0,numFrames): e1[i] = np.sum( mX[i,k0+1:k1+1] * mX[i,k0+1:k1+1]) e2[i] = np.sum( mX[i,k1+1:k2+1] * mX[i,k1+1:k2+1]) # Convert back to dB e1 = 10*np.log10(e1) e2 = 10*np.log10(e2) mX = 20*np.log10(mX) engEnv[0:numFrames,0] = e1[:] engEnv[0:numFrames,1] = e2[:] # By looking at the plot you can see the frame numbers that have more <less 3k or 10k frequency content. # And by knowign the frame number, you can tell the time location of the change in frequency plt.plot(e1) plt.plot(e2) return(engEnv)
def residue_lpc(audio_inp, params,lpc_order): """ Obtains the LPC representation of the Residual Spectral(LPC envelope), and then generates the residual by IFFT'ing this representation with random phase. Parameters ---------- audio_inp : np.array Numpy array containing the audio signal, in the time domain params : dict Parameter dictionary for the sine model) containing the following keys - fs : Sampling rate of the audio - W : Window size(number of frames) - N : FFT size(multiple of 2) - H : Hop size - t : Threshold for sinusoidal detection in dB - maxnSines : Number of sinusoids to detect lpc_order : integer Number of coefficients in the LPC representation Returns ------- res_transformed : np.array Returns the transformed residue(LPC envelope approximation) in the time domain """ fs = params['fs'] W = params['W'] N = params['N'] H = params['H'] t = params['t'] maxnSines = params['maxnSines'] w = windows.hann(W) F,M,P,R = hprModelAnal(x = audio_inp, fs = fs, w = w, N = N, H = H, t = t, nH = maxnSines, minSineDur = 0.02, minf0 = 10, maxf0 = 400, f0et = 5, harmDevSlope = 0.01) harmonics_recon = sineModelSynth(tfreq = F, tmag = M, tphase = P, N = W, H = H, fs = fs) # Initializing an empty list to store the residual spectral approximations(LPC) xmX = [] # Normalize the Residue before analysis(throws a np zero error otherwise) nf = np.max(np.abs(R)) # nf = 1 # print(nf) R = R/nf for frame in ess.FrameGenerator(R.astype('float32'), W, H): inp = np.pad(frame,[0,N - W],mode = 'constant',constant_values=(0, 0)) env_frame = fe.lpc_envelope(inp,lpc_order,fs,len(inp)//2 + 1) xmX.append(env_frame) xmX = np.array(xmX) XpX = 2*np.pi*np.random.rand(xmX.shape[0],xmX.shape[1]) # xmX,XpX = stftAnal(audio_inp,w,N,H) # Obtain the audio from the above representation res_transformed = stftSynth(xmX, XpX, W, H)*nf # ***Re-normalize the Residual so that it lies in the same range as the original residue*** # scale_init = np.max(np.abs(audio_inp))/np.max(np.abs(R)) # scale_final = np.max(np.abs(harmonics_recon))/scale_init res_transformed = (res_transformed/np.max(np.abs(res_transformed))) return res_transformed
import os import sys import numpy as np import math from scipy.signal import get_window import matplotlib.pyplot as plt sys.path.append( os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../software/models/')) import stft import utilFunctions as UF eps = np.finfo(float).eps (fs, x) = UF.wavread(inputFile) w = get_window(window, M) mX, pX = stft.stftAnal(x, w, N, H) y = stft.stftSynth(mX, pX, M, H)
sys.path.append( os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../software/models/')) import stft as STFT import utilFunctions as UF import matplotlib.pyplot as plt from scipy.signal import hamming (fs, x) = UF.wavread('../../../sounds/piano.wav') w = np.hamming(1024) N = 1024 H = 512 mX, pX = STFT.stftAnal(x, fs, w, N, H) y = STFT.stftSynth(mX, pX, w.size, H) plt.figure(1, figsize=(9.5, 7)) plt.subplot(411) plt.plot(np.arange(x.size) / float(fs), x, 'b') plt.title('x (piano.wav)') plt.axis([0, x.size / float(fs), min(x), max(x)]) plt.subplot(412) numFrames = int(mX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = np.arange(N / 2) * float(fs) / N plt.pcolormesh(frmTime, binFreq, np.transpose(mX)) plt.title('mX, M=1024, N=1024, H=512') plt.autoscale(tight=True)
def main(inputFile='../../sounds/piano.wav', window='hamming', M=1024, N=1024, H=512): """ analysis/synthesis using the STFT inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (choice of rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size N: fft size (power of two, bigger or equal than M) H: hop size (at least 1/2 of analysis window size to have good overlap-add) """ # read input sound (monophonic with sampling rate of 44100) fs, x = UF.wavread(inputFile) # compute analysis window w = get_window(window, M) # compute the magnitude and phase spectrogram mX, pX = STFT.stftAnal(x, w, N, H) # perform the inverse stft y = STFT.stftSynth(mX, pX, M, H) # output sound file (monophonic with sampling rate of 44100) outputFile = 'output_sounds/' + os.path.basename( inputFile)[:-4] + '_stft.wav' # write the sound resulting from the inverse stft UF.wavwrite(y, fs, outputFile) # create figure to plot plt.figure(figsize=(12, 9)) # frequency range to plot maxplotfreq = 5000.0 # plot the input sound plt.subplot(4, 1, 1) plt.plot(np.arange(x.size) / float(fs), x) plt.axis([0, x.size / float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') # plot magnitude spectrogram plt.subplot(4, 1, 2) numFrames = int(mX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = fs * np.arange(N * maxplotfreq / fs) / N plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:, :(int)(N * maxplotfreq / fs + 1)])) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('magnitude spectrogram') plt.autoscale(tight=True) # plot the phase spectrogram plt.subplot(4, 1, 3) numFrames = int(pX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = fs * np.arange(N * maxplotfreq / fs) / N plt.pcolormesh( frmTime, binFreq, np.transpose(np.diff(pX[:, :(int)(N * maxplotfreq / fs + 1)], axis=1))) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('phase spectrogram (derivative)') plt.autoscale(tight=True) # plot the output sound plt.subplot(4, 1, 4) plt.plot(np.arange(y.size) / float(fs), y) plt.axis([0, y.size / float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.ion() plt.show()
def main(inputFile = '../../sounds/piano.wav', window = 'hamming', M = 1024, N = 1024, H = 512): """ analysis/synthesis using the STFT inputFile: input sound file (monophonic with sampling rate of 44100) window: analysis window type (choice of rectangular, hanning, hamming, blackman, blackmanharris) M: analysis window size N: fft size (power of two, bigger or equal than M) H: hop size (at least 1/2 of analysis window size to have good overlap-add) """ # read input sound (monophonic with sampling rate of 44100) fs, x = UF.wavread(inputFile) # compute analysis window w = get_window(window, M) # compute the magnitude and phase spectrogram mX, pX = STFT.stftAnal(x, w, N, H) # perform the inverse stft y = STFT.stftSynth(mX, pX, M, H) # output sound file (monophonic with sampling rate of 44100) outputFile = 'output_sounds/' + os.path.basename(inputFile)[:-4] + '_stft.wav' # write the sound resulting from the inverse stft UF.wavwrite(y, fs, outputFile) # create figure to plot plt.figure(figsize=(12, 9)) # frequency range to plot maxplotfreq = 5000.0 # plot the input sound plt.subplot(4,1,1) plt.plot(np.arange(x.size)/float(fs), x) plt.axis([0, x.size/float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') # plot magnitude spectrogram plt.subplot(4,1,2) numFrames = int(mX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(N*maxplotfreq/fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:,:N*maxplotfreq/fs+1])) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('magnitude spectrogram') plt.autoscale(tight=True) # plot the phase spectrogram plt.subplot(4,1,3) numFrames = int(pX[:,0].size) frmTime = H*np.arange(numFrames)/float(fs) binFreq = fs*np.arange(N*maxplotfreq/fs)/N plt.pcolormesh(frmTime, binFreq, np.transpose(np.diff(pX[:,:N*maxplotfreq/fs+1],axis=1))) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('phase spectrogram (derivative)') plt.autoscale(tight=True) # plot the output sound plt.subplot(4,1,4) plt.plot(np.arange(y.size)/float(fs), y) plt.axis([0, y.size/float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show()
def play(self): """ Play entire file """ data = self.wf.readframes(chunk) while data != '': self.stream.write(data) data = self.wf.readframes(chunk) #save data to file file = open("Text File/signal_data.txt", "w") for item in self.datas: file.write("%s " % item) file.close() ''' #Framing framerate = self.rates print framerate #menentukan jumlah frame frame = round(len(self.datas)/framerate) #mengukur banyak data/frame hop = 10 #jumlah frame yang diperiksa overlap = 5 #lompatan frame a = 0 while a < frame: f_data = self.datas[a*int(frame):(a+hop)*int(frame)] f_time = np.arange(a*(f_data.size/hop),(a+hop)*(f_data.size/hop))/float(self.rates) title = "Frame",a/50+1,"Time-domain" plt.title(title) plt.xlabel("Time") plt.ylabel("Amplitude") plt.plot(f_time,f_data) plt.show() a += overlap ''' ''' for i in range(hop): f_data = self.datas[i*int(frame):(i+1)*int(frame)] f_time = np.arange(i*f_data.size,(i+1)*f_data.size)/float(self.rates) plt.title("Frame Time-domain") plt.xlabel("Time") plt.ylabel("Amplitude") plt.plot(f_time,f_data) plt.show() ''' ''' f_time_sec = f_time[-1] f_data = np.asarray(()) f_time = np.asarray(()) for i in range(int(frame)): if len(f_data) == 0: f_data = self.datas[i*framerate:(i+1)*framerate] f_time = i*f_time_sec else: f_data = np.hstack((f_data,self.datas[i*framerate:(i+1)*framerate])) f_time = np.hstack((f_time,i*f_time_sec)) if len(self.datas) % framerate > 0: f_data = np.hstack((f_data,self.datas[(i+1)*framerate:])) temp = (i+1)*f_time_sec f_time = np.hstack((f_time,temp)) mod = len(self.datas) % framerate antimod = len(self.datas) - mod f_data = np.reshape(f_data[:antimod],(-1,framerate)) sisa_f_data = f_data[antimod:] print f_data print sisa_f_data print f_time ''' #proses STFT N = 2048 M = 501 #'''bisa di set''' H = M / 2 #bisa di set manual' x = self.datas x = np.float32(x) / norm_fact[x.dtype.name] fs = self.rates w = get_window('hamming', M) global mX mX, pX = stft.stftAnal(x, fs, w, N, H) y = stft.stftSynth(mX, pX, M, H) file = open("Text File/mX.txt", "w") for j in range(len(mX)): for item in mX[j]: file.write("%s " % item) file.write("\n\n") file.close() plt.figure(figsize=(12, 9)) maxplotfreq = 5000.0 plt.subplot(4, 1, 1) plt.plot(np.arange(x.size) / float(fs), x) plt.axis([0, x.size / float(fs), min(x), max(x)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('input sound: x') plt.subplot(4, 1, 2) numFrames = int(mX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = fs * np.arange(N * maxplotfreq / fs) / N plt.pcolormesh(frmTime, binFreq, np.transpose(mX[:, :int(N * maxplotfreq / fs + 1)])) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('magnitude spectrogram') plt.autoscale(tight=True) file = open("Text File/STFT frequencies.txt", "w") for item in binFreq: file.write("%s\n" % item) file.close() plt.subplot(4, 1, 3) numFrames = int(pX[:, 0].size) frmTime = H * np.arange(numFrames) / float(fs) binFreq = fs * np.arange(N * maxplotfreq / fs) / N plt.pcolormesh( frmTime, binFreq, np.transpose(np.diff(pX[:, :int(N * maxplotfreq / fs + 1)], axis=1))) plt.xlabel('time (sec)') plt.ylabel('frequency (Hz)') plt.title('phase spectrogram (derivative)') plt.autoscale(tight=True) plt.subplot(4, 1, 4) plt.plot(np.arange(y.size) / float(fs), y) plt.axis([0, y.size / float(fs), min(y), max(y)]) plt.ylabel('amplitude') plt.xlabel('time (sec)') plt.title('output sound: y') plt.tight_layout() plt.show(block=False) #print mX.shape #proses finding peak minimum = np.min(mX) maximum = np.max(mX) t = 0.8 sebaran = np.arange(minimum, maximum) s_index = int(sebaran.size * (1 - t)) treshold = sebaran[-s_index] print "treshold =", treshold ploc = peakdetect.peakDetection(mX, treshold) global peak_loc global pmag peak_loc = [] for i in range(len(ploc) - 1): if ploc[i] != ploc[i + 1]: peak_loc.append(ploc[i]) peak_loc.append(ploc[-1]) peak_loc = np.array(peak_loc) file = open("Text File/peaks location.txt", "w") for item in peak_loc: file.write("%s\n" % item) file.close() file = open("Text File/peaks magnitude.txt", "w") for item in peak_loc: file.write("%s " % item) for i in range(len(mX[item])): file.write("%s " % mX[item, i]) file.write("\n\n") file.close() pmag = mX[peak_loc] pl.plot(mX[-10, :]) pl.xlabel('Index') pl.ylabel('Value') pl.show() ''' plt.plot(pX[-10,:]) pl.xlabel('Index') pl.ylabel('Value') pl.show() ''' freqaxis = fs * np.arange(N / 2) / float(N) plt.plot(freqaxis, mX[peak_loc[0], :-1]) pl.xlabel("Frequency") pl.ylabel("Magnitude") pl.show() ''' file = open("Text File/peak frequencies.txt","w") file.write("Frequency\tMagnitude\n") for item in peak_loc: file.write("%s\t" % freqaxis[item]) file.write("%s\n" % mX[peak_loc[0],item]) file.close() ''' pl.plot(fs * peak_loc / float(N), pmag) pl.xlabel("Frequency") pl.ylabel("Magnitude") pl.show() #Menampilkan frequensi pada masing-masing Frame hasil STFT loc = [] for m in pmag: loc.append(np.argmax(m)) Freq = freqaxis[loc] df = pd.DataFrame(Freq) df.to_excel("Frekuensi Penyusun " + self.filename.split("/")[-1].split(".")[0] + ".xlsx", index=False) #execfile("find_peak_cwt.py") '''