## plotting the frequency spectrum

### aaa (view profile)

on 24 Apr 2012
Latest activity Commented on by Le Dung

### Le Dung (view profile)

on 29 Jan 2018
Accepted Answer by Rick Rosson

### Rick Rosson (view profile)

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))

### Tags

Answer by Rick Rosson

### Rick Rosson (view profile)

on 24 Apr 2012

%%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

Mere

### Mere (view profile)

on 2 Dec 2016
Could someone please explain how you know to set up the frequency vector that way?
shalabh mishra

### shalabh mishra (view profile)

on 5 Nov 2017
%%Fourier Transform:
X = fftshift(fft(x));
@Rick Rosson
In the above command, Why we are not using X=fftshift(x) or X=fft(x)?
Le Dung

### Le Dung (view profile)

on 29 Jan 2018
X = fftshift(fft(x)); is first to calculate fft of x, then you will shift the fft value. If you use fftshift(x), mean that you didn't have any fft value of x to shift, or more exactly, you shift values of x but not fft of values of x.

Answer by kittipob techawudtipat

### kittipob techawudtipat (view profile)

on 16 Apr 2016

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

Answer by Dr. Seis

### Dr. Seis (view profile)

on 24 Apr 2012

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

Dr. Seis

### Dr. Seis (view profile)

on 24 Apr 2012
Forgot the "== 0" in the if statement... whoops!

### reddy (view profile)

on 21 May 2015

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

Answer by Deepanshu Gupta

### Deepanshu Gupta (view profile)

on 23 Nov 2017

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')