
%====================================================================%
%设置ADC采集数据有效位宽
ADC_DATA_WIDTH = 16;
%ADC采集电压范围±VOL_RANGE (V)
VOL_RANGE = 5;
%采样率(Hz)
SAMPLE_RATE = 200000;
%ADC采集电压分辨率 LSB
LSB = VOL_RANGE*2/(2^ADC_DATA_WIDTH);
%====================================================================%
%读取文件数据，转换成波形显示
src_data=textread('D:\7606_DATA.txt','%s');
src_data_hex=hex2dec(src_data)
DATA_NUM = length(src_data_hex);


voltage_code = 1:DATA_NUM/2;
voltage_code = voltage_code';
for i=1:1:DATA_NUM/2
  if src_data_hex(2*i-1)+src_data_hex(i*2)*256 > 2^(ADC_DATA_WIDTH-1)
    voltage_code(i) = src_data_hex(2*i-1)+src_data_hex(i*2)*256-2^ADC_DATA_WIDTH;
  else
    voltage_code(i) = src_data_hex(2*i-1)+src_data_hex(i*2)*256;
  end
end

%二进制ADC数据转换为电压值
voltage = voltage_code*LSB - LSB/2;

%ADC采样时间
data_8_1 = voltage(1:8:end,1);
data_num = length(data_8_1);  %计算8组中一组的长度2048 

sample_t = 1:data_num;
sample_t = sample_t';
for i=1:1:data_num
  sample_t(i) = 1000000/SAMPLE_RATE * i;
end

subplot(4,2,1)
plot(sample_t,voltage(1:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
title('ADC采集电压显示','Fontsize',12,'position',[12000,6])
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第一通道')
grid on
 
subplot(4,2,2)
plot(sample_t,voltage(2:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第二通道')
grid on

subplot(4,2,3)
plot(sample_t,voltage(3:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第三通道')
grid on

subplot(4,2,4)
plot(sample_t,voltage(4:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第四通道')
grid on

subplot(4,2,5)
plot(sample_t,voltage(5:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第五通道')
grid on

subplot(4,2,6)
plot(sample_t,voltage(6:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第六通道')
grid on

subplot(4,2,7)
plot(sample_t,voltage(7:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第七通道')
grid on

subplot(4,2,8)
plot(sample_t,voltage(8:8:end,1));
axis([0 1000000/SAMPLE_RATE*data_num -VOL_RANGE VOL_RANGE]) 
xlabel('采样时间/(us)','Fontsize',10,'position',[9700,-7])
ylabel('电压/(V)','Fontsize',10)
legend('第八通道')
grid on
%双figure图形
figure
L = data_num/2;
NFFT = 2^nextpow2(L);

Y = fft(voltage(1:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,1)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第一通道')

Y = fft(voltage(2:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,2)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第二通道')

Y = fft(voltage(3:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,3)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第三通道')

Y = fft(voltage(4:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,4)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第四通道')

Y = fft(voltage(5:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,5)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第五通道')

Y = fft(voltage(6:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,6)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第六通道')

Y = fft(voltage(7:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,7)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第七通道')

Y = fft(voltage(8:8:end,1),NFFT)/L;
Fs = SAMPLE_RATE;
f = Fs/2*linspace(0,1,NFFT/2+1);
len = max(f);
data = 2*abs(Y(1:NFFT/2+1));
subplot(4,2,8)
plot(f,data);
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
axis([0 len 0 5])
legend('第八通道')

