I apply SWT method to remove artifact from EEG signal. The level of decomposition,5 and the length of the signal,2000.How to apply 2^Level in this code? What can I do?
4 views (last 30 days)
Show older comments
%%
clc;
clear
close all;
load B02T.mat;
fs=250;
% t = 0.004:1/fs:1;
eeg1 = data{1,1}.X;
channel_1= eeg1(:,1);
ch_1=channel_1(1:2000);
figure;
subplot(2,1,1);
plot(ch_1);
xlabel('Time (s)');
ylabel('Amplitude');
legend('ch1');
title("Plot channel "+1+ " for Structure 1X1");
hold on;
grid on;
eeg_signal = eeg1(1:2000); % first 2000 samples
subplot(2,1,2);
plot(eeg_signal,'black');
hold on;
grid on;
legend('EEG Signal');
title('Raw EEG Signal');
%%
% Set parameters
wavelet = 'db4'; % Wavelet family
level = 5; % Level of decomposition
% Perform the SWT on the raw signal
[swt_coeffs, ~] = swt(eeg_signal, level, wavelet);
% Plot the subbands of the SWT coefficients
figure;
for i = 1:level+1
subplot(level+1, 1, i);
plot(swt_coeffs(i, :));
title(['Subband ', num2str(i)]);
xlabel('Sample');
ylabel('Amplitude');
end
% Set the global threshold using a threshold selection rule or a fixed threshold value
thr = wthrmngr('sw2ddenoLVL', 'penalhi', swt_coeffs);
% Create an array to store denoised subbands
denoised_subbands = cell(level+1, 1);
% Apply thresholding to each subband
for i = 1:level
% Extract coefficients for the current subband
subband_coeffs = swt_coeffs(i, :);
% Apply global thresholding to the subband coefficients
denoised_subband_coeffs = subband_coeffs;
denoised_subband_coeffs(abs(denoised_subband_coeffs) < thr) = 0;
% Store denoised subband coefficients
denoised_subbands{i} = denoised_subband_coeffs;
end
% Reconstruct the denoised EEG signal by combining the denoised subbands
denoised_coeffs = [swt_coeffs(level+1, :), denoised_subbands{level:-1:1}];
cleaned_signal = iswt(denoised_coeffs, wavelet);
noise_signal = eeg_signal - cleaned_signal;
% Plot the original and denoised signals
t = 1:length(eeg_signal);
figure;
subplot(3, 1, 1);
plot(t, eeg_signal);
title('Original EEG Signal');
xlabel('Time');
ylabel('Amplitude');
subplot(3, 1, 2);
plot(t, cleaned_signal);
title('Denoised EEG Signal');
xlabel('Time');
ylabel('Amplitude');
subplot(3, 1, 3);
plot(t, noise_signal);
title('noised EEG Signal');
xlabel('Time');
ylabel('Amplitude');
%%
% Calculate SNR
SNR = 20 * log10(norm(eeg_signal)/ norm(noise_signal));
% Calculate MSE
MSE = ((norm(eeg_signal) - norm(cleaned_signal)).^2) / 2000;
% MSE = immse(norm(eeg_signal), norm(cleaned_signal))/2000;
disp(['SNR: ', num2str(SNR), ' dB']);
disp(['MSE: ', num2str(MSE)]);
0 Comments
Accepted Answer
Diwakar Diwakar
on 8 Jul 2023
To apply the `2^Level` in the code, you can modify the following lines:
level = 5; % Level of decomposition
Replace it with:
level = 5;
N = 2^level;
Then, when performing the SWT, use `N` as the length of the signal:
[swt_coeffs, ~] = swt(eeg_signal, N, wavelet);
Similarly, when storing the denoised subbands, use `N` as the length:
denoised_subbands = cell(N+1, 1);
In the loop for applying thresholding to each subband, change the loop condition:
for i = 1:N
Finally, when reconstructing the denoised EEG signal, modify the concatenation of coefficients:
denoised_coeffs = [swt_coeffs(N+1, :), denoised_subbands{N:-1:1}];
This way, you are using the desired level of decomposition, `level`, and applying `2^level` in the relevant parts of the code.
More Answers (0)
See Also
Categories
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!