-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_analysis.py
40 lines (32 loc) · 1.04 KB
/
fft_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
import wave
import matplotlib.pyplot as plt
import sys
# Open the WAV file
try:
file_path = sys.argv[1]
except:
file_path = 'wav/sample.wav'
wav_file = wave.open(file_path, 'r')
# Extract Raw Audio from Wav File
signal = wav_file.readframes(-1)
signal = np.frombuffer(signal, dtype=np.int16)
# Get the frame rate
framerate = wav_file.getframerate()
print(framerate)
# Time vector spaced linearly with the size of the audio file
t = np.linspace(0, len(signal) / framerate, num=len(signal))
# Perform the FFT on the signal
fft_signal = np.fft.fft(signal)
print(len(fft_signal))
# Calculate the frequencies for the FFT result
frequencies = np.fft.fftfreq(len(fft_signal), d=1/framerate)
print(len(frequencies))
# Plot the FFT
plt.figure(figsize=(12, 6))
plt.plot(frequencies, np.abs(fft_signal)) # Plot in frequency domain
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.title('FFT of Audio File')
plt.xlim(0, frequencies.max()) # Limit x-axis to positive frequencies only
plt.show()