Calculating SNR

85 Views Asked by At

I am having trouble calculating SNR. We are given a wave file that we have to filter and then calculate the SNR before and after each filter and compare. I calculated the SNR using:

dataNOTCH=filter(bNOTCH,aNOTCH,dataIIR);
NOTCHfft = fft(dataNOTCH);
NOTCHfft = NOTCHfft(1:N/2+1);
NOTCHps = (1/(Fs*N)) * abs(NOTCHfft).^2;
NOTCHps(2:end-1) = 2*NOTCHps(2:end-1);
SigPowN=sum(NOTCHps(pt100:pt4k))/(pt4k-pt100);
NoiPowN=(sum(NOTCHps(1:pt100-1))+sum(NOTCHps(pt4k+1:end)))/((length(NOTCHps)-(pt4k-pt100)));
NOTCHsnr=10*log10(SigPowN/NoiPowN);

And this seems to work, as far as I can tell. But our teacher wants us to use the time series not frequency to calculate power. Now I have tried

noisePow=mean((msg-dataNOTCH).^2);
NPow=mean((dataNOTCH).^2);
Nsnr=10*log10(NPow/noisePow);

Where msg is the original signal with noise and dataNOTCH is the most filtered signal. But this doesn't seem to be right, it doesn't match what I calculated in the frequency domain and the filtered signal is getting a lower SNR than unfiltered.

Any help would be greatly appreciated, thank you.

EDIT: More info-

Calculating in the frequency domain I am getting

Before Filtering -17.636dB
After Filtering 23.9784dB

Calculating in the time domain I am getting

Before Filtering 0.0221dB
After Filtering -24.9506dB