%进制转换
clc;clear;close all;
%导入数据
load('env_in0.mat');
RXI=real(env_inbf);
RXQ=imag(env_inbf);
%变换
width = 12;%精度
depth=length(env_inbf); %数据长度
% addr = [0:depth - 1];
RXI_Ts = fix(RXI*(2^width-1)/2 + 0.4999); %四舍五入
RXQ_Ts = fix(RXQ*(2^width-1)/2 + 0.4999); %四舍五入
% RXI_T=RXI_Ts;
% RXQ_T=RXQ_Ts;
% subplot(1,2,1)
% qwer(RXI_Ts,RXQ_Ts)
RXI_Ts(find(RXI_Ts<0))=RXI_Ts(find(RXI_Ts<0)) + 2^width; %求补码
RXQ_Ts(find(RXQ_Ts<0))=RXQ_Ts(find(RXQ_Ts<0)) + 2^width; %求补码
%补码恢复
% RXI_TR=RXI_Ts;
% RXQ_TR=RXQ_Ts;
% RXI_TR(find(RXI_TR>2^(width-1)))=RXI_TR(find(RXI_TR>2^(width-1)))-2^width;
% RXQ_TR(find(RXQ_TR>2^(width-1)))=RXQ_TR(find(RXQ_TR>2^(width-1)))-2^width;
% Error_findI = find(RXI_T~=RXI_TR);
% Error_findQ = find(RXQ_T~=RXQ_TR);
% subplot(1,2,2)
% qwer(RXI_TR,RXQ_TR)
file = fopen('RXI_Ts.txt','wt');
% fprintf(file,'DEPTH=%d;\n',depth);
for i=1:depth
fprintf(file,'%04X\n', RXI_Ts(i));
end
fclose(file);
file = fopen('RXQ_Ts.txt','wt');
% fprintf(file,'DEPTH=%d:\n',depth);
for i=1:depth
fprintf(file,'%04X\n', RXQ_Ts(i));
end
fclose(file);
% plotSpectrum( RXI_T,1024,92160000);grid on;
% hold on;
% plotSpectrum( RXI_TR+1i*RXQ_TR,1024,92160000);grid on;
%进制转换
clc;clear;close all;
% 导入数据
fid = fopen('RXQ_Ts.txt');
test = fscanf(fid, '%s\n');
tt = fi(0,1,16,0);
datai0 = zeros(1,length(test)/4);
for i = 1:length(test)/4
tt.hex = test((4*i-3):4*i);
datai0(1,i)=tt.data;
end
fid = fopen('RXQ_Ts.txt');
test = fscanf(fid, '%s\n');
tt = fi(0,1,16,0);
dataq0 = zeros(1,length(test)/4);
for i = 1:length(test)/4
tt.hex = test((4*i-3):4*i);
dataq0(1,i)=tt.data;
end
subplot(1,2,1)
qwer(dataq0,datai0)
width = 12;%精度
depth=length(datai0); %数据长度
%补码恢复
datai1=datai0;
dataq1=dataq0;
datai1(find(datai1>2^(width-1)))=datai1(find(datai1>2^(width-1)))-2^width;
dataq1(find(dataq1>2^(width-1)))=dataq1(find(dataq1>2^(width-1)))-2^width;
% datai1(find(datai1<0))=datai1(find(datai1<0)) + 2^width; %求补码
% dataq1(find(dataq1<0))=dataq1(find(dataq1<0)) + 2^width; %求补码
%
% subplot(1,2,2)
% qwer(dataq1,datai1)
scale0 = max(abs(datai1)); %输入的最大值
datai2 = datai1/scale0; %规一化
scale1 = max(abs(dataq1));
dataq2 = dataq1/scale1; %规一化
% %变换
% RXI_Ts = fix(RXI*(2^width-1)/2 + 0.4999); %四舍五入
% RXQ_Ts = fix(RXQ*(2^width-1)/2 + 0.4999); %四舍五入
file = fopen('dec_i.txt','wt');
% fprintf(file,'DEPTH=%d;\n',depth);
for i=1:depth
fprintf(file,'%.4f\n', datai2(i));
end
fclose(file);
file = fopen('dec_q.txt','wt');
% fprintf(file,'DEPTH=%d:\n',depth);
for i=1:depth
fprintf(file,'%.4f\n', dataq2(i));
end
fclose(file);
%
% plotSpectrum( RXI_T,1024,92160000);grid on;
% hold on;
% plotSpectrum( RXI_TR+1i*RXQ_TR,1024,92160000);grid on;
function qwer(Y2_I,Y2_Q)
% clc
% clear
K = 8192; % number of iterations%迭代次数
dim = K;
fs=92160000;
%10M F类
% load env_in1
% env_in = env_inbf.'; %%%%%低通等效包络复值输入信号
% load env_in2 %%%%%可以load cw_outN0,2,3,4,9,12
% env_out=env_out.'; %%%%%%低通等效包络复值输出信号
%收回的IQ
% Y2_I = load('data2I.txt');
% Y2_Q = load('data2Q.txt');
%归一化
Y2=Y2_I+1i*Y2_Q;
scale = max(abs(Y2));
Y2_m=Y2/scale;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
win=window(@blackmanharris,3000);
%win=hamming(4096);
xx=Y2_m(1:dim); %系统实际输出
%x=testenv_out; %实际数据输出
Pxx0 = pwelch (xx, win, []); %二. welch法得到的功率谱密度
Pxx0 = 10*log10(fftshift(Pxx0));
f0 = fs*((0:length(Pxx0)-1)/length(Pxx0) - 0.5);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% f0=f0+1900000000;
% f1=f1+1900000000;
% f2=f2+1900000000;
% f1=f1+2100000000;
% f1=f1/1000000000;
% f2=f2+2100000000;
% f2=f2/1000000000;
f0=f0+2400000000;
f0=f0/1000000000;
figure(1)
indx=1:2:length(f0);
plot(f0(indx),Pxx0(indx),'b-*','LineWidth',2,'markersize',3)
grid on
% axis([1.855e9 1.945e9 -90 10])
%axis([2.36 2.44 -100 11])
xlabel('Frequency(Hz)')
ylabel('PSD(dBm/Hz)')