首先補充:
randn()函數用來產生正態分布的隨機數或矩陣
conj()函數用來求負數的共軛:如果Z是一個復數組,那么conj(Z) = real(Z) - i*imag(Z)其中real(Z),imag(Z)分別代表Z的實部和虛部
1.首先看一下頻譜分析下,頻譜圖像展現的特征:
x = sin(2*pi*50*t)的原圖和頻譜圖:
代碼:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 x = sin(2*pi*50*t); 5 %x = 2*sin(2*pi*50*t+pi); 6 %x = sin(2*pi*50*t) + 2*sin(2*pi*140*t); 7 %x = sin(100*pi*t)+cos(280*pi*t); 8 figure(1); 9 plot(t,x); 10 xlabel('t'); ylabel('f(t)'); 11 y = x; 12 Y = fft(y,256); 13 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 14 f = 1000/256*(0:127); 15 figure(2); 16 plot(f,Pyy(1:128)); 17 title('Power spectral density'); 18 xlabel('Frequency (Hz)');
結果圖為:
頻譜圖中,峰值是正好是原函數的周期的頻率。 變下初始相位呢?
x = 2*sin(2*pi*50*t+pi);
代碼:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 %x = sin(2*pi*50*t); 5 x = 2*sin(2*pi*50*t+pi); 6 %x = sin(2*pi*50*t) + 2*sin(2*pi*140*t); 7 %x = sin(100*pi*t)+cos(280*pi*t); 8 figure(1); 9 plot(t,x); 10 xlabel('t'); ylabel('f(t)'); 11 y = x; 12 Y = fft(y,256); 13 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 14 f = 1000/256*(0:127); 15 figure(2); 16 plot(f,Pyy(1:128)); 17 title('Power spectral density'); 18 xlabel('Frequency (Hz)');
結果圖為:
仍然滿足那個結論。那么組合一下呢?
x = sin(2*pi*50*t) + 2*sin(2*pi*140*t);
代碼:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 %x = sin(2*pi*50*t); 5 %x = 2*sin(2*pi*50*t+pi); 6 x = sin(2*pi*50*t) + 2*sin(2*pi*140*t); 7 %x = sin(100*pi*t)+cos(280*pi*t); 8 figure(1); 9 plot(t,x); 10 xlabel('t'); ylabel('f(t)'); 11 y = x; 12 Y = fft(y,256); 13 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 14 f = 1000/256*(0:127); 15 figure(2); 16 plot(f,Pyy(1:128)); 17 title('Power spectral density'); 18 xlabel('Frequency (Hz)');
結果圖為:
頻譜圖中竟然把50Hz和140Hz都顯示出來哎,unbelievable!.不行,再試試,換成cos試試?
x = sin(100*pi*t)+cos(280*pi*t);
代碼:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 %x = sin(2*pi*50*t); 5 %x = 2*sin(2*pi*50*t+pi); 6 %x = sin(2*pi*50*t) + 2*sin(2*pi*140*t); 7 x = sin(100*pi*t)+cos(280*pi*t); 8 figure(1); 9 plot(t,x); 10 xlabel('t'); ylabel('f(t)'); 11 y = x; 12 Y = fft(y,256); 13 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 14 f = 1000/256*(0:127); 15 figure(2); 16 plot(f,Pyy(1:128)); 17 title('Power spectral density'); 18 xlabel('Frequency (Hz)');
結果圖為:
也是如此。這下終於可以放心了。原來頻譜圖是這個作用~。
(2)還是有點不放心。如果加上噪聲呢,還能識別出來么?
先測試下噪聲:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 figure(1); 5 y = 2*randn(size(t)); 6 plot(t,y); 7 xlabel('t');ylabel('y'); 8 Y = fft(y,256); 9 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 10 f = 1000/256*(0:127); 11 figure(2); 12 plot(f,Pyy(1:128)); 13 title('Power spectral density'); 14 xlabel('Frequency (Hz)');
結果圖為:
雜亂無章。。。再測一次試試:
還是雜亂無章,峰值也沒有個確定的值。確定為噪聲無疑了。
好,那么現在就用你加到上面的正弦函數圖像上去!
代碼:

1 clear; 2 clc; 3 t = 0:0.001:0.25; 4 x = sin(100*pi*t)+cos(280*pi*t); 5 figure(1); 6 plot(t,x); 7 xlabel('t'); ylabel('f(t)'); 8 y = x + 2*randn(size(t)); 9 figure(2); 10 plot(t,y); 11 xlabel('t');ylabel('y(t)'); 12 Y = fft(y,256); 13 Pyy = Y.*conj(Y)/256; %計算Y的模的長度 14 f = 1000/256*(0:127); 15 figure(3); 16 plot(f,Pyy(1:128)); 17 title('Power spectral density'); 18 xlabel('Frequency (Hz)');
結果圖為:比較下兩圖可以知道,由於受到噪聲干擾。圖像幾乎很難分辨出信號圖像。
頻譜分析圖這里,竟然有兩個突出的峰值:
仔細觀察,竟然就是50Hz和140Hz。這下好了,合成的信號用頻譜分析竟然可以清楚的找到原來的正弦信號~。
咳咳,總結:經過傅里葉變換之后,頻譜圖的確能夠幫助我們分析信號的成分,便於對信號進行處理。