import numpy as np
from scipy.io.wavfile import write
import math
import matplotlib.pyplot as plt
from scipy.signal import stft
from scipy.fft import fft, fftfreq
## PARAMETROS DE LA SENIAL
amplitud = 32767 # Amplitud máxima (para 16-bit PCM)
fs = 44100 # Frecuencia de muestreo en Hz (común para audio de alta calidad)
ts = 1/fs
## PARAMETROS DE LA SENIAL
f0 = 330
t0 = 1/f0
ti = 0.0
tf = 30*t0
Nt = int((tf-ti)*fs)
t = np.zeros(Nt+1)
st = np.zeros(Nt+1)
fsignal = np.zeros(Nt+1)
fsignal_i = 0.5*f0
fsignal_f = 1.5*f0
dsignal = (fsignal_f-fsignal_i)/Nt
for it in range(Nt+1):
t[it] = ti+it*ts
fsignal[it] = fsignal_i+it*dsignal
st[it] = math.sin(2.0*math.pi*fsignal[it]*t[it])
# archivo wav
senal = amplitud*st
senal_int16 = np.int16(senal)
write("senal_sinusoidal.wav", fs, senal_int16)
print("Archivo WAV generado con éxito.")
# Zero-padding: Agregar ceros para aumentar la resolución
n_padded = 20096 # Número de puntos para la DFT (aumenta la resolución)
signal_padded = np.pad(st, (0, n_padded - len(st)), 'constant')
# Calcular la DFT
fft_values = fft(signal_padded)
fft_freqs = fftfreq(len(fft_values), 1/fs)
frequencies, times, Zxx = stft(st, fs=fs, nperseg=556)
plt.figure(figsize=(12, 6))
plt.subplot(2,2,1)
plt.plot(t,fsignal)
plt.xlabel('Tiempo (s)')
plt.ylabel('f(t)')
plt.grid()
plt.subplot(2, 2, 2)
plt.plot(t,st)
plt.xlabel('Tiempo (s)')
plt.ylabel('s(t)')
#plt.xlabel('Tiempo (s)')
plt.subplot(2, 2, 3)
plt.plot( np.abs(fft_values)[:len(fft_values)//2], fft_freqs[:len(fft_values)//2],'r')
#plt.title("Transformada Discreta de Fourier (DFT)")
plt.ylabel("Frecuencia (Hz)")
plt.xlabel("Magnitud")
plt.ylim([200,800])
plt.grid()
plt.subplot(2,2,4)
plt.pcolormesh(times, frequencies, np.abs(Zxx), shading='gouraud')
#plt.colorbar(label='Magnitud')
#plt.title('STFT de la Señal Sinusoidal de 330 Hz')
#plt.ylabel('Frecuencia (Hz)')
plt.xlabel('Tiempo (s)')
plt.ylim(200, 800) # Limitar el rango de frecuencias
plt.grid()
plt.pcolormesh(times, frequencies, np.abs(Zxx), shading='gouraud')
plt.subplots_adjust(hspace=0.4)
plt.show()
import numpy as np
from scipy.io.wavfile import write
import math
import matplotlib.pyplot as plt
## PARAMETROS DE LA SENIAL
amplitud = 32767 # Amplitud máxima (para 16-bit PCM)
fs = 44100 # Frecuencia de muestreo en Hz (común para audio de alta calidad)
ts = 1/fs
## PARAMETROS DE LA SENIAL
f0 = 330
t0 = 1/f0
ti = 0.0
tf = 10*t0
Nt = int((tf-ti)*fs)
t = np.zeros(Nt+1)
st = np.zeros(Nt+1)
fsignal = np.zeros(Nt+1)
fsignal_i = 0.5*f0
fsignal_f = 1.5*f0
dsignal = (fsignal_f-fsignal_i)/Nt
for it in range(Nt+1):
t[it] = ti+it*ts
fsignal[it] = fsignal_i+it*dsignal
st[it] = math.sin(2.0*math.pi*fsignal[it]*t[it])
plt.plot(t,st,'b')
plt.grid()
plt.show()
senal = amplitud*st
senal_int16 = np.int16(senal)
write("senal_sinusoidal.wav", fs, senal_int16)
print("Archivo WAV generado con éxito.")