-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfft.py
More file actions
58 lines (44 loc) · 1.91 KB
/
fft.py
File metadata and controls
58 lines (44 loc) · 1.91 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
import matplotlib
import os
import sys
import warnings
use_non_interactive = os.environ.get('CI') or not os.environ.get('DISPLAY')
if use_non_interactive:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
warnings.filterwarnings('ignore', message='.*FigureCanvasAgg is non-interactive.*')
# Parameters
fs = 1000 # Sampling frequency
t = np.arange(0, 1, 1/fs) # Time vector from 0 to 1 second in 1/fs increments
# Define different frequencies for comparison
frequencies = [5, 10, 15, 20] # Different sine wave frequencies
colors = ['blue', 'orange', 'green', 'red']
# Create a figure with subplots
fig, axes = plt.subplots(len(frequencies), 2, figsize=(12, 10))
fig.suptitle('FFT Analysis of Multiple Sine Waves', fontsize=16, fontweight='bold')
# Process each frequency
for i, (f, color) in enumerate(zip(frequencies, colors)):
# Create a signal: sine wave
signal = np.sin(2 * np.pi * f * t)
# Compute the Fourier transform
ft_signal = np.fft.fft(signal)
freq = np.fft.fftfreq(len(signal), d=1/fs) # Frequency vector
# Plot the original signal
axes[i, 0].plot(t, signal, color=color, linewidth=1.5)
axes[i, 0].set_title(f'Signal: {f} Hz', fontweight='bold')
axes[i, 0].set_xlabel('Time [s]')
axes[i, 0].set_ylabel('Amplitude')
axes[i, 0].grid(True, alpha=0.3)
axes[i, 0].set_xlim(0, 0.5) # Show only first 0.5 seconds for clarity
# Plot the Fourier transform
axes[i, 1].plot(freq, np.abs(ft_signal), color=color, linewidth=1.5)
axes[i, 1].set_title(f'FFT: Peak at {f} Hz', fontweight='bold')
axes[i, 1].set_xlabel('Frequency [Hz]')
axes[i, 1].set_ylabel('Magnitude')
axes[i, 1].set_xlim(0, 50) # Limit frequency range for better visibility
axes[i, 1].grid(True, alpha=0.3)
plt.tight_layout()
# Always save the plot to a file
output_file = 'fft_analysis.png'
plt.savefig(output_file, dpi=150, bbox_inches='tight')