import wave
import numpy as np
import matplotlib.pyplot as plt

wr = wave.open('original.wav', 'r')
sz = 44100 * 2 # vzorkovanie vaw krat 2 sekundy videa
da = np.fromstring(wr.readframes(sz), dtype=np.int16)

# oddelenie praveho a laveho signalu staci nam jeden
left, right = da[0::2], da[1::2]

lf, rf = abs(np.fft.rfft(left)), abs(np.fft.rfft(right))


##
##plt.figure(1)
##a = plt.subplot(211)
##r = 2**16/2
##a.set_ylim([-r, r])
##a.set_xlabel('time [s]')
##a.set_ylabel('sample value [-]')
##x = np.arange(44100)/44100
##plt.plot(x, left)
##b = plt.subplot(212)
##b.set_xscale('log')
##b.set_xlabel('frequency [Hz]')
##b.set_ylabel('|amplitude|')
##plt.plot(lf)
##plt.savefig('sample-graph.png')

t = np.arange(44100)/44100

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(5, 5))

# plot time signal:
axes[0, 0].set_title("Signal")
axes[0, 0].plot(t, left, color='C0')
axes[0, 0].set_xlabel("Time")
axes[0, 0].set_ylabel("Amplitude")

# plot different spectrum types:
axes[0, 1].set_title("Magnitude Spectrum")
axes[0, 1].magnitude_spectrum(left, Fs=sz, color='C1')

axes[1, 0].set_title("Log. Magnitude Spectrum")
axes[1, 0].magnitude_spectrum(left, Fs=sz, scale='dB', color='C1')

axes[1, 1].set_title("Phase Spectrum ")
axes[1, 1].phase_spectrum(left, Fs=sz, color='C2')

##axes[0, 1].remove()  
##axes[2, 1].remove()  

fig.tight_layout()
plt.show()

