[英]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種情況),那么在所有其他頻率的零點處,您將獲得1 Hz的完美峰值。
在情況2中,您的集合中沒有1 Hz的頻率,因此您將在最近的頻率上獲得峰值(並且它將具有有限的寬度)。
最終,在您的原始代碼中,您的整個頻率中沒有50,90,180 Hz的頻率。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.