
file_path = 'C:\Users\saber\data\sample20s.csv';
Fs = 256;  

data = readtable(file_path, 'VariableNamingRule', 'preserve');
channels = {'TP9', 'AF7', 'AF8', 'TP10'};
eeg = [data.TP9, data.AF7, data.AF8, data.TP10];

n_samples = height(data);
n = n_samples;
f = (0:n-1) * (Fs/n);  
alpha_range = [8 13];
time = (0:n-1) / Fs;   


figure('Name', 'Raw EEG Time Series');
for i = 1:4
    subplot(4, 1, i);
    plot(time, eeg(:, i));
    title(['Raw EEG Signal - Channel: ', channels{i}]);
    xlabel('Time (s)');
    ylabel('Amplitude (\muV)');
    xlim([0 max(time)]);
    grid on;
end
sgtitle('Muse EEG - 4 Channel Raw Signals (0–20s)');


figure('Name', 'EEG FFT with Alpha Band');
for i = 1:4

    signal = eeg(:, i);
    signal = detrend(signal);

    Y_raw = fft(signal);
    freqs = (0:n-1) * (Fs/n);
    mask = (freqs >= 1 & freqs <= 40) | ...
           (freqs >= (Fs - 40) & freqs <= (Fs - 1));
    Y_filtered = Y_raw .* mask';
    signal_filtered = real(ifft(Y_filtered));

    Y = fft(signal_filtered);
    amplitude = abs(Y) / n;
    amplitude = amplitude * 1e6;  

    subplot(4, 1, i);
    plot(f(1:n/2), amplitude(1:n/2), 'LineWidth', 1.2);
    hold on;

    idx_alpha = f >= alpha_range(1) & f <= alpha_range(2);
    area(f(idx_alpha), amplitude(idx_alpha), ...
        'FaceColor', [0.9 0.7 0.7], 'EdgeColor', 'none');

    title(['FFT Spectrum - Channel: ', channels{i}]);
    xlabel('Frequency (Hz)');
    ylabel('Amplitude (\muV)');
    xlim([0 50]);
    grid on;
end
sgtitle('Muse EEG - FFT Spectra with Alpha Band Highlighted (8–13 Hz)');
