graficar_respuesta_del_barrido_en_frecuencia_con_python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| graficar_respuesta_del_barrido_en_frecuencia_con_python [2024/06/02 22:36] – [Notas Adicionales] oso | graficar_respuesta_del_barrido_en_frecuencia_con_python [2024/10/17 21:42] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 212: | Line 212: | ||
| * **Seguridad Auditiva**: Asegúrate de mantener el volumen a un nivel seguro para evitar daños auditivos, especialmente cuando trabajes con frecuencias altas y barridos de amplio rango. | * **Seguridad Auditiva**: Asegúrate de mantener el volumen a un nivel seguro para evitar daños auditivos, especialmente cuando trabajes con frecuencias altas y barridos de amplio rango. | ||
| - | Prueba este script | + | ===== Ventana de Hann y media móvil ===== |
| + | <code python: sweep_Hanning_5x_movingavg.py> | ||
| + | import numpy as np | ||
| + | import pyaudio | ||
| + | import matplotlib.pyplot as plt | ||
| + | from scipy.signal import get_window, butter, filtfilt | ||
| + | from scipy.fft import fft | ||
| + | def generate_sweep(start_freq, | ||
| + | t = np.linspace(0, | ||
| + | sweep = np.sin(2 * np.pi * np.logspace(np.log10(start_freq), | ||
| + | return t, sweep | ||
| + | |||
| + | def play_and_record(sweep, | ||
| + | p = pyaudio.PyAudio() | ||
| + | | ||
| + | stream = p.open(format=pyaudio.paFloat32, | ||
| + | channels=channels, | ||
| + | rate=sample_rate, | ||
| + | output=True, | ||
| + | input=True, | ||
| + | frames_per_buffer=1024) | ||
| + | | ||
| + | recorded_frames = [] | ||
| + | | ||
| + | stream.start_stream() | ||
| + | stream.write(sweep.astype(np.float32).tobytes()) | ||
| + | | ||
| + | for _ in range(0, int(sample_rate / 1024 * duration)): | ||
| + | data = stream.read(1024) | ||
| + | recorded_frames.append(np.frombuffer(data, | ||
| + | | ||
| + | stream.stop_stream() | ||
| + | stream.close() | ||
| + | p.terminate() | ||
| + | | ||
| + | recorded_signal = np.hstack(recorded_frames) | ||
| + | return recorded_signal | ||
| + | |||
| + | def analyze_frequency_response(sweep, | ||
| + | len_min = min(len(sweep), | ||
| + | sweep = sweep[: | ||
| + | recorded_signal = recorded_signal[: | ||
| + | | ||
| + | # Aplicar una ventana de Hann | ||
| + | window = get_window(' | ||
| + | yf = fft(recorded_signal * window) | ||
| + | xf = np.fft.fftfreq(len(recorded_signal), | ||
| + | | ||
| + | # Solo tomar la parte positiva del espectro | ||
| + | pos_freqs = xf[:len(xf) // 2] | ||
| + | power = np.abs(yf[: | ||
| + | | ||
| + | return pos_freqs, power | ||
| + | |||
| + | def moving_average(data, | ||
| + | return np.convolve(data, | ||
| + | |||
| + | def plot_frequency_response(frequencies, | ||
| + | power += 1e-10 # Agregar un valor pequeño para evitar log10(0) | ||
| + | smoothed_power = moving_average(10 * np.log10(power), | ||
| + | |||
| + | plt.figure(figsize=(10, | ||
| + | plt.plot(frequencies[: | ||
| + | plt.plot(frequencies, | ||
| + | plt.title(' | ||
| + | plt.xlabel(' | ||
| + | plt.ylabel(' | ||
| + | plt.xscale(' | ||
| + | plt.grid() | ||
| + | plt.legend() | ||
| + | plt.show() | ||
| + | |||
| + | if __name__ == " | ||
| + | start_freq = 20 | ||
| + | end_freq = 20000 | ||
| + | duration = 10 # Duración en segundos | ||
| + | sample_rate = 44100 | ||
| + | | ||
| + | t, sweep = generate_sweep(start_freq, | ||
| + | recorded_signals = [play_and_record(sweep, | ||
| + | | ||
| + | # Promediar las respuestas en frecuencia | ||
| + | power_avg = np.zeros(len(recorded_signals[0]) // 2) | ||
| + | for recorded_signal in recorded_signals: | ||
| + | frequencies, | ||
| + | power_avg += power | ||
| + | power_avg /= len(recorded_signals) | ||
| + | | ||
| + | plot_frequency_response(frequencies, | ||
| + | </ | ||
| + | |||
| + | ==== Resultado ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
graficar_respuesta_del_barrido_en_frecuencia_con_python.1717367794.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
