Matlab中信号的频谱
Frequency spectrum of signal 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
问题是,当我放大图表时,我看不到 50Hz、90Hz 和 180Hz 上的峰值。
我的代码哪里做错了?
问题是为了获得完美的频谱(峰值在 50、90、180 上,否则为 0),您的间隔应该是所有频率的乘数。
解释:考虑y=sin(2.*pi.*t)
。使用您的代码绘制它:
1) et = 1-dt;
2) et = 1;
在第一种情况下,如果放大,您会看到峰值完全在 1 Hz 上。在第二种情况下它不是(也非常接近)。
为什么?因为您使用的是有限数量的点,因此也是有限数量的频率。如果 1 Hz 在这组频率中(第一种情况),您将在 1 Hz 处获得完美的峰值,而在所有其他频率处为零。
在情况 2 中,您的集合中没有 1 Hz 频率,因此您将在最近的频率上获得峰值(并且它的宽度也是有限的)。
最终,在您的原始代码中,您的全套频率中没有 50、90、180 Hz 频率。
这是我用来在 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
问题是,当我放大图表时,我看不到 50Hz、90Hz 和 180Hz 上的峰值。
我的代码哪里做错了?
问题是为了获得完美的频谱(峰值在 50、90、180 上,否则为 0),您的间隔应该是所有频率的乘数。
解释:考虑y=sin(2.*pi.*t)
。使用您的代码绘制它:
1) et = 1-dt;
2) et = 1;
在第一种情况下,如果放大,您会看到峰值完全在 1 Hz 上。在第二种情况下它不是(也非常接近)。
为什么?因为您使用的是有限数量的点,因此也是有限数量的频率。如果 1 Hz 在这组频率中(第一种情况),您将在 1 Hz 处获得完美的峰值,而在所有其他频率处为零。
在情况 2 中,您的集合中没有 1 Hz 频率,因此您将在最近的频率上获得峰值(并且它的宽度也是有限的)。
最终,在您的原始代码中,您的全套频率中没有 50、90、180 Hz 频率。