Plot frequency spectrum of a signal

33 次查看(过去 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
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