Why SNR value is negative in my code ? Is the MSE value is correct?

조회 수: 7 (최근 30일)
Komal
Komal 2023년 6월 19일
댓글: Komal 2023년 6월 19일
%%
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');
%%
[IMF, residual, info] = emd(eeg_signal);
imf_count = max(info.NumIMF);
figure;
for i=1:imf_count
subplot(6,2,i)
plot(IMF(:,i))
title("IMF"+i);
end
subplot(6,2,imf_count+1)
plot(residual)
title("Residue");
%%
thresholds = thselect(IMF, 'rigrsure'); % determine threshold for each IMF using universal thresholding
for i = 1:size(IMF, 2)
IMF(:, i) = wthresh(IMF(:, i), 's', thresholds(i));
end
% Reconstruct denoised signal from thresholded IMFs
denoised_signal = sum(IMF, 2) + residual;
clean_EEG = denoised_signal(1:2000);
% Plot results
figure;
subplot(2,1,1);
plot(eeg_signal);
title('Original signal');
subplot(2,1,2);
plot(clean_EEG);
legend( 'Denoised signal');
title('Denoised signal');
%%
original_signal = eeg_signal;
reconstructed_signal =clean_EEG;
% Calculate SNR
noise = original_signal - reconstructed_signal;
SNR = 20 * log10(norm(original_signal) / norm(noise));
% Calculate MSE
% MSE = mean((norm(original_signal) - norm(reconstructed_signal)).^2);
MSE = immse(norm(original_signal), norm(reconstructed_signal));
disp(['SNR: ', num2str(SNR), ' dB']);
disp(['MSE: ', num2str(MSE)]);

채택된 답변

Diwakar Diwakar
Diwakar Diwakar 2023년 6월 19일
In your code, the SNR value is negative because the calculation is incorrect. You are calculating the SNR using the formula:
SNR = 20 * log10(norm(original_signal) / norm(noise));
However, the denominator should be the norm of the noise, not just the noise itself. The correct calculation should be:
SNR = 20 * log10(norm(original_signal) / norm(noise));
Regarding the MSE value, you are using the `immse` function, which calculates the mean squared error between two signals. However, you are passing the norm of the signals instead of the signals themselves. To calculate the MSE correctly, you should use the original signals, like this:
MSE = immse(original_signal, reconstructed_signal);
  댓글 수: 1
Komal
Komal 2023년 6월 19일
What is the right formula and How to calculate SNR for geting right positive answer?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 EEG/MEG/ECoG에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by