使用matlab进行频域分析

信号的频谱 频谱密度 功率谱密度 能量谱密度的区别

详见参考

信号按能量是否有限分为:

  1. 能量信号:能量有限,平均功率为0。如单位冲击信号。
  2. 功率信号:能量无限,功率非0。如节约信号,或某个电压非ode直流或周期信号。

信号的频率特性共有以下四种

  1. 功率信号的频谱
  2. 能量信号的频谱密度
    设一个能量信号s(t),则它的傅里叶变换定义为频谱密度函数。
  3. 能量信号的能量谱密度
  4. 功率信号的功率谱(密度)

利用Matlab画频谱图

  1. 定义信号为f(t) = 2*sin(2*pi*f*t), f = 4;
    信号的时域波形
fo = 4; %frequency of the sine wave
Fs = 100; %sampling rate 
Ts = 1/Fs; %sampling time interval 
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples 
y = 2*sin(2*pi*fo*t); %the sine curve      
%plot the cosine curve in the time domain
sinePlot = figure; 
plot(t,y) 
xlabel('time (seconds)') 
ylabel('y(t)') 
title('Sample Sine Wave') 
grid
  1. 信号的频谱定义为信号在各个频率上的分布大小,对于一个能量有限的信号,它的频谱密度函数为它的傅里叶变换的结果。
    其中傅里叶变换提供了一个将信号从时域转变到频域的方法。
  2. 下图为fft之后,又abs的结果,因为傅里叶变换结果是个complex。存在以下问题:x轴不能提供频率信息;幅度都是100;没有让频谱中心为0.

在这里插入图片描述

 
%plot the frequency spectrum using the MATLAB fft command
matlabFFT = figure; %create a new figure
YfreqDomain = fft(y); %take the fft of our sin wave, y(t) 
stem(abs(YfreqDomain)); %use abs command to get the magnitude 
%similary, we would use angle command to get the phase plot! 
%we'll discuss phase in another post though!
xlabel('Sample Number')
ylabel('Amplitude')
title('Using the Matlab fft command')
grid
axis([0,100,0,120])

为FFT定义一个函数来获取双边频谱

以下代码可以简化获取双边频谱的过程,复制并保存到你的.m文件中

function [X,freq]=centeredFFT(x,Fs)
%this is a custom function that helps in plotting the two-sided spectrum 
%x is the signal that is to be transformed 
%Fs is the sampling rate
N=length(x);
%this part of the code generates that frequency axis
if mod(N,2)==0 
k=-N/2:N/2-1; % N even 
else 
k=-(N-1)/2:(N-1)/2; % N odd 
end 
T=N/Fs;
freq=k/T; %the frequency axis
%takes the fft of the signal, and adjusts the amplitude accordingly 
X=fft(x)/N; % normalize the data 
X=fftshift(X); %shifts the fft data so that it is centered

这个函数输出正确的频域范围和变换后的信号,它需要输入需要变换的信号和采样率。

接下来使用前文的正弦信号做一个简单的示例,注意你的示例.m文件要和centeredFFT.m文件在一个目录下

在这里插入图片描述

[YfreqDomain,frequencyRange] = centeredFFT(y,Fs);
centeredFFT = figure;
%remember to take the abs of YfreqDomain to get the magnitude! 
stem(frequencyRange,abs(YfreqDomain)); 
xlabel('Freq (Hz)') 
ylabel('Amplitude') 
title('Using the centeredFFT function') 
grid 
axis([-6,6,0,1.5])

为FFT定义一个函数来获取右边频谱

从上图可以看出,FFT变换得到的频谱是左右对称的,因此,我们只需要其中一边就能获得信号的所有信息,我们一般保留正频率一侧。

以下的函数对上面的自定义函数做了一些修改,让它可以帮助我们只画出信号的正频率一侧

function [X,freq]=positiveFFT(x,Fs) 
N=length(x); %get the number of points 
k=0:N-1; %create a vector from 0 to N-1
T=N/Fs; %get the frequency interval 
freq=k/T; %create the frequency range 
X=fft(x)/N; % normalize the data 
%only want the first half of the FFT, since it is redundant 
cutOff = ceil(N/2); 
%take only the first half of the spectrum 
X = X(1:cutOff); 
freq = freq(1:cutOff);

和前面一样,使用正弦信号做一个示例,下面是示例代码

[YfreqDomain,frequencyRange] = positiveFFT(y,Fs); 
positiveFFT = figure; 
stem(frequencyRange,abs(YfreqDomain));
set(positiveFFT,'Position',[500,500,500,300]) 
xlabel('Freq (Hz)')
ylabel('Amplitude') 
title('Using the positiveFFT function') 
grid 
axis([0,20,0,1.5])

参考资料

  1. https://blog.csdn.net/juhou/article/details/82024846
  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值