Guest User

Popruntheworld

a guest
Sep 14th, 2018
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Sep 11 20:27:29 2018
  4.  
  5. @author: user
  6. """
  7.  
  8. from scipy import signal
  9. import matplotlib.pyplot as plt
  10. import matplotlib.pyplot as pltT
  11. import numpy as np
  12. from scipy.io import wavfile
  13. import matplotlib.colors as colors
  14. import math as math
  15.  
  16.  
  17. import scipy.ndimage as ndimage
  18. import scipy.ndimage.filters as filters
  19.  
  20.  
  21.  
  22. def extremes(t,f,Sx, hashesPerSecond, neighborhoodSize):
  23.    
  24.     data_max = filters.maximum_filter(Sx, neighborhoodSize)
  25.     maxima = (Sxx == data_max)
  26.  
  27.     labeled, num_objects = ndimage.label(maxima)
  28.     slices = ndimage.find_objects(labeled)
  29.  
  30.     numHashes = int(round(hashesPerSecond*( max(t)-min(t))))
  31.    
  32.     hashes = []
  33.     x, y = [], []
  34.     for dy,dx in slices:
  35.         x_center = (dx.start + dx.stop - 1)/2
  36.         y_center = (dy.start + dy.stop - 1)/2  
  37.        
  38.         value = Sx[dy.start,dx.start]
  39.         x_center=x_center*(max(t)-min(t))/len(t)+min(t)
  40.         x.append(x_center)
  41.        
  42.         y_center = y_center*(max(f)-min(f))/len(f)
  43.         y.append(y_center)
  44.         hashes.append([x_center,y_center,value])
  45.    
  46.         hashes = sorted(hashes, key=lambda x: -x[2])
  47.         hashes = list(filter(lambda x: x[1]>=250 and
  48.                x[1]<=15000,
  49.                hashes))
  50.     return hashes[:numHashes]
  51.  
  52.  
  53. fs, data = wavfile.read('D:/Machine Learning/WinPython/audio/06 Shake It Off.wav')
  54.  
  55. size = len(data)/fs
  56.  
  57. #for step in range(0,round(size)+1,5):
  58.  
  59.    
  60.  
  61. t_start = 45
  62. t_long = 5
  63.                  
  64. x = data[t_start*fs:(t_start*fs+t_long*fs),1]
  65.                        
  66. f, t, Sxx = signal.spectrogram(x, fs)
  67.  
  68. t = t+t_start
  69.  
  70. """pltT.plot(x,'r-')
  71. pltT.ylabel('Wartość próbki')
  72. pltT.xlabel('Number próbki')
  73. pltT.title('"Shake It Off" - Taylor Swift\nPrzedział t[s] = 45 - 50')
  74. pltT.Show()"""
  75.  
  76. plt.figure(num=None, figsize=(8, 6), dpi=80)
  77. plt.pcolormesh(t, f, Sxx,
  78.                norm=colors.LogNorm(vmin=Sxx.min(), vmax=Sxx.max()))
  79. plt.ylabel('Częstotliwość [Hz]')
  80. plt.xlabel('Czas [s]')
  81. plt.title('"Shake It Off" - Taylor Swift\nSpektrogram i hashe')
  82.  
  83.  
  84. hashes = extremes(t,f,Sxx,75,8)
  85. matches = []
  86.  
  87. for item in hashes:
  88.     matchedItems = list(filter(lambda x: (x[0]-item[0] <= 0.15)and
  89.                                (x[0]>item[0]) and
  90.                                abs(math.log10(x[1]/item[1]))<=0.05946,hashes))
  91.     for match in matchedItems:
  92.         ar1 = np.asarray(item)
  93.         ar2 = np.asarray(match)
  94.         sumArr = np.concatenate((ar1,ar2),axis=0)
  95.         matches.append(sumArr)
  96.        
  97.  
  98.    
  99. lines = np.asarray(matches)
  100. hashesArray = np.asarray(hashes)
  101.  
  102. plt.plot(hashesArray[:,0],hashesArray[:,1], 'r+')
  103.  
  104.  
  105. plt.plot([lines[:,0],lines[:,3]],[lines[:,1],lines[:,4]], 'k-')
  106.  
  107. plt.show()
  108.  
  109.  
  110. """hashesDT = np.round(lines[:,3]-lines[:,0],2)
  111. hashesDF = np.round(np.log10(lines[:,4]/lines[:,1])/0.05946,2)
  112. hashesTstart = lines[:,0]
  113. hashesF = np.round(np.log10(lines[:,1]),1)
  114. with open('D:/Machine Learning/WinPython/F - YouTube Cover.txt', 'a') as the_file:
  115.    for i in range(0,len(hashesDT)-1):
  116.         the_file.write(str(hashesTstart[i])+'\t'+str(hashesDT[i])+'\t'+str(hashesF[i])+'\t'+str(hashesDF[i])+'\n')"""
Add Comment
Please, Sign In to add comment