Advertisement
Guest User

Untitled

a guest
May 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. import scipy.io.wavfile as wavfile
  2. import scipy
  3. import scipy.fftpack
  4. import numpy as np
  5. from matplotlib import pyplot as plt
  6.  
  7. fs_rate, signal = wavfile.read("sound.wav")
  8. START = 0
  9. STOP = 4
  10. signal = signal[START*fs_rate:STOP*fs_rate]
  11. print ("Frequency sampling", fs_rate)
  12. l_audio = len(signal.shape)
  13. print ("Channels", l_audio)
  14. if l_audio == 2:
  15.     signal = signal.sum(axis=1) / 2
  16. N = signal.shape[0]
  17. print ("Complete Samplings N", N)
  18. secs = N / float(fs_rate)
  19. print ("secs", secs)
  20. Ts = 1.0/fs_rate
  21. print ("Timestep between samples Ts", Ts)
  22. t = scipy.arange(0, secs, Ts)
  23. FFT = abs(scipy.fft(signal))
  24. FFT_side = FFT[range(N//2)]
  25. freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])
  26. fft_freqs = np.array(freqs)
  27. freqs_side = freqs[range(N//2)]
  28. fft_freqs_side = np.array(freqs_side)
  29. plt.subplot(311)
  30. p1 = plt.plot(t, signal, "g")
  31. plt.xlabel('Time')
  32. plt.ylabel('Amplitude')
  33. plt.subplot(312)
  34. p2 = plt.plot(freqs, FFT, "r")
  35. plt.xlabel('Frequency (Hz)')
  36. plt.ylabel('Count dbl-sided')
  37. plt.subplot(313)
  38. p3 = plt.plot(freqs_side, abs(FFT_side), "b")
  39. plt.xlabel('Frequency (Hz)')
  40. plt.ylabel('Count single-sided')
  41. plt.tight_layout()
  42. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement