필터 지우기
필터 지우기

plotting the frequency spectrum

조회 수: 379 (최근 30일)
aaa
aaa 2012년 4월 24일
답변: Frantz Bouchereau 2021년 7월 29일
Hi,
I am having trouble plotting the frequency spectrum of a sine wave. For this code, i expect the main frequency component to be centered around 1/(2*pi), but they are not. Is there something I am missing in my code?
x = [0:0.1:2*pi];
y = sin(x);
z = fft(y);
z = fftshift(z);
N = length(y);
f = [-N/2:N/2-1]/N;
plot(f,abs(z))

채택된 답변

Rick Rosson
Rick Rosson 2012년 4월 24일
Please try:
%%Time specifications:
Fs = 100; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 1; % seconds
t = (0:dt:StopTime-dt)';
N = size(t,1);
%%Sine wave:
Fc = 12; % hertz
x = cos(2*pi*Fc*t);
%%Fourier Transform:
X = fftshift(fft(x));
%%Frequency specifications:
dF = Fs/N; % hertz
f = -Fs/2:dF:Fs/2-dF; % hertz
%%Plot the spectrum:
figure;
plot(f,abs(X)/N);
xlabel('Frequency (in hertz)');
title('Magnitude Response');
HTH.
Rick
  댓글 수: 6
Kyle Ohlschlager
Kyle Ohlschlager 2020년 11월 17일
This worked great and was exactly what I needed in knowing how to create the frequency axis on my own- it's hard to find code that doesnt rely on built in functions too much- Thank you!
Anya Getman
Anya Getman 2021년 2월 9일
Karan, looks like Rick chose 100, and he either guessed, or looked at your time samples and found them to be 0.01 apart. Take whatever your sample time step is and 1/step. If your time samples are unevenly sampled, you will need to use a function like decimate to make them evenly sampled for analysis.

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

추가 답변 (7개)

kittipob techawudtipat
kittipob techawudtipat 2016년 4월 16일
i'm having trouble with this >> n = 1:2:5; >> x = -3.98*(sin((pi*n)/2))/(n*pi)+32*(sin((pi*n)/2))/((n.^3)*(pi.^3)*(10.^6)); >> y = 1.996 + x; >> plot(y(1:50)) Index exceeds matrix dimensions.
how can i solve this problem. the graph i order to plot dont happen thankyou bro

Frantz Bouchereau
Frantz Bouchereau 2021년 7월 29일
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.
Finally, here is a popular MATLAB doc page that explains the relationship between FFT and true power spectra: Power Spectral Density Estimates Using FFT.

Dr. Seis
Dr. Seis 2012년 4월 24일
Your "f" looks off. For your configuration (which uses fftshift):
N = length(y);
dt = x(2)-x(1); % Time increment
Nyq = 1/(2*dt); % Nyquist frequency
df = 1/(N*dt); % Frequency increment
if mod(N,2) == 0 % N is even
f = -Nyq : df : Nyq-df;
else % N is odd
f = [sort(-1*(df:df:Nyq)) (0:df:Nyq)];
end
  댓글 수: 1
Dr. Seis
Dr. Seis 2012년 4월 24일
Forgot the "== 0" in the if statement... whoops!

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


reddy
reddy 2015년 5월 21일
Hi every one
could you please plot spectrum for the variables that are in EXCEL file which is attached.?
thanks.
Please provide me code also.
Phanindra

Deepanshu Gupta
Deepanshu Gupta 2017년 11월 23일
I am trying to plot spectrum of wave for a string plucked halfway along it's length.I am not seeing any figure when I run it. c = 343; % speed of sound air% L = 1000; % string length% w0 = 200; % Displacement distance % Bn = 0; % string at rest, -ve part is zero% x = (0:L); % distance on L%
n = 2; %no. of modes%
fn = (n*c)/2*L;
omega = 2*pi*fn; %omega%
omegaN = n * omega; %w is displacement%
kn = w0/c; % wave no.%
t = 1/fn; %time%
An = ((8*w0)/n^2*pi^2)*sin((n*pi)/2); %incident wave part +ve%
W = sum((An*cos(omegaN*t) + Bn*sin(omegaN*t))*sin(kn*x)); %Displacement eq. sum n=1:10%
plot(W,t); title('hinged string') xlabel('displacement') ylabel('time')

Khathutshelo Mudau
Khathutshelo Mudau 2020년 9월 27일
Hy, can someone help me with determining or plotting the error spectrum of a sound signal

Nermin Hamdan
Nermin Hamdan 2020년 11월 24일
it is requested that 0 0 1 0 data be transmitted through a narrow communication channel. Using the MATLAB program, find the signal received at the receiver with the effect of the band-limited channel
First plot the submitted 0 0 1 0 data in time plane. Then find and plot the frequency spectrum. I do not have an idea what to do can you help me please

카테고리

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