How to plot frequency spectrum of a signal in matlab?

조회 수: 1,260 (최근 30일)
Nur Fauzira Saidin
Nur Fauzira Saidin 2015년 10월 26일
편집: Frantz Bouchereau 2021년 7월 29일
Hi. I want to plot frequency spectrum of a signal. I got this coding based on the sources that I found from the internet but my lecturer said this is not frequency spectrum. Can somebody help me on this?
close all;
%Define number of samples to take
fs = 8000;
f = 400; %Hz
%Define signal
t = 0:1/fs:1-1/fs;
signal = sin(2*pi*f*t);
%Plot to illustrate that it is a sine wave
plot(t, signal);
title('Time-Domain signal');
%Take fourier transform
fftSignal = fft(signal);
%apply fftshift to put it in the form we are used to (see documentation)
fftSignal = fftshift(fftSignal);
%Next, calculate the frequency axis, which is defined by the sampling rate
f = fs/2*linspace(-1,1,fs);
%Since the signal is complex, we need to plot the magnitude to get it to
%look right, so we use abs (absolute value)
figure;
plot(f, abs(fftSignal));
title('magnitude FFT of sine');
xlabel('Frequency (Hz)');
ylabel('magnitude');
%noise
noise = 2*randn(size(signal));
figure, plot(t,noise), title('Time-Domain Noise');
fftNoise = fft(noise);
fftNoise = fftshift(fftNoise);
figure, plot(f,abs(fftNoise)), title('Magnitude FFT of noise');
xlabel('Frequency (Hz)');
ylabel('magnitude');
%noisy signal
noisySignal = signal + noise;
figure, plot(t,noisySignal), title('Time-Domain Noisy Signal');
fftNoisySignal = fft(noisySignal);
fftNoisySignal = fftshift(fftNoisySignal);
figure, plot(f,abs(fftNoisySignal)), title('Magnitude FFT of noisy signal');
xlabel('Frequency (Hz)');
ylabel('magnitude');

채택된 답변

Frantz Bouchereau
Frantz Bouchereau 2021년 7월 29일
편집: Frantz Bouchereau 2021년 7월 29일
Your code computes FFT and needs some extra normalization steps to calculate true power spectrum.
There are various ways in which you can compute and plot true power spectrum or power spectral density in MATLAB (when I say 'true power spectrum' I mean that the output values correspond to actual power values).
1) If you want to compute the power spectrum without having to specify many parameters and want the function to choose the best parameters for you, you can use pspectrum. Calling the function without outputs will give you a plot with the computed power spectrum.
2) If you want to compute power spectrum or power spectral density and want full control over the window size, window overlap, window type, and number of FFT points, you can use the Welch periodogram pwelch function. Calling the function without outputs will give you a plot with the computed power spectrum.
3) If you want to just visualize the power spectrum, you can use the Signal Analyzer app. The app let's you visualize your signals simultaneously in the time, frequency, and time-frequency domains. You can zoom into signal regions of interest and analyze the spectra at those zoomed regions.
4) If you have split your signals into multiple signal frames you can use the Spectrum Analyzer scope.
Computing true power spectra requires normalization of the FFT and the window used in the computations. Here is a popular MATLAB doc page that explains the relationship between FFT and true power spectra: Power Spectral Density Estimates Using FFT.

추가 답변 (3개)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2015년 10월 26일
Hi there!
I don't know why your instructor didn't like it! Maybe it's missing a division and he is picky on that!
fftNoisySignal = fft(NoisySignal)./numel(NoisySignal);
Maybe that is the case, basically the magnitude of the fft has an issue, I guess! You need to apply this division on every fft command you use.
Hope this helps.
Good luck!
  댓글 수: 3
Lazaros Moysis
Lazaros Moysis 2021년 2월 10일
Dear Salaheddin, may I ask, in order to compute the spectrum of a signal, why is it required to divide the fft with the length of the signal?
Jonas
Jonas 2021년 5월 1일
@Lazaros: because a longer signal with the same spectral content would apprar with higher amplitude in the spectrum. if you dont divide, a 2 second sine would have double spectrum amplitude compared to the same sine with only one second. normally in a one sided spectrum you want to see the amplitude of a specific frequency directly, regardless of signal length

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 10월 26일
편집: Image Analyst 2015년 10월 26일
It's not? I always thought that the FFT gave the frequency spectrum. Why does he say no? Perhaps he would like to see you use pwelch(), periodgram(), or spectrogram() in preference to fft()???
  댓글 수: 2
Nur Fauzira Saidin
Nur Fauzira Saidin 2015년 10월 27일
I'm not sure. He just said, 'this is not frequency spectrum' crying. Thank you for your reply!
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2015년 10월 27일
As Image Analyst said there are several different methods. Some of which have different names. Power spectrum, Power spectrum density and ... each of which have slightly different method of calculation. Yet, the simple fft is the heart of them, which is performed correctly in your code.
You already accepted my answer, tnx, but if your problem was not and you're looking for something specific search and if no success let me know :)
Good Luck!

댓글을 달려면 로그인하십시오.


Viktor Bolgov
Viktor Bolgov 2019년 12월 1일
I do not get it. You have sin wave with frequency 400 Hz and magnitude of 1. Your spectrum indeed shows frequncy 400 Hz, but the magnitude is over 4000! How could it be?
  댓글 수: 1
Sandro Yemi Okutuga
Sandro Yemi Okutuga 2020년 8월 14일
편집: Sandro Yemi Okutuga 2020년 8월 14일
The analytical Fourier transform of that sinewave is 0 everywhere except for infinite at 400 Hz. The fact that it results only 4000 and not infinite is because the signal is finite in time and the calculation numerical. It wouldn't be 1 anyway.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by