Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- # import Python modules
- import os
- import sys
- import numpy as np
- import matplotlib.pyplot as plt
- import scipy
- from scipy.io import wavfile
- from scipy import signal
- ## 1.
- # Edit the script to analyse the audio wav file (short recording of a musical instrument) in time domain and frequency domain using python modules
- # you can resuse parts of exercise0.py from your previous exercise
- #!! FILL IN PARTS WITH "None"
- # Load audio signal
- fs, x = wavfile.read('/Users/mac/OneDrive - TUNI.fi/SGN/Audio/Ex1/glockenspiel-a-2.wav')
- number_of_samples = x.size
- duration = x.size/fs
- print('{} {}'.format('Sampling frequency (in Hz) is: ', fs))
- print('{} {}'.format('Duration (in s) is: ', duration))
- t_scale = np.linspace(0, float(x.size)/fs, x.size) # Hint: use the function np.linspace
- amp_scale = x
- # Plot time domain visualization
- plt.figure(1)
- plt.subplot(2, 1, 1)
- plt.plot(t_scale, amp_scale)
- plt.title('Time domain visualization of the audio signal')
- plt.axis('tight')
- plt.grid('on')
- plt.ylabel('Amplitude')
- plt.xlabel('Time (s)')
- # Plot frequency domain visualization using FFT
- max_freq = fs/2
- win_len = 2048
- number_frequencies = int(win_len / 2) - 1
- t_start = 10000
- X = np.fft.fft(x[t_start:t_start+win_len])
- frq_scale = np.linspace(0, max_freq, number_frequencies)
- mag_scale = 20*np.log10(np.abs(X[0:int(win_len / 2) - 1]))
- plt.subplot(2, 1, 2)
- plt.plot(frq_scale, mag_scale)
- plt.title('Frequency domain visualization of the audio signal')
- plt.axis('tight')
- plt.grid('on')
- plt.ylabel('Magnitude (dB)')
- plt.xlabel('Frequency (Hz)')
- plt.tight_layout()
- plt.show()
- # length of signal is 3,6 s
- # Fundamental frequency is 1878 Hz
- # 2. frequency: 7012
- # 3. frequency: 10032
- ## 2
- # Edit the script to generate a Sine wave signal y0,
- # with amplitude 3000 (so you will hear something if you play the signal),
- # frequency F0, and length of t seconds, as calculated from step 1.
- # Generate sine wave y0
- F0 = 1878 # To be checked from the previous plot
- amplitude = 3000
- y0 = amplitude * np.sin(2 * np.pi * F0 * t_scale)
- # Plot time domain visualization
- short_length = 100
- t_scale_short = np.linspace(0, float(short_length)/fs, short_length)
- amp_scale_short = y0[0:short_length]
- plt.figure(2)
- plt.subplot(2, 1, 1)
- plt.plot(t_scale_short, amp_scale_short)
- plt.title('Time domain visualization of the sine wave y0')
- plt.axis('tight')
- plt.grid('on')
- plt.ylabel('Amplitude')
- plt.xlabel('Time (samples)')
- # Apply fast Fourier transform (FFT) to the Sine wave. Display the FFT of the waveform.
- # Plot frequency domain visualization using FFT
- Y0 = np.fft.fft(y0)
- mag_scale = 20*np.log10(np.abs(x[0:int(win_len / 2) - 1]))
- plt.subplot(2, 1, 2)
- plt.plot(frq_scale, mag_scale)
- plt.title('Frequency domain visualization of the sine wave y0')
- plt.axis('tight')
- plt.grid('on')
- plt.ylabel('Magnitude (dB)')
- plt.xlabel('Frequency (Hz)')
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement