以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  如何計算SNR (signal-to-ratio)? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=73135)


--  作者:卷积内核
--  发布时间:3/17/2009 8:08:00 AM

--  如何計算SNR (signal-to-ratio)? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)
SNR公式如下
按此在新窗口浏览图片


1
/**//*
2(C)
3
4Filename    : SNR.cpp
5Compiler    : Visual C++ 8.0 / C++/CLI
6Description : Demo how to compute SNR
7Release     : 12/20/2006 1.0
8*/
9#include "stdafx.h"
10#include <iostream>
11#include <cmath>
12
13using namespace System::Drawing;
14using namespace System::Drawing::Imaging;
15using namespace std;
16
17// Convert RGB to gray level int
18int colorToInt(Color);
19
20// Calculate signal to ratio
21double SNR(Bitmap^, Bitmap^);
22
23int main() {
24  // Read lena.jpg
25  Bitmap^ oriImg = gcnew Bitmap("lena.jpg");
26  // Declare Gaussian image for lena.jpg
27  Bitmap^ noiseImg = gcnew Bitmap("lena_gaussian.jpg");
28
29  double snr = SNR(oriImg, noiseImg);
30
31  cout << "SNR of image:" << snr << endl;
32
33  return 0;
34}
35
36// Convert RGB to gray level int
37int colorToInt(Color color) {
38  return (color.R + color.G + color.B) / 3;
39}
40
41
42// Calculate signal to ratio
43double SNR(Bitmap^ oriImg, Bitmap^ noiseImg) {
44
45  // ||n||
46  int n = oriImg->Height * oriImg->Width;
47
48  // compute mean of signal
49  double sumU = 0.0;
50  for(int y = 0; y != oriImg->Height; ++y) {
51    for(int x = 0; x != oriImg->Width; ++x) {
52      sumU += (double)colorToInt(oriImg->GetPixel(x, y));
53    }
54  }
55  double u = (double)sumU / n;
56  
57  // compute variance of signal
58  double diffVS = 0.0, sumVS  = 0.0;
59  for (int y = 0; y != oriImg->Height; ++y) {
60    for (int x = 0; x != oriImg->Width; ++x) {
61      diffVS = (double)colorToInt(oriImg->GetPixel(x, y)) - u;
62      sumVS += diffVS * diffVS;
63    }
64  }
65  double VS = (double)sumVS / n;
66
67  // compute mean of noise
68  double sumN = 0.0;
69  for (int y = 0; y != noiseImg->Height; ++y) {
70    for (int x = 0; x != noiseImg->Width; ++x) {
71      sumN += (colorToInt(noiseImg->GetPixel(x, y)) - colorToInt(oriImg->GetPixel(x, y)));
72    }
73  }
74  double un = (double)sumN / n;
75
76  // compute variance of noise
77  double diffVN = 0.0, sumVN = 0.0;
78  for (int y = 0; y != noiseImg->Height; ++y) {
79    for (int x = 0; x != noiseImg->Width; ++x) {
80      diffVN = (double)colorToInt(noiseImg->GetPixel(x, y)) -colorToInt(oriImg->GetPixel(x, y))- un;
81      sumVN += diffVN * diffVN;
82    }
83  }
84  double VN = (double)sumVN / n;
85
86  return 20 * log10(sqrt(VS) / sqrt(VN));
87}

執行結果


SNR of image:0.16249
請按任意鍵繼續 . . .

原圖


按此在新窗口浏览图片

Noise圖片
按此在新窗口浏览图片


--  作者:卷积内核
--  发布时间:3/17/2009 8:16:00 AM

