Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. # import Python modules
  4. import os
  5. import sys
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import scipy
  9. from scipy.io import wavfile
  10. from scipy import signal
  11.  
  12. ## 1.
  13. # Edit the script to analyse the audio wav file (short recording of a musical instrument) in time domain and frequency domain using python modules
  14. # you can resuse parts of exercise0.py from your previous exercise
  15. #!! FILL IN PARTS WITH "None"
  16.  
  17. # Load audio signal
  18. fs, x = wavfile.read('/Users/mac/OneDrive - TUNI.fi/SGN/Audio/Ex1/glockenspiel-a-2.wav')
  19. number_of_samples = x.size
  20. duration = x.size/fs
  21. print('{} {}'.format('Sampling frequency (in Hz) is: ', fs))
  22. print('{} {}'.format('Duration (in s) is: ', duration))
  23.  
  24. t_scale = np.linspace(0, float(x.size)/fs, x.size) # Hint: use the function np.linspace
  25. amp_scale = x
  26.  
  27. # Plot time domain visualization
  28. plt.figure(1)
  29. plt.subplot(2, 1, 1)
  30. plt.plot(t_scale, amp_scale)
  31. plt.title('Time domain visualization of the audio signal')
  32. plt.axis('tight')
  33. plt.grid('on')
  34. plt.ylabel('Amplitude')
  35. plt.xlabel('Time (s)')
  36.  
  37. # Plot frequency domain visualization using FFT
  38. max_freq = fs/2
  39. win_len = 2048
  40. number_frequencies = int(win_len / 2) - 1
  41. t_start = 10000
  42. X = np.fft.fft(x[t_start:t_start+win_len])
  43. frq_scale = np.linspace(0, max_freq, number_frequencies)
  44. mag_scale = 20*np.log10(np.abs(X[0:int(win_len / 2) - 1]))
  45.  
  46. plt.subplot(2, 1, 2)
  47. plt.plot(frq_scale, mag_scale)
  48. plt.title('Frequency domain visualization of the audio signal')
  49. plt.axis('tight')
  50. plt.grid('on')
  51. plt.ylabel('Magnitude (dB)')
  52. plt.xlabel('Frequency (Hz)')
  53.  
  54. plt.tight_layout()
  55. plt.show()
  56. # length of signal is 3,6 s
  57. # Fundamental frequency is 1878 Hz
  58. # 2. frequency: 7012
  59. # 3. frequency: 10032
  60.  
  61. ## 2
  62. # Edit the script to generate a Sine wave signal y0,
  63. # with amplitude 3000 (so you will hear something if you play the signal),
  64. # frequency F0, and length of t seconds, as calculated from step 1.
  65.  
  66. # Generate sine wave y0
  67. F0 = 1878 # To be checked from the previous plot
  68. amplitude = 3000
  69. y0 = amplitude * np.sin(2 * np.pi * F0 * t_scale)
  70.  
  71. # Plot time domain visualization
  72. short_length = 100
  73. t_scale_short = np.linspace(0, float(short_length)/fs, short_length)
  74. amp_scale_short = y0[0:short_length]
  75.  
  76. plt.figure(2)
  77. plt.subplot(2, 1, 1)
  78. plt.plot(t_scale_short, amp_scale_short)
  79. plt.title('Time domain visualization of the sine wave y0')
  80. plt.axis('tight')
  81. plt.grid('on')
  82. plt.ylabel('Amplitude')
  83. plt.xlabel('Time (samples)')
  84.  
  85. # Apply fast Fourier transform (FFT) to the Sine wave. Display the FFT of the waveform.
  86.  
  87. # Plot frequency domain visualization using FFT
  88. Y0 = np.fft.fft(y0)
  89. mag_scale = 20*np.log10(np.abs(x[0:int(win_len / 2) - 1]))
  90.  
  91. plt.subplot(2, 1, 2)
  92. plt.plot(frq_scale, mag_scale)
  93. plt.title('Frequency domain visualization of the sine wave y0')
  94. plt.axis('tight')
  95. plt.grid('on')
  96. plt.ylabel('Magnitude (dB)')
  97. plt.xlabel('Frequency (Hz)')
  98.  
  99. plt.tight_layout()
  100. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement