How to calculate SNR before and after filtering in Matlab?

6 views (last 30 days)
Hi. I have this noisy signal and I want to calculate its SNR before and after filtering process. Please help me!!
f1=50;
f2=100;
fsampling=1000;
fn1_normfreq=0.4;
fn2_normfreq=0.5;
x1 = cos(2*pi*f1*[0:1/fsampling:1.23]);
x2 = cos(2*pi*f2*[0:1/fsampling:1.23]);
x = x1 + x2;
x(end) = [];
[b,a] = butter(2,[fn1_normfreq fn2_normfreq],'bandpass');
filtered_noise = filter(b,a,randn(1, length(x)*2));
noise = 0.5*filtered_noise(500:500+length(x)-1);
y = (x + noise)/length(x)*2; %noisy signal
%Lowpass FIR filter using rectangular window
fp=300;
fs=400;
rp=0.005;
rs=0.1;
% Normalizing the frequencies
wp=2*fp/fsampling;
ws=2*fs/fsampling;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/fsampling;
n=ceil(num/dem);
n1=n+1;
if (rem(n,2)~=0)
n1=n;
n=n-1;
end
w=rectwin(n1);
b=fir1(n,wp,'high',w);%Filter coefficients
%filtering
Y_filtered=filtfilt(b,1,y); %filtered signal

Answers (1)

Walter Roberson
Walter Roberson on 30 Nov 2015
A signal in isolation must always be considered to be a perfect signal, noiseless. You can only calculate SNR if you have at least two signals (one of which might be the constant signal, all 0, if you know that the other signal consists entirely of noise.)

Community Treasure Hunt

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

Start Hunting!