@@ -81,26 +81,27 @@ def compute_fft(self, channel, time_data):
81
81
signal_chunk = np .array (time_data [- FFT_WINDOW_SIZE :], dtype = np .float64 )
82
82
windowed_signal = signal_chunk * self .fft_window
83
83
fft_result = np .fft .rfft (windowed_signal )
84
- fft_magnitude = np .abs (fft_result ) * (2.0 / self .window_correction )
84
+ fft_magnitude = np .abs (fft_result [1 :]) * (2.0 / self .window_correction ) # Skip first value
85
+ adjusted_freqs = self .freqs [1 :] # Skip DC frequency (0 Hz)
85
86
86
87
# DEBUG: Print detected peak frequency
87
88
if channel == 0 :
88
89
start_idx = int (2.0 * len (fft_magnitude ) / (self .sampling_rate / 2 ))
89
90
sorted_indices = np .argsort (fft_magnitude [start_idx :])[::- 1 ] + start_idx
90
91
peak1_idx = sorted_indices [0 ]
91
- peak1_freq = self . freqs [peak1_idx ]
92
+ peak1_freq = adjusted_freqs [peak1_idx ]
92
93
print (f"Peak Frequency: { peak1_freq :.2f} Hz" )
93
94
94
95
# Update smoothing buffer
95
96
self .smoothing_buffers [channel ].append (fft_magnitude )
96
97
97
98
# Return smoothed FFT
98
99
smoothed_fft = np .mean (self .smoothing_buffers [channel ], axis = 0 ) if self .smoothing_buffers [channel ] else fft_magnitude
99
- return self . freqs , smoothed_fft
100
+ return adjusted_freqs , smoothed_fft
100
101
101
102
def calculate_band_power (self , fft_magnitudes , freq_range ):
102
103
low , high = freq_range
103
- mask = (self .freqs >= low ) & (self .freqs <= high )
104
+ mask = (self .freqs [ 1 :] >= low ) & (self .freqs [ 1 :] <= high )
104
105
return np .sum (fft_magnitudes [mask ] ** 2 ) # Total power in band
105
106
106
107
def compute_band_powers (self , channel , time_data ):
0 commit comments