ADC测试matlab代码

前面有做过ADC性能测试,测试方式是先使用ADC采集一个单频信号,然后利用matlab进行性能分析。

下面把matlab分析的代码记录下来:

  1 %The following program code plots the FFT spectrum of a desired test tone. Test tone based on coherent sampling criteria, and
  2 %computes SNR, SINAD, THD and SFDR.
  3 %This program is believed to be accurate and reliable. This program may get altered without prior notification.;
  4  
  5 %fid=fopen('F:pelican_ADC_testvjtag_prjdata_analysissingle_tone.txt','r');
  6 %numpt=input('Number of  Points in FFT? ');
  7 %fclk=input('Sampling Frequency (MHz)? ');
  8 %numbit=input('ADC Resolution (bits)? ');
  9 close all;
 10 clear all;
 11 
 12 numpt=4096;
 13 fclk=455/16;
 14 % fclk=455/16/14;
 15 numbit=12;
 16 
 17 % a=textread('dds_data_out.txt','%s')';%以字符形式打开文件 
 18 % a=dlmread('sample_data.txt','%s')';%以字符形式打开文件 
 19 a=dlmread('sample_data.txt','%s')';%以字符形式打开文件 
 20 v1=a'; %16进制转化为10进制数,存储进v1矩阵
 21 % for i = 1:numpt
 22 %     v1(i) = bitxor(v1(i),15872);
 23 % end
 24 
 25 %v1 is a column vector(1:numpt), and count is its length 
 26 %v1=dlmread('F:pelican_ADC_testvjtag_prjdata_analysissingle_tone.txt','');
 27 %fclose(fid);
 28 %change v1 into a row vector
 29 code=v1';
 30  
 31 %Warning: ADC output may be clipping - reduce input amplitude
 32 if (max(code)==2^numbit-1) | (min(code)==0)
 33   disp('WARNING: ADC OUTPUT MAYBE CLIPPING - CHECK INPUT AMPLITUDE!');
 34 end
 35  
 36 figure;
 37 plot(code);
 38 title('TIME DOMAIN')
 39 xlabel('SAMPLES');
 40 ylabel('DIGITAL OUTPUT CODE');
 41 Dout=code-(2^numbit-1)/2; %Re-center the digitized sinusoidal input
 42 Voltage=Dout./((2^numbit-1)/2)*(0.5);
 43 figure;
 44 plot([1:numpt],Voltage);
 45 
 46 Doutw=(Dout').*blackmanharris(numpt);               %add Minimum 4-term Blackman-Harris window
 47 Dout_spect=fft(Doutw);
 48 Dout_dB=20*log10(abs(Dout_spect));
 49 
 50 figure;
 51 maxdB=max(Dout_dB(1:numpt/2));     %numpt points FFT result in numpt/2 points spectrum
 52 
 53 %计算距离满量程的幅度差
 54 max_voltage=max(Voltage);
 55 delta_amplitude=20*log10(max_voltage/0.5);      %full scale voltage amplitude is 0.5v
 56 
 57 plot([0:numpt/2-1].*fclk/numpt,Dout_dB(1:numpt/2)-maxdB+delta_amplitude);
 58 grid on;
 59 title('SINGLE TONE FFT PLOT');
 60 xlabel('ANALOG INPUT FREQUENCY (MHz)');
 61 ylabel('AMPLITUDE (dBfs)');
 62 a1=axis; axis([a1(1) a1(2) -140 a1(4)]);
 63 fin=find(Dout_dB(1:numpt/2)==maxdB); %Find the signal bin (DC represents bin=1)
 64 DC_span=6;              %default DC leakage bins are 6 bins    
 65 signal_span = 10;       %signal leakage bins are ±10 bins for minumun 4-term black-harris window
 66 spanh=3;                %%default harmonic leakage bins are ±3 bins 
 67 spectP=(abs(Dout_spect)).*(abs(Dout_spect));
 68 %Determine power level
 69 Pdc=sum(spectP(1:DC_span)); %Determine DC offset power level
 70 Ps=sum(spectP(fin-signal_span:fin+signal_span)); %Determine signal power level
 71 Fh=[];
 72 %Vector storing frequency and power components of signal and harmonics
 73 Ph=[]; %HD1=signal, HD2=2nd harmonic, HD3=3rd harmonic, etc.
 74  
 75 %Find the harmonic frequencies/power within the FFT plot
 76 for har_num=1:10
 77   tone=rem((har_num*(fin- 1)+1)/numpt,1);   %Note: tones > fSAMPLE are aliased back
 78   if tone>0.5
 79      tone=1-tone;
 80   end
 81   Fh=[Fh tone];
 82  
 83 %For this method to work properly, make sure that the folded back high order harmonics do not overlap with DC and signal
 84 %components or lower order harmonics.
 85   har_peak=max(spectP(round(tone*numpt)-spanh:round(tone*numpt)+spanh));
 86   har_bin=find(spectP(round(tone*numpt)-spanh:round(tone*numpt)+spanh)==har_peak);
 87   har_bin=har_bin+round(tone*numpt)-spanh- 1;     %make sure that the folded back high order harmonics do not overlap with DC and signal components or lower order harmonics
 88   Ph=[Ph sum(spectP(har_bin-3:har_bin+3))];
 89 end
 90  
 91 Pd=sum(Ph(2:10)); %Total distortion power level
 92 Pn=sum(spectP(1:numpt/2))-Pdc-Ps-Pd; %Extract noise power level
 93 format;
 94 A=(max(code)-min(code))/(2^numbit) %Analog input amplitude in mV
 95 AdB=20*log10(A) %Analog input amplitude in dB
 96 SNR=10*log10(Ps/Pn) %SNR in dB
 97 SINAD=10*log10(Ps/(Pn+Pd))  %SINAD in dB
 98 disp('THD - HD2 through HD9');
 99 THD=10*log10(Pd/Ph(1)) %THD in dB
100 SFDR=10*log10(Ph(1)/max(Ph(2:10))) %SFDR in dB
101 disp('SIGNAL AND HARMONIC POWER (dB)');
102 HD=10*log10(Ph(1:10)/Ph(1))
103  
104 hold on;
105 plot(Fh(2)*fclk,-70,'bo',Fh(3)*fclk,-70,'bx',Fh(4)*fclk,-70,'b+',Fh(5)*fclk,-70,'b*',Fh(6)*fclk,-70,'bs',Fh(7)*fclk,-70,'bd',Fh(8)*fclk,-70,'bv',Fh(9)*fclk,-70,'b^');
106 legend('SIGNAL','HD2','HD3','HD4','HD5','HD6','HD7','HD8','HD9');
107 hold off;

输出结果

 1 A =
 2 
 3     0.5432
 4 
 5 
 6 AdB =
 7 
 8    -5.3006
 9 
10 
11 SNR =
12 
13    54.0005
14 
15 
16 SINAD =
17 
18    53.4240
19 
20 THD - HD2 through HD9
21 
22 THD =
23 
24   -62.4784
25 
26 
27 SFDR =
28 
29    63.0618
30 
31 SIGNAL AND HARMONIC POWER (dB)
32 
33 HD =
34 
35          0  -63.0618  -78.1190  -79.6691  -82.4058  -86.1153  -90.7795  -91.1868  -89.8460  -74.6853

会标记出谐波的位置。

原文地址:https://www.cnblogs.com/chenbei-blog/p/4682395.html