Plot frequency spectrum of a signal

15 visualizaciones (últimos 30 días)
Aaron Karlo Maranan
Aaron Karlo Maranan el 26 de Abr. de 2017
Editada: Sai Charan Sampara el 30 de Jun. de 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 comentarios
Aaron Karlo Maranan
Aaron Karlo Maranan el 26 de Abr. de 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 el 30 de Nov. de 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

Iniciar sesión para comentar.

Respuestas (2)

Star Strider
Star Strider el 26 de Abr. de 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 el 30 de Jun. de 2022
Editada: Sai Charan Sampara el 30 de Jun. de 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.

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by