clf;
fs=50;%采样频率,每秒钟采样多少个点
N=60;%采样点数量
T=N/fs;%采样时间
n=0:N-1;t=n/fs;%时间序列
f=n*fs/N; %频率序列
y1=10*sin(2*pi*15*t);
y2=10*sin(2*pi*20*t);
y3=10*sin(2*pi*60*t);
y4=y1+y2;
N1=N*10;
n1=0:N1-1;
t1=n1/(fs*10);%时间序列
y11=10*sin(2*pi*15*t1);
y22=10*sin(2*pi*20*t1);
y33=10*sin(2*pi*60*t1);
y44=y11+y22;
l=uint16(length(n)/7.5);
subplot(4,2,1)
plot(t1,y11,'b','LineWidth',1.2)
hold on
stem(t(1:l),y1(1:l),'r')
xlabel('时间/s','FontWeight','bold');
ylabel('振幅','FontWeight','bold');
title("15Hz正弦波时域图",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
y1_FFT=fft(y1,N); %对信号进行快速Fourier变换
y1_FFT_Mag=abs(y1_FFT);
subplot(4,2,2)
plot(f(1:N/2),y1_FFT_Mag(1:N/2),'b','LineWidth',1.2)
hold on
stem(f(1:N/2),y1_FFT_Mag(1:N/2),'r')
xlabel('频率/Hz','FontWeight','bold');
ylabel('幅值','FontWeight','bold');
title("15Hz正弦波频谱(采样频率50Hz)",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
subplot(4,2,3)
plot(t1,y22,'b','LineWidth',1.2)
hold on
stem(t(1:l),y2(1:l),'r')
xlabel('时间/s','FontWeight','bold');
ylabel('振幅','FontWeight','bold');
title("20Hz正弦波时域图",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
y2_FFT=fft(y2,N); %对信号进行快速Fourier变换
y2_FFT_Mag=abs(y2_FFT);
subplot(4,2,4)
plot(f(1:N/2),y2_FFT_Mag(1:N/2),'b','LineWidth',1.2)
hold on
stem(f(1:N/2),y2_FFT_Mag(1:N/2),'r')
xlabel('频率/Hz','FontWeight','bold');
ylabel('幅值','FontWeight','bold');
title("20Hz正弦波频谱(采样频率50Hz)",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
subplot(4,2,5)
plot(t1,y33,'b','LineWidth',1.2)
hold on
stem(t(1:l),y3(1:l),'r')
xlabel('时间/s','FontWeight','bold');
ylabel('振幅','FontWeight','bold');
title("50Hz正弦波时域图",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
y3_FFT=fft(y3,N); %对信号进行快速Fourier变换
y3_FFT_Mag=abs(y3_FFT);
subplot(4,2,6)
plot(f(1:N/2),y3_FFT_Mag(1:N/2),'b','LineWidth',1.2)
hold on
stem(f(1:N/2),y3_FFT_Mag(1:N/2),'r')
xlabel('频率/Hz','FontWeight','bold');
ylabel('幅值','FontWeight','bold');
title("50Hz正弦波频谱(采样频率50Hz)",'FontWeight','bold')
subplot(4,2,7)
plot(t1,y44,'b','LineWidth',1.2)
hold on
stem(t(1:l),y4(1:l),'r')
xlabel('时间/s','FontWeight','bold');
ylabel('振幅','FontWeight','bold');
title("15+20Hz正弦波时域图",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
y4_FFT=fft(y4,N); %对信号进行快速Fourier变换
y4_FFT_Mag=abs(y4_FFT);
subplot(4,2,8)
plot(f(1:N/2),y4_FFT_Mag(1:N/2),'b','LineWidth',1.2)
hold on
stem(f(1:N/2),y4_FFT_Mag(1:N/2),'r')
xlabel('频率/Hz','FontWeight','bold');
ylabel('幅值','FontWeight','bold');
title("15+20Hz正弦波频谱(采样频率50Hz)",'FontWeight','bold')
grid on
set(gca,'GridLineStyle',':','GridColor','k', 'GridAlpha',1)
set(gcf,'unit','centimeters','position',[15 10 13.53 9.03],'color','white')