--  
理論上,一個的(信號與噪聲的比值)等於(6.02N+1.76)dB,這裏N等於ADC的位數。雖然我的數學技巧有點生疏,但我認為任何一個16位轉換器的信噪比應該是98.08dB。但當我查看模數轉換器的數據手冊時,我看到一些不同的情況。比如,16位的(逐次逼近型)模數轉換器指標的典型值通常可低至84dB高達95dB。生產廠家很自豪地把這些值寫在產品的數據手冊的首頁,而且坦率地說,信噪比為95dB的16位ADC具有競爭力。除非我錯了,計算的98.08dB高於所找到最好的16位ADC數據手冊中的96dB。那麼,這些位數到那去了?
  讓我們先找出理想化的公式(6.02N+1.76)從何而來。任何係統的信噪比,用分貝來表示的話,等於20log10(信號的均方根/噪音的均方根)。推導出理想的信噪比公式時,首先定義信號的均方根。如果把信號的峰峰值轉換為均方根,則除以按此在新窗口浏览图片 即可。ADC的均方根信號用位數表示等於按此在新窗口浏览图片,這裏q是LSB(最低有效位)。

  所有ADC產生量化噪聲是把輸入信號抽樣成離散“桶”的後果。這些桶的理想寬度等於轉換器LSB的大小。任何ADC位的不確定值是±1/2 LSB 。如果假定對應每個位誤差的響應是三角形的話,則其均方根等於LSB信號的幅值除以按此在新窗口浏览图片,均方根的噪聲則按此在新窗口浏览图片

  綜合均方根和均方根噪聲條件,理想ADC的SNR用分貝表示為:

按此在新窗口浏览图片

  重複剛才的問題,那些位數到底去那了? 那些ADC的供應商熱情地解釋這個失位現象,因為他們的眾多試驗裝置表明產品具有良好的信噪比。從根本上說,他們認為電阻和晶體管的噪聲導致了這種結果。供應商測試其ADC的SNR是通過將他們的數據帶入下麵的公式:

按此在新窗口浏览图片

  這些理論和測試SNR的公式是完善的,但他們隻能提供部分你需要知道的轉換器到底能給予你的位數。 (總諧波失真),另一個要注意的ADC指標,定義為諧波成分的均方根和,或者是輸入信號功率的比值

按此在新窗口浏览图片或者

