Plot frequency spectrum of a signal

33 views (last 30 days)
Aaron Karlo Maranan
Aaron Karlo Maranan on 26 Apr 2017
Edited: Sai Charan Sampara on 30 Jun 2022
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 Comments
Aaron Karlo Maranan
Aaron Karlo Maranan on 26 Apr 2017
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 on 30 Nov 2020
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

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 26 Apr 2017
‘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 on 30 Jun 2022
Edited: Sai Charan Sampara on 30 Jun 2022
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.

Community Treasure Hunt

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

Start Hunting!