简体   繁体   English

Matlab中信号的频谱

[英]Frequency spectrum of signal in Matlab

Here is the code I use to plot a function in frequency domain in Matlab: 这是我用来在Matlab中绘制频域函数的代码:

    dt = 1/10000; % sampling rate
    et = 0.1; % end of the interval
    t = 0:dt:et; % sampling range
    y = 2+sin(2.*pi.*50.*t)+18.*sin(2.*pi.*90.*t)+6.*sin(2.*pi.*180.*t); %     sample the signal
    subplot(2,1,1); % first of two plots
    plot(t,y); grid on % plot with grid
    xlabel('Time (s)'); % time expressed in seconds
    ylabel('Amplitude'); % amplitude as function of time
    Y = fft(y); % compute Fourier transform
    n = size(y,2)/2; % 2nd half are complex conjugates
    amp_spec = abs(Y)/n; % absolute value and normalize
    subplot(2,1,2); % second of two plots
    freq = (0:100)/(2*n*dt); % abscissa viewing window
    stem(freq,amp_spec(1:101)); grid on % plot amplitude spectrum
    xlabel('Frequency (Hz)'); % 1 Herz = number of cycles/second
    ylabel('Amplitude'); % amplitude as function of frequency

The problem is, when I zoom in my graph I don't see peaks exactly on 50Hz, 90Hz and 180Hz. 问题是,当我放大图表时,我看不到50Hz,90Hz和180Hz的峰值。

What did I do wrong in my code? 我的代码中出了什么问题?

The problem is that in order to get perfect spectra (peaks on 50,90,180 and 0 otherwise), your interval should be a multiplier of all the frequencies. 问题是为了获得完美的光谱(峰值在50,90,180和0),你的间隔应该是所有频率的乘数。

Explanation: consider y=sin(2.*pi.*t) . 说明:考虑y=sin(2.*pi.*t) Use your code to plot it with: 使用您的代码将其绘制为:

1) et = 1-dt; 1) et = 1-dt;

2) et = 1; 2) et = 1;

In the first case, if you zoom in, you will see that peak is perfectly on 1 Hz. 在第一种情况下,如果放大,您将看到峰值完全在1 Hz。 In the second case it is not (also very close). 在第二种情况下,它不是(也非常接近)。

Why? 为什么? Because you are working with the finite number of points, and, consequently, finite number of frequencies. 因为您正在处理有限数量的点,因此,有限数量的频率。 If 1 Hz is among this set of frequencies (1st case), you will get perfect peak at 1 Hz at zeros at all others frequencies. 如果1 Hz是这组频率之一(第1种情况),那么在所有其他频率的零点处,您将获得1 Hz的完美峰值。

In case 2 there is no 1 Hz frequency in your set, so you will get peak on the nearest frequencies (and also it will have finite width). 在情况2中,您的集合中没有1 Hz的频率,因此您将在最近的频率上获得峰值(并且它将具有有限的宽度)。

Ultimately, in your original code, there are no 50, 90, 180 Hz frequencies in your full set of frequencies. 最终,在您的原始代码中,您的整个频率中没有50,90,180 Hz的频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM