I am working on an EEG emotion processing project using MATLAB and EEGLAB. My goal is to extract time-domain, frequency-domain, and wavelet-domain features from EEG data. While I have successfully extracted features from segmented data, I am facing issues when applying the pmtm function to compute the Power Spectral Density (PSD) on non-segmented data.
Problem Description
For certain EEG data files, the pmtm function hangs indefinitely, causing the script to become non-responsive. This happens even after detrending the data. The code processes some files without any issues, but for others, it gets stuck during the PSD computation.
Steps Taken
Detrending the Data: Changed from
[pxx, f] = pmtm(data, 4, [], EEG.srate);
to
[pxx, f] = pmtm(detrend(data), 4, [], EEG.srate);
which resolved the issue for some files but caused new issues with others.
Error Handling: Added error handling to log problematic files and bypass them.
Data Format: Attempted to process the data from scratch using .mat files instead of .set files, but the problem persisted.
Here is the relevant portion of my code with the timeout mechanism and error handling:
for chan = 1:n_channels
data = EEG.data(chan, :);
% Frequency domain features
for band = 1:n_bands
try
% Power Spectral Density using multitaper method
[pxx, f] = pmtm(detrend(data), 4, [], EEG.srate);
band_pxx = pxx(f >= freq_bands(band, 1) & f <= freq_bands(band, 2));
if isempty(band_pxx)
disp(['Warning: No data in frequency band ', band_names{band}, ' for channel ', num2str(chan)]);
continue;
end
power_features(chan, band) = sum(band_pxx);
std_features(chan, band) = std(band_pxx);
entropy_features(chan, band) = wentropy(band_pxx, 'shannon');
% Wavelet transform (using Morlet wavelet)
scales = freq2scales(freq_bands(band, :), EEG.srate);
wavelet_data = cwt(data, 'amor', EEG.srate, 'FrequencyLimits', freq_bands(band, :));
wavelet_band_data = abs(wavelet_data);
power_wavelet_features(chan, band) = sum(wavelet_band_data(:).^2);
std_wavelet_features(chan, band) = std(wavelet_band_data(:));
entropy_wavelet_features(chan, band) = wentropy(wavelet_band_data(:), 'shannon');
catch ME
disp(['Error processing channel ', num2str(chan), ' band ', band_names{band}, ': ', ME.message]);
power_features(chan, band) = NaN;
std_features(chan, band) = NaN;
entropy_features(chan, band) = NaN;
power_wavelet_features(chan, band) = NaN;
std_wavelet_features(chan, band) = NaN;
entropy_wavelet_features(chan, band) = NaN;
end
end
end
Questions
- What could be causing the pmtm function to hang indefinitely for certain EEG data files?
- How can I further debug or resolve this issue to ensure the pmtm function completes for all files?
Any insights or suggestions would be greatly appreciated!
2