%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 自适应对消(反馈)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;
clc;
%% 测试信号生成
% 信号参数初始化
fs=40e6; %系统采样率40MHz
fnc=10e6; %传输的单频信号频率为10MHz
fpulse=1000; %信号周期
dt=1/fs; %每个时钟周期间隔时间
tt=1e-3;
t=0:dt:tt; %得到每一周期的时间序列
amp=1; %信号幅度
duty=2; %信号占空比
phase_shift=pi/2; %噪声相对于信号的相移
% 测试信号生成
signal_c = 0.5*(square(2*pi*fpulse*t,duty)+1);
noise_c = 0.5*(square(2*pi*fpulse*t-phase_shift,duty)+1);
% sing = amp*sin(2*pi*fnc*t).*signal_c ; %理想信号
signal = amp*sin(2*pi*fnc*t).*signal_c;%+wgn(1,length(t),-40); %加高斯白噪声信号,噪声信号强度为-15dBW
noise = amp*sin(2*pi*fnc*t).*noise_c;%+wgn(1,length(t),-40); %转发信号
%% 生成FPGA仿真文件
% Q=16;
% Q_r=round((signal+amp*sin(2*pi*fnc*t).*noise_c)*(2^(Q-1)-1));%16比特量化
% fid=fopen('C:\Users\zhang\Desktop\小毕设\Code\Matlab\xn_test.txt','w');
% for k=1:length(Q_r)
%
% aaa(k)=Q_r(k)+(Q_r(k)<0)*2^Q;
%
% B_si=dec2bin(Q_r(k)+(Q_r(k)<0)*2^Q,Q);
% for q=1:Q
% if B_si(q)=='1'
% tb=1;
% else
% tb=0;
% end
% fprintf(fid,'%d',tb);
% end
% fprintf(fid,'\r\n');
% end
% fprintf(fid,';');
% fclose(fid);
%
% Q_n=round(noise*(2^(Q-1)-1));%16比特量化
% fid1=fopen('C:\Users\zhang\Desktop\小毕设\Code\Matlab\dn_test.txt','w');
% for k=1:length(Q_r)
% B_ni=dec2bin(Q_n(k)+(Q_n(k)<0)*2^Q,Q);
% for q=1:Q
% if B_ni(q)=='1'
% tb=1;
% else
% tb=0;
% end
% fprintf(fid1,'%d',tb);
% end
% fprintf(fid1,'\r\n');
% end
% fprintf(fid1,';');
% fclose(fid1);
%%
figure(1)
subplot(211)
plot(t,signal); title('加高斯白噪声信号'); axis([0,tt,-2,2]);
subplot(212)
plot(t,noise); title('转发信号'); axis([0,tt,-2,2]);
%% LMS
delay=8000; %延迟时间
k=1; %反馈系数
len=length(t); %信号长度
M=7; %滤波器阶数
u=1/256; %收敛因子
xi=zeros(1,len); %滤波器输入信号
yi=zeros(1,len); %LMS输出信号
di=zeros(1,len+delay); %理想参考信号
ei=zeros(1,len); %误差信号
wi=zeros(M,len); %滤波器参数数组,行数与滤波器阶数一样,列数与仿真时间点数一致
ee=zeros(1,len+delay); %滤波器输出信号转发后
% xx=zeros(1,len);
% xx=signal+noise;
% rho=1/(max(eig(xx*xx.')));
% di=sing;
for i=M+1:len-1
% yi(i)=sum(xi(i-M+1:i)*wi(:,i));
yi(i)=sum(di(i-M+1:i)*wi(:,i)); %
ei(i)=xi(i)-yi(i); %
ee(i+delay)=k*ei(i);
di(i+delay)=ei(i);
xi(i+1)=ee(i)+signal(i);
% wi(:,i+1)=wi(:,i)+u*(di(i-M+1:i)*ei(i)).';
wi(:,i+1)=wi(:,i)+2*u*(di(i-M+1:i)*ei(i)).'; %计算wi
end
figure(2)
subplot(311);
plot(t,ei); title('对消输出信号');
axis([0,tt,-2,2]);
subplot(312);
plot(t,xi);
axis([0,tt,-2,2]);title('对消输入信号');
subplot(313);
plot(t,yi);
axis([0,tt,-2,2]);title('LMS输出信号');
figure(3)
plot(t,wi.'); title('滤波器系数');
figure(4)
v=abs(ei);
plot(t,v); title('信号幅值');
%% FFT求功率
figure(5) %输入功率
subplot(121)
nfft=8192;
P_in=abs(fft(xi,nfft)).^2/length(t);
t1=0:round(nfft/2-1);
f=t1*fs/nfft;
P_in_dB=10*log10(P_in(t1+1));
f=f/1e+6;
plot(f,P_in_dB); axis([0,20,-20,30]);
xlabel('频率/MHz'); ylabel('功率/dB'); title('对消前功率谱');
% hold on
P_max_y=max(P_in_dB);
P_max=find(P_in_dB == P_max_y);
% text(P_max(f),P_max(P_in_dB),'*','color','g');
text(f(P_max),P_in_dB(P_max),['[',num2str(f(P_max)),',',num2str(P_in_dB(P_max)),']'],'color','r');
%输出功率
subplot(122)
nfft=8192;
P_out=abs(fft(ei,nfft)).^2/length(t);
t1=0:round(nfft/2-1);
f=t1*fs/nfft;
P_out_dB=10*log10(P_out(t1+1));
f=f/1e+6;
plot(f,P_out_dB); axis([0,20,-20,30]);
xlabel('频率/MHz'); ylabel('功率/dB'); title('对消后功率谱');
% hold on
P_max_y=max(P_out_dB);
P_max=find(P_out_dB == P_max_y);
% text(P_max(f),P_max(P_in_dB),'*','color','g');
text(f(P_max),P_out_dB(P_max),['[',num2str(f(P_max)),',',num2str(P_in_dB(P_max)),']'],'color','r');
%% 公式法求功率
% figure
% subplot(121)
% N=length(t);
% window=boxcar(length(xi));
% [Pin,f]=periodogram(xi,window,length(t),fs);
% f=f/1e+6;
% plot(f,10*log10(1e6*Pin));title('对消输入功率');axis([0,20,-20,30]);
%
% subplot(122)
% window=boxcar(length(ei));
% [Pout,f]=periodogram(ei,window,N,fs);
%
% % Pout_mw=Pout*1e+6;
% f=f/1e+6;
% plot(f,10*log10(1e6*Pout));title('对消输出功率');axis([0,20,-20,30]);
%%
% figure
% double_power_spec_w=abs(xi).^2;
% single_power_spec_w=2*double_power_spec_w(1:floor(4096/2));
% single_power_spec_mW=single_power_spec_w*1e+3;
% single_power_spec_dBm=10*log10(single_power_spec_mW);
% plot(single_power_spec_dBm,'linewidth',2.5);
%% PSD
% figure
% [PSD,fp];