andr3cf

Xereco deus dos code

Nov 24th, 2025
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import sounddevice as sd
  4. import pywt
  5. import threading
  6.  
  7. # PARAMETROS ---------------------------
  8. N_CELLS = 120
  9. FREQ_MIN = 100
  10. FREQ_MAX = 8000
  11. fs = 44100
  12. frame = 2048  # ~46ms
  13.  
  14. center_freqs = np.logspace(np.log10(FREQ_MIN), np.log10(FREQ_MAX), N_CELLS)
  15.  
  16. latest_audio = None
  17. lock = threading.Lock()
  18.  
  19. def cochlea_waveletbank(frame, fs):
  20.     wavelet = 'morl'
  21.     fc = pywt.central_frequency(wavelet)
  22.     scales = fc * fs / center_freqs
  23.  
  24.     coeffs, freqs = pywt.cwt(frame, scales, wavelet, sampling_period=1/fs)
  25.     power = np.mean(np.abs(coeffs) ** 2, axis=1)
  26.  
  27.     # Converte energia para dB (escala logarítmica)
  28.     power_db = 10 * np.log10(power + 1e-12)
  29.  
  30.     return power_db
  31.  
  32. def audio_callback(indata, frames, time, status):
  33.     global latest_audio
  34.     mono = indata[:, 0]
  35.  
  36.     with lock:
  37.         latest_audio = mono.copy()   # só copia o frame, nada pesado
  38.  
  39. plt.ion()
  40. fig, ax = plt.subplots(figsize=(14,5))
  41.  
  42. larguras = np.diff(np.logspace(np.log10(FREQ_MIN), np.log10(FREQ_MAX), N_CELLS+1))
  43. bars = ax.bar(center_freqs,
  44.               np.zeros(N_CELLS),
  45.               width=larguras,
  46.               bottom=-80,          # <<--- faz as barras nascerem em -80 dB
  47.               color="dodgerblue",
  48.               alpha=0.7,
  49.               align="edge")
  50.  
  51. ax.set_xscale("log")
  52. ax.set_ylim(-80, 20)   # de -80 dB (silêncio) até 0 dB (máximo)
  53. ax.set_xlabel("Frequência (Hz)")
  54. ax.set_ylabel("Intensidade (dB)")
  55. ax.set_title("Demonstração de Cóclea em Tempo Real")
  56.  
  57. # LOOP PRINCIPAL ---------------------------------------------------
  58. with sd.InputStream(channels=1, samplerate=fs, blocksize=frame, callback=audio_callback):
  59.  
  60.     print("!! rodando em tempo real !!")
  61.  
  62.     while True:
  63.         plt.pause(0.001)
  64.  
  65.         with lock:
  66.             audio = latest_audio
  67.  
  68.         if audio is None:
  69.             continue
  70.  
  71.         # wavelet
  72.         ativ = cochlea_waveletbank(audio, fs)
  73.  
  74.         # atualiza grafico
  75.         for b, h in zip(bars, ativ):
  76.             b.set_height(h + 80)
  77.  
  78.         regions = [
  79.             (3000, 6000,  "#0f62fe"),   # azul
  80.             (0, 150,  "#42be65"),   # verde
  81.             (1500, 3000, "#be95ff"),   # roxo
  82.             (150, 375,"#ff832b"),   # laranja
  83.             (375, 750,"#fa4d56"),   # vermelho
  84.             (750, 1500,"#ff7eb6"),   # rosa
  85.             (6000, 10000,"#08bdba")   # ciano
  86.         ]
  87.  
  88.         for b, f in zip(bars, center_freqs):
  89.             for fmin, fmax, color in regions:
  90.                 if fmin <= f < fmax:
  91.                     b.set_color(color)
  92.                     break
  93.  
  94.         fig.canvas.draw()
  95.         fig.canvas.flush_events()
Advertisement
Add Comment
Please, Sign In to add comment