# How to calculate SNR before and after filtering for noise cancellation in an ecg signal in matlab?

I am working on noise cancellation in ecg signal and I want to calculate the Signal to noise ratio(SNR) before applying filter to the signal and after applying filter i.e., snr_before and snr_after. I used the following commands:

snr_before = mean(input_or_desired_signal.^2)/mean(noise_signal.^2);
residual_noise = desired_signal - filtered_output_signal;
snr_after = mean( desired_signal .^ 2 ) / mean( residual_noise .^ 2 );


The code works as no error is generated. But only the box is displayed when I plot d same in each case and no graph is generated.. Pls help Thanks in advance..

• It's not clear what do you ask. Is your question about how to calculate SNR or how to plot something? – Gluttton Mar 22 '15 at 9:31
• @Gluttton Actually, I tried to plot SNR before filtering my signal and after that. The code works completely fine as it runs when I simulate it. A figure for SNR is generated but it is empty... Since no plot or graph is generated. M doing it for( i )iterations. This is the actual problem, an empty figure... I can't find out the reason. Is the code that I'm using correct? – me001 Mar 23 '15 at 12:44
• snr_before = mean(input_or_desired_signal.^2)/mean(noise_signal.^2); residual_noise = desired_signal - filtered_output_signal; snr_after = mean( desired_signal .^ 2 ) / mean( residual_noise .^ 2 ); – me001 Mar 23 '15 at 12:46
• Are snr_before and snr_after scalar values or matrices (vectors)? I think they are scalar values, so they are just two digits and couldn’t be plotted as lines. – Gluttton Mar 23 '15 at 12:53