修改老师的程序 测试ENOB
%**************************************************************************
% Read AD result from file to evaluate TMS320F2812 AD performance
%
% 2010.04.18
%**************************************************************************
clc;
clear;
close all;
[FileName,PathName] = uigetfile('*.txt','Select the *.dat file');
f = fullfile(PathName,filesep,FileName);
fid = fopen(f,'r');
[data,M] = fscanf(fid,'%x');
fclose(fid);
data = data';
data1 = zeros(1,2*length(data));
data1(1:2:end) = bitand(data,hex2dec('ffff'));
data1(2:2:end) = fix(data/hex2dec('ffff'));
N=2*M;
data2=data1;
for i = 1:N
if(data1(i)>2^15-1)
data2(i)=data1(i)-2^16;
end
end
figure(1)
plot(data2);
datfft = fft(data2);
nordat = abs(datfft)/max(abs(datfft));%归一化
figure(2)
stem(nordat);
datfft(1) = 0;
[maxval,pos] = max(abs(datfft(1:N)));
datfft_hf = nordat(1:N/2);
sig_rms = (norm(datfft_hf(pos-5:pos+5)));
nos_rms = sqrt((sum(datfft_hf(2:N/2).^2)-sig_rms.^2));
SINAD = 20*log10(sig_rms/nos_rms);
ENOB = (SINAD-1.76)/6.02;
fs = 32e3;
figure(3),
plot([0:N/2-1]*fs/N/1000,20*log10(nordat(1:N/2)));xlabel('频率/KHz');ylabel('幅度/dB');axis tight;
title(['SINAD=',num2str(SINAD),'dB',char(10),'ENOB=',num2str(ENOB),'bit']);