Advertisement
Rendier

Spectrogram image of demo3.wav file

Apr 3rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.66 KB | None | 0 0
  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. import scipy.io.wavfile as wav
  4. from numpy.lib import stride_tricks
  5.  
  6. colormaps = "Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, " \
  7.            "GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, " \
  8.            "Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, " \
  9.            "Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, " \
  10.            "Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, " \
  11.            "Vega20b_r, Vega20c, Vega20c_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, " \
  12.            "YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, " \
  13.            "cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, " \
  14.            "gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, " \
  15.            "gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, " \
  16.            "hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, " \
  17.            "pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spectral, spectral_r, spring, " \
  18.            "spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, " \
  19.            "viridis, viridis_r, winter, winter_r"
  20.  
  21. """ short time fourier transform of audio signal """
  22. def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
  23.     win = window(frameSize)
  24.     hopSize = int(frameSize - np.floor(overlapFac * frameSize))
  25.  
  26.     # zeros at beginning (thus center of 1st window should be for sample nr. 0)
  27.     samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)
  28.     # cols for windowing
  29.     cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
  30.     # zeros at end (thus samples can be fully covered by frames)
  31.     samples = np.append(samples, np.zeros(frameSize))
  32.  
  33.     frames = stride_tricks.as_strided(samples, shape=(int(cols), frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
  34.     frames *= win
  35.  
  36.     return np.fft.rfft(frames)
  37.  
  38. """ scale frequency axis logarithmically """
  39. def logscale_spec(spec, sr=44100, factor=20.):
  40.     timebins, freqbins = np.shape(spec)
  41.  
  42.     scale = np.linspace(0, 1, freqbins) ** factor
  43.     scale *= (freqbins-1)/max(scale)
  44.     scale = np.unique(np.round(scale))
  45.  
  46.     # create spectrogram with new freq bins
  47.     newspec = np.complex128(np.zeros([timebins, len(scale)]))
  48.     for i in range(0, len(scale)):
  49.         if i == len(scale)-1:
  50.             newspec[:,i] = np.sum(spec[:,int(scale[i]):], axis=1)
  51.         else:
  52.             newspec[:,i] = np.sum(spec[:,int(scale[i]):int(scale[i+1])], axis=1)
  53.  
  54.     # list center freq of bins
  55.     allfreqs = np.abs(np.fft.fftfreq(freqbins*2, 1./sr)[:freqbins+1])
  56.     freqs = []
  57.     for i in range(0, len(scale)):
  58.         if i == len(scale)-1:
  59.             freqs += [np.mean(allfreqs[int(scale[i]):])]
  60.         else:
  61.             freqs += [np.mean(allfreqs[int(scale[i]):int(scale[i+1])])]
  62.  
  63.     return newspec, freqs
  64.  
  65. """ plot spectrogram"""
  66. def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"):
  67.     samplerate, samples = wav.read(audiopath)
  68.  
  69.     s = stft(samples, binsize)
  70.  
  71.     sshow, freq = logscale_spec(s, factor=2.0, sr=samplerate)
  72.  
  73.     ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel
  74.  
  75.     timebins, freqbins = np.shape(ims)
  76.  
  77.     print("timebins: ", timebins)
  78.     print("freqbins: ", freqbins)
  79.  
  80.     plt.figure(figsize=(15, 7.5))
  81.     plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none")
  82.     plt.colorbar()
  83.  
  84.     plt.xlabel("time (s)")
  85.     plt.ylabel("frequency (hz)")
  86.     plt.xlim([0, timebins-1])
  87.     plt.ylim([0, freqbins])
  88.  
  89.     xlocs = np.float32(np.linspace(0, timebins-1, 5))
  90.     plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate])
  91.     ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10)))
  92.     plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs])
  93.  
  94.     if plotpath:
  95.         plt.savefig(plotpath, bbox_inches="tight")
  96.     else:
  97.         plt.show()
  98.  
  99.     plt.clf()
  100.  
  101.     return ims
  102.  
  103. ims = plotstft('/home/rendier/Ptolemy/Pharos/audio/demo3.wav') # You will need to provide your own wave file here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement