Advertisement
fabi2295

DUAL TONE

Oct 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. '''
  2. Created on 4 de set de 2017
  3.  
  4. @author: Professor, Dalcy Fabrício e Tiago
  5. '''
  6. import matplotlib.pyplot as plt
  7. import tkinter as tk
  8. import numpy as np
  9. import pyaudio
  10. import math as mt
  11.  
  12. '''
  13.    @:argument position - posição do botão na lista
  14.    @:argument row - linha onde se encontra o button
  15.    @:argument colum - coluna onde se encontra o button
  16.    @:argument text - o caractere correspondente do button
  17. '''
  18. def mudarCor(position, row, column, text):
  19.     List[position] = tk.Button(janela, text=text, bg="black", fg="white").grid(row=row, column=column, padx=10, pady=10)
  20.  
  21.  
  22. janela = tk.Tk()
  23. janela.title("Telefone")
  24. janela.geometry("100x200")
  25.  
  26. #cria os buttons
  27. List = [tk.Button(janela, text="1", bg="blue", fg="white"),
  28.         tk.Button(janela, text="2", bg="blue", fg="white"),
  29.         tk.Button(janela, text="3", bg="blue", fg="white"),
  30.         tk.Button(janela, text="4", bg="blue", fg="white"),
  31.         tk.Button(janela, text="5", bg="blue", fg="white"),
  32.         tk.Button(janela, text="6", bg="blue", fg="white"),
  33.         tk.Button(janela, text="7", bg="blue", fg="white"),
  34.         tk.Button(janela, text="8", bg="blue", fg="white"),
  35.         tk.Button(janela, text="9", bg="blue", fg="white"),
  36.         tk.Button(janela, text="*", bg="blue", fg="white"),
  37.         tk.Button(janela, text="0", bg="blue", fg="white"),
  38.         tk.Button(janela, text="#", bg="blue", fg="white")]
  39.  
  40. #coloca os Buttons em grid
  41. List[0].grid(row=0, column=0, padx=10, pady=10)
  42. List[1].grid(row=0, column=1, padx=10, pady=10)
  43. List[2].grid(row=0, column=2, padx=10, pady=10)
  44. List[3].grid(row=1, column=0, padx=10, pady=10)
  45. List[4].grid(row=1, column=1, padx=10, pady=10)
  46. List[5].grid(row=1, column=2, padx=10, pady=10)
  47. List[6].grid(row=2, column=0, padx=10, pady=10)
  48. List[7].grid(row=2, column=1, padx=10, pady=10)
  49. List[8].grid(row=2, column=2, padx=10, pady=10)
  50. List[9].grid(row=3, column=0, padx=10, pady=10)
  51. List[10].grid(row=3, column=1, padx=10, pady=10)
  52. List[11].grid(row=3, column=2, padx=10, pady=10)
  53.  
  54. '''
  55.    1 - 697.0 e 1209
  56.    2 - 697.0 e 1336
  57.    3 - 697.0 e 1477
  58.    4 - 770.0 e 1209
  59.    5 - 770.0 e 1336
  60.    6 - 770.0 e 1477
  61.    7 - 852.0 e 1209
  62.    8 - 852.0 e 1336
  63.    9 - 852.0 e 1477
  64.    * - 941.0 e 1029
  65.    0 - 941.0 e 1336
  66.    # - 941.0 e 1477
  67. '''
  68.  
  69. duracao = 1.5  # em segundos
  70. f1 = 697.0
  71. f2 = 1209.0
  72. volume = 0.5  # intervalo [0.0, 1.0]
  73.  
  74. Fs = 44100  # frequencia de amostragem - DEVE SER INTEIRO
  75. Ts = 1.0 / Fs;  # intervalo de amostragem
  76.  
  77. t = np.arange(start=0, stop=duracao, step=Ts)  # vetor do tempo
  78.  
  79. y1 = np.sin(2 * np.pi * f1 * t)
  80. y2 = np.sin(2 * np.pi * f2 * t)
  81.  
  82. y = y1 + y2
  83.  
  84. samples = y.astype(np.float32)
  85. p = pyaudio.PyAudio()
  86. stream = p.open(format=pyaudio.paFloat32,
  87.                 channels=1,
  88.                 rate=Fs,
  89.                 output=True)
  90. stream.write(volume * samples)
  91. stream.stop_stream()
  92. stream.close()
  93. p.terminate()
  94.  
  95. n = len(y)  # tamanho do signal
  96. k = np.arange(n)
  97. T = n / Fs
  98. frq = k / T  # dois lados do range de frequencia
  99. frq = frq[np.arange(int(n / 2))]  # um lado do range de frequencia
  100.  
  101. # computando a Transformada de fourier e normalizando
  102. Y = np.fft.fft(y) / n
  103. Y = Y[range(int(n / 2))]
  104.  
  105. a = np.argsort(abs(Y))
  106.  
  107. # hint arredonda o numero para o valor mais proximo
  108. # Fmax - frenquência alta.
  109. # Fmin - frenquência baixa.
  110. Fmax = np.rint(frq[a[-1]])
  111. # print(frq[1155])
  112. # print(frq[2004])
  113. Fmin = np.rint(frq[a[-2]])
  114.  
  115. '''
  116.    com algumas entradas de f1 e f2 o Fmax e Fmin trocam os valores, pra resolver só fiz uma troca de variavel
  117.    caso o Fmax seja menor que o Fmin
  118.    PS: AINDA NÃO TIVE TEMPO DE DEBUGAR PRA SABER PQ OS VALORES SÃO TROCADOS
  119. '''
  120. if Fmax < Fmin:
  121.     aux = Fmax
  122.     Fmax = Fmin
  123.     Fmin = aux
  124.  
  125. print(Fmax)
  126. print(Fmin)
  127.  
  128. # verifica pela frequencia e imprime o digito correspondente
  129. if Fmax == 1209.0:
  130.     if Fmin == 697.0:
  131.         print("1")
  132.         mudarCor(0, 0, 0, "1")
  133.     elif Fmin == 770.0:
  134.         print("4")
  135.         mudarCor(3, 1, 0, "4")
  136.     elif Fmin == 852.0:
  137.         print("7")
  138.         mudarCor(6, 2, 0, "7")
  139.     elif Fmin == 941.0:
  140.         print("*")
  141.         mudarCor(9, 3, 0, "*")
  142. elif Fmax == 1336.0:
  143.     if Fmin == 697.0:
  144.         print("2")
  145.         mudarCor(1, 0, 1, "2")
  146.     elif Fmin == 770.0:
  147.         print("5")
  148.         mudarCor(4, 1, 1, "5")
  149.     elif Fmin == 852.0:
  150.         print("8")
  151.         mudarCor(7, 2, 1, "8")
  152.     elif Fmin == 941.0:
  153.         print("0")
  154.         mudarCor(10, 3, 1, "0")
  155. elif Fmax == 1477.0:
  156.     if Fmin == 697.0:
  157.         print("3")
  158.         mudarCor(2, 0, 2, "3")
  159.     elif Fmin == 770.0:
  160.         print("6")
  161.         mudarCor(5, 1, 2, "6")
  162.     elif Fmin == 852.0:
  163.         print("9")
  164.         mudarCor(8, 2, 2, "9")
  165.     elif Fmin == 941.0:
  166.         print("#")
  167.         mudarCor(11, 3, 2, "#")
  168.  
  169. janela.mainloop()
  170.  
  171. # plotando
  172. fig, ax = plt.subplots(2, 1)
  173.  
  174. ax[0].plot(t[:1000], y[:1000])
  175. ax[0].set_xlabel('Tempo')
  176. ax[0].set_ylabel('y')
  177.  
  178. ax[1].plot(frq[:2500], abs(Y[:2500]), 'r')
  179. ax[1].set_xlabel('Freq (Hz)')
  180. ax[1].set_ylabel('|Y(freq)|')
  181. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement