Guest User

Untitled

a guest
Mar 23rd, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from scipy.fftpack import fft
  3. from scipy.io import wavfile # get the api
  4. fs, data = wavfile.read('test.wav') # load the data
  5. a = data.T[0] # this is a two channel soundtrack, I get the first track
  6. b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
  7. c = fft(b) # calculate fourier transform (complex numbers list)
  8. d = len(c)/2 # you only need half of the fft list (real signal symmetry)
  9. plt.plot(abs(c[:(d-1)]),'r')
  10. plt.show()
  11.  
  12. k = arange(len(data))
  13. T = len(data)/fs # where fs is the sampling frequency
  14. frqLabel = k/T
  15.  
  16. import matplotlib.pyplot as plt
  17. from scipy.io import wavfile # get the api
  18. from scipy.fftpack import fft
  19. from pylab import *
  20.  
  21. def f(filename):
  22. fs, data = wavfile.read(filename) # load the data
  23. a = data.T[0] # this is a two channel soundtrack, I get the first track
  24. b=[(ele/2**8.)*2-1 for ele in a] # this is 8-bit track, b is now normalized on [-1,1)
  25. c = fft(b) # create a list of complex number
  26. d = len(c)/2 # you only need half of the fft list
  27. plt.plot(abs(c[:(d-1)]),'r')
  28. savefig(filename+'.png',bbox_inches='tight')
  29.  
  30. import glob
  31. import test2
  32. files = glob.glob('./*.wav')
  33. for ele in files:
  34. f(ele)
  35. quit()
  36.  
  37. #!/usr/bin/env python
  38. # -*- coding: utf-8 -*-
  39.  
  40. from __future__ import print_function
  41. import scipy.io.wavfile as wavfile
  42. import scipy
  43. import scipy.fftpack
  44. import numpy as np
  45. from matplotlib import pyplot as plt
  46.  
  47. fs_rate, signal = wavfile.read("output.wav")
  48. print ("Frequency sampling", fs_rate)
  49. l_audio = len(signal.shape)
  50. print ("Channels", l_audio)
  51. if l_audio == 2:
  52. signal = signal.sum(axis=1) / 2
  53. N = signal.shape[0]
  54. print ("Complete Samplings N", N)
  55. secs = N / float(fs_rate)
  56. print ("secs", secs)
  57. Ts = 1.0/fs_rate # sampling interval in time
  58. print ("Timestep between samples Ts", Ts)
  59. t = scipy.arange(0, secs, Ts) # time vector as scipy arange field / numpy.ndarray
  60. FFT = abs(scipy.fft(signal))
  61. FFT_side = FFT[range(N/2)] # one side FFT range
  62. freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])
  63. fft_freqs = np.array(freqs)
  64. freqs_side = freqs[range(N/2)] # one side frequency range
  65. fft_freqs_side = np.array(freqs_side)
  66. plt.subplot(311)
  67. p1 = plt.plot(t, signal, "g") # plotting the signal
  68. plt.xlabel('Time')
  69. plt.ylabel('Amplitude')
  70. plt.subplot(312)
  71. p2 = plt.plot(freqs, FFT, "r") # plotting the complete fft spectrum
  72. plt.xlabel('Frequency (Hz)')
  73. plt.ylabel('Count dbl-sided')
  74. plt.subplot(313)
  75. p3 = plt.plot(freqs_side, abs(FFT_side), "b") # plotting the positive fft spectrum
  76. plt.xlabel('Frequency (Hz)')
  77. plt.ylabel('Count single-sided')
  78. plt.show()
Add Comment
Please, Sign In to add comment