Skip to content

Commit 7635568

Browse files
committed
Improve fft
1 parent d5be3b5 commit 7635568

File tree

2 files changed

+342
-297
lines changed

2 files changed

+342
-297
lines changed

src/components/BandPowerGraph.tsx

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -48,88 +48,7 @@ const Graph
4848
],
4949
[]
5050
);
51-
////
5251

53-
const calculateBrainwavePower = (fftData: number[], freqs: number[]) => {
54-
const sumPower = (minFreq: number, maxFreq: number, divisor: number) => {
55-
const filtered = fftData.filter((_, index) => freqs[index] >= minFreq && freqs[index] <= maxFreq);
56-
return Math.sqrt(math.sum(filtered.map(x => x ** 2)) / divisor);
57-
};
58-
59-
const deltaPower = sumPower(0.5, 4, 4);
60-
const thetaPower = sumPower(4, 8, 5);
61-
const alphaPower = sumPower(8, 13, 6);
62-
const betaPower = sumPower(13, 30, 18);
63-
const gammaPower = sumPower(30, 45, 16);
64-
65-
return { deltaPower, thetaPower, alphaPower, betaPower, gammaPower };
66-
};
67-
68-
69-
// const calculateBandPower = useCallback(
70-
// (fftChannelData: number[]) => {
71-
// const freqResolution = samplingRate / (fftChannelData.length * 2);
72-
73-
// // Compute raw power for each band
74-
// const bandPowers = bandRanges.map(([low, high]) => {
75-
// const startIndex = Math.max(1, Math.floor(low / freqResolution));
76-
// const endIndex = Math.min(
77-
// Math.ceil(high / freqResolution),
78-
// fftChannelData.length - 1
79-
// );
80-
81-
// let bandPower = 0;
82-
// for (let i = startIndex; i <= endIndex; i++) {
83-
// if (!isNaN(fftChannelData[i]) && i < fftChannelData.length) {
84-
// bandPower += Math.pow(fftChannelData[i], 2); // Use square of magnitude
85-
// }
86-
// }
87-
88-
// // Normalize by number of bins
89-
// return bandPower / (endIndex - startIndex + 1);
90-
// });
91-
92-
// // Total power across all bands
93-
// const totalPower = bandPowers.reduce((sum, power) => sum + power, 0);
94-
95-
// // Compute normalized power in dB
96-
// return bandPowers.map((bandPower, index) => {
97-
// if (index === 0) {
98-
// return 10; // Fixed value for the first band
99-
// } else if (index === 1) {
100-
// const alpha = bandPowers[2] || 0;
101-
// const beta = bandPowers[3] || 0;
102-
// const alphaBetaPower = alpha + beta;
103-
// const normalizedPower = totalPower > 0 ? alphaBetaPower / totalPower : 0;
104-
// console.log(normalizedPower);
105-
// return 10 * Math.log10(normalizedPower);
106-
// } else {
107-
// return -30; // Rest of the values set to 0
108-
// }
109-
// });
110-
111-
// },
112-
// [bandRanges, samplingRate]
113-
// );
114-
115-
116-
// useEffect(() => {
117-
// if (fftData.length > 0 && fftData[0].length > 0) {
118-
// const channelData = fftData[0];
119-
// const newBandPowerData = calculateBrainwavePower(channelData);
120-
// console.log(newBandPowerData);
121-
// if (
122-
// newBandPowerData.some((value) => !isNaN(value) && value > -Infinity)
123-
// ) {
124-
// setHasValidData(true);
125-
// setBandPowerData(newBandPowerData);
126-
// } else if (!hasValidData) {
127-
// setBandPowerData(Array(5).fill(-100));
128-
// }
129-
// }
130-
// }, [fftData, calculateBandPower, hasValidData]);
131-
132-
///
13352
const calculateBandPower = useCallback(
13453
(fftChannelData: number[]) => {
13554
const freqResolution = samplingRate / (fftChannelData.length * 2);
@@ -248,7 +167,7 @@ const calculateBrainwavePower = (fftData: number[], freqs: number[]) => {
248167
});
249168

250169
ctx.font = "14px Arial";
251-
ctx.fillText("EEG Power Bands", width / 2, height - 15);
170+
ctx.fillText("EEG Band Power ", width / 2, height - 15);
252171

253172
// Rotate and position the y-axis label
254173
ctx.save();

0 commit comments

Comments
 (0)