Plot frequency spectrum of a signal

조회 수: 31 (최근 30일)
Aaron Karlo Maranan
Aaron Karlo Maranan 2017년 4월 26일
편집: Sai Charan Sampara 2022년 6월 30일
what does f = fs/2*linspace(-1,1,fs); mean in the following code. close all; %Define number of samples to take fs = 100; f = 400; %Hz %N =length(signal); %Define signal t = 0:1/fs:1-1/fs; %signal = sin(2*pi*f*t); signal = xlsread('testdata.xlsx'); %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) fftSignal2 = fftshift(fftSignal); %xdft = xdft(1:length(s)/2+1); %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(fftSignal2)); title('magnitude FFT of sine'); xlabel('Frequency (Hz)'); ylabel('magnitude');
  댓글 수: 2
Aaron Karlo Maranan
Aaron Karlo Maranan 2017년 4월 26일
Here is the code:
close all;
%Define number of samples to take
fs = 100;
f = 400; %Hz
%N =length(signal);
%Define signal
t = 0:1/fs:1-1/fs;
%signal = sin(2*pi*f*t);
signal = xlsread('testdata.xlsx');
%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)
fftSignal2 = fftshift(fftSignal);
%xdft = xdft(1:length(s)/2+1);
%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(fftSignal2));
title('magnitude FFT of sine');
xlabel('Frequency (Hz)');
ylabel('magnitude');
farzad
farzad 2020년 11월 30일
please edit your codein the question text and add code formatting to be readable. did you solve your problem, now I am searching for the same question

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

답변 (2개)

Star Strider
Star Strider 2017년 4월 26일
‘what does f = fs/2*linspace(-1,1,fs); mean in the following code.’
It creates a frequency vector ‘f’ from the negative Nyquist frequency (half of the sampling frequency, or fs/2) to the positive Nyquist frequency with a vector length equal to the sampling frequency, that here is apparently equal to the length of the signal vector (and the Fourier transform of it). It then uses ‘f’ to plot the two-sided Fourier transform.

Sai Charan Sampara
Sai Charan Sampara 2022년 6월 30일
편집: Sai Charan Sampara 2022년 6월 30일
linspace(-1,1,fs) creates an array of numbers between -1 and 1. The number of such numbers in between are fs. So this function gives fs number of equally spaced numbers in between -1 and 1.By doing f =fs/2*linspace(-1,1,fs) we are multiplying all the earlier elements by fs/2 and storing that array in variable f. So we are changing the range from [-1,1] to [-fs/2,fs/2] . So the array f has fs number of equally spaced numbers between -fs/2 and fs/2. This variable f is used as x variable for the Fourier transform plot.

카테고리

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