Commit d9461d5d authored by Jan Luc Tavares's avatar Jan Luc Tavares

acrescentada visualização de espectro que varia com o tempo

parent 55e6b0be
......@@ -11,25 +11,39 @@ if len(sys.argv[0]) < 1:
# Determinando local do arquivo:
local = "./400Hz_sine.wav"
local = "./Bach_Prelude_from_Cello_Suite_in_G.wav"
print("Utilização:\n>>>python3 ./visualiza_audio.py -a ./arquivo-de-audio.wav")
#local = "./400Hz_sine.wav"
if "-a" in sys.argv:
print("Aquivo especificado\n")
local = sys.argv[sys.argv.index("-a") + 1]
################################
##### Lendo arquivo de som #####
################################
import soundfile as sf
som = sf.read(local)
samplerate=som[1]
som = np.array(som[0])
if (len(som.shape) > 1): # audio estereo. Assumindo somente um canal.
som.shape = (som.shape[1], som.shape[0])
som = som[0]
duracao = len(som)/samplerate
print("Quantidade de pontos: {}\n".format(len(som)))
print("Sample rate: {}\n".format(samplerate))
print("Duracao em segundos: {}\n".format(duracao))
print("Sample rate: {} \n ".format(samplerate))
print("Duracao em segundos: {} \n ".format(duracao))
sf.write("./replica.wav", som, samplerate)
tempo = np.arange(0, duracao, 1/samplerate)
######### Calculando o espectro de frequencias ##########
######### PARA O AUDIO INTEIRO ##########
fft = np.fft.rfft(som)
intensidades = fft*(np.conj(fft))
intensidades = intensidades.real
......@@ -37,12 +51,27 @@ freqs = np.fft.fftfreq(len(som), 1/samplerate)
### normalizando as intensidades ###
intensidades = intensidades/(max(intensidades))
####### Espectro dividindo o audio em partes ############
import scipy.signal as sig
frequencias, tempo, espectrograma= sig.spectrogram(som, fs=samplerate, mode="magnitude")
import matplotlib.pyplot as plt
fig = plt.figure()
plt.pcolormesh(tempo, frequencias, espectrograma)
plt.title("Espectro que varia no tempo com divisão grosseira de frequencias")
plt.ylabel('Freq [Hz]')
plt.xlabel('Tempo [s]')
plt.show()
fig.savefig("./{}-espectrograma.png".format(local[2:-3]), dpi=400)
######### 'Graficando o som' ###########
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import column
TOOLS="hover,crosshair,wheel_zoom,box_zoom,undo,redo,reset,save"
TOOLS="pan,hover,crosshair,wheel_zoom,box_zoom,undo,redo,reset,save"
output_file("./{}-graficos.html".format(local[2:-3]))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment