Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on 4 de set de 2017
- @author: Professor, Dalcy Fabrício e Tiago
- '''
- import matplotlib.pyplot as plt
- import tkinter as tk
- import numpy as np
- import pyaudio
- import math as mt
- '''
- @:argument position - posição do botão na lista
- @:argument row - linha onde se encontra o button
- @:argument colum - coluna onde se encontra o button
- @:argument text - o caractere correspondente do button
- '''
- def mudarCor(position, row, column, text):
- List[position] = tk.Button(janela, text=text, bg="black", fg="white").grid(row=row, column=column, padx=10, pady=10)
- janela = tk.Tk()
- janela.title("Telefone")
- janela.geometry("100x200")
- #cria os buttons
- List = [tk.Button(janela, text="1", bg="blue", fg="white"),
- tk.Button(janela, text="2", bg="blue", fg="white"),
- tk.Button(janela, text="3", bg="blue", fg="white"),
- tk.Button(janela, text="4", bg="blue", fg="white"),
- tk.Button(janela, text="5", bg="blue", fg="white"),
- tk.Button(janela, text="6", bg="blue", fg="white"),
- tk.Button(janela, text="7", bg="blue", fg="white"),
- tk.Button(janela, text="8", bg="blue", fg="white"),
- tk.Button(janela, text="9", bg="blue", fg="white"),
- tk.Button(janela, text="*", bg="blue", fg="white"),
- tk.Button(janela, text="0", bg="blue", fg="white"),
- tk.Button(janela, text="#", bg="blue", fg="white")]
- #coloca os Buttons em grid
- List[0].grid(row=0, column=0, padx=10, pady=10)
- List[1].grid(row=0, column=1, padx=10, pady=10)
- List[2].grid(row=0, column=2, padx=10, pady=10)
- List[3].grid(row=1, column=0, padx=10, pady=10)
- List[4].grid(row=1, column=1, padx=10, pady=10)
- List[5].grid(row=1, column=2, padx=10, pady=10)
- List[6].grid(row=2, column=0, padx=10, pady=10)
- List[7].grid(row=2, column=1, padx=10, pady=10)
- List[8].grid(row=2, column=2, padx=10, pady=10)
- List[9].grid(row=3, column=0, padx=10, pady=10)
- List[10].grid(row=3, column=1, padx=10, pady=10)
- List[11].grid(row=3, column=2, padx=10, pady=10)
- '''
- 1 - 697.0 e 1209
- 2 - 697.0 e 1336
- 3 - 697.0 e 1477
- 4 - 770.0 e 1209
- 5 - 770.0 e 1336
- 6 - 770.0 e 1477
- 7 - 852.0 e 1209
- 8 - 852.0 e 1336
- 9 - 852.0 e 1477
- * - 941.0 e 1029
- 0 - 941.0 e 1336
- # - 941.0 e 1477
- '''
- duracao = 1.5 # em segundos
- f1 = 697.0
- f2 = 1209.0
- volume = 0.5 # intervalo [0.0, 1.0]
- Fs = 44100 # frequencia de amostragem - DEVE SER INTEIRO
- Ts = 1.0 / Fs; # intervalo de amostragem
- t = np.arange(start=0, stop=duracao, step=Ts) # vetor do tempo
- y1 = np.sin(2 * np.pi * f1 * t)
- y2 = np.sin(2 * np.pi * f2 * t)
- y = y1 + y2
- samples = y.astype(np.float32)
- p = pyaudio.PyAudio()
- stream = p.open(format=pyaudio.paFloat32,
- channels=1,
- rate=Fs,
- output=True)
- stream.write(volume * samples)
- stream.stop_stream()
- stream.close()
- p.terminate()
- n = len(y) # tamanho do signal
- k = np.arange(n)
- T = n / Fs
- frq = k / T # dois lados do range de frequencia
- frq = frq[np.arange(int(n / 2))] # um lado do range de frequencia
- # computando a Transformada de fourier e normalizando
- Y = np.fft.fft(y) / n
- Y = Y[range(int(n / 2))]
- a = np.argsort(abs(Y))
- # hint arredonda o numero para o valor mais proximo
- # Fmax - frenquência alta.
- # Fmin - frenquência baixa.
- Fmax = np.rint(frq[a[-1]])
- # print(frq[1155])
- # print(frq[2004])
- Fmin = np.rint(frq[a[-2]])
- '''
- com algumas entradas de f1 e f2 o Fmax e Fmin trocam os valores, pra resolver só fiz uma troca de variavel
- caso o Fmax seja menor que o Fmin
- PS: AINDA NÃO TIVE TEMPO DE DEBUGAR PRA SABER PQ OS VALORES SÃO TROCADOS
- '''
- if Fmax < Fmin:
- aux = Fmax
- Fmax = Fmin
- Fmin = aux
- print(Fmax)
- print(Fmin)
- # verifica pela frequencia e imprime o digito correspondente
- if Fmax == 1209.0:
- if Fmin == 697.0:
- print("1")
- mudarCor(0, 0, 0, "1")
- elif Fmin == 770.0:
- print("4")
- mudarCor(3, 1, 0, "4")
- elif Fmin == 852.0:
- print("7")
- mudarCor(6, 2, 0, "7")
- elif Fmin == 941.0:
- print("*")
- mudarCor(9, 3, 0, "*")
- elif Fmax == 1336.0:
- if Fmin == 697.0:
- print("2")
- mudarCor(1, 0, 1, "2")
- elif Fmin == 770.0:
- print("5")
- mudarCor(4, 1, 1, "5")
- elif Fmin == 852.0:
- print("8")
- mudarCor(7, 2, 1, "8")
- elif Fmin == 941.0:
- print("0")
- mudarCor(10, 3, 1, "0")
- elif Fmax == 1477.0:
- if Fmin == 697.0:
- print("3")
- mudarCor(2, 0, 2, "3")
- elif Fmin == 770.0:
- print("6")
- mudarCor(5, 1, 2, "6")
- elif Fmin == 852.0:
- print("9")
- mudarCor(8, 2, 2, "9")
- elif Fmin == 941.0:
- print("#")
- mudarCor(11, 3, 2, "#")
- janela.mainloop()
- # plotando
- fig, ax = plt.subplots(2, 1)
- ax[0].plot(t[:1000], y[:1000])
- ax[0].set_xlabel('Tempo')
- ax[0].set_ylabel('y')
- ax[1].plot(frq[:2500], abs(Y[:2500]), 'r')
- ax[1].set_xlabel('Freq (Hz)')
- ax[1].set_ylabel('|Y(freq)|')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement