[英]How to calculate SNR of a signal in Matlab?
我想計算噪聲信號和濾波后信號的SNR。 我使用了以下代碼,但是不確定結果是否正確。 有人可以幫我嗎?
%generate the noisy signal which will be filtered
x= cos(2*pi*12*[0:0.001:1.23]);
x(end) = [];
[b,a] = butter(2,[0.6 0.7],'bandpass');
filtered_noise = filter(b,a,randn(1, length(x)*2));
y = (x + 0.5*filtered_noise(500:500+length(x)-1))/length(x)*2;
%Use matlabs built-in buttord function to get the optimum order to meet a
specification
[N,Wn] = buttord(0.1, 0.5, 5, 40)
%use the N and Wn values obtained above to design the filter in the usual
way
[b,a] = butter(N, Wn, 'low');
%filter the signal and plot the ouput of the filter
Y_filtered = filter(b,a,y);
%calculation of snr before filtering
snr_before = mean(x.^2) / mean(filtered_noise.^2);
snr_before_db = 10 * log10(snr_before) % in dB
%calculation of snr after filtering
residual_noise = x - Y_filtered;
snr_after = mean(x.^2) / mean(residual_noise.^2);
snr_after_db = 10 * log10(snr_after)
我得到的結果是:
snr_before_db =
6.8725
snr_after_db =
0.0132
我認為在濾波前后計算PSNR的正確方法不正確。 另外,您計算PSNR的方式也不正確。 僅在濾波后才可以計算PSNR。 嘗試這個
peaksnr = psnr(X,Y_filtered)
要么
MSE = (1/length(X))*sum((X-Y_filtered).^2);
peaksnr = 10*log10(max(X(:))^2/MSE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.