![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172939/image.jpeg)
Plotting the frequency spectrum / didnt understand the code
1 回表示 (過去 30 日間)
古いコメントを表示
X_mags = abs(fftshift(fft(signal)));
bin_vals = [0 : N-1];
N_2 = ceil(N/2);
fax_Hz = (bin_vals-N_2)*fs/N;
plot(fax_Hz, X_mags)
0 件のコメント
採用された回答
Geoff Hayes
2015 年 1 月 17 日
Qusay - let us assume the following concerning your signal, sampling rate (Fs), and FFT block size (N)
Fs = 4096; % sampling rate in Hertz
t = linspace(0,1-1/Fs,Fs); % one second time vector given Fs
N = 4096; % FFT block size is same as sampling rate (true for your ex)
signal = 2.3*sin(2*pi*t*227); % signal with amplitude of 2.3 and frequency of 227 Hertz
Now for your code
% transforms the signal from the time domain to the frequency domain with fft
% note that y is complex
Y = fft(signal);
% shifts the frequency components so that the zero frequency is at the centre
% of spectrum
Y = fftshift(Y);
% determines the magnitude of the complex data
Y = abs(Y);
The following lines of code just sets the frequencies for each bin and can be reduced to
f = [0:N-1] * Fs/N;
% must ensure that zero is the centre frequency due to our fftshift from above
f = f - ceil(N/2);
% plot the data
plot(f,Y);
The result of the plot is
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172939/image.jpeg)
Note the frequency at 227 Hz, as expected. The symmetry is due to the input signal being real.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spectral Measurements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!