clear;close all;
% clc;
tic
NN=1;
j=sqrt(-1);
for iii=1:NN
LOADING=0;
save_dir = 'E:\Test_Xiang_SC4QAM\';
SNR_Nx=[];SNR_Ny=[];
file_index=1;
resolusion=0.004;
loop_res=10;
calc_times=0;
if(LOADING) % 若使用历史数据空转则不关闭此段采集
%===================================================
% Note: 任何时候使用TekVISA 建立Matlab与连接时,每次提示
% Warning: VISA: Error - The specified operation or request timed out
% 后停止都需要重启Matlab, don't ask
% 同时检查channel 是否都有数据输入、到设备连接是否OK(用Openchoice talk\listener query 就OK)。
srcName = 'ch1,ch2,ch3,ch4'; % 指定采集全部4个通道
% recordLen = 1e6; % 指定存储记录长度等于示波器水平窗口长度。 对应DSA 上有5个帧间隔,与下列参数关联
SampleRate = 1/20e-12; % 设置示波器采样率 20ps/pt
paraHs = 1e-6; % 设置示波器水平分辨率 4us/div
recordLen = paraHs*10*SampleRate+1; % 指定存储记录长度等于示波器水平窗口长度。 对应DSA 上有5个帧间隔,与下列参数关联
instr = visa('tek','tcpip0::172.168.0.6::instr'); % DSA 的IP 172.168.0.2
set(instr, 'InputBufferSize', recordLen*4); % 4个channel 则size需乘以对应倍数
set(instr, 'OutputBufferSize', recordLen*4);
set(instr, 'Timeout', 3); % 避免超时空等,最多3秒
fopen(instr);
disp '==Begin ==============================================='
idn = query(instr,'*IDN?'); % 例行查询,若能正常echo证明连接OK.
% 设置好抓取参数后开跑,此部分参数需再确认!
% 参见\matlab控制示波器实例\tek scope matlab support
% 中代码指定采样点数和interval等参数,强制指定参数,避免每次手动调整的差异
%fprintf(instr,'HEADER OFF');
fprintf(instr,['DATA:SOURCE ' srcName]);
fprintf(instr,'DATA:ENCDG SRIBINARY;');
fprintf(instr,'DATA:WIDTH 1');
fprintf(instr,'ACQUIRE:STATE OFF');
fprintf(instr,'ACQUIRE:MODE NORMALSAMPLE');
fprintf(instr,'ACQUIRE:STOPAFTER SEQUENCE');
fprintf(instr,['HORizontal:RECOrdlength ', num2str(recordLen)]);
fprintf(instr,['HORizontal:MODE:SAMPLERate ', num2str(SampleRate)]);
fprintf(instr,['HORizontal:MODE:SCAle ' num2str(paraHs)]);
fprintf(instr,'ACQUIRE:STATE RUN');
% fprintf(instr,'ACQUIRE:STATE OFF')
disp 'Scope parameters set, Now pausing to finish acquisition'
pause(0.5);
% 因为实验开始示波器的X、Y轴等参数就不动了,且各通道一致,此时可先计算。
% % get x zero
% xZ = str2num(query(instr,'WFMOUTPRE:XZERO?'));
% % get the sampling interval
sI = str2num(query(instr,'WFMOUTPRE:XINCR?'));
% % get the trigger point within the record
% iT = str2num(query(instr,'WFMOUTPRE:PT_OFF?'));
horizLen = str2num(query(instr,':HOR:RECORD?'));
ymult = str2num(query(instr,'WFMOUTPRE:YMULT?'));
yoff = str2num(query(instr,'WFMOUTPRE:YOFF?'));
yzero = str2num(query(instr,'WFMOUTPRE:YZERO?'));
%-------------------------------------------------------------------------
% 从结果中依次query 4个通道的结果,各通道大同小异
for ch_index = 1:1:4
fprintf(instr,[':DATA:SOUR ' 'ch' num2str(ch_index) ';START ' num2str(1) ';STOP ' num2str(horizLen)]);
fprintf(instr,':CURVE?');
[header, count] = fscanf(instr,'%c', 2);
header1 = fscanf(instr,'%c',str2num(header(2)));
data = []; % 每次使用前清空data
Len = min(horizLen, recordLen); % 取buf和实际采样窗口的大者,size过大会溢出,过小则找不到帧头
[data,count] = fread(instr,Len,'int8');
%Curveterminator = fread(instr,1,'char');
% scale the data to the correct values
waveform_ch(:,ch_index) = ymult*(data - yoff) - yzero;
% 若存成.txt 且带时间后缀 ------------------------------------------------------
% save_time = clock; %c = [year month day hour minute seconds]
% file_sufix = ['(' num2str(save_time(3)) '_' num2str(save_time(4)) '_' num2str(save_time(5)) '_' num2str(save_time(6)) ').txt'];
% fid = fopen([save_dir 'ch' num2str(ch_index) file_sufix],'w');
% 若存成.dat 文本 ------------------------------------------------------
% [path num2str(1000+datafile) '_Ch1.dat'];
% fid = fopen([save_dir num2str(1000 + file_index) '_Ch' num2str(ch_index) '.dat'],'w');
% fprintf(fid,'%d\n',waveform_ch(:,ch_index));
% fclose(fid);
end;
% 将waveform_ch矩阵 存到 .mat ,文件尺寸能小至1/8 ------------------------------------
savefile = [save_dir num2str(1000 + file_index) '.mat'];
save(savefile, 'waveform_ch');
% fprintf(instr,'ACQUIRE:STATE ON');
% figure if necessary --------------
% figure(1);
% subplot(221);
% plot(waveform_ch(:,1),'Color','Yellow'); % Ch1
% subplot(222);
% plot(waveform_ch(:,2),'Color','Blue'); % Ch2
% subplot(223);
% plot(waveform_ch(:,3),'Color','Red'); % Ch3
% subplot(224);
% plot(waveform_ch(:,4),'Color','Green'); % Ch4
% flush 干净再关闭,避免溢出风险
flushinput(instr);
flushoutput(instr);
% fprintf(instr,'ACQUIRE:STATE ON');
fclose(instr);
delete(instr);
disp '--Finished ---------------------------------------'
%-------------------------------
end; % 采集数据结束
Nb=2; % over sampling, e.g., 128 subcarriers, but samplied 2*128 times.
AWG=5;
SCOPE=50;
% [stream,transmitted,pattern,synchead,TS,Band1,Band2]=OFDMgenerator_Xiang(0);% 0 ---> no loading data out 1 ---> yes
% stream_len=length(stream);
% close all;
if(LOADING) % 直接使用前面采集的数据结果
% datafile=1;
% % path=['z:\'];
% path = save_dir;
% path1=[path num2str(1000+datafile) '_Ch1.dat'];
% Ix=load(path1);
% path1=[path num2str(1000+datafile) '_Ch2.dat'];
% Qx=load(path1);
% path1=[path num2str(1000+datafile) '_Ch3.dat'];
% Iy=load(path1);
% path1=[path num2str(1000+datafile) '_Ch4.dat'];
% Qy=load(path1);Qy=-Qy;
%
% fprintf('******calibration begin******\n');
% Qy=procedureRIGHT2(Qy).';
% % Qy=Qy.';
% Qx=procedureLEFT2(Qx).';
% % Qx=Qx.';
% fprintf('******calibration end******\n');
%
% x_stream=Ix+j*Qx;%for inte3rleaving
% y_stream=Iy+j*Qy;%for interleaving
%
% save ('x_stream.mat','x_stream');
% save ('y_stream.mat','y_stream');
% 若直接共享前面的变量,就不用从文件中load. 注意对应关系和正负
if(CPshift==CPshiftLOW)
fprintf('******calibration begin******\n');
% [waveform_ch(:,4),phase_R_mis]=procedureRIGHT2(waveform_ch(:,4),-0.022); % C-band
[waveform_ch(:,4),phase_R_mis]=procedureRIGHT2(waveform_ch(:,4),-0.013); % L-band
% waveform_ch(:,4)=waveform_ch(:,4).';
[waveform_ch(:,2),phase_L_mis]=procedureLEFT2(waveform_ch(:,2),0.014);
% waveform_ch(:,2)=waveform_ch(:,2).';
fprintf('******calibration end******\n');
phase_L_mis=1.62;
I= 1.09*waveform_ch(:,1);
Q= waveform_ch(:,2);
Q=(Q+I*sin(phase_L_mis-pi/2))/cos(phase_L_mis-pi/2);
x_stream = Q+j*I;
phase_R_mis=1.59; % C-band
% phase_R_mis=1.55; %L-band
I=1.09*waveform_ch(:,3);
Q= waveform_ch(:,4);
Q=(Q+I*sin(phase_R_mis-pi/2))/cos(phase_R_mis-pi/2);
y_stream = Q+j*I;
save ('x_stream.mat','x_stream');
save ('y_stream.mat','y_stream');
else
load ('x_stream.mat');
load ('y_stream.mat');
end
else % 使用转储文件中的历史结果
% stream1=[stream stream].';
% stream2=circshift(stream1.',[0 sync_len]).';
%
% Pratio=1;
% x_stream=stream1.*Pratio.^0.5+stream2.*(1-Pratio).^0.5;
% y_stream=stream2.*Pratio.^0.5+stream1.*(1-Pratio).^0.5;
%
%
% Sim_SNR=30;
% x_stream=awgn(x_stream,Sim_SNR,'measured')*0.7;
% y_stream=awgn(y_stream,Sim_SNR,'measured')*0.7;
savefile = [save_dir num2str(10