%% %                              FFT实践及频谱分析                          % %% %1.正弦波*% fs=100;%设定采样频率 N=128; n=0:N-1; t=n/fs; f0=10;%设定正弦信号频率 x=sin(2*pi*f0*t); %生成正弦信号 figure(1); subplot(231); plot(t,x);%作正弦信号的时域波形 xlabel(‘t’); ylabel(‘y’); title(‘正弦信号y=2*pi*10t时域波形’); grid; %进行FFT变换并做频谱图 y=fft(x,N);%进行fft变换 mag=abs(y);%求幅值 f=(0:length(y)-1)'*fs/length(y);%进行对应的频率转换 figure(1); subplot(232); plot(f,mag);%做频谱图 axis([0,100,0,80]); xlabel(‘频率(Hz)'); ylabel(‘幅值’); title(‘正弦信号y=2*pi*10t幅频谱图N=128’); grid; %求均方根谱 sq=abs(y); figure(1); subplot(233); plot(f,sq); xlabel(‘频率(Hz)'); ylabel(‘均方根谱’); title(‘正弦信号y=2*pi*10t均方根谱’); grid; %求功率谱 power=sq.^2; figure(1); subplot(234); plot(f,power); xlabel(‘频率(Hz)'); ylabel(‘功率谱’); title(‘正弦信号y=2*pi*10t功率谱’); grid; %求对数谱 ln=log(sq); figure(1); subplot(235); plot(f,ln); xlabel(‘频率(Hz)'); ylabel(‘对数谱’); title(‘正弦信号y=2*pi*10t对数谱’); grid; %用IFFT恢复原始信号 xifft=ifft(y); magx=real(xifft); ti=[0:length(xifft)-1]/fs; figure(1); subplot(236); plot(ti,magx); xlabel(‘t’); ylabel(‘y’); title(‘通过IFFT转换的正弦信号波形’); grid;

===================================================================

% This example demonstrates the two-side DFT of a sinusoidal function % (此範例展示一個簡單正弦波的傅立葉轉換,以雙邊頻譜來顯示) % Since the sinusoidal function has a frequency to be a multiple of fs/N, the two-side DFT have only two nonzero terms. % (此正弦波的頻率恰巧是 freqStep 的整數倍,所以雙邊頻譜應該只有兩個非零點)

clear; N = 256; % length of vector (點數) fs = 8000; % sample rate (取樣頻率) freqStep = fs/N; % freq resolution in spectrum (頻域的頻率的解析度) f = 10freqStep; % freq of the sinusoid (正弦波的頻率,恰是 freqStep 的整數倍) time = (0:N-1)/fs; % time resolution in time-domain (時域的時間刻度) y = cos(2piftime); % signal to analyze Y = fft(y); % spectrum Y = fftshift(Y); % put zero freq at the center (將頻率軸的零點置中) % Plot time data subplot(3,1,1); plot(time, y, ‘.-'); title(‘Sinusoidal signals’); xlabel(‘Time (seconds)'); ylabel(‘Amplitude’); axis tight % Plot spectral magnitude freq = freqStep*(-N/2:N/2-1); % freq resolution in spectrum (頻域的頻率的解析度) subplot(3,1,2); plot(freq, abs(Y), ‘.-b’); grid on; xlabel(‘Frequency’); ylabel(‘Magnitude (Linear)'); % Plot phase subplot(3,1,3); plot(freq, angle(Y), ‘.-b’); grid on; xlabel(‘Frequency’); ylabel(‘Phase (Radian)');