clear all; close all; clc;
f0=1000; %方波频率1000Hz
%采样率
fs=10000; %采样率10KHz
%采样时间
Ts=1/fs;
%采样数量
N=10240;
t = 0:Ts:Ts*(N-1);
y = square(2*pi*30*t);
figure
subplot(211);plot(t,y);ylim([-2,2]);
%傅立叶变换
YFFT = abs(fft(y,N));
F = (0:N-1)*fs;
subplot(212);plot(F(1:N/10),YFFT(1:N/10)); %显示低频部分(清楚一些)