這裏HDx是x次諧波失真諧波的幅值,PS是一次諧波的信號功率,Po是二次到八次諧波的功率。ADC的重要指標,INL(積分非線性)誤差清晰地出現在THD結果中。

  最後,SINAD(信號與噪聲+失真比)定義為信號基波輸入的RMS值與在半采樣頻率之下其它諧波成分RMS值之和的比值,但不包括直流信號。對SAR和流水線型而言,SINAD的理論最小值等於理想的信噪比,或6.02N+1.76dB。至於Δ-Σ轉換器的理想SINAD等於(6.02N+1.76dB+按此在新窗口浏览图片,其中fS是轉換器采樣頻率,BW是感興趣的最大帶寬。非理想SINAD值為按此在新窗口浏览图片或者按此在新窗口浏览图片

       其中PS是基波信號功率,PN是所有噪聲譜成分的功率,PD是失真譜成分功率。

  因此,下一次當你尋找丟失的位數時,記住它是結合了SNR、THD和SINAD等多個指標,這些可以讓您全麵了解ADC的真實位數--無論它采用的是逐次逼近型、流水線型還是Δ-Σ技術,不管在數據手冊的第一頁中提到有多少位。

       附英文原文:

  SNR in ADCs: Where did all the bits go?

  Theoretically, the SNR for any 16-bit converter should be 98.08 dB. But I see something different when I read converter data sheets.

  By Bonnie Baker -- EDN, 6/7/2007

  Theoretically, the SNR (signal-to-noise ratio) of an ADC is equal to (6.02N+1.76) dB, where N equals the number of ADC bits. Although I’m a little rusty with my algebra skills, I think that the SNR for any 16-bit converter should be 98.08 dB. However, I see something different when I read converter data sheets. For instance, the specification for a 16-bit SAR (successive-approximation-register) converter can typically be as low as 84 dB and as high as 95 dB. Manufacturers proudly advertise these values on the front page of their data sheets, and, frankly, an SNR of 95 dB for a 16-bit SAR converter is competitive. Unless I am wrong, the 98.08 dB I calculate is higher than the 95-dB specification that I find with the best of the 16-bit-converter data sheets. So, where did the bits go?


  Let’s start by finding out where this ideal formula, 6.02N+1.76, comes from. The of any system, in decibels, is equal to 20 log10 (rms signal/rms noise). When you d erive the ideal SNR formula, you first define the rms signal. If you change a peak-to-peak signal to rms, you divide it by the 按此在新窗口浏览图片The rms signal in bits is equal 按此在新窗口浏览图片 where q is the LSB (least-significant bit).

  All ADCs generate quantization noise as a consequence of dividing the input signal into discrete “buckets.” The ideal width of these buckets is equal to the converter’s LSB size. The uncertainty of any ADC bit is ±1/2 LSB. If you assume that this error’s response is triangular across each bit, the rms value equals this LSB signal’s magnitude divided by按此在新窗口浏览图片:rms noise按此在新窗口浏览图片

  Combining the rms-signal and rms-noise terms, the ideal ADC SNR in decibels is:

按此在新窗口浏览图片

  Again, where did the bits go? The ADC vendors enthusiastically explain the missing-bits phenomenon, because they bench-test their devices to see how good the SNR is. Fundamentally, they find that the device noise from resistors and transistors creeps into the results. Vendors test their ADC SNR by inputting their data into the following formula:

按此在新窗口浏览图片

  These theoretical and tested SNR formulas are complete, but they provide only part of what you need to know about how many bits your converter is truly giving you. (total harmonic distortion), another ADC specification you need to watch, is the ratio of the rms sum of the powers of the harmonic components, or spurs, to the input-signal power:  

按此在新窗口浏览图片or

  where HDx is the magnitude of distortion at the Xth harmonic, PS is the signal power of the first harmonic, and PO is the power of harmonics two through eight. Significant ADC INL (integral-nonlinearity) errors typically appear in the THD results.


  Finally, SINAD (signal-to-noise and distortion) is the ratio of the fundamental input signal’s rms amplitude to the rms sum of all other spectral components below half of the sampling frequency, excluding dc. The theoretical minimum for SINAD is equal to the ideal SNR, or 6.02N+1.76 dB, with SAR and pipeline converters. For delta-sigma converters, the ideal SINAD equals 6.02N+1.76 dB+10 log10(fS/(2BW)), where fS is the converter sampling frequency and BW is the maximum bandwidth of interest. The not-so-ideal value of SINAD is 按此在新窗口浏览图片 or按此在新窗口浏览图片.

  where PS is the fundamental signal power, PN is the power of all the noise spectral components, and PD is the power of all the distortion spectral components.

  So, the next time you’re looking for lost bits, remember that it is the combination of SNR, THD, and SINAD that gives you the complete picture of the real bits in your ADC―regardless of whether it’s SAR, pipeline, or delta-sigma technology and regardless of the number of bits that the first page of the data sheet mentions.


--  作者:卷积内核
--  发布时间:3/17/2009 8:19:00 AM

--  
求信噪比计算公式(摘自论坛)

各位高手大家好!求各位给个信噪比的计算公式。数据都是现场故障数据,所以公式必须是原始信号和降噪后信号的关系。
一下是我计算信噪比的公式,但是可能有错误!
function y=snr(x1,x2);%x1是原始信号,x2是降噪后信号
N=length(x1);
y1=sum(x1.^2);
y2=sum((x1-x2).^2);
y=10*log((y1/y2));

你得到的是原始信号相对于被降噪噪声的能量比[/quote]
[quote]function y=snr(x1,x2);%x1是原始信号,x2是降噪后信号
N=length(x1);
y1=sum(x1.^2);
y2=sum((x1-x2).^2);
y=10*log((y1/y2));

function y=snr(x1,x2);%x1是原始信号,x2是降噪后信号


y1=sum(x2.^2); %不是x1
y2=sum((x1-x2).^2);
y=10*log10((y1/y2)); %不是log()


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
1,371.094ms