Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scipy.io.wavfile as wavfile
- import scipy
- import scipy.fftpack
- import numpy as np
- from matplotlib import pyplot as plt
- fs_rate, signal = wavfile.read("sound.wav")
- START = 0
- STOP = 4
- signal = signal[START*fs_rate:STOP*fs_rate]
- print ("Frequency sampling", fs_rate)
- l_audio = len(signal.shape)
- print ("Channels", l_audio)
- if l_audio == 2:
- signal = signal.sum(axis=1) / 2
- N = signal.shape[0]
- print ("Complete Samplings N", N)
- secs = N / float(fs_rate)
- print ("secs", secs)
- Ts = 1.0/fs_rate
- print ("Timestep between samples Ts", Ts)
- t = scipy.arange(0, secs, Ts)
- FFT = abs(scipy.fft(signal))
- FFT_side = FFT[range(N//2)]
- freqs = scipy.fftpack.fftfreq(signal.size, t[1]-t[0])
- fft_freqs = np.array(freqs)
- freqs_side = freqs[range(N//2)]
- fft_freqs_side = np.array(freqs_side)
- plt.subplot(311)
- p1 = plt.plot(t, signal, "g")
- plt.xlabel('Time')
- plt.ylabel('Amplitude')
- plt.subplot(312)
- p2 = plt.plot(freqs, FFT, "r")
- plt.xlabel('Frequency (Hz)')
- plt.ylabel('Count dbl-sided')
- plt.subplot(313)
- p3 = plt.plot(freqs_side, abs(FFT_side), "b")
- plt.xlabel('Frequency (Hz)')
- plt.ylabel('Count single-sided')
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement