当前位置:文档之家› 应用MATLAB对信号进行频谱分析及滤波

应用MATLAB对信号进行频谱分析及滤波

应用MATLAB对信号进行频谱分析及滤波fs=input('please input the fs:');%设定采样频率
N=input('please input the N:');%设定数据长度
t=0:0.001:1;
f=100;%设定正弦信号频率
%生成正弦信号
x=sin(2*pi*f*t);
figure(1);
subplot(211);
plot(t,x);%作正弦信号的时域波形
axis([0,0.1,-1,1]);
title('正弦信号时域波形');
z=square(50*t);
subplot(212)
plot(t,z)
axis([0,1,-2,2]);
title('方波信号时域波形');grid;
%进行FFT变换并做频谱图
y=fft(x,N);%进行fft变换
mag=abs(y);%求幅值
f=(0:N-1)*fs/N;%横坐标频率的表达式为f=(0:M-1)*Fs/M; figure(2);
subplot(211);
plot(f,mag);%做频谱图
axis([0,1000,0,200]);
title('正弦信号幅频谱图');
y1=fft(z,N);%进行fft变换
mag=abs(y1);%求幅值
f=(0:N-1)*fs/N;%横坐标频率的表达式为f=(0:M-1)*Fs/M; subplot(212);
plot(f,mag);%做频谱图
axis([0,1000,0,200]);
title('方波信号幅频谱图');grid;
%求功率谱
sq=abs(y);
power=sq.^2;
figure(3)
subplot(211);
plot(f,power);
title('正弦信号功率谱');grid;
sq1=abs(y1);
power1=sq1.^2;
subplot(212);
plot(f,power1);
title('方波信号功率谱');grid;
%用IFFT恢复原始信号
xifft=ifft(y);
magx=real(xifft);
ti=[0:length(xifft)-1]/fs;
figure(4);
subplot(211);
plot(ti,magx);
axis([0,0.1,-1,1]);
title('通过IFFT转换的正弦信号波形');
zifft=ifft(y1);
magz=real(zifft);
ti1=[0:length(zifft)-1]/fs;
subplot(212);
plot(ti1,magz);
title('通过IFFT转换的方波信号波形');grid; please input the fs:1000
please input the N:1024。

相关主题