%% Homework2.2-Down-conversion Simulation-chirp signal
clc;
clear all;
close all;
%% 参数设置
f0=60e6; %chirp信号中频
fs=80e6; %采样频率
B=8e6;
ts=1/fs;
tau=1e-3; %脉冲宽度
t=0:ts:tau-ts;
k=B/tau; % chirp slope
f_nco=20e6; %NCO频率
%% 信号
ut=cos(2*pi*f0.*t+pi*k.*t.^2); %Chirp信号
signal_in=ut;
signal_nco_i=sin(2*pi*f_nco*t); %NCO
signal_nco_q=cos(2*pi*f_nco*t);
xi=signal_in.*signal_nco_i;
xq=signal_in.*signal_nco_q;
%% 低通滤波器
Hd=[-0.000509552317425067 0.000371832719073747 ...
-2.44618142336023e-18 -0.000422345371283588 0.000653914483674189 ...
-0.00051300840086792 3.97172054701057e-19 0.000648237670285558 ...
-0.00103805305948637 0.000832713046447832 -1.44055249402109e-17 ...
-0.00107147663368522 0.00171533438865906 -0.0013700728022455 ...
1.49748424538358e-17 0.00173474602821205 -0.00274933143849888 ...
0.00217272016206513 -1.00664883097235e-17 -0.00269259477337679 ...
0.00422346878396029 -0.00330491373545649 -5.06324472469145e-18 ...
0.0040229937639382 -0.00626058957283252 0.00486415663677792 ...
-1.66404228224871e-17 -0.00585160870489404 0.00906475339859715 ...
-0.00701739683384083 5.85437570436228e-17 0.00840723125601122 ...
-0.0130193885689127 0.0100887097935733 -2.30053463735395e-17 ...
-0.0121661147234315 0.018953868399222 -0.0148088567812163 ...
2.56041597453839e-17 0.0183109311931974 -0.0290491896372965 ...
0.0232296218816149 -2.75866619122871e-17 -0.0307619445738023 ...
0.051363785669012 -0.0440244907599678 2.88282849719697e-17 ...
0.0744525331295221 -0.158657515688882 0.224988028604281 ...
0.750377726738553 0.224988028604281 -0.158657515688882 ...
0.0744525331295221 2.88282849719697e-17 -0.0440244907599678 ...
0.051363785669012 -0.0307619445738023 -2.75866619122871e-17 ...
0.0232296218816149 -0.0290491896372965 0.0183109311931974 ...
2.56041597453839e-17 -0.0148088567812163 0.018953868399222 ...
-0.0121661147234315 -2.30053463735395e-17 0.0100887097935733 ...
-0.0130193885689127 0.00840723125601122 5.85437570436228e-17 ...
-0.00701739683384083 0.00906475339859715 -0.00585160870489404 ...
-1.66404228224871e-17 0.00486415663677792 -0.00626058957283252 ...
0.0040229937639382 -5.06324472469145e-18 -0.00330491373545649 ...
0.00422346878396029 -0.00269259477337679 -1.00664883097235e-17 ...
0.00217272016206513 -0.00274933143849888 0.00173474602821205 ...
1.49748424538358e-17 -0.0013700728022455 0.00171533438865906 ...
-0.00107147663368522 -1.44055249402109e-17 0.000832713046447832 ...
-0.00103805305948637 0.000648237670285558 3.97172054701057e-19 ...
-0.00051300840086792 0.000653914483674189 -0.000422345371283588 ...
-2.44618142336023e-18 0.000371832719073747 -0.000509552317425067]; %利用fdatool设计滤波器的系参数
%I=lowpassfilter1(xi);
I=filter(Hd,1,xi); %经过低通滤波
Q=filter(Hd,1,xq);
%% 基带信号生成
signal_baseband=I-1i.*Q; %基带信号
%% 画图
N=length(t);
f=((0:N-1)/N-0.5)*fs/1e6;
f1=(0:N-1)*fs/1e6;
F_signal=fftshift(fft(signal_in));
figure(1) %输入信号时/频域图
subplot(211)
plot(t,signal_in);
xlabel('时间/s');
ylabel('幅度');
axis([0 5000/fs -1.1 1.1]);
title('输入信号时域波形')
subplot(212)
plot(f,abs(F_signal));
xlabel('频率/MHz');
title('输入信号频谱')
F_ncoi=fftshift(fft(signal_nco_i));
F_ncoq=fftshift(fft(signal_nco_q));
figure(2) %NCO信号时/频域图
subplot(221)
plot(t,signal_nco_i);
xlabel('时间/s');
ylabel('幅度');
axis([0 100/fs -1 1]);
title('NCO-I信号')
subplot(222)
plot(f,abs(F_ncoi));
xlabel('频率/MHz');
title('NCO-I频谱')
subplot(223)
plot(t,signal_nco_q);
xlabel('时间/s');
ylabel('幅度');
axis([0 100/fs -1 1]);
title('NCO-Q信号')
subplot(224)
plot(f,abs(F_ncoq));
xlabel('频率/MHz');
title('NCO-Q频谱')
figure(3)
figure(3) %滤波器幅相特性
freqz(Hd);
title('滤波器幅相特性');
F_I=fftshift(fft(I));
F_Q=fftshift(fft(Q));
figure(4) %DDC输出I/Q两路信号时/频域
subplot(221)
plot(t,I);
xlabel('时间/s');
ylabel('幅度');
axis([0 10000/fs -1 1]);
title('DDC-I时域信号')
subplot(222)
plot(f,abs(F_I));
xlabel('频率/MHz');
title('I路信号频谱')
subplot(223)
plot(t,Q);
xlabel('时间/s');
ylabel('幅度');
axis([0 10000/fs -1 1]);
title('DDC-Q时域信号')
subplot(224)
plot(f,abs(F_Q));
xlabel('频率/MHz');
title('Q路信号频谱')
F_baseband=fftshift(fft(signal_baseband));
figure(5) %基带信号时/频域
subplot(211)
plot(t,real(signal_baseband));
title('基带信号时域波形')
xlabel('时间/s');
ylabel('幅度');
%axis tight;
subplot(212)
plot(f,abs(F_baseband));
xlabel('频率/MHz');
title('基带信号频谱')