Transfromada de fourier de pulso corto (con libreria)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 = 20*t0
Nt = int((tf-ti)*fs)
t = np.zeros(Nt+1)
st = np.zeros(Nt+1)
for it in range(Nt+1):
t[it] = ti+it*ts
st[it] = math.sin(2.0*math.pi*f0*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.")
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 = 20*t0 Nt = int((tf-ti)*fs) t = np.zeros(Nt+1) st = np.zeros(Nt+1) for it in range(Nt+1): t[it] = ti+it*ts st[it] = math.sin(2.0*math.pi*f0*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.")
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       = 20*t0
Nt       = int((tf-ti)*fs)
t        = np.zeros(Nt+1)
st       = np.zeros(Nt+1)



for it in range(Nt+1):
 t[it]  = ti+it*ts
 st[it] = math.sin(2.0*math.pi*f0*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.")
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 = 20*t0
Nt = int((tf-ti)*fs)
t = np.zeros(Nt+1)
st = np.zeros(Nt+1)
for it in range(Nt+1):
t[it] = ti+it*ts
st[it] = math.sin(2.0*math.pi*f0*t[it])
# 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=2256)
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(0, 600) # Limitar el rango de frecuencias
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.")
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 = 20*t0 Nt = int((tf-ti)*fs) t = np.zeros(Nt+1) st = np.zeros(Nt+1) for it in range(Nt+1): t[it] = ti+it*ts st[it] = math.sin(2.0*math.pi*f0*t[it]) # 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=2256) 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(0, 600) # Limitar el rango de frecuencias 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.")
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       = 20*t0
Nt       = int((tf-ti)*fs)
t        = np.zeros(Nt+1)
st       = np.zeros(Nt+1)



for it in range(Nt+1):
 t[it]  = ti+it*ts
 st[it] = math.sin(2.0*math.pi*f0*t[it])

# 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=2256)

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(0, 600)  # Limitar el rango de frecuencias
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.")
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plt.plot(fft_freqs[:len(fft_values)//2], np.abs(fft_values)[:len(fft_values)//2], 'r')
plt.title("Transformada Discreta de Fourier (DFT)")
plt.xlabel("Frecuencia (Hz)")
plt.ylabel("Magnitud")
plt.xlim([0,600])
plt.grid()
plt.plot(fft_freqs[:len(fft_values)//2], np.abs(fft_values)[:len(fft_values)//2], 'r') plt.title("Transformada Discreta de Fourier (DFT)") plt.xlabel("Frecuencia (Hz)") plt.ylabel("Magnitud") plt.xlim([0,600]) plt.grid()
plt.plot(fft_freqs[:len(fft_values)//2], np.abs(fft_values)[:len(fft_values)//2], 'r')
plt.title("Transformada Discreta de Fourier (DFT)")
plt.xlabel("Frecuencia (Hz)")
plt.ylabel("Magnitud")
plt.xlim([0,600])
plt.grid()
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plt.figure(figsize=(12, 6))
plt.subplot(2, 2, 2)
plt.plot(t,st)
#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([0,600])
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(0, 600) # Limitar el rango de frecuencias
plt.grid()
plt.pcolormesh(times, frequencies, np.abs(Zxx), shading='gouraud')
plt.figure(figsize=(12, 6)) plt.subplot(2, 2, 2) plt.plot(t,st) #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([0,600]) 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(0, 600) # Limitar el rango de frecuencias plt.grid() plt.pcolormesh(times, frequencies, np.abs(Zxx), shading='gouraud')
plt.figure(figsize=(12, 6))

plt.subplot(2, 2, 2)

plt.plot(t,st)

#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([0,600])
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(0, 600)  # Limitar el rango de frecuencias
plt.grid()
plt.pcolormesh(times, frequencies, np.abs(Zxx), shading='gouraud')