GPT SECOND Script
import numpy as np
import matplotlib.pyplot as plt
import wave
# Load WAV file
filename = "original.wav"
with wave.open(filename, 'rb') as wf:
sample_rate = wf.getframerate()
n_frames = wf.getnframes()
audio = wf.readframes(n_frames)
# Convert to numpy array
data = np.frombuffer(audio, dtype=np.int16)
# If stereo, take one channel
if wf.getnchannels() == 2:
data = data[::2]
# Parameters
window_size = 4096
hop_size = 2048
targets = [1492, 1987, 2255]
time_axis = []
band_energy = {freq: [] for freq in targets}
# Sliding FFT
for start in range(0, len(data) - window_size, hop_size):
window = data[start:start + window_size]
window = window * np.hanning(window_size)
spectrum = np.fft.rfft(window)
freqs = np.fft.rfftfreq(window_size, 1 / sample_rate)
time_axis.append(start / sample_rate)
for target in targets:
idx = np.argmin(np.abs(freqs - target))
band_energy[target].append(np.abs(spectrum[idx]))
# Plot
plt.figure(figsize=(12, 8))
for target in targets:
plt.plot(time_axis, band_energy[target], label=f"{target} Hz")
plt.xlabel("Time (seconds)")
plt.ylabel("Amplitude")
plt.title("Band Energy Over Time")
plt.legend()
plt.show()